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_node_ref.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_NODE_REF_H
13 #define DOCWIRE_XML_NODE_REF_H
14 
15 #include <memory>
16 #include "xml_iterator_state.h"
17 #include "not_null.h"
18 #include "ranged.h"
19 #include "convert_base.h"
20 
21 namespace docwire::xml
22 {
23 
31 template <safety_policy safety_level = default_safety_level>
32 class node_ref
33 {
34 public:
39  explicit node_ref(not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level> state)
40  : m_state(std::move(state)) {}
42  std::string_view name() const { return m_state->xml_reader.name(); }
44  std::string_view full_name() const { return m_state->xml_reader.full_name(); }
46  std::string_view content() const { return m_state->xml_reader.content(); }
48  std::string_view string_value() const { return m_state->xml_reader.string_value(); }
50  non_negative<int, safety_level> depth() const { return m_state->xml_reader.depth(); }
52  node_type type() const { return m_state->xml_reader.type(); }
54  const not_null<std::shared_ptr<iterator_state<safety_level>>, safety_level>& state() const { return m_state; }
55 
56 private:
58 };
59 
66 template<typename T, safety_policy safety_level>
67 requires convert::conversion_implementation_exists<T, std::string_view>
68 std::optional<T> convert_impl(const node_ref<safety_level>& node, convert::dest_type_tag<T>) noexcept
69 {
70  return convert::try_to<T>(node.string_value());
71 }
72 
73 }
74 
75 #endif
A wrapper for pointer-like types that enforces a non-null invariant.
Definition: not_null.h:41
A wrapper for numeric types that enforces a range [Min, Max].
Definition: ranged.h:46
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
std::string_view name() const
Returns the local name of the node.
Definition: xml_node_ref.h:42
node_ref(not_null< std::shared_ptr< iterator_state< safety_level >>, safety_level > state)
Constructs a reference from an iterator state.
Definition: xml_node_ref.h:39
node_type type() const
Returns the type of the node.
Definition: xml_node_ref.h:52
std::string_view content() const
Returns the content of the node (e.g., text content).
Definition: xml_node_ref.h:46
std::string_view full_name() const
Returns the full name (including namespace prefix) of the node.
Definition: xml_node_ref.h:44
std::string_view string_value() const
Returns the string value of the node (concatenated text of children).
Definition: xml_node_ref.h:48
XML processing utilities.
node_type
Represents the type of an XML node.
Definition: xml_reader.h:31
requires convert::conversion_implementation_exists< T, std::string_view > std::optional< T > convert_impl(const attribute_ref< safety_level > &attr, convert::dest_type_tag< T >) noexcept
Conversion implementation to convert an attribute_ref to another type T. This is done by attempting t...
requires(!string_method_equipped< T >) struct stringifier< T >
Specialization for types that are streamable to std::ostream.
A tag type used for dispatching convert_impl overloads based on the destination type.
Definition: convert_base.h:32
Shared state for XML iterators to coordinate traversal.