1 /* Copyright (c) 2004-2021. 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. */
19 #include <type_traits>
23 #include "simgrid/Exception.hpp"
24 #include "simgrid/sg_config.hpp"
25 #include "xbt/dynar.h"
28 #include "xbt/sysdep.h"
29 #include <xbt/config.h>
30 #include <xbt/config.hpp>
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
34 XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr;
41 bool parse_bool(const char* value)
43 for (const char* true_value : {"yes", "on", "true", "1"})
44 if (std::strcmp(true_value, value) == 0)
46 for (const char* false_value : {"no", "off", "false", "0"})
47 if (std::strcmp(false_value, value) == 0)
49 throw std::range_error("not a boolean");
52 double parse_double(const char* value)
56 double res = std::strtod(value, &end);
58 throw std::range_error("out of range");
60 xbt_die("Unexpected errno");
61 if (end == value || *end != '\0')
62 throw std::range_error("invalid double");
67 long int parse_long(const char* value)
71 long int res = std::strtol(value, &end, 0);
73 if (res == LONG_MIN && errno == ERANGE)
74 throw std::range_error("underflow");
75 else if (res == LONG_MAX && errno == ERANGE)
76 throw std::range_error("overflow");
77 xbt_die("Unexpected errno");
79 if (end == value || *end != '\0')
80 throw std::range_error("invalid integer");
85 // ***** ConfigType *****
87 /// A trait which define possible options types:
88 template <class T> class ConfigType;
90 template <> class ConfigType<int> {
92 static constexpr const char* type_name = "int";
93 static inline int parse(const char* value)
95 long val = parse_long(value);
97 throw std::range_error("underflow");
99 throw std::range_error("overflow");
100 return static_cast<int>(val);
103 template <> class ConfigType<double> {
105 static constexpr const char* type_name = "double";
106 static inline double parse(const char* value)
108 return parse_double(value);
111 template <> class ConfigType<std::string> {
113 static constexpr const char* type_name = "string";
114 static inline std::string parse(const char* value)
116 return std::string(value);
119 template <> class ConfigType<bool> {
121 static constexpr const char* type_name = "boolean";
122 static inline bool parse(const char* value)
124 return parse_bool(value);
128 // **** Forward declarations ****
130 class ConfigurationElement ;
131 template<class T> class TypedConfigurationElement;
133 // **** ConfigurationElement ****
135 class ConfigurationElement {
139 bool isdefault = true;
142 ConfigurationElement(const std::string& key, const std::string& desc) : key(key), desc(desc) {}
144 virtual ~ConfigurationElement() = default;
146 virtual std::string get_string_value() = 0;
147 virtual void set_string_value(const char* value) = 0;
148 virtual const char* get_type_name() = 0;
150 template <class T> T const& get_value() const
152 return static_cast<const TypedConfigurationElement<T>&>(*this).get_value();
154 template <class T> void set_value(T value)
156 static_cast<TypedConfigurationElement<T>&>(*this).set_value(std::move(value));
158 template <class T> void set_default_value(T value)
160 static_cast<TypedConfigurationElement<T>&>(*this).set_default_value(std::move(value));
162 void unset_default() { isdefault = false; }
163 bool is_default() const { return isdefault; }
165 std::string const& get_description() const { return desc; }
166 std::string const& get_key() const { return key; }
169 // **** TypedConfigurationElement<T> ****
171 // TODO, could we use boost::any with some Type* reference?
173 class TypedConfigurationElement : public ConfigurationElement {
176 std::function<void(T&)> callback;
179 TypedConfigurationElement(const std::string& key, const std::string& desc, T value = T())
180 : ConfigurationElement(key, desc), content(std::move(value))
182 TypedConfigurationElement(const std::string& key, const std::string& desc, T value, std::function<void(T&)> callback)
183 : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback))
186 std::string get_string_value() override;
187 const char* get_type_name() override;
188 void set_string_value(const char* value) override;
193 this->callback(this->content);
196 T const& get_value() const { return content; }
198 void set_value(T value)
200 this->content = std::move(value);
202 this->unset_default();
205 void set_default_value(T value)
207 if (this->is_default()) {
208 this->content = std::move(value);
211 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
212 get_key().c_str(), get_string_value().c_str());
217 template <class T> std::string TypedConfigurationElement<T>::get_string_value() // override
219 return to_string(content);
222 template <> std::string TypedConfigurationElement<double>::get_string_value() // override
224 // don't want std::to_string which uses %f, and formats very small values as 0.000000
225 return xbt::string_printf("%g", content);
228 template <class T> void TypedConfigurationElement<T>::set_string_value(const char* value) // override
230 this->content = ConfigType<T>::parse(value);
231 this->unset_default();
235 template <class T> const char* TypedConfigurationElement<T>::get_type_name() // override
237 return ConfigType<T>::type_name;
240 } // end of anonymous namespace
246 // name -> ConfigElement:
247 std::map<std::string, std::unique_ptr<ConfigurationElement>, std::less<>> options;
248 // alias -> ConfigElement from options:
249 std::map<std::string, ConfigurationElement*, std::less<>> aliases;
250 bool warn_for_aliases = true;
256 Config(Config const&) = delete;
257 Config& operator=(Config const&) = delete;
259 ConfigurationElement& operator[](const std::string& name);
260 void alias(const std::string& realname, const std::string& aliasname);
262 template <class T, class... A> TypedConfigurationElement<T>* register_option(const std::string& name, A&&... a)
264 xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.",
266 auto* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
267 XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
268 variable->get_type_name(), variable, this);
269 options[name].reset(variable);
275 void dump(const char* name, const char* indent) const;
276 void show_aliases() const;
280 ConfigurationElement* get_dict_element(const std::string& name);
285 atexit(&sg_config_finalize);
288 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
290 auto opt = options.find(name);
291 if (opt != options.end()) {
292 return opt->second.get();
294 auto als = aliases.find(name);
295 if (als != aliases.end()) {
296 ConfigurationElement* res = als->second;
297 if (warn_for_aliases)
298 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str());
301 std::string msg = "Bad config key: " + name + "\nExisting config keys:\n";
302 for (auto const& elm : options)
303 msg += " " + elm.first + ": (" + elm.second->get_type_name() + ")" + elm.second->get_string_value() + "\n";
304 throw std::out_of_range(msg);
309 inline ConfigurationElement& Config::operator[](const std::string& name)
311 return *(get_dict_element(name));
314 void Config::alias(const std::string& realname, const std::string& aliasname)
316 xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
317 ConfigurationElement* element = this->get_dict_element(realname);
318 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
319 this->aliases.insert({aliasname, element});
322 /** @brief Dump a config set for debugging purpose
324 * @param name The name to give to this config set
325 * @param indent what to write at the beginning of each line (right number of spaces)
327 void Config::dump(const char* name, const char* indent) const
330 XBT_CVERB(xbt_help, "%s>> Dumping of the config set '%s':", indent, name);
332 for (auto const& elm : options)
333 XBT_CVERB(xbt_help, "%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(),
334 elm.second->get_string_value().c_str());
337 XBT_CVERB(xbt_help, "%s<< End of the config set '%s'", indent, name);
340 /** @brief Displays the declared aliases and their replacement */
341 void Config::show_aliases() const
343 for (auto const& elm : aliases)
344 XBT_HELP(" %-40s %s", elm.first.c_str(), elm.second->get_key().c_str());
347 /** @brief Displays the declared options and their description */
348 void Config::help() const
350 for (auto const& elm : options) {
351 simgrid::config::ConfigurationElement* variable = elm.second.get();
352 XBT_HELP(" %s: %s", elm.first.c_str(), variable->get_description().c_str());
353 XBT_HELP(" Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
357 // ***** set_default *****
359 template <class T> XBT_PUBLIC void set_default(const char* name, T value)
361 (*simgrid_config)[name].set_default_value<T>(std::move(value));
364 template XBT_PUBLIC void set_default<int>(const char* name, int value);
365 template XBT_PUBLIC void set_default<double>(const char* name, double value);
366 template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
367 template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
369 bool is_default(const char* name)
371 return (*simgrid_config)[name].is_default();
374 // ***** set_value *****
376 template <class T> XBT_PUBLIC void set_value(const char* name, T value)
378 (*simgrid_config)[name].set_value<T>(std::move(value));
381 template XBT_PUBLIC void set_value<int>(const char* name, int value);
382 template XBT_PUBLIC void set_value<double>(const char* name, double value);
383 template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
384 template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
386 void set_as_string(const char* name, const std::string& value)
388 (*simgrid_config)[name].set_string_value(value.c_str());
391 void set_parse(const std::string& opt)
393 std::string options(opt);
394 XBT_DEBUG("List to parse and set:'%s'", options.c_str());
395 while (not options.empty()) {
396 XBT_DEBUG("Still to parse and set: '%s'", options.c_str());
399 size_t pos = options.find_first_not_of(" \t\n,");
400 options.erase(0, pos);
402 pos = options.find_first_of(" \t\n,");
403 std::string name = options.substr(0, pos);
404 options.erase(0, pos);
405 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), options.c_str());
410 pos = name.find(':');
411 xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
414 std::string val = name.substr(pos + 1);
417 const std::string path("path");
418 if (name.compare(0, path.length(), path) != 0)
419 XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
421 set_as_string(name.c_str(), val);
425 // ***** get_value *****
427 template <class T> XBT_PUBLIC T const& get_value(const std::string& name)
429 return (*simgrid_config)[name].get_value<T>();
432 template XBT_PUBLIC int const& get_value<int>(const std::string& name);
433 template XBT_PUBLIC double const& get_value<double>(const std::string& name);
434 template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
435 template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
439 void alias(const char* realname, std::initializer_list<const char*> aliases)
441 for (auto const& aliasname : aliases)
442 simgrid_config->alias(realname, aliasname);
445 // ***** declare_flag *****
448 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
449 std::function<void(const T&)> callback)
451 if (simgrid_config == nullptr)
452 simgrid_config = new simgrid::config::Config();
453 simgrid_config->register_option<T>(name, description, std::move(value), std::move(callback));
456 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
457 std::function<void(int const&)> callback);
458 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
459 std::function<void(double const&)> callback);
460 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
461 std::function<void(bool const&)> callback);
462 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
463 std::function<void(std::string const&)> callback);
467 delete simgrid_config;
468 simgrid_config = nullptr;
473 simgrid_config->show_aliases();
478 simgrid_config->help();
483 /*----[ Setting ]---------------------------------------------------------*/
485 /** @brief Set an integer value to \a name within \a cfg
487 * @param key the name of the variable
488 * @param value the value of the variable
490 void sg_cfg_set_int(const char* key, int value)
492 (*simgrid_config)[key].set_value<int>(value);
495 /** @brief Set or add a double value to \a name within \a cfg
497 * @param key the name of the variable
498 * @param value the double to set
500 void sg_cfg_set_double(const char* key, double value)
502 (*simgrid_config)[key].set_value<double>(value);
505 /** @brief Set or add a string value to \a name within \a cfg
507 * @param key the name of the variable
508 * @param value the value to be added
511 void sg_cfg_set_string(const char* key, const char* value)
513 (*simgrid_config)[key].set_value<std::string>(value);
516 /** @brief Set or add a boolean value to \a name within \a cfg
518 * @param key the name of the variable
519 * @param value the value of the variable
521 void sg_cfg_set_boolean(const char* key, const char* value)
523 (*simgrid_config)[key].set_value<bool>(simgrid::config::parse_bool(value));
526 /*----[ Getting ]---------------------------------------------------------*/
527 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
529 * @param key the name of the variable
531 * Returns the first value from the config set under the given name.
533 int sg_cfg_get_int(const char* key)
535 return (*simgrid_config)[key].get_value<int>();
538 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
540 * @param key the name of the variable
542 * Returns the first value from the config set under the given name.
544 double sg_cfg_get_double(const char* key)
546 return (*simgrid_config)[key].get_value<double>();
549 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
551 * @param key the name of the variable
553 * Returns the first value from the config set under the given name.
554 * If there is more than one value, it will issue a warning.
556 int sg_cfg_get_boolean(const char* key)
558 return (*simgrid_config)[key].get_value<bool>() ? 1 : 0;