ARGoS
3
A parallel, multi-engine simulator for swarm robotics
|
00001 00013 #ifndef ARGOS_CONFIGURATION_H 00014 #define ARGOS_CONFIGURATION_H 00015 00016 #include <argos3/core/utility/datatypes/datatypes.h> 00017 #include <argos3/core/utility/configuration/argos_exception.h> 00018 #include <argos3/core/utility/configuration/tinyxml/ticpp.h> 00019 #include <string> 00020 00021 namespace argos { 00022 00023 /****************************************/ 00024 /****************************************/ 00025 00027 typedef ticpp::Element TConfigurationNode; 00029 typedef ticpp::Iterator <ticpp::Element> TConfigurationNodeIterator; 00030 00031 /****************************************/ 00032 /****************************************/ 00033 00042 inline bool NodeExists(TConfigurationNode& t_node, 00043 const std::string& str_tag) throw() { 00044 TConfigurationNodeIterator it(str_tag); 00045 it = it.begin(&t_node); 00046 return it != NULL; 00047 } 00048 00049 /****************************************/ 00050 /****************************************/ 00051 00061 inline TConfigurationNode& GetNode(TConfigurationNode& t_node, 00062 const std::string& str_tag) { 00063 try { 00064 TConfigurationNodeIterator it(str_tag); 00065 it = it.begin(&t_node); 00066 if(it == NULL) { 00067 THROW_ARGOSEXCEPTION("Node '" << str_tag << "' not found"); 00068 } 00069 return *it; 00070 } 00071 catch(ticpp::Exception& ex) { 00072 THROW_ARGOSEXCEPTION_NESTED("Error searching for '" << str_tag << "' ", ex); 00073 } 00074 } 00075 00076 /****************************************/ 00077 /****************************************/ 00078 00086 inline void AddChildNode(TConfigurationNode& t_parent_node, 00087 TConfigurationNode& t_child_node) { 00088 try { 00089 t_parent_node.InsertEndChild(t_child_node); 00090 } 00091 catch(ticpp::Exception& ex) { 00092 THROW_ARGOSEXCEPTION_NESTED("Error inserting node '" << t_child_node << "' into node '" << t_parent_node << "'", ex); 00093 } 00094 } 00095 00096 /****************************************/ 00097 /****************************************/ 00098 00125 template <typename T> 00126 void GetNodeText(TConfigurationNode& t_node, 00127 T& t_buffer) { 00128 try { 00129 t_node.GetText(&t_buffer); 00130 } 00131 catch(std::exception& ex) { 00132 THROW_ARGOSEXCEPTION_NESTED("Parse error", ex); 00133 } 00134 } 00135 00136 /****************************************/ 00137 /****************************************/ 00138 00149 template <typename T> 00150 void GetNodeTextOrDefault(TConfigurationNode& t_node, 00151 T& t_buffer, 00152 const T& t_default) { 00153 try { 00154 t_node.GetTextOrDefault(&t_buffer, t_default); 00155 } 00156 catch(std::exception& ex) { 00157 THROW_ARGOSEXCEPTION_NESTED("Parse error", ex); 00158 } 00159 } 00160 00161 /****************************************/ 00162 /****************************************/ 00163 00170 inline bool NodeAttributeExists(TConfigurationNode& t_node, 00171 const std::string& str_attribute) { 00172 return t_node.HasAttribute(str_attribute); 00173 } 00174 00175 /****************************************/ 00176 /****************************************/ 00177 00205 template <typename T> 00206 void GetNodeAttribute(TConfigurationNode& t_node, 00207 const std::string& str_attribute, 00208 T& t_buffer) { 00209 try { 00210 t_node.GetAttribute(str_attribute, &t_buffer, true); 00211 } 00212 catch(ticpp::Exception& ex) { 00213 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00214 } 00215 } 00216 00217 /****************************************/ 00218 /****************************************/ 00219 00229 inline void GetNodeAttribute(TConfigurationNode& t_node, 00230 const std::string& str_attribute, 00231 bool& b_buffer) { 00232 std::string strBuffer; 00233 try { 00234 t_node.GetAttribute(str_attribute, &strBuffer, true); 00235 if(strBuffer == "true") { 00236 b_buffer = true; 00237 } 00238 else if(strBuffer == "false") { 00239 b_buffer = false; 00240 } 00241 else { 00242 THROW_ARGOSEXCEPTION("Cannot convert '" << strBuffer << "' into a bool. Accepted values: 'true', 'false'."); 00243 } 00244 } 00245 catch(ticpp::Exception& ex) { 00246 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00247 } 00248 } 00249 00250 /****************************************/ 00251 /****************************************/ 00252 00262 inline void GetNodeAttribute(TConfigurationNode& t_node, 00263 const std::string& str_attribute, 00264 UInt8& un_buffer) { 00265 try { 00266 UInt32 unTmpBuffer; 00267 t_node.GetAttribute(str_attribute, &unTmpBuffer, true); 00268 un_buffer = unTmpBuffer; 00269 } 00270 catch(ticpp::Exception& ex) { 00271 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00272 } 00273 } 00274 00275 /****************************************/ 00276 /****************************************/ 00277 00287 inline void GetNodeAttribute(TConfigurationNode& t_node, 00288 const std::string& str_attribute, 00289 SInt8& n_buffer) { 00290 try { 00291 SInt32 nTmpBuffer; 00292 t_node.GetAttribute(str_attribute, &nTmpBuffer, true); 00293 n_buffer = nTmpBuffer; 00294 } 00295 catch(ticpp::Exception& ex) { 00296 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00297 } 00298 } 00299 00300 /****************************************/ 00301 /****************************************/ 00302 00315 template <typename T> 00316 void GetNodeAttributeOrDefault(TConfigurationNode& t_node, 00317 const std::string& str_attribute, 00318 T& t_buffer, 00319 const T& t_default) { 00320 try { 00321 t_node.GetAttributeOrDefault(str_attribute, &t_buffer, t_default); 00322 } 00323 catch(ticpp::Exception& ex) { 00324 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00325 } 00326 } 00327 00328 /****************************************/ 00329 /****************************************/ 00330 00342 inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node, 00343 const std::string& str_attribute, 00344 bool& b_buffer, 00345 const bool b_default) { 00346 std::string strBuffer; 00347 const std::string strDefault = (b_default ? "true" : "false"); 00348 try { 00349 t_node.GetAttributeOrDefault(str_attribute, &strBuffer, strDefault); 00350 if(strBuffer == "true") { 00351 b_buffer = true; 00352 } 00353 else if(strBuffer == "false") { 00354 b_buffer = false; 00355 } 00356 else { 00357 THROW_ARGOSEXCEPTION("Cannot convert '" << strBuffer << "' into a bool. Accepted values: 'true', 'false'."); 00358 } 00359 } 00360 catch(ticpp::Exception& ex) { 00361 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00362 } 00363 } 00364 00365 /****************************************/ 00366 /****************************************/ 00367 00379 inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node, 00380 const std::string& str_attribute, 00381 UInt8& un_buffer, 00382 const UInt8 un_default) { 00383 try { 00384 UInt32 unTmpBuffer; 00385 t_node.GetAttributeOrDefault(str_attribute, &unTmpBuffer, static_cast<UInt32>(un_default)); 00386 un_buffer = unTmpBuffer; 00387 } 00388 catch(ticpp::Exception& ex) { 00389 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00390 } 00391 } 00392 00393 /****************************************/ 00394 /****************************************/ 00395 00407 inline void GetNodeAttributeOrDefault(TConfigurationNode& t_node, 00408 const std::string& str_attribute, 00409 SInt8& n_buffer, 00410 const SInt8 n_default) { 00411 try { 00412 SInt32 nTmpBuffer; 00413 t_node.GetAttributeOrDefault(str_attribute, &nTmpBuffer, static_cast<SInt32>(n_default)); 00414 n_buffer = nTmpBuffer; 00415 } 00416 catch(ticpp::Exception& ex) { 00417 THROW_ARGOSEXCEPTION_NESTED("Error parsing attribute \"" << str_attribute << "\"", ex); 00418 } 00419 } 00420 00421 /****************************************/ 00422 /****************************************/ 00423 00431 template <typename T> 00432 void SetNodeAttribute(TConfigurationNode& t_node, 00433 const std::string& str_attribute, 00434 const T& t_value) { 00435 t_node.SetAttribute(str_attribute, t_value); 00436 } 00437 00438 /****************************************/ 00439 /****************************************/ 00440 00449 inline void SetNodeAttribute(TConfigurationNode& t_node, 00450 const std::string& str_attribute, 00451 const bool b_value) { 00452 if(b_value) { 00453 t_node.SetAttribute(str_attribute, "true"); 00454 } 00455 else { 00456 t_node.SetAttribute(str_attribute, "false"); 00457 } 00458 } 00459 00460 /****************************************/ 00461 /****************************************/ 00462 00471 inline void SetNodeAttribute(TConfigurationNode& t_node, 00472 const std::string& str_attribute, 00473 const SInt8 n_value) { 00474 t_node.SetAttribute(str_attribute, static_cast<SInt32>(n_value)); 00475 } 00476 00477 /****************************************/ 00478 /****************************************/ 00479 00488 inline void SetNodeAttribute(TConfigurationNode& t_node, 00489 const std::string& str_attribute, 00490 const UInt8 un_value) { 00491 t_node.SetAttribute(str_attribute, static_cast<UInt32>(un_value)); 00492 } 00493 00494 /****************************************/ 00495 /****************************************/ 00496 00497 } 00498 00499 #endif