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_children.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_CHILDREN_VIEW_H
13 #define DOCWIRE_XML_CHILDREN_VIEW_H
14 
15 #include "xml_descendants.h"
16 #include "log_scope.h"
17 #include <ranges>
18 #include "xml_node_ref.h"
19 
20 namespace docwire::xml
21 {
22 
31 template <safety_policy safety_level = default_safety_level>
32 class children_view : public std::ranges::view_base
33 {
34 public:
35  class iterator;
36  iterator begin() const
37  {
38  return iterator{m_state, m_depth};
39  }
40  sentinel end() const
41  {
42  return {};
43  }
44 
50  explicit children_view(not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level> state, int depth)
51  : m_state(std::move(state)), m_depth(depth) {}
52 
53 private:
55  int m_depth;
56 };
57 
61 template <safety_policy safety_level>
62 class children_view<safety_level>::iterator final
63 {
64  friend class children_view<safety_level>;
65 
66 public:
67  using iterator_concept = std::input_iterator_tag;
68  using difference_type = std::ptrdiff_t;
70  using pointer = const value_type*;
71  using reference = const value_type&;
72 
73  bool operator==(const sentinel& s) const { return m_desc_iter == s; }
74 
75  reference operator*() const { return *m_desc_iter; }
76  pointer operator->() const { return m_desc_iter.operator->(); }
77 
78  iterator& operator++()
79  {
80  log::scope _{ "depth"_v = m_depth };
81  ++m_desc_iter;
82  find_next_child();
83  return *this;
84  }
85 
86  void operator++(int)
87  {
88  ++(*this);
89  }
90 
91 private:
92  explicit iterator(not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level> state, int depth)
93  : m_depth{depth}, m_desc_iter(descendants_view<safety_level>{state, m_depth - 1}.begin())
94  {
95  find_next_child();
96  }
97 
99  void find_next_child()
100  {
101  while (m_desc_iter != sentinel{} && ((*m_desc_iter).depth() != m_depth || (*m_desc_iter).type() == node_type::end_element))
102  {
103  ++m_desc_iter;
104  }
105  }
106 
107  int m_depth;
108  typename descendants_view<safety_level>::iterator m_desc_iter;
109 };
110 
118 template<safety_policy safety_level>
120 {
121  return children_view<safety_level>{node.state(), node.depth() + 1};
122 }
123 
131 template<safety_policy safety_level>
133 {
134  return children_view<safety_level>{std::make_shared<iterator_state<safety_level>>(reader), 0};
135 }
136 
137 }
138 
139 #endif
Represents a logging scope.
Definition: log_scope.h:89
A wrapper for pointer-like types that enforces a non-null invariant.
Definition: not_null.h:41
Iterator for traversing direct child nodes.
Definition: xml_children.h:63
A view over the direct children of an XML node.
Definition: xml_children.h:33
children_view(not_null< std::shared_ptr< iterator_state< safety_level >>, safety_level > state, int depth)
Constructs a view from an iterator state and depth.
Definition: xml_children.h:50
A reference to the current XML node in the reader.
Definition: xml_node_ref.h:33
non_negative< int, safety_level > depth() const
Returns the depth of the node in the XML tree.
Definition: xml_node_ref.h:50
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
XML processing utilities.
children_view< safety_level > children(const node_ref< safety_level > &node)
Returns a view of the direct children of the given node.
Definition: xml_children.h:119
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.