1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
18 #include <type_traits>
22 #include "simgrid/Exception.hpp"
23 #include "simgrid/sg_config.hpp"
24 #include "xbt/dynar.h"
27 #include "xbt/sysdep.h"
28 #include <xbt/config.h>
29 #include <xbt/config.hpp>
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
33 XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr;
40 const char* true_values[] = {
41 "yes", "on", "true", "1"
43 const char* false_values[] = {
44 "no", "off", "false", "0"
47 static bool parse_bool(const char* value)
49 for (const char* const& true_value : true_values)
50 if (std::strcmp(true_value, value) == 0)
52 for (const char* const& false_value : false_values)
53 if (std::strcmp(false_value, value) == 0)
55 throw std::range_error("not a boolean");
58 static double parse_double(const char* value)
62 double res = std::strtod(value, &end);
64 throw std::range_error("out of range");
66 xbt_die("Unexpected errno");
67 if (end == value || *end != '\0')
68 throw std::range_error("invalid double");
73 static long int parse_long(const char* value)
77 long int res = std::strtol(value, &end, 0);
79 if (res == LONG_MIN && errno == ERANGE)
80 throw std::range_error("underflow");
81 else if (res == LONG_MAX && errno == ERANGE)
82 throw std::range_error("overflow");
83 xbt_die("Unexpected errno");
85 if (end == value || *end != '\0')
86 throw std::range_error("invalid integer");
91 // ***** ConfigType *****
93 /// A trait which define possible options types:
94 template <class T> class ConfigType;
96 template <> class ConfigType<int> {
98 static constexpr const char* type_name = "int";
99 static inline double parse(const char* value)
101 return parse_long(value);
104 template <> class ConfigType<double> {
106 static constexpr const char* type_name = "double";
107 static inline double parse(const char* value)
109 return parse_double(value);
112 template <> class ConfigType<std::string> {
114 static constexpr const char* type_name = "string";
115 static inline std::string parse(const char* value)
117 return std::string(value);
120 template <> class ConfigType<bool> {
122 static constexpr const char* type_name = "boolean";
123 static inline bool parse(const char* value)
125 return parse_bool(value);
129 // **** Forward declarations ****
131 class ConfigurationElement ;
132 template<class T> class TypedConfigurationElement;
134 // **** ConfigurationElement ****
136 class ConfigurationElement {
140 bool isdefault = true;
144 xbt_cfg_cb_t old_callback = nullptr;
146 ConfigurationElement(const std::string& key, const std::string& desc) : key(key), desc(desc) {}
147 ConfigurationElement(const std::string& key, const std::string& desc, xbt_cfg_cb_t cb)
148 : key(key), desc(desc), old_callback(cb)
152 virtual ~ConfigurationElement() = default;
154 virtual std::string get_string_value() = 0;
155 virtual void set_string_value(const char* value) = 0;
156 virtual const char* get_type_name() = 0;
158 template <class T> T const& get_value() const
160 return static_cast<const TypedConfigurationElement<T>&>(*this).get_value();
162 template <class T> void set_value(T value)
164 static_cast<TypedConfigurationElement<T>&>(*this).set_value(std::move(value));
166 template <class T> void set_default_value(T value)
168 static_cast<TypedConfigurationElement<T>&>(*this).set_default_value(std::move(value));
170 void unset_default() { isdefault = false; }
171 bool is_default() const { return isdefault; }
173 std::string const& get_description() const { return desc; }
174 std::string const& get_key() const { return key; }
177 // **** TypedConfigurationElement<T> ****
179 // TODO, could we use boost::any with some Type* reference?
181 class TypedConfigurationElement : public ConfigurationElement {
184 std::function<void(T&)> callback;
187 TypedConfigurationElement(const std::string& key, const std::string& desc, T value = T())
188 : ConfigurationElement(key, desc), content(std::move(value))
190 TypedConfigurationElement(const std::string& key, const std::string& desc, T value, xbt_cfg_cb_t cb)
191 : ConfigurationElement(key, desc, cb), content(std::move(value))
193 TypedConfigurationElement(const std::string& key, const std::string& desc, T value, std::function<void(T&)> callback)
194 : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback))
196 ~TypedConfigurationElement() = default;
198 std::string get_string_value() override;
199 const char* get_type_name() override;
200 void set_string_value(const char* value) override;
205 this->old_callback(get_key().c_str());
207 this->callback(this->content);
210 T const& get_value() const { return content; }
212 void set_value(T value)
214 this->content = std::move(value);
216 this->unset_default();
219 void set_default_value(T value)
221 if (this->is_default()) {
222 this->content = std::move(value);
225 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
226 get_key().c_str(), to_string(value).c_str());
231 template <class T> std::string TypedConfigurationElement<T>::get_string_value() // override
233 return to_string(content);
236 template <class T> void TypedConfigurationElement<T>::set_string_value(const char* value) // override
238 this->content = ConfigType<T>::parse(value);
239 this->unset_default();
243 template <class T> const char* TypedConfigurationElement<T>::get_type_name() // override
245 return ConfigType<T>::type_name;
248 } // end of anonymous namespace
254 // name -> ConfigElement:
255 std::map<std::string, simgrid::config::ConfigurationElement*> options;
256 // alias -> ConfigElement from options:
257 std::map<std::string, simgrid::config::ConfigurationElement*> aliases;
258 bool warn_for_aliases = true;
265 Config(Config const&) = delete;
266 Config& operator=(Config const&) = delete;
268 ConfigurationElement& operator[](const std::string& name);
269 void alias(const std::string& realname, const std::string& aliasname);
271 template <class T, class... A>
272 simgrid::config::TypedConfigurationElement<T>* register_option(const std::string& name, A&&... a)
274 xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.",
276 TypedConfigurationElement<T>* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
277 XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
278 variable->get_type_name(), variable, this);
279 options.insert({name, variable});
285 void dump(const char *name, const char *indent);
290 ConfigurationElement* get_dict_element(const std::string& name);
295 atexit(&sg_config_finalize);
299 XBT_DEBUG("Frees cfg set %p", this);
300 for (auto const& elm : options)
304 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
306 auto opt = options.find(name);
307 if (opt != options.end()) {
310 auto als = aliases.find(name);
311 if (als != aliases.end()) {
312 ConfigurationElement* res = als->second;
313 if (warn_for_aliases)
314 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str());
317 THROWF(not_found_error, 0, "Bad config key: %s", name.c_str());
322 inline ConfigurationElement& Config::operator[](const std::string& name)
324 return *(get_dict_element(name));
327 void Config::alias(const std::string& realname, const std::string& aliasname)
329 xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
330 ConfigurationElement* element = this->get_dict_element(realname);
331 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
332 this->aliases.insert({aliasname, element});
335 /** @brief Dump a config set for debuging purpose
337 * @param name The name to give to this config set
338 * @param indent what to write at the beginning of each line (right number of spaces)
340 void Config::dump(const char *name, const char *indent)
342 XBT_LOG_DEFAULT_CATEGORY(xbt_help);
344 XBT_VERB("%s>> Dumping of the config set '%s':", indent, name);
346 for (auto const& elm : options)
347 XBT_VERB("%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(),
348 elm.second->get_string_value().c_str());
351 XBT_VERB("%s<< End of the config set '%s'", indent, name);
354 /** @brief Displays the declared aliases and their replacement */
355 void Config::show_aliases()
357 for (auto const& elm : aliases)
358 XBT_HELP(" %-40s %s", elm.first.c_str(), elm.second->get_key().c_str());
361 /** @brief Displays the declared options and their description */
364 for (auto const& elm : options) {
365 simgrid::config::ConfigurationElement* variable = this->options.at(elm.first);
366 XBT_HELP(" %s: %s", elm.first.c_str(), variable->get_description().c_str());
367 XBT_HELP(" Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
371 // ***** set_default *****
373 template <class T> XBT_PUBLIC void set_default(const char* name, T value)
375 (*simgrid_config)[name].set_default_value<T>(std::move(value));
378 template XBT_PUBLIC void set_default<int>(const char* name, int value);
379 template XBT_PUBLIC void set_default<double>(const char* name, double value);
380 template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
381 template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
383 bool is_default(const char* name)
385 return (*simgrid_config)[name].is_default();
388 // ***** set_value *****
390 template <class T> XBT_PUBLIC void set_value(const char* name, T value)
392 (*simgrid_config)[name].set_value<T>(std::move(value));
395 template XBT_PUBLIC void set_value<int>(const char* name, int value);
396 template XBT_PUBLIC void set_value<double>(const char* name, double value);
397 template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
398 template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
400 void set_as_string(const char* name, const std::string& value)
402 (*simgrid_config)[name].set_string_value(value.c_str());
405 void set_parse(const std::string& opt)
407 std::string options(opt);
408 XBT_DEBUG("List to parse and set:'%s'", options.c_str());
409 while (not options.empty()) {
410 XBT_DEBUG("Still to parse and set: '%s'", options.c_str());
413 size_t pos = options.find_first_not_of(" \t\n,");
414 options.erase(0, pos);
416 pos = options.find_first_of(" \t\n,");
417 std::string name = options.substr(0, pos);
418 options.erase(0, pos);
419 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), options.c_str());
424 pos = name.find(':');
425 xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
428 std::string val = name.substr(pos + 1);
431 const std::string path("path");
432 if (name.compare(0, path.length(), path) != 0)
433 XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
435 set_as_string(name.c_str(), val);
439 // ***** get_value *****
441 template <class T> XBT_PUBLIC T const& get_value(const std::string& name)
443 return (*simgrid_config)[name].get_value<T>();
446 template XBT_PUBLIC int const& get_value<int>(const std::string& name);
447 template XBT_PUBLIC double const& get_value<double>(const std::string& name);
448 template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
449 template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
453 void alias(const char* realname, std::initializer_list<const char*> aliases)
455 for (auto const& aliasname : aliases)
456 simgrid_config->alias(realname, aliasname);
459 // ***** declare_flag *****
462 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
463 std::function<void(const T&)> callback)
465 if (simgrid_config == nullptr)
466 simgrid_config = new simgrid::config::Config();
467 simgrid_config->register_option<T>(name, description, std::move(value), std::move(callback));
470 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
471 std::function<void(int const&)> callback);
472 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
473 std::function<void(double const&)> callback);
474 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
475 std::function<void(bool const&)> callback);
476 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
477 std::function<void(std::string const&)> callback);
481 delete simgrid_config;
482 simgrid_config = nullptr;
487 simgrid_config->show_aliases();
492 simgrid_config->help();
497 /*----[ Setting ]---------------------------------------------------------*/
499 /** @brief Set an integer value to \a name within \a cfg
501 * @param key the name of the variable
502 * @param value the value of the variable
504 void xbt_cfg_set_int(const char *key, int value)
506 (*simgrid_config)[key].set_value<int>(value);
509 /** @brief Set or add a double value to \a name within \a cfg
511 * @param key the name of the variable
512 * @param value the double to set
514 void xbt_cfg_set_double(const char *key, double value)
516 (*simgrid_config)[key].set_value<double>(value);
519 /** @brief Set or add a string value to \a name within \a cfg
521 * @param key the name of the variable
522 * @param value the value to be added
525 void xbt_cfg_set_string(const char* key, const char* value)
527 (*simgrid_config)[key].set_value<std::string>(value);
530 /** @brief Set or add a boolean value to \a name within \a cfg
532 * @param key the name of the variable
533 * @param value the value of the variable
535 void xbt_cfg_set_boolean(const char *key, const char *value)
537 (*simgrid_config)[key].set_value<bool>(simgrid::config::parse_bool(value));
540 /*----[ Getting ]---------------------------------------------------------*/
541 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
543 * @param key the name of the variable
545 * Returns the first value from the config set under the given name.
547 int xbt_cfg_get_int(const char *key)
549 return (*simgrid_config)[key].get_value<int>();
552 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
554 * @param key the name of the variable
556 * Returns the first value from the config set under the given name.
558 double xbt_cfg_get_double(const char *key)
560 return (*simgrid_config)[key].get_value<double>();
563 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
565 * @param key the name of the variable
567 * Returns the first value from the config set under the given name.
568 * If there is more than one value, it will issue a warning.
570 int xbt_cfg_get_boolean(const char *key)
572 return (*simgrid_config)[key].get_value<bool>() ? 1 : 0;