2 #ifndef GRAMODS_CORE_CONFIGURATION
3 #define GRAMODS_CORE_CONFIGURATION
5 #include <gmCore/config.hh>
6 #include <gmCore/Console.hh>
15 #ifdef gramods_ENABLE_TinyXML2
19 BEGIN_NAMESPACE_GMCORE;
61 std::vector<std::string> *error_list =
nullptr);
68 std::vector<std::string> *error_list =
nullptr);
75 std::vector<std::string> *error_list =
nullptr);
85 bool hasParam(std::string name);
91 std::size_t getAllParamNames(std::vector<std::string> &name);
98 std::size_t getAllObjectKeys(std::vector<std::string> &name);
103 void addParam(std::string name, std::string value);
121 bool getParam(std::string name, T &value)
const;
131 std::size_t getAllParams(std::string name, std::vector<T> &value)
const;
146 bool getObjectByKey(std::string key, std::shared_ptr<T> &ptr)
const;
159 bool getObjectByDef(std::string def, std::shared_ptr<T> &ptr)
const;
172 bool getObject(std::shared_ptr<T> &ptr)
const;
178 void addObject(std::shared_ptr<T> ptr, std::string key, std::string def);
190 std::size_t getAllObjects(std::vector<std::shared_ptr<T>> &value)
const;
203 std::size_t getAllObjectsByKey(std::string key, std::vector<std::shared_ptr<T>> &ptr)
const;
207 typedef std::map<std::string, std::shared_ptr<Object>> def_list;
209 void load(tinyxml2::XMLNode *node,
210 std::vector<std::string> *error_list =
nullptr);
213 parameter_t(std::string value =
"")
214 : value(value), checked(false) {}
219 typedef std::vector<std::pair<std::string, parameter_t>> parameter_list;
220 typedef std::map<std::string, std::shared_ptr<parameter_t>> overrides_list;
222 std::shared_ptr<Object> object;
226 typedef std::vector<child_t> child_object_list;
228 child_object_list child_objects;
229 parameter_list parameters;
231 std::shared_ptr<def_list> def_objects;
232 overrides_list parameter_overrides;
233 bool warn_unused_overrides;
235 bool parse_if(tinyxml2::XMLElement *element,
236 std::vector<std::string> *error_list);
237 void parse_param(tinyxml2::XMLElement *element,
238 std::vector<std::string> *error_list);
240 overrides_list propagateOverrides(
const overrides_list &,
241 std::vector<std::string>);
247 Configuration(tinyxml2::XMLNode *node,
248 std::shared_ptr<def_list> defs,
249 overrides_list overrides,
250 std::vector<std::string> *error_list =
nullptr);
255 std::size_t Configuration::getAllObjects(std::vector<std::shared_ptr<T>> &value)
const {
259 auto original_size = value.size();
261 for (
auto &child : _this->child_objects) {
262 std::shared_ptr<T> node = std::dynamic_pointer_cast<T>(child.object);
263 if (node) value.push_back(node);
266 return value.size() - original_size;
270 std::size_t Configuration::getAllObjectsByKey(
271 std::string key, std::vector<std::shared_ptr<T>> &value)
const {
275 auto original_size = value.size();
277 for (
auto &child : _this->child_objects) {
279 if (child.key != key)
continue;
281 std::shared_ptr<T> node = std::dynamic_pointer_cast<T>(child.object);
282 if (node) value.push_back(node);
285 return value.size() - original_size;
289 bool Configuration::getObjectByKey(std::string key, std::shared_ptr<T> &ptr)
const {
291 auto it = std::find_if(child_objects.begin(),
293 [key](
const child_t &child) {
294 return key == child.key;
296 if (it == child_objects.end())
return false;
298 std::shared_ptr<T> _value = std::dynamic_pointer_cast<T>(it->object);
299 if (!_value)
return false;
306 bool Configuration::getObjectByDef(std::string def, std::shared_ptr<T> &ptr)
const {
308 if (def_objects->count(def) == 0)
return false;
310 std::shared_ptr<T> _value = std::dynamic_pointer_cast<T>((*def_objects)[def]);
311 if (!_value)
return false;
318 bool Configuration::getObject(std::shared_ptr<T> &ptr)
const {
322 for (
auto &child : _this->child_objects) {
324 std::shared_ptr<T> _value = std::dynamic_pointer_cast<T>(child.object);
326 if (_value ==
nullptr)
continue;
336 void Configuration::addObject(std::shared_ptr<T> ptr,
339 child_objects.push_back(child_t({ptr, key, def}));
343 bool Configuration::getParam(std::string name, T &value)
const {
345 std::string string_value;
346 if (!getParam(name, string_value))
351 std::stringstream string_value_stream(string_value);
354 string_value_stream >> _value;
356 if (!string_value_stream) {
357 GM_WRN(
"Configuration",
"While getting '" << name <<
"', could not parse '" << string_value <<
"' as " <<
typeid(T).name() <<
"!");
364 catch (std::exception){
365 GM_WRN(
"Configuration",
"While getting '" << name <<
"', could not parse '" << string_value <<
"' as " <<
typeid(T).name() <<
"!");
371 inline bool Configuration::getParam(std::string name, std::string &value)
const {
375 auto it = std::find_if(_this->parameters.begin(),
376 _this->parameters.end(),
377 [name](std::pair<std::string, parameter_t> pair) {
378 return pair.first == name;
380 if (it == _this->parameters.end()) {
381 GM_DBG1(
"Configuration",
"Could not find " << name);
385 value = it->second.value;
386 it->second.checked =
true;
387 GM_DBG1(
"Configuration",
"Read " << name <<
" = " << value);
392 inline bool Configuration::getParam(std::string name,
bool &value)
const {
394 std::string string_value;
396 if (!getParam(name, string_value))
399 std::transform(string_value.begin(), string_value.end(), string_value.begin(),
400 [](
unsigned char c){ return std::tolower(c); });
402 if (string_value ==
"true") { value =
true;
return true; }
403 if (string_value ==
"on") { value =
true;
return true; }
404 if (string_value ==
"1") { value =
true;
return true; }
406 if (string_value ==
"false") { value =
false;
return true; }
407 if (string_value ==
"off") { value =
false;
return true; }
408 if (string_value ==
"0") { value =
false;
return true; }
410 GM_WRN(
"Configuration",
"Could not parse '" << string_value <<
"' as bool!");
415 inline std::size_t Configuration::getAllParams(std::string name, std::vector<std::string> &value)
const {
417 Configuration * _this =
const_cast<Configuration*
>(
this);
418 std::size_t original_size = value.size();
420 for (
auto ¶m : _this->parameters)
421 if (param.first == name) {
422 value.push_back(param.second.value);
423 param.second.checked =
true;
424 GM_DBG1(
"Configuration",
"Read " << name <<
" = " << param.second.value);
427 return value.size() - original_size;
431 inline std::size_t Configuration::getAllParams(std::string name, std::vector<bool> &value)
const {
432 std::vector<std::string> values;
433 getAllParams(name, values);
435 std::size_t original_size = value.size();
436 for (
auto string_value : values)
438 std::transform(string_value.begin(), string_value.end(), string_value.begin(),
439 [](
unsigned char c){ return std::tolower(c); });
441 if (string_value ==
"true") { value.push_back(
true);
continue; }
442 if (string_value ==
"on") { value.push_back(
true);
continue; }
443 if (string_value ==
"1") { value.push_back(
true);
continue; }
445 if (string_value ==
"false") { value.push_back(
false);
continue; }
446 if (string_value ==
"off") { value.push_back(
false);
continue; }
447 if (string_value ==
"0") { value.push_back(
false);
continue; }
449 GM_WRN(
"Configuration",
"Could not parse '" << string_value <<
"' as bool!");
450 }
catch (std::exception &) {
451 GM_WRN(
"Configuration",
"Could not parse '" << string_value <<
"' as bool!");
453 return value.size() - original_size;
457 inline std::size_t Configuration::getAllParams(std::string name, std::vector<T> &value)
const {
458 std::vector<std::string> values;
459 getAllParams(name, values);
461 std::size_t original_size = value.size();
462 for (
auto string_value : values)
465 std::stringstream string_value_stream(string_value);
468 string_value_stream >> _value;
470 if (!string_value_stream) {
471 GM_WRN(
"Configuration",
"While getting '" << name <<
"', could not parse '" << string_value <<
"' as " <<
typeid(T).name() <<
"!");
475 value.push_back(_value);
477 catch (std::exception){
478 GM_WRN(
"Configuration",
"While getting '" << name <<
"', could not parse '" << string_value <<
"' as " <<
typeid(T).name() <<
"!");
480 return value.size() - original_size;
483 END_NAMESPACE_GMCORE;
A wrapper for the XML parser that also creates and configures the system objects and holds temporary ...
Definition: Configuration.hh:41
Configuration(tinyxml2::XMLNode *node, std::vector< std::string > *error_list=nullptr)
Read the XML data, create objects as specified by the XML data and configure the objects.