File indexing completed on 2025-12-16 09:26:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef JSON_ELEMENTS_H
0014 #define JSON_ELEMENTS_H
0015
0016
0017 #include <cmath>
0018 #include <string>
0019 #include <vector>
0020
0021
0022 #include <JSON/config.h>
0023
0024 #ifndef RAD_2_DEGREE
0025 #define RAD_2_DEGREE 57.295779513082320876798154814105
0026 #endif
0027 #ifndef M_PI
0028 #define M_PI 3.14159265358979323846
0029 #endif
0030
0031
0032 namespace dd4hep {
0033
0034
0035 namespace json {
0036
0037 typedef const JsonAttr* Attribute;
0038
0039
0040 std::string _toString(const Attribute attr);
0041
0042 std::string _toString(const char *toTranscode);
0043
0044 std::string _toString(const char* s);
0045
0046 std::string _toString(const std::string& s);
0047
0048 std::string _toString(unsigned long i, const char* fmt = "%lu");
0049
0050 std::string _toString(unsigned int i, const char* fmt = "%u");
0051
0052 std::string _toString(int i, const char* fmt = "%d");
0053
0054 std::string _toString(long i, const char* fmt = "%ld");
0055
0056 std::string _toString(float d, const char* fmt = "%.17e");
0057
0058 std::string _toString(double d, const char* fmt = "%.17e");
0059
0060 std::string _ptrToString(const void* p, const char* fmt = "%p");
0061
0062 template <typename T> std::string _toString(const T* p, const char* fmt = "%p")
0063 { return _ptrToString((void*)p,fmt); }
0064
0065
0066 template <typename T> void _toDictionary(const char* name, T value);
0067
0068 void _toDictionary(const char* name, const char* value);
0069
0070 inline void _toDictionary(const std::string& name, const char* value)
0071 { _toDictionary(name.c_str(), value); }
0072
0073 void _toDictionary(const char* name, float value);
0074
0075 void _toDictionary(const char* name, double value);
0076
0077 std::string getEnviron(const std::string& env);
0078
0079
0080 bool _toBool(const char* value);
0081
0082 int _toInt(const char* value);
0083
0084 long _toLong(const char* value);
0085
0086 float _toFloat(const char* value);
0087
0088 double _toDouble(const char* value);
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 class NodeList {
0103 public:
0104 typedef std::pair<JsonElement::second_type::assoc_iterator,JsonElement::second_type::assoc_iterator> iter_t;
0105 std::string m_tag;
0106 JsonElement* m_node;
0107 mutable iter_t m_ptr;
0108
0109
0110 NodeList(const NodeList& l);
0111
0112 NodeList(JsonElement* frst, const std::string& tag);
0113
0114 ~NodeList();
0115
0116 JsonElement* reset();
0117
0118 JsonElement* next() const;
0119
0120 JsonElement* previous() const;
0121
0122 NodeList& operator=(const NodeList& l);
0123 };
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 class Handle_t {
0135 public:
0136
0137 typedef JsonElement* Elt_t;
0138
0139
0140 mutable Elt_t m_node;
0141
0142
0143 Handle_t(Elt_t e = 0) : m_node(e) { }
0144
0145 Elt_t operator->() const { return m_node; }
0146
0147 operator Elt_t() const { return m_node; }
0148
0149 Elt_t ptr() const { return m_node; }
0150
0151 const char* rawTag() const;
0152
0153 const char* rawText() const;
0154
0155 const char* rawValue() const;
0156
0157 std::string tag() const { return _toString(rawTag()); }
0158
0159 std::string text() const { return _toString(rawText()); }
0160
0161 std::string value() const { return _toString(rawValue()); }
0162
0163
0164
0165 const char* attr_name(const Attribute attr) const;
0166
0167 const char* attr_value(const Attribute attr) const;
0168
0169 const char* attr_value(const char* attr) const;
0170
0171 const char* attr_value_nothrow(const char* attr) const;
0172
0173
0174 Attribute attr_ptr(const char* attr) const;
0175
0176 Attribute attr_nothrow(const char* tag) const;
0177
0178 bool hasAttr(const char* t) const;
0179
0180 std::vector<Attribute> attributes() const;
0181
0182 template <class T> T attr(const Attribute a) const {
0183 return this->attr<T>(this->attr_name(a));
0184 }
0185
0186 template <class T> T attr(const char* name) const;
0187
0188
0189
0190 bool hasChild(const char* tag) const;
0191
0192 Handle_t child(const char* tag, bool throw_exception = true) const;
0193
0194 NodeList children(const char* tag) const;
0195
0196 size_t numChildren(const char* tag, bool throw_exception) const;
0197 };
0198
0199 #define INLINE inline
0200 template <> INLINE Attribute Handle_t::attr<Attribute>(const char* tag_value) const {
0201 return attr_ptr(tag_value);
0202 }
0203
0204 template <> INLINE const char* Handle_t::attr<const char*>(const char* tag_value) const {
0205 return attr_value(tag_value);
0206 }
0207
0208 template <> INLINE bool Handle_t::attr<bool>(const char* tag_value) const {
0209 return _toBool(attr_value(tag_value));
0210 }
0211
0212 template <> INLINE int Handle_t::attr<int>(const char* tag_value) const {
0213 return _toInt(attr_value(tag_value));
0214 }
0215
0216 template <> INLINE long Handle_t::attr<long>(const char* tag_value) const {
0217 return _toLong(attr_value(tag_value));
0218 }
0219
0220 template <> INLINE float Handle_t::attr<float>(const char* tag_value) const {
0221 return _toFloat(attr_value(tag_value));
0222 }
0223
0224 template <> INLINE double Handle_t::attr<double>(const char* tag_value) const {
0225 return _toDouble(attr_value(tag_value));
0226 }
0227
0228 template <> INLINE std::string Handle_t::attr<std::string>(const char* tag_value) const {
0229 return _toString(attr_value(tag_value));
0230 }
0231
0232
0233
0234
0235
0236
0237
0238 class Collection_t : public Handle_t {
0239 public:
0240
0241 NodeList m_children;
0242
0243 Collection_t(Handle_t node, const char* tag);
0244
0245 Collection_t(NodeList children);
0246
0247 Collection_t& reset();
0248
0249 size_t size() const;
0250
0251 void operator++() const;
0252
0253 void operator++(int) const;
0254
0255 void operator--() const;
0256
0257 void operator--(int) const;
0258
0259 Elt_t current() const {
0260 return m_node;
0261 }
0262
0263 void throw_loop_exception(const std::exception& e) const;
0264
0265 template <class T> void for_each(T oper) const {
0266 try {
0267 for (const Collection_t& c = *this; c; ++c)
0268 oper(*this);
0269 }
0270 catch (const std::exception& e) {
0271 throw_loop_exception(e);
0272 }
0273 }
0274
0275 template <class T> void for_each(const char* tag_name, T oper) const {
0276 try {
0277 for (const Collection_t& c = *this; c; ++c)
0278 Collection_t(c.m_node, tag_name).for_each(oper);
0279 }
0280 catch (const std::exception& e) {
0281 throw_loop_exception(e);
0282 }
0283 }
0284 };
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 class Document {
0296 public:
0297 typedef JsonElement* DOC;
0298 DOC m_doc;
0299
0300
0301 Document(DOC d=0) : m_doc(d) {
0302 }
0303
0304 operator DOC() const {
0305 return m_doc;
0306 }
0307
0308 DOC operator->() const {
0309 return m_doc;
0310 }
0311
0312 DOC ptr() const {
0313 return m_doc;
0314 }
0315
0316 Handle_t root() const;
0317 };
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 class DocumentHolder : public Document {
0332 public:
0333
0334 DocumentHolder() : Document() {
0335 }
0336
0337 DocumentHolder(DOC d) : Document(d) {
0338 }
0339
0340 DocumentHolder& assign(DOC d);
0341
0342 virtual ~DocumentHolder();
0343 };
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357 class Element {
0358 public:
0359
0360 typedef Handle_t::Elt_t Elt_t;
0361
0362
0363 Handle_t m_element;
0364
0365
0366 Element(const Handle_t& e) : m_element(e) { }
0367
0368 Element(const Element& e) : m_element(e.m_element) { }
0369
0370
0371 Element& operator=(const Element& c) {
0372 m_element = c.m_element;
0373 return *this;
0374 }
0375
0376 Element& operator=(Handle_t handle) {
0377 m_element = handle;
0378 return *this;
0379 }
0380
0381 operator bool() const { return 0 != m_element.ptr(); }
0382
0383 bool operator!() const { return 0 == m_element.ptr(); }
0384
0385 operator Handle_t() const { return m_element; }
0386
0387 operator Elt_t() const { return m_element; }
0388
0389 Elt_t ptr() const { return m_element; }
0390
0391 std::string tag() const { return m_element.tag(); }
0392
0393 const char* tagName() const { return m_element.rawTag(); }
0394
0395 std::string text() const { return m_element.text(); }
0396
0397 bool hasAttr(const char* name) const {return m_element.hasAttr(name); }
0398
0399 template <class T> T attr(const char* tag_value) const
0400 { return m_element.attr<T>(tag_value); }
0401
0402 const char* attr_name(const Attribute a) const
0403 { return m_element.attr_name(a); }
0404
0405 const char* attr_value(const Attribute a) const
0406 { return m_element.attr_value(a); }
0407
0408 size_t numChildren(const char* tag_value, bool exc = true) const
0409 { return m_element.numChildren(tag_value, exc); }
0410
0411 std::vector<Attribute> attributes() const
0412 { return m_element.attributes(); }
0413
0414 Attribute getAttr(const char* name) const;
0415
0416 Handle_t child(const char* tag_value, bool except = true) const
0417 { return m_element.child(tag_value, except); }
0418
0419 bool hasChild(const char* tag_value) const
0420 { return m_element.hasChild(tag_value); }
0421 };
0422
0423
0424 class DocumentHandler;
0425 #undef INLINE
0426
0427 void dumpTree(Handle_t elt);
0428 void dumpTree(Element elt);
0429 void dumpTree(const JsonElement* elt);
0430
0431 }
0432 }
0433 #endif