argos_configuration.h
Go to the documentation of this file.
1 
13 #ifndef ARGOS_CONFIGURATION_H
14 #define ARGOS_CONFIGURATION_H
15 
16 #include <argos3/core/utility/datatypes/datatypes.h>
17 #include <argos3/core/utility/configuration/argos_exception.h>
18 #include <argos3/core/utility/configuration/tinyxml/ticpp.h>
19 #include <string>
20 
21 namespace argos {
22 
23  /****************************************/
24  /****************************************/
25 
27  typedef ticpp::Element TConfigurationNode;
29  typedef ticpp::Iterator <ticpp::Element> TConfigurationNodeIterator;
31  typedef ticpp::Iterator <ticpp::Attribute> TConfigurationAttributeIterator;
32 
33  /****************************************/
34  /****************************************/
35 
44  inline bool NodeExists(TConfigurationNode& t_node,
45  const std::string& str_tag) throw() {
46  TConfigurationNodeIterator it(str_tag);
47  it = it.begin(&t_node);
48  return it != NULL;
49  }
50 
51  /****************************************/
52  /****************************************/
53 
63  inline TConfigurationNode& GetNode(TConfigurationNode& t_node,
64  const std::string& str_tag) {
65  try {
66  TConfigurationNodeIterator it(str_tag);
67  it = it.begin(&t_node);
68  if(it == NULL) {
69  THROW_ARGOSEXCEPTION("Node '" << str_tag << "' not found");
70  }
71  return *it;
72  }
73  catch(ticpp::Exception& ex) {
74  THROW_ARGOSEXCEPTION_NESTED("Error searching for '" << str_tag << "' ", ex);
75  }
76  }
77 
78  /****************************************/
79  /****************************************/
80 
88  inline void AddChildNode(TConfigurationNode& t_parent_node,
89  TConfigurationNode& t_child_node) {
90  try {
91  t_parent_node.InsertEndChild(t_child_node);
92  }
93  catch(ticpp::Exception& ex) {
94  THROW_ARGOSEXCEPTION_NESTED("Error inserting node '" << t_child_node << "' into node '" << t_parent_node << "'", ex);
95  }
96  }
97 
98  /****************************************/
99  /****************************************/
100 
127  template <typename T>
128  void GetNodeText(TConfigurationNode& t_node,
129  T& t_buffer) {
130  try {
131  t_node.GetText(&t_buffer);
132  }
133  catch(std::exception& ex) {
134  THROW_ARGOSEXCEPTION_NESTED("Parse error", ex);
135  }
136  }
137 
138  /****************************************/
139  /****************************************/
140 
151  template <typename T>
152  void GetNodeTextOrDefault(TConfigurationNode& t_node,
153  T& t_buffer,
154  const T& t_default) {
155  try {
156  t_node.GetTextOrDefault(&t_buffer, t_default);
157  }
158  catch(std::exception& ex) {
159  THROW_ARGOSEXCEPTION_NESTED("Parse error", ex);
160  }
161  }
162 
163  /****************************************/
164  /****************************************/
165 
172  inline bool NodeAttributeExists(TConfigurationNode& t_node,
173  const std::string& str_attribute) {
174  return t_node.HasAttribute(str_attribute);
175  }
176 
177  /****************************************/
178  /****************************************/
179 
207  template <typename T>
208  void GetNodeAttribute(TConfigurationNode& t_node,
209  const std::string& str_attribute,
210  T& t_buffer) {
211  try {
212  t_node.GetAttribute(str_attribute, &t_buffer, true);
213  }
214  catch(ticpp::Exception& ex) {
215  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
216  }
217  }
218 
219  /****************************************/
220  /****************************************/
221 
231  inline void GetNodeAttribute(TConfigurationNode& t_node,
232  const std::string& str_attribute,
233  bool& b_buffer) {
234  std::string strBuffer;
235  try {
236  t_node.GetAttribute(str_attribute, &strBuffer, true);
237  if(strBuffer == "true") {
238  b_buffer = true;
239  }
240  else if(strBuffer == "false") {
241  b_buffer = false;
242  }
243  else {
244  THROW_ARGOSEXCEPTION("Cannot convert '" << strBuffer << "' into a bool. Accepted values: 'true', 'false'.");
245  }
246  }
247  catch(ticpp::Exception& ex) {
248  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
249  }
250  }
251 
252  /****************************************/
253  /****************************************/
254 
264  inline void GetNodeAttribute(TConfigurationNode& t_node,
265  const std::string& str_attribute,
266  UInt8& un_buffer) {
267  try {
268  UInt32 unTmpBuffer;
269  t_node.GetAttribute(str_attribute, &unTmpBuffer, true);
270  un_buffer = unTmpBuffer;
271  }
272  catch(ticpp::Exception& ex) {
273  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
274  }
275  }
276 
277  /****************************************/
278  /****************************************/
279 
289  inline void GetNodeAttribute(TConfigurationNode& t_node,
290  const std::string& str_attribute,
291  SInt8& n_buffer) {
292  try {
293  SInt32 nTmpBuffer;
294  t_node.GetAttribute(str_attribute, &nTmpBuffer, true);
295  n_buffer = nTmpBuffer;
296  }
297  catch(ticpp::Exception& ex) {
298  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
299  }
300  }
301 
302  /****************************************/
303  /****************************************/
304 
317  template <typename T>
318  void GetNodeAttributeOrDefault(TConfigurationNode& t_node,
319  const std::string& str_attribute,
320  T& t_buffer,
321  const T& t_default) {
322  try {
323  t_node.GetAttributeOrDefault(str_attribute, &t_buffer, t_default);
324  }
325  catch(ticpp::Exception& ex) {
326  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
327  }
328  }
329 
330  /****************************************/
331  /****************************************/
332 
344  inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node,
345  const std::string& str_attribute,
346  bool& b_buffer,
347  const bool b_default) {
348  std::string strBuffer;
349  const std::string strDefault = (b_default ? "true" : "false");
350  try {
351  t_node.GetAttributeOrDefault(str_attribute, &strBuffer, strDefault);
352  if(strBuffer == "true") {
353  b_buffer = true;
354  }
355  else if(strBuffer == "false") {
356  b_buffer = false;
357  }
358  else {
359  THROW_ARGOSEXCEPTION("Cannot convert '" << strBuffer << "' into a bool. Accepted values: 'true', 'false'.");
360  }
361  }
362  catch(ticpp::Exception& ex) {
363  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
364  }
365  }
366 
367  /****************************************/
368  /****************************************/
369 
381  inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node,
382  const std::string& str_attribute,
383  UInt8& un_buffer,
384  const UInt8 un_default) {
385  try {
386  UInt32 unTmpBuffer;
387  t_node.GetAttributeOrDefault(str_attribute, &unTmpBuffer, static_cast<UInt32>(un_default));
388  un_buffer = unTmpBuffer;
389  }
390  catch(ticpp::Exception& ex) {
391  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
392  }
393  }
394 
395  /****************************************/
396  /****************************************/
397 
409  inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node,
410  const std::string& str_attribute,
411  SInt8& n_buffer,
412  const SInt8 n_default) {
413  try {
414  SInt32 nTmpBuffer;
415  t_node.GetAttributeOrDefault(str_attribute, &nTmpBuffer, static_cast<SInt32>(n_default));
416  n_buffer = nTmpBuffer;
417  }
418  catch(ticpp::Exception& ex) {
419  THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex);
420  }
421  }
422 
423  /****************************************/
424  /****************************************/
425 
433  template <typename T>
434  void SetNodeAttribute(TConfigurationNode& t_node,
435  const std::string& str_attribute,
436  const T& t_value) {
437  t_node.SetAttribute(str_attribute, t_value);
438  }
439 
440  /****************************************/
441  /****************************************/
442 
451  inline void SetNodeAttribute(TConfigurationNode& t_node,
452  const std::string& str_attribute,
453  const bool b_value) {
454  if(b_value) {
455  t_node.SetAttribute(str_attribute, "true");
456  }
457  else {
458  t_node.SetAttribute(str_attribute, "false");
459  }
460  }
461 
462  /****************************************/
463  /****************************************/
464 
473  inline void SetNodeAttribute(TConfigurationNode& t_node,
474  const std::string& str_attribute,
475  const SInt8 n_value) {
476  t_node.SetAttribute(str_attribute, static_cast<SInt32>(n_value));
477  }
478 
479  /****************************************/
480  /****************************************/
481 
490  inline void SetNodeAttribute(TConfigurationNode& t_node,
491  const std::string& str_attribute,
492  const UInt8 un_value) {
493  t_node.SetAttribute(str_attribute, static_cast<UInt32>(un_value));
494  }
495 
496  /****************************************/
497  /****************************************/
498 
499 }
500 
501 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
void GetNodeTextOrDefault(TConfigurationNode &t_node, T &t_buffer, const T &t_default)
Returns the text of the passed XML node, or the passed default value.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned char UInt8
8-bit unsigned integer.
Definition: datatypes.h:60
TConfigurationNode & GetNode(TConfigurationNode &t_node, const std::string &str_tag)
Given a tree root node, returns the first of its child nodes with the wanted name.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception...
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
void AddChildNode(TConfigurationNode &t_parent_node, TConfigurationNode &t_child_node)
Adds an XML node as child of another XML node.
ticpp::Iterator< ticpp::Attribute > TConfigurationAttributeIterator
The iterator for the attributes of an XML node.
bool NodeExists(TConfigurationNode &t_node, const std::string &str_tag)
Given a tree root node, returns true if one of its child nodes has the wanted name.
bool NodeAttributeExists(TConfigurationNode &t_node, const std::string &str_attribute)
Returns true if the specified attribute of a node exists.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
void SetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, const T &t_value)
Sets the value of the wanted node's attribute.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
signed char SInt8
8-bit signed integer.
Definition: datatypes.h:45
void GetNodeText(TConfigurationNode &t_node, T &t_buffer)
Returns the text of the passed XML node A node text is as follows: