herwig is hosted by Hepforge, IPPP Durham
Herwig 7.3.0
Element.h
1// -*- C++ -*-
2//
3// Element.hpp is a part of myXML
4// Copyright (C) 2012-2019 Simon Platzer, The Herwig Collaboration
5//
6// myXML is licenced under version 3 of the GPL, see COPYING for details.
7//
8
9#ifndef MYXML_Element_hpp_included
10#define MYXML_Element_hpp_included
11
12#include <string>
13#include <map>
14#include <list>
15#include <sstream>
16#include <iomanip>
17
18namespace XML {
19
24 namespace ElementTypes {
25
30
31 Unknown = -1,
33 Root = 0,
45 Comment = 6
48 };
49
50 }
51
56 class Element {
57
58 public:
59
64 : theType(ElementTypes::Unknown) {}
65
69 explicit Element(int newType,
70 const std::string& newNameOrContent = "")
71 : theType(newType), theNameOrContent(newNameOrContent) {}
72
73
77 Element(const Element& other);
78
82 Element& operator=(const Element& other);
83
88 friend bool operator==(const XML::Element &one, const XML::Element &two);
89 friend bool operator!=(const XML::Element &one, const XML::Element &two);
90
96 friend XML::Element operator+(const XML::Element& one, const XML::Element& two);
97
98 public:
99
103 int type() const { return theType; }
104
108 const std::string& name() const;
109
110 public:
111
115 const std::string& content() const;
116
120 std::string& content();
121
125 template<class T>
126 Element& operator<<(const T& t) {
128 std::ostringstream out;
129 out << std::setprecision(16) << t;
130 theNameOrContent += out.str();
131 return *this;
132 }
133
134 public:
135
139 bool hasAttributes() const {
140 return
143 }
144
148 bool hasAttribute(const std::string&) const;
149
153 const std::map<std::string,std::string>& attributes() const;
154
158 const std::string& attribute(const std::string&) const;
159
163 std::string& attribute(const std::string&);
164
168 struct Attribute {
169
173 std::string name;
174
178 std::string value;
179
183 template<class T>
184 Attribute(const std::string& newName,
185 const T& newValue)
186 : name(newName) {
187 std::ostringstream valueOut;
188 valueOut << std::setprecision(16) << newValue;
189 value = valueOut.str();
190
191
192
193 }
194
198 inline bool operator==(const Attribute& other) {
199 return ( name == other.name &&
200 value == other.value);
201 }
202 inline bool operator!=(const Attribute& other) {
203 return !(*this == other);
204 }
205 };
206
211
215 template<class T>
216 void appendAttribute(const std::string& name, const T& t) {
217 *this << Attribute(name,t);
218 }
219
223 template<class T>
224 void getFromAttribute(const std::string& name, T& t) const {
225 std::istringstream in(attribute(name));
226 in >> std::setprecision(16) >> t;
227 }
228
229 public:
230
234 bool hasChildren() const {
235 return
238 }
239
243 const std::list<Element>& children() const;
244
248 std::list<Element>& children();
249
254
259
264
268 void insert(std::list<Element>::iterator, const Element&);
269
273 void erase(std::list<Element>::iterator);
274
278 std::list<Element>::const_iterator
279 findFirst(int type, const std::string& name) const;
280
284 std::pair<std::multimap<std::pair<int,std::string>,std::list<Element>::iterator>::const_iterator,
285 std::multimap<std::pair<int,std::string>,std::list<Element>::iterator>::const_iterator>
286 findAll(int type, const std::string& name) const;
287
288 private:
289
294
298 std::string theNameOrContent;
299
303 std::map<std::string,std::string> theAttributes;
304
308 std::list<Element> theChildren;
309
313 std::multimap<std::pair<int,std::string>,std::list<Element>::iterator> theIndex;
314
315 private:
316
320 void index();
321
325 void assertContent() const;
326
330 void assertNamed() const;
331
335 void assertAttributes() const;
336
340 void assertChildren() const;
341
342 };
343
344}
345
346#endif // MYXML_Element_hpp_included
Element represents a (tree of) XML elements.
Definition: Element.h:56
Element(int newType, const std::string &newNameOrContent="")
The standard constructor.
Definition: Element.h:69
void appendAttribute(const std::string &name, const T &t)
Append an attribute to this element.
Definition: Element.h:216
std::map< std::string, std::string > theAttributes
The attributes of this element.
Definition: Element.h:303
std::string theNameOrContent
The name or character content of the element.
Definition: Element.h:298
Element & prepend(const Element &)
Prepend a child element to this element.
Element & operator<<(const T &t)
Append to the content of this element.
Definition: Element.h:126
Element(const Element &other)
The copy constructor.
void assertNamed() const
Assert that this element is named.
const std::string & name() const
Return the name of this element.
std::pair< std::multimap< std::pair< int, std::string >, std::list< Element >::iterator >::const_iterator, std::multimap< std::pair< int, std::string >, std::list< Element >::iterator >::const_iterator > findAll(int type, const std::string &name) const
Find all elements of the given type and name.
bool hasChildren() const
Return true, if this element has children.
Definition: Element.h:234
int theType
The type of this element.
Definition: Element.h:293
friend bool operator==(const XML::Element &one, const XML::Element &two)
Comparison operator.
void assertAttributes() const
Assert that this element contains attributes.
std::list< Element > theChildren
The children elements of this element.
Definition: Element.h:308
std::string & attribute(const std::string &)
Access the attribute of the given name.
bool hasAttribute(const std::string &) const
Return true, if this element contains an attribute of the given name.
Element & append(const Element &)
Append a child element to this element.
void assertContent() const
Assert that this element got a character content.
void erase(std::list< Element >::iterator)
Erase an element at the given position.
Element & operator<<(const Attribute &)
Append an attribute to this element.
const std::map< std::string, std::string > & attributes() const
Return the attributes.
std::list< Element > & children()
Access the list of children elements.
bool hasAttributes() const
Return true, if this element has attributes.
Definition: Element.h:139
int type() const
Return the type of this element.
Definition: Element.h:103
friend XML::Element operator+(const XML::Element &one, const XML::Element &two)
Combine operator.
void assertChildren() const
Assert that this element contains children elements.
Element & operator=(const Element &other)
Assignment.
void getFromAttribute(const std::string &name, T &t) const
Get a value from an attribute.
Definition: Element.h:224
const std::string & content() const
Return the content of this element.
const std::list< Element > & children() const
Return the list of children elements.
void insert(std::list< Element >::iterator, const Element &)
Insert an element before the given position.
Element & operator<<(const Element &)
Append a child element to this element.
const std::string & attribute(const std::string &) const
Return the attribute of the given name.
std::multimap< std::pair< int, std::string >, std::list< Element >::iterator > theIndex
Index children elements by type and name.
Definition: Element.h:313
void index()
Index elements by type and name.
Element()
The default constructor.
Definition: Element.h:63
std::list< Element >::const_iterator findFirst(int type, const std::string &name) const
Find the first element of the given type and name.
std::string & content()
Access the content of this element.
EnumerateElementTypes
The element type enumeration.
Definition: Element.h:29
@ ParsedCharacterData
Character data.
Definition: Element.h:43
@ CharacterData
A processing instruction.
Definition: Element.h:41
@ ProcessingInstruction
An element.
Definition: Element.h:39
@ Element
An empty element.
Definition: Element.h:37
@ EmptyElement
The entire document.
Definition: Element.h:35
@ Root
Unknown element type.
Definition: Element.h:33
@ Comment
Parsed character data.
Definition: Element.h:45
Represent an attribute.
Definition: Element.h:168
std::string value
The attribute value.
Definition: Element.h:178
bool operator==(const Attribute &other)
Comparison operators for attributes.
Definition: Element.h:198
std::string name
The attribute name.
Definition: Element.h:173
Attribute(const std::string &newName, const T &newValue)
Construct an attribute.
Definition: Element.h:184