herwig is hosted by Hepforge, IPPP Durham
Herwig  7.2.1
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 
18 namespace XML {
19 
24  namespace ElementTypes {
25 
30 
31  Unknown = -1,
33  Root = 0,
37  Element = 2,
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) {
127  assertContent();
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
141  type() == ElementTypes::Element ||
142  type() == ElementTypes::EmptyElement;
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 
210  Element& operator<<(const Attribute&);
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
236  type() == ElementTypes::Root ||
237  type() == ElementTypes::Element;
238  }
239 
243  const std::list<Element>& children() const;
244 
248  std::list<Element>& children();
249 
253  Element& append(const Element&);
254 
258  Element& prepend(const Element&);
259 
263  Element& operator<<(const Element&);
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 
293  int theType;
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
EnumerateElementTypes
The element type enumeration.
Definition: Element.h:29
An empty element.
Definition: Element.h:37
std::map< std::string, std::string > theAttributes
The attributes of this element.
Definition: Element.h:303
bool hasChildren() const
Return true, if this element has children.
Definition: Element.h:234
void appendAttribute(const std::string &name, const T &t)
Append an attribute to this element.
Definition: Element.h:216
bool operator==(const LorentzVector< Value > &a, const LorentzVector< Value > &b)
std::list< Element > theChildren
The children elements of this element.
Definition: Element.h:308
The entire document.
Definition: Element.h:35
Generator< Sum< Density1, Density2 > > operator+(const Generator< Density1 > &first, const Generator< Density2 > &second)
Construct the sum of two densities.
bool hasAttributes() const
Return true, if this element has attributes.
Definition: Element.h:139
Attribute(const std::string &newName, const T &newValue)
Construct an attribute.
Definition: Element.h:184
int type() const
Return the type of this element.
Definition: Element.h:103
int theType
The type of this element.
Definition: Element.h:293
bool operator==(const Attribute &other)
Comparison operators for attributes.
Definition: Element.h:198
Unknown element type.
Definition: Element.h:33
Definition: Element.h:18
Element & operator<<(const T &t)
Append to the content of this element.
Definition: Element.h:126
std::string theNameOrContent
The name or character content of the element.
Definition: Element.h:298
Element(int newType, const std::string &newNameOrContent="")
The standard constructor.
Definition: Element.h:69
std::string value
The attribute value.
Definition: Element.h:178
Element represents a (tree of) XML elements.
Definition: Element.h:56
void getFromAttribute(const std::string &name, T &t) const
Get a value from an attribute.
Definition: Element.h:224
std::multimap< std::pair< int, std::string >, std::list< Element >::iterator > theIndex
Index children elements by type and name.
Definition: Element.h:313
Represent an attribute.
Definition: Element.h:168
std::string name
The attribute name.
Definition: Element.h:173
vector< T > & operator<<(vector< T > &tv, const U &u)
A processing instruction.
Definition: Element.h:41
Element()
The default constructor.
Definition: Element.h:63
Parsed character data.
Definition: Element.h:45