ATLAS Offline Software
Loading...
Searching...
No Matches
XMLCoreNode.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef XMLCOREPARSER_XMLCORENODE_H
14#define XMLCOREPARSER_XMLCORENODE_H
15
16
17#include <string>
18#include <string_view>
19#include <map>
20#include <vector>
21#include <memory>
22#include <optional>
23#include <iosfwd>
24#include <stdexcept>
25
26
31 : public std::runtime_error
32{
33public:
38 ExcXMLCore (const std::string& what);
39};
40
41
46{
47public:
60
61
69 const std::string& name="",
70 const std::string& value="");
71
72
78 void set_attrib (const std::string& name, const std::string& value);
79
80
87 XMLCoreNode* add_child (std::unique_ptr<XMLCoreNode> child);
88
89
94
95
99 const XMLCoreNode* get_parent() const;
100
101
105 NodeType get_type () const;
106
107
111 const std::string& get_name () const;
112
113
117 const std::string& get_value () const;
118
119
123 size_t n_attribs() const;
124
125
133 std::string get_attrib_name (size_t i) const;
134
135
140 bool has_attrib (const std::string& name) const;
141
142
150 std::optional<int> try_int_attrib (std::string_view name) const;
151
152
160 std::optional<double> try_double_attrib (std::string_view name) const;
161
162
169 const std::string& get_attrib (std::string_view name) const;
170
171
179 int get_int_attrib (std::string_view name) const;
180
181
189 double get_double_attrib (std::string_view name) const;
190
191
201 const XMLCoreNode* get_child (const std::string& path) const;
202
203
213 std::vector<const XMLCoreNode*> get_children (const std::string& path ="*") const;
214
215
216 /*
217 * @brief Print the node structure in XML format (to cout).
218 * @param header String to print on hte first line.
219 * @param depth Initial nesting depth.
220 */
221 void print (const std::string& header, int depth = 0) const;
222
223
224 /*
225 * @brief Print the node structure in XML format.
226 * @param os Stream to which to print.
227 * @param header String to print on hte first line.
228 * @param depth Initial nesting depth.
229 */
230 void print (std::ostream& os, const std::string& header, int depth = 0) const;
231
232
233private:
238 void set_parent (XMLCoreNode* parent);
239
240
248 template <class T>
249 std::optional<T> try_attrib (std::string_view name) const;
250
251
260 void collect_children (const std::string& path,
261 std::vector<const XMLCoreNode*>& children,
262 bool only_one) const;
263
264
267
270
272 std::string m_name;
273
275 std::string m_value;
276
278 std::map<std::string, std::string, std::less<>> m_attribs;
279
281 std::vector<std::unique_ptr<XMLCoreNode> > m_children;
282};
283
284
285#endif // not XMLCOREPARSER_XMLCORENODE_H
ExcXMLCore(const std::string &what)
Constructor.
Simple DOM-like node structure to hold the result of XML parsing.
Definition XMLCoreNode.h:46
std::string m_value
The value fo this node.
int get_int_attrib(std::string_view name) const
Retrieve the value of an attribute as an integer.
NodeType
Classify node types.
Definition XMLCoreNode.h:52
@ ENTITY_REFERENCE_NODE
Definition XMLCoreNode.h:58
std::map< std::string, std::string, std::less<> > m_attribs
Attributes of this node.
std::string m_name
The name of this node.
XMLCoreNode * m_parent
The parent of this node, or null for a top-level node.
NodeType m_type
The type of this node.
XMLCoreNode(NodeType type, const std::string &name="", const std::string &value="")
Constructor.
std::string get_attrib_name(size_t i) const
Return the name of the i'th attribute.
bool has_attrib(const std::string &name) const
Test for presence of an attribute with a given name.
std::vector< const XMLCoreNode * > get_children(const std::string &path="*") const
Return all children matching a pattern.
void print(const std::string &header, int depth=0) const
XMLCoreNode * add_child(std::unique_ptr< XMLCoreNode > child)
Add a new child to this node.
void set_parent(XMLCoreNode *parent)
Set the parent for this node.
std::optional< double > try_double_attrib(std::string_view name) const
Try to retrieve an double attribute.
NodeType get_type() const
Return the type of this node.
size_t n_attribs() const
Return the number of attributes for this node.
const std::string & get_value() const
Return the value of this node, or an empty string if no value.
double get_double_attrib(std::string_view name) const
Retrieve the value of an attribute as a double.
XMLCoreNode * get_parent()
Get the parent of this node, or nullptr for a top-level node.
void set_attrib(const std::string &name, const std::string &value)
Set the value of an attribute for this node.
void collect_children(const std::string &path, std::vector< const XMLCoreNode * > &children, bool only_one) const
Collect children of this node matching a path.
const std::string & get_name() const
Return the name of this node, or an empty string if no name.
std::optional< int > try_int_attrib(std::string_view name) const
Try to retrieve an integer attribute.
std::vector< std::unique_ptr< XMLCoreNode > > m_children
Children of this node.
std::optional< T > try_attrib(std::string_view name) const
Try to retrieve an attribute of type T.
const XMLCoreNode * get_child(const std::string &path) const
Return the first child matching a pattern, or nullptr.
const std::string & get_attrib(std::string_view name) const
Retrieve the value of an attribute.
std::string depth
tag string for intendation
Definition fastadd.cxx:46