Robotics Library  0.7.0
Object.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_XML_OBJECT_H
28 #define RL_XML_OBJECT_H
29 
30 #include <memory>
31 #include <string>
32 #include <boost/numeric/conversion/cast.hpp>
33 #include <libxml/xpath.h>
34 
35 #include "Node.h"
36 #include "NodeSet.h"
37 
38 namespace rl
39 {
40  namespace xml
41  {
42  class Object
43  {
44  public:
45  explicit Object(::xmlXPathObjectPtr object) :
46  object(object, ::xmlXPathFreeNodeSetList),
47  nodeSet(object->nodesetval, ::xmlXPathFreeNodeSet)
48  {
49  }
50 
52  {
53  }
54 
55  xmlXPathObjectPtr get() const
56  {
57  return this->object.get();
58  }
59 
60  xmlXPathObjectType getType() const
61  {
62  return this->object->type;
63  }
64 
65  template<typename T>
66  T getValue() const;
67 
68  template<typename T>
69  T getValue(const T& val) const;
70 
71  xmlXPathObject& operator*() const
72  {
73  return *this->object;
74  }
75 
76  const ::std::type_info& type() const
77  {
78  switch (this->object->type)
79  {
80  case XPATH_UNDEFINED:
81  throw ::std::bad_typeid();
82  break;
83  case XPATH_NODESET:
84  return typeid(NodeSet);
85  break;
86  case XPATH_BOOLEAN:
87  return typeid(this->object->boolval);
88  break;
89  case XPATH_NUMBER:
90  return typeid(this->object->floatval);
91  break;
92  case XPATH_STRING:
93  return typeid(this->object->stringval);
94  break;
95  case XPATH_POINT:
96  case XPATH_RANGE:
97  case XPATH_LOCATIONSET:
98  case XPATH_USERS:
99  case XPATH_XSLT_TREE:
100  default:
101  throw ::std::bad_typeid();
102  break;
103  }
104  }
105 
106  protected:
107 
108  private:
109  ::std::shared_ptr< ::xmlXPathObject> object;
110 
111  ::std::shared_ptr< ::xmlNodeSet> nodeSet;
112  };
113 
114  template<>
115  inline
116  double Object::getValue<double>(const double& val) const
117  {
118  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? this->object->floatval : val;
119  }
120 
121  template<>
122  inline
123  float Object::getValue<float>(const float& val) const
124  {
125  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<float>(this->object->floatval) : val;
126  }
127 
128  template<>
129  inline
130  short int Object::getValue<short int>(const short int& val) const
131  {
132  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<short int>(this->object->floatval) : val;
133  }
134 
135  template<>
136  inline
137  int Object::getValue<int>(const int& val) const
138  {
139  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<int>(this->object->floatval) : val;
140  }
141 
142  template<>
143  inline
144  long int Object::getValue<long int>(const long int& val) const
145  {
146  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<long int>(this->object->floatval) : val;
147  }
148 
149  template<>
150  inline
151  long long int Object::getValue<long long int>(const long long int& val) const
152  {
153  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<long long int>(this->object->floatval) : val;
154  }
155 
156  template<>
157  inline
158  unsigned short int Object::getValue<unsigned short int>(const unsigned short int& val) const
159  {
160  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<unsigned short int>(this->object->floatval) : val;
161  }
162 
163  template<>
164  inline
165  unsigned int Object::getValue<unsigned int>(const unsigned int& val) const
166  {
167  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<unsigned int>(this->object->floatval) : val;
168  }
169 
170  template<>
171  inline
172  unsigned long int Object::getValue<unsigned long int>(const unsigned long int& val) const
173  {
174  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<unsigned long int>(this->object->floatval) : val;
175  }
176 
177  template<>
178  inline
179  unsigned long long int Object::getValue<unsigned long long int>(const unsigned long long int& val) const
180  {
181  return 0 == ::xmlXPathIsNaN(this->object->floatval) ? ::boost::numeric_cast<unsigned long long int>(this->object->floatval) : val;
182  }
183 
184  template<>
185  inline
186  bool Object::getValue<bool>() const
187  {
188  return 1 == this->object->boolval ? true : false;
189  }
190 
191  template<>
192  inline
193  double Object::getValue<double>() const
194  {
195  return this->getValue<double>(::std::numeric_limits<double>::quiet_NaN());
196  }
197 
198  template<>
199  inline
200  float Object::getValue<float>() const
201  {
202  return this->getValue<float>(::std::numeric_limits<float>::quiet_NaN());
203  }
204 
205  template<>
206  inline
207  short int Object::getValue<short int>() const
208  {
209  return this->getValue<short int>(::std::numeric_limits<short int>::quiet_NaN());
210  }
211 
212  template<>
213  inline
214  int Object::getValue<int>() const
215  {
216  return this->getValue<int>(::std::numeric_limits<int>::quiet_NaN());
217  }
218 
219  template<>
220  inline
221  long int Object::getValue<long int>() const
222  {
223  return this->getValue<long int>(::std::numeric_limits<long int>::quiet_NaN());
224  }
225 
226  template<>
227  inline
228  long long int Object::getValue<long long int>() const
229  {
230  return this->getValue<long long int>(::std::numeric_limits<long long int>::quiet_NaN());
231  }
232 
233  template<>
234  inline
235  unsigned short int Object::getValue<unsigned short int>() const
236  {
237  return this->getValue<unsigned short int>(::std::numeric_limits<unsigned short int>::quiet_NaN());
238  }
239 
240  template<>
241  inline
242  unsigned int Object::getValue<unsigned int>() const
243  {
244  return this->getValue<unsigned int>(::std::numeric_limits<unsigned int>::quiet_NaN());
245  }
246 
247  template<>
248  inline
249  unsigned long int Object::getValue<unsigned long int>() const
250  {
251  return this->getValue<unsigned long int>(::std::numeric_limits<unsigned long int>::quiet_NaN());
252  }
253 
254  template<>
255  inline
256  unsigned long long int Object::getValue<unsigned long long int>() const
257  {
258  return this->getValue<unsigned long long int>(::std::numeric_limits<unsigned long long int>::quiet_NaN());
259  }
260 
261  template<>
262  inline
263  NodeSet Object::getValue<NodeSet>() const
264  {
265  return NodeSet(this->nodeSet);
266  }
267 
268  template<>
269  inline
270  ::std::string Object::getValue< ::std::string>() const
271  {
272  return nullptr != this->object->stringval ? reinterpret_cast<char*>(this->object->stringval) : ::std::string();
273  }
274  }
275 }
276 
277 #endif // RL_XML_PATH_H
rl::xml::Object::nodeSet
::std::shared_ptr< ::xmlNodeSet > nodeSet
Definition: Object.h:111
NodeSet.h
rl::xml::Object::~Object
~Object()
Definition: Object.h:51
rl::xml::Object::get
xmlXPathObjectPtr get() const
Definition: Object.h:55
rl::xml::Object::getType
xmlXPathObjectType getType() const
Definition: Object.h:60
rl::xml::Object::getValue
T getValue() const
rl::xml::Object::getValue
T getValue(const T &val) const
rl::xml::Object::operator*
xmlXPathObject & operator*() const
Definition: Object.h:71
rl::xml::Object::Object
Object(::xmlXPathObjectPtr object)
Definition: Object.h:45
rl::xml::Object::type
const ::std::type_info & type() const
Definition: Object.h:76
rl::xml::Object
Definition: Object.h:43
Node.h
rl::xml::Object::object
::std::shared_ptr< ::xmlXPathObject > object
Definition: Object.h:109
rl::xml::NodeSet
Definition: NodeSet.h:41
rl
Robotics Library.
Definition: AnalogInput.cpp:30