AGX Dynamics  2.37.2.0
Xml.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_XML_H
18 #define AGX_XML_H
19 
20 #include <agx/String.h>
21 #include <agx/Name.h>
22 #include <agx/debug.h>
23 #include <tinyxml/tinyxml.h>
24 
25 
26 
27 
28 #define agxXmlVerify(x, element) \
29 if (!(x)) \
30 { \
31  if (!element) \
32  agxThrow std::runtime_error(std::string("Error in xml-check: Both x and element are nullptr.")); \
33  std::stringstream __stream; \
34  __stream << "[" << __FILE__ << ":" << __LINE__ << "] XML error at row " << (element)->Row() << std::endl; \
35  __stream << "File: " << (element)->GetDocument()->Value() << std::endl; \
36  __stream << "==============================================================================" << std::endl; \
37  (element)->Print(__stream, 0); \
38  __stream << std::endl; \
39  __stream << "==============================================================================" << std::endl; \
40  const std::string& __string = __stream.str(); \
41  std::cerr << __string << std::endl; \
42  agxThrow std::runtime_error(__string); \
43 }
44 
45 #define agxXmlVerify1(x, element, msg) \
46 if (!(x)) \
47 { \
48  std::stringstream __stream; \
49  __stream << "[" << __FILE__ << ":" << __LINE__ << "] XML error: " << msg << ". Error at row " << (element)->Row() << std::endl; \
50  __stream << "File: " << (element)->GetDocument()->Value() << std::endl; \
51  __stream << "==============================================================================" << std::endl; \
52  (element)->Print(__stream, 0); \
53  __stream << std::endl; \
54  __stream << "==============================================================================" << std::endl; \
55  const std::string& __string = __stream.str(); \
56  std::cerr << __string << std::endl; \
57  agxThrow std::runtime_error(__string); \
58 }
59 
60 #define AGX_XML_VERIFY_BUFFER_SIZE 8192
61 #define agxXmlVerifyN(x, element, format, ...) \
62 if (!(x)) \
63 { \
64  char __str[AGX_XML_VERIFY_BUFFER_SIZE]; \
65  agx_snprintf(__str, AGX_XML_VERIFY_BUFFER_SIZE, format, ##__VA_ARGS__); \
66  std::stringstream __stream; \
67  __stream << "[" << __FILE__ << ":" << __LINE__ << "] XML error: " << __str << ". Error at row " << (element)->Row() << std::endl; \
68  __stream << "File: " << (element)->GetDocument()->Value() << std::endl; \
69  __stream << "==============================================================================" << std::endl; \
70  (element)->Print(__stream, 0); \
71  __stream << std::endl; \
72  __stream << "==============================================================================" << std::endl; \
73  const std::string& __string = __stream.str(); \
74  std::cerr << __string << std::endl; \
75  agxThrow std::runtime_error(__string); \
76 }
77 
78 
79 #define agxXmlAbort(element, format, ...) \
80 { \
81  char __str[AGX_XML_VERIFY_BUFFER_SIZE]; \
82  agx_snprintf(__str, AGX_XML_VERIFY_BUFFER_SIZE, format, ##__VA_ARGS__); \
83  std::stringstream __stream; \
84  __stream << "[" << __FILE__ << ":" << __LINE__ << "] XML error: " << __str << ". Error at row " << (element)->Row() << std::endl; \
85  __stream << "File: " << (element)->GetDocument()->Value() << std::endl; \
86  __stream << "==============================================================================" << std::endl; \
87  (element)->Print(__stream, 0); \
88  __stream << std::endl; \
89  __stream << "==============================================================================" << std::endl; \
90  const std::string& __string = __stream.str(); \
91  std::cerr << __string << std::endl; \
92  agxThrow std::runtime_error(__string); \
93 }
94 
95 
96 
97 
98 namespace agx
99 {
100  template <typename T>
101  inline T parseAttribute(const TiXmlElement *element, const char *attributeName)
102  {
103  agxXmlVerifyN(element->Attribute(attributeName), element, "Could not locate attribute \'%s\'", attributeName);
104 
105  const char *attrString = element->Attribute(attributeName);
106  std::stringstream stream(attrString);
107  T val;
108  stream >> std::boolalpha >> val;
109  agxXmlVerifyN(stream, element, "Unable to parse attribute %s", attributeName);
110  return val;
111  }
112 
113  template <>
114  inline String parseAttribute<String>(const TiXmlElement *element, const char *attributeName)
115  {
116  agxXmlVerifyN(element->Attribute(attributeName), element, "Could not locate attribute \'%s\'", attributeName);
117  return element->Attribute(attributeName);
118  }
119 
120  template <>
121  inline Name parseAttribute<Name>(const TiXmlElement *element, const char *attributeName)
122  {
123  agxXmlVerifyN(element->Attribute(attributeName), element, "Could not locate attribute \'%s\'", attributeName);
124  return element->Attribute(attributeName);
125  }
126 
127 }
128 
129 
130 #endif /* AGX_XML_H */
#define agxXmlVerifyN(x, element, format,...)
Definition: Xml.h:61
Representation of a name string.
Definition: Name.h:32
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Name parseAttribute< Name >(const TiXmlElement *element, const char *attributeName)
Definition: Xml.h:121
String parseAttribute< String >(const TiXmlElement *element, const char *attributeName)
Definition: Xml.h:114
T parseAttribute(const TiXmlElement *element, const char *attributeName)
Definition: Xml.h:101