DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
xml_nodes.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /* *******************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_XML_NODES_VIEW_H
13 #define DOCWIRE_XML_NODES_VIEW_H
14 
15 #include <optional>
16 #include "checked.h"
17 #include <ranges>
18 #include "sentinel.h"
19 #include "xml_node_ref.h"
20 #include "xml_iterator_state.h"
21 #include "xml_reader.h"
22 
23 namespace docwire::xml
24 {
25 
32 template<safety_policy safety_level = default_safety_level>
33 class nodes_view : public std::ranges::view_base
34 {
35 public:
36  class iterator;
37  iterator begin() const { return iterator{m_state}; }
38  sentinel end() const { return {}; }
39 
44  explicit nodes_view(not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level> state)
45  : m_state(std::move(state)) {}
46 private:
48 };
49 
53 template<safety_policy safety_level>
54 class nodes_view<safety_level>::iterator
55 {
56  friend class nodes_view<safety_level>;
57 
58 public:
59  using iterator_concept = std::input_iterator_tag;
60  using difference_type = std::ptrdiff_t;
62  using pointer = const value_type*;
63  using reference = const value_type&;
64 
65  bool operator==(const sentinel& s) const { return !m_node.has_value(); }
66  reference operator*() const { return *m_node; }
67  pointer operator->() const { return &*m_node; }
68  iterator& operator++()
69  {
70  if (!m_state->xml_reader.read_next())
71  {
72  m_node.reset();
73  return *this;
74  }
75  m_node.emplace(m_state);
76  return *this;
77  }
78  void operator++(int)
79  {
80  ++(*this);
81  }
85  void reset() { m_node.reset(); }
86 
87 private:
88  explicit iterator(not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level> state)
89  : m_state(std::move(state))
90  {
91  ++(*this); // Advance to the first node upon construction
92  }
94  checked<std::optional<node_ref<safety_level>>, safety_level> m_node;
95 };
96 
103 template<safety_policy safety_level>
105 {
106  return nodes_view<safety_level>{node.state()};
107 }
108 
115 template<safety_policy safety_level>
117 {
118  return nodes_view<safety_level>{std::make_shared<iterator_state<safety_level>>(reader)};
119 }
120 
121 }
122 
123 #endif
A generic wrapper for dereferenceable types (like pointers and optionals) that provides checked acces...
Definition: checked.h:39
A wrapper for pointer-like types that enforces a non-null invariant.
Definition: not_null.h:41
A reference to the current XML node in the reader.
Definition: xml_node_ref.h:33
const not_null< std::shared_ptr< iterator_state< safety_level > >, safety_level > & state() const
Returns the shared iterator state associated with this node reference.
Definition: xml_node_ref.h:54
Iterator for traversing a sequence of XML nodes.
Definition: xml_nodes.h:55
void reset()
Resets the iterator to the end state (invalidates it).
Definition: xml_nodes.h:85
A view over a sequence of XML nodes.
Definition: xml_nodes.h:34
nodes_view(not_null< std::shared_ptr< iterator_state< safety_level >>, safety_level > state)
Constructs a view from an iterator state.
Definition: xml_nodes.h:44
XML processing utilities.
nodes_view< safety_level > nodes(const node_ref< safety_level > &node)
Creates a view of nodes starting from the given node's state.
Definition: xml_nodes.h:104
A sentinel type used to define the end of a range or view.
Definition: sentinel.h:23
Shared state for XML iterators to coordinate traversal.