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. */
21 #include "simgrid/Exception.hpp"
22 #include "simgrid/sg_config.hpp"
23 #include "xbt/dynar.h"
26 #include "xbt/sysdep.h"
27 #include <xbt/config.h>
28 #include <xbt/config.hpp>
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
32 XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr;
39 bool parse_bool(const char* value)
41 for (const char* true_value : {"yes", "on", "true", "1"})
42 if (std::strcmp(true_value, value) == 0)
44 for (const char* false_value : {"no", "off", "false", "0"})
45 if (std::strcmp(false_value, value) == 0)
47 throw std::range_error("not a boolean");
50 double parse_double(const char* value)
54 double res = std::strtod(value, &end);
56 throw std::range_error("out of range");
57 xbt_assert(errno == 0, "Unexpected errno: %d", errno);
58 if (end == value || *end != '\0')
59 throw std::range_error("invalid double");
63 long int parse_long(const char* value)
67 long int res = std::strtol(value, &end, 0);
69 throw std::range_error(res == LONG_MIN ? "underflow" : "overflow");
70 xbt_assert(errno == 0, "Unexpected errno: %d", errno);
71 if (end == value || *end != '\0')
72 throw std::range_error("invalid integer");
76 // ***** ConfigType *****
78 /// A trait which define possible options types:
79 template <class T> class ConfigType;
81 template <> class ConfigType<int> {
83 static constexpr const char* type_name = "int";
84 static inline int parse(const char* value)
86 long val = parse_long(value);
88 throw std::range_error("underflow");
90 throw std::range_error("overflow");
91 return static_cast<int>(val);
94 template <> class ConfigType<double> {
96 static constexpr const char* type_name = "double";
97 static inline double parse(const char* value)
99 return parse_double(value);
102 template <> class ConfigType<std::string> {
104 static constexpr const char* type_name = "string";
105 static inline std::string parse(const char* value)
107 return std::string(value);
110 template <> class ConfigType<bool> {
112 static constexpr const char* type_name = "boolean";
113 static inline bool parse(const char* value)
115 return parse_bool(value);
119 // **** Forward declarations ****
121 class ConfigurationElement ;
122 template<class T> class TypedConfigurationElement;
124 // **** ConfigurationElement ****
126 class ConfigurationElement {
130 bool isdefault = true;
133 ConfigurationElement(const std::string& key, const std::string& desc) : key(key), desc(desc) {}
135 virtual ~ConfigurationElement() = default;
137 virtual std::string get_string_value() = 0;
138 virtual void set_string_value(const char* value) = 0;
139 virtual const char* get_type_name() = 0;
141 template <class T> T const& get_value() const
143 return static_cast<const TypedConfigurationElement<T>&>(*this).get_value();
145 template <class T> void set_value(T value)
147 static_cast<TypedConfigurationElement<T>&>(*this).set_value(std::move(value));
149 template <class T> void set_default_value(T value)
151 static_cast<TypedConfigurationElement<T>&>(*this).set_default_value(std::move(value));
153 void unset_default() { isdefault = false; }
154 bool is_default() const { return isdefault; }
156 std::string const& get_description() const { return desc; }
157 std::string const& get_key() const { return key; }
160 // **** TypedConfigurationElement<T> ****
162 // TODO, could we use boost::any with some Type* reference?
164 class TypedConfigurationElement : public ConfigurationElement {
167 std::function<void(T&)> callback;
170 TypedConfigurationElement(const std::string& key, const std::string& desc, T value = T())
171 : ConfigurationElement(key, desc), content(std::move(value))
173 TypedConfigurationElement(const std::string& key, const std::string& desc, T value, std::function<void(T&)> callback)
174 : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback))
177 std::string get_string_value() override;
178 const char* get_type_name() override;
179 void set_string_value(const char* value) override;
184 this->callback(this->content);
187 T const& get_value() const { return content; }
189 void set_value(T value)
191 this->content = std::move(value);
193 this->unset_default();
196 void set_default_value(T value)
198 if (this->is_default()) {
199 this->content = std::move(value);
202 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
203 get_key().c_str(), get_string_value().c_str());
208 template <class T> std::string TypedConfigurationElement<T>::get_string_value() // override
210 return to_string(content);
213 template <> std::string TypedConfigurationElement<double>::get_string_value() // override
215 // don't want std::to_string which uses %f, and formats very small values as 0.000000
216 return xbt::string_printf("%g", content);
219 template <class T> void TypedConfigurationElement<T>::set_string_value(const char* value) // override
221 this->content = ConfigType<T>::parse(value);
222 this->unset_default();
226 template <class T> const char* TypedConfigurationElement<T>::get_type_name() // override
228 return ConfigType<T>::type_name;
231 } // end of anonymous namespace
237 // name -> ConfigElement:
238 std::map<std::string, std::unique_ptr<ConfigurationElement>, std::less<>> options;
239 // alias -> ConfigElement from options:
240 std::map<std::string, ConfigurationElement*, std::less<>> aliases;
241 bool warn_for_aliases = true;
247 Config(Config const&) = delete;
248 Config& operator=(Config const&) = delete;
250 ConfigurationElement& operator[](const std::string& name);
251 void alias(const std::string& realname, const std::string& aliasname);
253 template <class T, class... A> TypedConfigurationElement<T>* register_option(const std::string& name, A&&... a)
255 xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.",
257 auto* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
258 XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
259 variable->get_type_name(), variable, this);
260 options[name].reset(variable);
266 void dump(const char* name, const char* indent) const;
267 void show_aliases() const;
271 ConfigurationElement* get_dict_element(const std::string& name);
276 atexit(&sg_config_finalize);
279 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
281 auto opt = options.find(name);
282 if (opt != options.end()) {
283 return opt->second.get();
285 auto als = aliases.find(name);
286 if (als != aliases.end()) {
287 ConfigurationElement* res = als->second;
288 if (warn_for_aliases)
289 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str());
292 std::string msg = "Bad config key: " + name + "\n";
293 std::string kebab = name;
294 std::replace(begin(kebab), end(kebab), '_', '-'); // convert from snake_case to kebab-case
295 if (options.count(kebab) > 0)
296 msg += "Did you mean '" + kebab + "'?\n";
297 msg += "Existing config keys:\n";
298 for (auto const& elm : options)
299 msg += " " + elm.first + ": (" + elm.second->get_type_name() + ")" + elm.second->get_string_value() + "\n";
300 throw std::out_of_range(msg);
305 inline ConfigurationElement& Config::operator[](const std::string& name)
307 return *(get_dict_element(name));
310 void Config::alias(const std::string& realname, const std::string& aliasname)
312 xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
313 ConfigurationElement* element = this->get_dict_element(realname);
314 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
315 this->aliases.insert({aliasname, element});
318 /** @brief Dump a config set for debugging purpose
320 * @param name The name to give to this config set
321 * @param indent what to write at the beginning of each line (right number of spaces)
323 void Config::dump(const char* name, const char* indent) const
326 XBT_CVERB(xbt_help, "%s>> Dumping of the config set '%s':", indent, name);
328 for (auto const& elm : options)
329 XBT_CVERB(xbt_help, "%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(),
330 elm.second->get_string_value().c_str());
333 XBT_CVERB(xbt_help, "%s<< End of the config set '%s'", indent, name);
336 /** @brief Displays the declared aliases and their replacement */
337 void Config::show_aliases() const
339 for (auto const& elm : aliases)
340 XBT_HELP(" %-40s %s", elm.first.c_str(), elm.second->get_key().c_str());
343 /** @brief Displays the declared options and their description */
344 void Config::help() const
346 for (auto const& elm : options) {
347 simgrid::config::ConfigurationElement* variable = elm.second.get();
348 XBT_HELP(" %s: %s", elm.first.c_str(), variable->get_description().c_str());
349 XBT_HELP(" Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
353 // ***** set_default *****
355 template <class T> XBT_PUBLIC void set_default(const char* name, T value)
357 (*simgrid_config)[name].set_default_value<T>(std::move(value));
360 template XBT_PUBLIC void set_default<int>(const char* name, int value);
361 template XBT_PUBLIC void set_default<double>(const char* name, double value);
362 template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
363 template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
365 bool is_default(const char* name)
367 return (*simgrid_config)[name].is_default();
370 // ***** set_value *****
372 template <class T> XBT_PUBLIC void set_value(const char* name, T value)
374 (*simgrid_config)[name].set_value<T>(std::move(value));
377 template XBT_PUBLIC void set_value<int>(const char* name, int value);
378 template XBT_PUBLIC void set_value<double>(const char* name, double value);
379 template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
380 template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
382 void set_as_string(const char* name, const std::string& value)
384 (*simgrid_config)[name].set_string_value(value.c_str());
387 void set_parse(const std::string& opt)
389 std::string options(opt);
390 XBT_DEBUG("List to parse and set:'%s'", options.c_str());
391 while (not options.empty()) {
392 XBT_DEBUG("Still to parse and set: '%s'", options.c_str());
395 size_t pos = options.find_first_not_of(" \t\n,");
396 options.erase(0, pos);
398 pos = options.find_first_of(" \t\n,");
399 std::string name = options.substr(0, pos);
400 options.erase(0, pos);
401 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), options.c_str());
406 pos = name.find(':');
407 xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
410 std::string val = name.substr(pos + 1);
413 const std::string path("path");
414 if (name.compare(0, path.length(), path) != 0)
415 XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
417 set_as_string(name.c_str(), val);
421 // ***** get_value *****
423 template <class T> XBT_PUBLIC T const& get_value(const std::string& name)
425 return (*simgrid_config)[name].get_value<T>();
428 template XBT_PUBLIC int const& get_value<int>(const std::string& name);
429 template XBT_PUBLIC double const& get_value<double>(const std::string& name);
430 template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
431 template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
435 void alias(const char* realname, std::initializer_list<const char*> aliases)
437 for (auto const& aliasname : aliases)
438 simgrid_config->alias(realname, aliasname);
441 // ***** declare_flag *****
444 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
445 std::function<void(const T&)> callback)
447 if (simgrid_config == nullptr)
448 simgrid_config = new simgrid::config::Config();
449 simgrid_config->register_option<T>(name, description, std::move(value), std::move(callback));
452 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
453 std::function<void(int const&)> callback);
454 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
455 std::function<void(double const&)> callback);
456 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
457 std::function<void(bool const&)> callback);
458 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
459 std::function<void(std::string const&)> callback);
463 delete simgrid_config;
464 simgrid_config = nullptr;
469 simgrid_config->show_aliases();
474 simgrid_config->help();
479 /*----[ Setting ]---------------------------------------------------------*/
481 /** @brief Set an integer value to \a name within \a cfg
483 * @param key the name of the variable
484 * @param value the value of the variable
486 void sg_cfg_set_int(const char* key, int value)
488 (*simgrid_config)[key].set_value<int>(value);
491 /** @brief Set or add a double value to \a name within \a cfg
493 * @param key the name of the variable
494 * @param value the double to set
496 void sg_cfg_set_double(const char* key, double value)
498 (*simgrid_config)[key].set_value<double>(value);
501 /** @brief Set or add a string value to \a name within \a cfg
503 * @param key the name of the variable
504 * @param value the value to be added
507 void sg_cfg_set_string(const char* key, const char* value)
509 (*simgrid_config)[key].set_value<std::string>(value);
512 /** @brief Set or add a boolean value to \a name within \a cfg
514 * @param key the name of the variable
515 * @param value the value of the variable
517 void sg_cfg_set_boolean(const char* key, const char* value)
519 (*simgrid_config)[key].set_value<bool>(simgrid::config::parse_bool(value));
522 /*----[ Getting ]---------------------------------------------------------*/
523 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
525 * @param key the name of the variable
527 * Returns the first value from the config set under the given name.
529 int sg_cfg_get_int(const char* key)
531 return (*simgrid_config)[key].get_value<int>();
534 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
536 * @param key the name of the variable
538 * Returns the first value from the config set under the given name.
540 double sg_cfg_get_double(const char* key)
542 return (*simgrid_config)[key].get_value<double>();
545 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
547 * @param key the name of the variable
549 * Returns the first value from the config set under the given name.
550 * If there is more than one value, it will issue a warning.
552 int sg_cfg_get_boolean(const char* key)
554 return (*simgrid_config)[key].get_value<bool>() ? 1 : 0;