DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Elements.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef JSON_ELEMENTS_H
14 #define JSON_ELEMENTS_H
15 
16 // C/C++ include files
17 #include <cmath>
18 #include <string>
19 #include <vector>
20 #include <stdexcept>
21 
22 // Framework include files
23 #include <JSON/config.h>
24 
25 #ifndef RAD_2_DEGREE
26 #define RAD_2_DEGREE 57.295779513082320876798154814105
27 #endif
28 #ifndef M_PI
29 #define M_PI 3.14159265358979323846
30 #endif
31 
33 namespace dd4hep {
34 
36  namespace json {
37 
38  typedef const JsonAttr* Attribute;
39 
41  std::string _toString(const Attribute attr);
43  std::string _toString(const char *toTranscode);
45  std::string _toString(const char* s);
47  std::string _toString(const std::string& s);
49  std::string _toString(unsigned long i, const char* fmt = "%lu");
51  std::string _toString(unsigned int i, const char* fmt = "%u");
53  std::string _toString(int i, const char* fmt = "%d");
55  std::string _toString(long i, const char* fmt = "%ld");
57  std::string _toString(float d, const char* fmt = "%.17e");
59  std::string _toString(double d, const char* fmt = "%.17e");
61  std::string _ptrToString(const void* p, const char* fmt = "%p");
63  template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
64  { return _ptrToString((void*)p,fmt); }
65 
67  template <typename T> void _toDictionary(const char* name, T value);
69  void _toDictionary(const char* name, const char* value);
71  inline void _toDictionary(const std::string& name, const char* value)
72  { _toDictionary(name.c_str(), value); }
74  void _toDictionary(const char* name, float value);
76  void _toDictionary(const char* name, double value);
78  std::string getEnviron(const std::string& env);
79 
81  bool _toBool(const char* value);
83  int _toInt(const char* value);
85  long _toLong(const char* value);
87  float _toFloat(const char* value);
89  double _toDouble(const char* value);
90 
92 
103  class NodeList {
104  public:
105  typedef std::pair<JsonElement::second_type::assoc_iterator,JsonElement::second_type::assoc_iterator> iter_t;
106  std::string m_tag;
108  mutable iter_t m_ptr;
109 
111  NodeList(const NodeList& l);
113  NodeList(JsonElement* frst, const std::string& tag);
115  ~NodeList();
117  JsonElement* reset();
119  JsonElement* next() const;
121  JsonElement* previous() const;
123  NodeList& operator=(const NodeList& l);
124  };
125 
127 
135  class Handle_t {
136  public:
137  // Abbreviation for internal use
138  typedef JsonElement* Elt_t;
139 
141  mutable Elt_t m_node;
142 
144  Handle_t(Elt_t e = 0) : m_node(e) { }
146  Elt_t operator->() const { return m_node; }
148  operator Elt_t() const { return m_node; }
150  Elt_t ptr() const { return m_node; }
152  const char* rawTag() const;
154  const char* rawText() const;
156  const char* rawValue() const;
158  std::string tag() const { return _toString(rawTag()); }
160  std::string text() const { return _toString(rawText()); }
162  std::string value() const { return _toString(rawValue()); }
163 
164  /*** Attribute handling */
166  const char* attr_name(const Attribute attr) const;
168  const char* attr_value(const Attribute attr) const;
170  const char* attr_value(const char* attr) const;
172  const char* attr_value_nothrow(const char* attr) const;
173 
175  Attribute attr_ptr(const char* attr) const;
177  Attribute attr_nothrow(const char* tag) const;
179  bool hasAttr(const char* t) const;
181  std::vector<Attribute> attributes() const;
183  template <class T> T attr(const Attribute a) const {
184  return this->attr<T>(this->attr_name(a));
185  }
187  template <class T> T attr(const char* name) const;
188 
189  /*** DOM Element child handling */
191  bool hasChild(const char* tag) const;
193  Handle_t child(const char* tag, bool throw_exception = true) const;
195  NodeList children(const char* tag) const;
197  size_t numChildren(const char* tag, bool throw_exception) const;
198  };
199 
200 #define INLINE inline
201  template <> INLINE Attribute Handle_t::attr<Attribute>(const char* tag_value) const {
202  return attr_ptr(tag_value);
203  }
204 
205  template <> INLINE const char* Handle_t::attr<const char*>(const char* tag_value) const {
206  return attr_value(tag_value);
207  }
208 
209  template <> INLINE bool Handle_t::attr<bool>(const char* tag_value) const {
210  return _toBool(attr_value(tag_value));
211  }
212 
213  template <> INLINE int Handle_t::attr<int>(const char* tag_value) const {
214  return _toInt(attr_value(tag_value));
215  }
216 
217  template <> INLINE long Handle_t::attr<long>(const char* tag_value) const {
218  return _toLong(attr_value(tag_value));
219  }
220 
221  template <> INLINE float Handle_t::attr<float>(const char* tag_value) const {
222  return _toFloat(attr_value(tag_value));
223  }
224 
225  template <> INLINE double Handle_t::attr<double>(const char* tag_value) const {
226  return _toDouble(attr_value(tag_value));
227  }
228 
229  template <> INLINE std::string Handle_t::attr<std::string>(const char* tag_value) const {
230  return _toString(attr_value(tag_value));
231  }
232 
234 
239  class Collection_t : public Handle_t {
240  public:
244  Collection_t(Handle_t node, const char* tag);
248  Collection_t& reset();
250  size_t size() const;
252  void operator++() const;
254  void operator++(int) const;
256  void operator--() const;
258  void operator--(int) const;
260  Elt_t current() const {
261  return m_node;
262  }
264  void throw_loop_exception(const std::exception& e) const;
266  template <class T> void for_each(T oper) const {
267  try {
268  for (const Collection_t& c = *this; c; ++c)
269  oper(*this);
270  }
271  catch (const std::exception& e) {
273  }
274  }
276  template <class T> void for_each(const char* tag_name, T oper) const {
277  try {
278  for (const Collection_t& c = *this; c; ++c)
279  Collection_t(c.m_node, tag_name).for_each(oper);
280  }
281  catch (const std::exception& e) {
283  }
284  }
285  };
286 
288 
296  class Document {
297  public:
298  typedef JsonElement* DOC;
300 
302  Document(DOC d=0) : m_doc(d) {
303  }
305  operator DOC() const {
306  return m_doc;
307  }
309  DOC operator->() const {
310  return m_doc;
311  }
313  DOC ptr() const {
314  return m_doc;
315  }
317  Handle_t root() const;
318  };
319 
321 
332  class DocumentHolder : public Document {
333  public:
336  }
339  }
343  virtual ~DocumentHolder();
344  };
345 
347 
358  class Element {
359  public:
362 
365 
367  Element(const Handle_t& e) : m_element(e) { }
369  Element(const Element& e) : m_element(e.m_element) { }
370 
372  Element& operator=(const Element& c) {
373  m_element = c.m_element;
374  return *this;
375  }
378  m_element = handle;
379  return *this;
380  }
382  operator bool() const { return 0 != m_element.ptr(); }
384  bool operator!() const { return 0 == m_element.ptr(); }
386  operator Handle_t() const { return m_element; }
388  operator Elt_t() const { return m_element; }
390  Elt_t ptr() const { return m_element; }
392  std::string tag() const { return m_element.tag(); }
394  const char* tagName() const { return m_element.rawTag(); }
396  std::string text() const { return m_element.text(); }
398  bool hasAttr(const char* name) const {return m_element.hasAttr(name); }
400  template <class T> T attr(const char* tag_value) const
401  { return m_element.attr<T>(tag_value); }
403  const char* attr_name(const Attribute a) const
404  { return m_element.attr_name(a); }
406  const char* attr_value(const Attribute a) const
407  { return m_element.attr_value(a); }
409  size_t numChildren(const char* tag_value, bool exc = true) const
410  { return m_element.numChildren(tag_value, exc); }
412  std::vector<Attribute> attributes() const
413  { return m_element.attributes(); }
415  Attribute getAttr(const char* name) const;
417  Handle_t child(const char* tag_value, bool except = true) const
418  { return m_element.child(tag_value, except); }
420  bool hasChild(const char* tag_value) const
421  { return m_element.hasChild(tag_value); }
422  };
423 
425  class DocumentHandler;
426 #undef INLINE
427 
428  void dumpTree(Handle_t elt);
429  void dumpTree(Element elt);
430  void dumpTree(const JsonElement* elt);
431 
432  } /* End namespace json */
433 } /* End namespace dd4hep */
434 #endif // JSON_ELEMENTS_H
dd4hep::json::Handle_t::attr_name
const char * attr_name(const Attribute attr) const
Access attribute name (throws exception if not present)
Definition: Elements.cpp:331
dd4hep::json::Element::m_element
Handle_t m_element
The underlying object holding the JsonElement pointer.
Definition: Elements.h:364
dd4hep::json::Collection_t::size
size_t size() const
Access the collection size. Avoid this call – sloooow!
Definition: Elements.cpp:400
dd4hep::json::Handle_t::Elt_t
JsonElement * Elt_t
Definition: Elements.h:138
dd4hep::json::Element::hasChild
bool hasChild(const char *tag_value) const
Check the existence of a child with a given tag name.
Definition: Elements.h:420
dd4hep::json::Handle_t::Handle_t
Handle_t(Elt_t e=0)
Initializing constructor.
Definition: Elements.h:144
dd4hep::json::Collection_t::for_each
void for_each(const char *tag_name, T oper) const
Loop processor using function object.
Definition: Elements.h:276
dd4hep::json::Element::operator!
bool operator!() const
operator NOT: check handle validity
Definition: Elements.h:384
dd4hep::json::Collection_t::operator++
void operator++() const
Operator to advance the collection (pre increment)
Definition: Elements.cpp:412
dd4hep::json::_toInt
int _toInt(const char *value)
Conversion function from raw unicode string to int.
Definition: Elements.cpp:145
dd4hep::json::_toBool
bool _toBool(const char *value)
Conversion function from raw unicode string to bool.
Definition: Elements.cpp:149
dd4hep::json::Collection_t::Collection_t
Collection_t(Handle_t node, const char *tag)
Constructor over JsonElements with a given tag name.
Definition: Elements.cpp:382
dd4hep::json::DocumentHolder::assign
DocumentHolder & assign(DOC d)
Assign new document. Old document is dropped.
Definition: Elements.cpp:364
dd4hep::json::_ptrToString
std::string _ptrToString(const void *p, const char *fmt="%p")
Format void pointer (64 bits) to string with arbitrary format.
Definition: Elements.cpp:137
dd4hep::json::NodeList::~NodeList
~NodeList()
Default destructor.
Definition: Elements.cpp:203
dd4hep::json::Element::attr_name
const char * attr_name(const Attribute a) const
Access attribute name (throws exception if not present)
Definition: Elements.h:403
dd4hep::json::Document::Document
Document(DOC d=0)
Constructor.
Definition: Elements.h:302
dd4hep::json::Collection_t::throw_loop_exception
void throw_loop_exception(const std::exception &e) const
Helper function to throw an exception.
Definition: Elements.cpp:405
dd4hep::json::Handle_t::tag
std::string tag() const
Text access to the element's tag.
Definition: Elements.h:158
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::json::Handle_t::m_node
Elt_t m_node
The pointer to the JsonElement.
Definition: Elements.h:141
dd4hep::json::Document::m_doc
DOC m_doc
Definition: Elements.h:299
dd4hep::json::Handle_t::attr
T attr(const Attribute a) const
Access typed attribute value by the JsonAttr.
Definition: Elements.h:183
dd4hep::json::Handle_t::attr_value
const char * attr_value(const Attribute attr) const
Access attribute value by the attribute (throws exception if not present)
Definition: Elements.cpp:344
dd4hep::json::_toString
std::string _toString(const Attribute attr)
Convert json attribute to STL string.
Definition: Elements.cpp:80
dd4hep::json::Attribute
const JsonAttr * Attribute
Definition: Elements.h:38
dd4hep::json::NodeList::operator=
NodeList & operator=(const NodeList &l)
Assignment operator.
Definition: Elements.cpp:236
dd4hep::json::Collection_t::reset
Collection_t & reset()
Reset the collection object to restart the iteration.
Definition: Elements.cpp:394
dd4hep::json::Element::tag
std::string tag() const
Access the tag name of this element.
Definition: Elements.h:392
dd4hep::json::_toFloat
float _toFloat(const char *value)
Conversion function from raw unicode string to float.
Definition: Elements.cpp:157
dd4hep::json::Element::attr
T attr(const char *tag_value) const
Access attribute with implicit return type conversion.
Definition: Elements.h:400
dd4hep::json::NodeList::m_node
JsonElement * m_node
Definition: Elements.h:107
dd4hep::json::_toLong
long _toLong(const char *value)
Conversion function from raw unicode string to long.
Definition: Elements.cpp:141
dd4hep::json::Element::Elt_t
Handle_t::Elt_t Elt_t
Simplification type declarations.
Definition: Elements.h:361
dd4hep::json::Element::hasAttr
bool hasAttr(const char *name) const
Check for the existence of a named attribute.
Definition: Elements.h:398
dd4hep::json::NodeList::next
JsonElement * next() const
Advance to next element.
Definition: Elements.cpp:218
dd4hep::json::Handle_t::children
NodeList children(const char *tag) const
Access a group of children identified by the same tag name.
Definition: Elements.cpp:309
dd4hep::json::Handle_t::attr_nothrow
Attribute attr_nothrow(const char *tag) const
Access attribute pointer by the attribute's unicode name (no exception thrown if not present)
Definition: Elements.cpp:261
dd4hep::json::NodeList::reset
JsonElement * reset()
Reset the nodelist - e.g. restart iteration from beginning.
Definition: Elements.cpp:207
dd4hep::json::JsonElement
boost::property_tree::ptree::value_type JsonElement
Definition: config.h:33
dd4hep::json::Element::operator=
Element & operator=(const Element &c)
Assignment operator.
Definition: Elements.h:372
dd4hep::json::Element::operator=
Element & operator=(Handle_t handle)
Assignment operator.
Definition: Elements.h:377
dd4hep::json::NodeList::m_ptr
iter_t m_ptr
Definition: Elements.h:108
dd4hep::json::Handle_t::hasAttr
bool hasAttr(const char *t) const
Check for the existence of a named attribute.
Definition: Elements.cpp:266
dd4hep::json::Handle_t::rawText
const char * rawText() const
Unicode text access to the element's text.
Definition: Elements.cpp:251
dd4hep::json::Document
Class supporting the basic functionality of an JSON document.
Definition: Elements.h:296
dd4hep::json::Element::getAttr
Attribute getAttr(const char *name) const
Access single attribute by its name.
Definition: Elements.cpp:378
dd4hep::json::Handle_t::rawValue
const char * rawValue() const
Unicode text access to the element's value.
Definition: Elements.cpp:256
dd4hep::json::DocumentHolder::DocumentHolder
DocumentHolder()
Default Constructor.
Definition: Elements.h:335
dd4hep::json::Handle_t::ptr
Elt_t ptr() const
Direct access to the JsonElement by function.
Definition: Elements.h:150
dd4hep::json::Collection_t::m_children
NodeList m_children
Reference to the list of child nodes.
Definition: Elements.h:242
dd4hep::json::_toDouble
double _toDouble(const char *value)
Conversion function from raw unicode string to double.
Definition: Elements.cpp:161
dd4hep::json::Document::DOC
JsonElement * DOC
Definition: Elements.h:298
dd4hep::json::Document::ptr
DOC ptr() const
Accessot to DOM document behaviour.
Definition: Elements.h:313
dd4hep::json::NodeList::m_tag
std::string m_tag
Definition: Elements.h:106
dd4hep::json::_toDictionary
void _toDictionary(const char *name, T value)
Helper function to populate the evaluator dictionary.
Definition: Elements.cpp:169
dd4hep::json::Collection_t::for_each
void for_each(T oper) const
Loop processor using function object.
Definition: Elements.h:266
dd4hep::json::NodeList
Class describing a list of JSON nodes.
Definition: Elements.h:103
dd4hep::json::Document::operator->
DOC operator->() const
Accessot to DOM document behaviour using arrow operator.
Definition: Elements.h:309
dd4hep::json::NodeList::NodeList
NodeList(const NodeList &l)
Copy constructor.
Definition: Elements.cpp:189
dd4hep::json::Element::tagName
const char * tagName() const
Access the tag name of this element.
Definition: Elements.h:394
dd4hep::json::Handle_t::numChildren
size_t numChildren(const char *tag, bool throw_exception) const
Access the number of children of this DOM element with a given tag name.
Definition: Elements.cpp:282
dd4hep::json::Handle_t::attributes
std::vector< Attribute > attributes() const
Retrieve a collection of all attributes of this DOM element.
Definition: Elements.cpp:271
dd4hep::json::Element::text
std::string text() const
Access the tag name of this element.
Definition: Elements.h:396
dd4hep::json::Element::child
Handle_t child(const char *tag_value, bool except=true) const
Access child by tag name. Thow an exception if required in case the child is not present.
Definition: Elements.h:417
dd4hep::json::Handle_t::operator->
Elt_t operator->() const
Direct access to the JsonElement using the operator->
Definition: Elements.h:146
dd4hep::json::DocumentHolder::~DocumentHolder
virtual ~DocumentHolder()
Standard destructor - releases the document.
Definition: Elements.cpp:374
dd4hep::json::NodeList::iter_t
std::pair< JsonElement::second_type::assoc_iterator, JsonElement::second_type::assoc_iterator > iter_t
Definition: Elements.h:105
dd4hep::json::Handle_t::rawTag
const char * rawTag() const
Unicode text access to the element's tag.
Definition: Elements.cpp:246
dd4hep::json::Handle_t::hasChild
bool hasChild(const char *tag) const
Check the existence of a child with a given tag name.
Definition: Elements.cpp:313
dd4hep::json::Handle_t::value
std::string value() const
Text access to the element's value.
Definition: Elements.h:162
dd4hep::json::dumpTree
void dumpTree(Handle_t elt)
Definition: Elements.cpp:436
dd4hep::json::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: Elements.cpp:356
dd4hep::json::Collection_t::operator--
void operator--() const
Operator to advance the collection (pre decrement)
Definition: Elements.cpp:420
dd4hep::json::DocumentHolder
Class supporting the basic functionality of an JSON document including ownership.
Definition: Elements.h:332
dd4hep::json::Element::attr_value
const char * attr_value(const Attribute a) const
Access attribute value by the attribute (throws exception if not present)
Definition: Elements.h:406
INLINE
#define INLINE
Definition: Elements.h:200
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::json::Element::Element
Element(const Element &e)
Constructor from JsonElement handle.
Definition: Elements.h:369
dd4hep::json::Handle_t
Class to easily access the properties of single JsonElements.
Definition: Elements.h:135
dd4hep::json::Handle_t::text
std::string text() const
Text access to the element's text.
Definition: Elements.h:160
dd4hep::json::Collection_t
Class to support the access to collections of JsonNodes (or JsonElements)
Definition: Elements.h:239
dd4hep::json::Element::numChildren
size_t numChildren(const char *tag_value, bool exc=true) const
Access the number of children of this element with a given tag name.
Definition: Elements.h:409
dd4hep::json::Element::Element
Element(const Handle_t &e)
Constructor from JsonElement handle.
Definition: Elements.h:367
dd4hep::json::Element::attributes
std::vector< Attribute > attributes() const
Retrieve a collection of all attributes of this element.
Definition: Elements.h:412
dd4hep::json::Handle_t::child
Handle_t child(const char *tag, bool throw_exception=true) const
Access a single child by its tag name (unicode)
Definition: Elements.cpp:297
dd4hep::json::Element::ptr
Elt_t ptr() const
Access to JsonElement pointer.
Definition: Elements.h:390
dd4hep::json::Element
User abstraction class to manipulate JSON elements within a document.
Definition: Elements.h:358
dd4hep::json::Handle_t::attr_value_nothrow
const char * attr_value_nothrow(const char *attr) const
Access attribute value by the attribute's unicode name (no exception thrown if not present)
Definition: Elements.cpp:349
dd4hep::json::Collection_t::current
Elt_t current() const
Access to current element.
Definition: Elements.h:260
dd4hep::json::getEnviron
std::string getEnviron(const std::string &env)
Helper function to lookup environment from the expression evaluator.
Definition: Elements.cpp:184
dd4hep::json::DocumentHolder::DocumentHolder
DocumentHolder(DOC d)
Constructor.
Definition: Elements.h:338
dd4hep::json::JsonAttr
boost::property_tree::ptree::value_type JsonAttr
Definition: config.h:32
handle
void handle(const O *o, const C &c, F pmf)
Definition: LCDDConverter.cpp:1105
dd4hep::json::Handle_t::attr
T attr(const char *name) const
Access typed attribute value by its unicode name.
config.h
dd4hep::json::NodeList::previous
JsonElement * previous() const
Go back to previous element.
Definition: Elements.cpp:227
dd4hep::json::Handle_t::attr_ptr
Attribute attr_ptr(const char *attr) const
Access attribute pointer by the attribute's unicode name (throws exception if not present)
Definition: Elements.cpp:318