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. */
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 const char* true_values[] = {
42 "yes", "on", "true", "1"
44 const char* false_values[] = {
45 "no", "off", "false", "0"
48 static bool parse_bool(const char* value)
50 for (const char* const& true_value : true_values)
51 if (std::strcmp(true_value, value) == 0)
53 for (const char* const& false_value : false_values)
54 if (std::strcmp(false_value, value) == 0)
56 throw std::range_error("not a boolean");
59 static double parse_double(const char* value)
63 double res = std::strtod(value, &end);
65 throw std::range_error("out of range");
67 xbt_die("Unexpected errno");
68 if (end == value || *end != '\0')
69 throw std::range_error("invalid double");
74 static long int parse_long(const char* value)
78 long int res = std::strtol(value, &end, 0);
80 if (res == LONG_MIN && errno == ERANGE)
81 throw std::range_error("underflow");
82 else if (res == LONG_MAX && errno == ERANGE)
83 throw std::range_error("overflow");
84 xbt_die("Unexpected errno");
86 if (end == value || *end != '\0')
87 throw std::range_error("invalid integer");
92 // ***** ConfigType *****
94 /// A trait which define possible options types:
95 template <class T> class ConfigType;
97 template <> class ConfigType<int> {
99 static constexpr const char* type_name = "int";
100 static inline double parse(const char* value)
102 return parse_long(value);
105 template <> class ConfigType<double> {
107 static constexpr const char* type_name = "double";
108 static inline double parse(const char* value)
110 return parse_double(value);
113 template <> class ConfigType<std::string> {
115 static constexpr const char* type_name = "string";
116 static inline std::string parse(const char* value)
118 return std::string(value);
121 template <> class ConfigType<bool> {
123 static constexpr const char* type_name = "boolean";
124 static inline bool parse(const char* value)
126 return parse_bool(value);
130 // **** Forward declarations ****
132 class ConfigurationElement ;
133 template<class T> class TypedConfigurationElement;
135 // **** ConfigurationElement ****
137 class ConfigurationElement {
141 bool isdefault = true;
144 ConfigurationElement(const std::string& key, const std::string& desc) : key(key), desc(desc) {}
146 virtual ~ConfigurationElement() = default;
148 virtual std::string get_string_value() = 0;
149 virtual void set_string_value(const char* value) = 0;
150 virtual const char* get_type_name() = 0;
152 template <class T> T const& get_value() const
154 return static_cast<const TypedConfigurationElement<T>&>(*this).get_value();
156 template <class T> void set_value(T value)
158 static_cast<TypedConfigurationElement<T>&>(*this).set_value(std::move(value));
160 template <class T> void set_default_value(T value)
162 static_cast<TypedConfigurationElement<T>&>(*this).set_default_value(std::move(value));
164 void unset_default() { isdefault = false; }
165 bool is_default() const { return isdefault; }
167 std::string const& get_description() const { return desc; }
168 std::string const& get_key() const { return key; }
171 // **** TypedConfigurationElement<T> ****
173 // TODO, could we use boost::any with some Type* reference?
175 class TypedConfigurationElement : public ConfigurationElement {
178 std::function<void(T&)> callback;
181 TypedConfigurationElement(const std::string& key, const std::string& desc, T value = T())
182 : ConfigurationElement(key, desc), content(std::move(value))
184 TypedConfigurationElement(const std::string& key, const std::string& desc, T value, std::function<void(T&)> callback)
185 : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback))
187 ~TypedConfigurationElement() = default;
189 std::string get_string_value() override;
190 const char* get_type_name() override;
191 void set_string_value(const char* value) override;
196 this->callback(this->content);
199 T const& get_value() const { return content; }
201 void set_value(T value)
203 this->content = std::move(value);
205 this->unset_default();
208 void set_default_value(T value)
210 if (this->is_default()) {
211 this->content = std::move(value);
214 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
215 get_key().c_str(), to_string(value).c_str());
220 template <class T> std::string TypedConfigurationElement<T>::get_string_value() // override
222 return to_string(content);
225 template <class T> void TypedConfigurationElement<T>::set_string_value(const char* value) // override
227 this->content = ConfigType<T>::parse(value);
228 this->unset_default();
232 template <class T> const char* TypedConfigurationElement<T>::get_type_name() // override
234 return ConfigType<T>::type_name;
237 } // end of anonymous namespace
243 // name -> ConfigElement:
244 std::map<std::string, std::unique_ptr<ConfigurationElement>> options;
245 // alias -> ConfigElement from options:
246 std::map<std::string, ConfigurationElement*> aliases;
247 bool warn_for_aliases = true;
253 Config(Config const&) = delete;
254 Config& operator=(Config const&) = delete;
256 ConfigurationElement& operator[](const std::string& name);
257 void alias(const std::string& realname, const std::string& aliasname);
259 template <class T, class... A> TypedConfigurationElement<T>* register_option(const std::string& name, A&&... a)
261 xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.",
263 TypedConfigurationElement<T>* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
264 XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
265 variable->get_type_name(), variable, this);
266 options[name].reset(variable);
272 void dump(const char *name, const char *indent);
277 ConfigurationElement* get_dict_element(const std::string& name);
282 atexit(&sg_config_finalize);
285 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
287 auto opt = options.find(name);
288 if (opt != options.end()) {
289 return opt->second.get();
291 auto als = aliases.find(name);
292 if (als != aliases.end()) {
293 ConfigurationElement* res = als->second;
294 if (warn_for_aliases)
295 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str());
298 std::string msg = "Bad config key: " + name + "\nExisting config keys:\n";
299 for (auto const& elm : options)
300 msg += " " + elm.first + ": (" + elm.second->get_type_name() + ")" + elm.second->get_string_value() + "\n";
301 throw std::out_of_range(msg);
306 inline ConfigurationElement& Config::operator[](const std::string& name)
308 return *(get_dict_element(name));
311 void Config::alias(const std::string& realname, const std::string& aliasname)
313 xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
314 ConfigurationElement* element = this->get_dict_element(realname);
315 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
316 this->aliases.insert({aliasname, element});
319 /** @brief Dump a config set for debugging purpose
321 * @param name The name to give to this config set
322 * @param indent what to write at the beginning of each line (right number of spaces)
324 void Config::dump(const char *name, const char *indent)
326 XBT_LOG_DEFAULT_CATEGORY(xbt_help);
328 XBT_VERB("%s>> Dumping of the config set '%s':", indent, name);
330 for (auto const& elm : options)
331 XBT_VERB("%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(),
332 elm.second->get_string_value().c_str());
335 XBT_VERB("%s<< End of the config set '%s'", indent, name);
338 /** @brief Displays the declared aliases and their replacement */
339 void Config::show_aliases()
341 for (auto const& elm : aliases)
342 XBT_HELP(" %-40s %s", elm.first.c_str(), elm.second->get_key().c_str());
345 /** @brief Displays the declared options and their description */
348 for (auto const& elm : options) {
349 simgrid::config::ConfigurationElement* variable = elm.second.get();
350 XBT_HELP(" %s: %s", elm.first.c_str(), variable->get_description().c_str());
351 XBT_HELP(" Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
355 // ***** set_default *****
357 template <class T> XBT_PUBLIC void set_default(const char* name, T value)
359 (*simgrid_config)[name].set_default_value<T>(std::move(value));
362 template XBT_PUBLIC void set_default<int>(const char* name, int value);
363 template XBT_PUBLIC void set_default<double>(const char* name, double value);
364 template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
365 template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
367 bool is_default(const char* name)
369 return (*simgrid_config)[name].is_default();
372 // ***** set_value *****
374 template <class T> XBT_PUBLIC void set_value(const char* name, T value)
376 (*simgrid_config)[name].set_value<T>(std::move(value));
379 template XBT_PUBLIC void set_value<int>(const char* name, int value);
380 template XBT_PUBLIC void set_value<double>(const char* name, double value);
381 template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
382 template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
384 void set_as_string(const char* name, const std::string& value)
386 (*simgrid_config)[name].set_string_value(value.c_str());
389 void set_parse(const std::string& opt)
391 std::string options(opt);
392 XBT_DEBUG("List to parse and set:'%s'", options.c_str());
393 while (not options.empty()) {
394 XBT_DEBUG("Still to parse and set: '%s'", options.c_str());
397 size_t pos = options.find_first_not_of(" \t\n,");
398 options.erase(0, pos);
400 pos = options.find_first_of(" \t\n,");
401 std::string name = options.substr(0, pos);
402 options.erase(0, pos);
403 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), options.c_str());
408 pos = name.find(':');
409 xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
412 std::string val = name.substr(pos + 1);
415 const std::string path("path");
416 if (name.compare(0, path.length(), path) != 0)
417 XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
419 set_as_string(name.c_str(), val);
423 // ***** get_value *****
425 template <class T> XBT_PUBLIC T const& get_value(const std::string& name)
427 return (*simgrid_config)[name].get_value<T>();
430 template XBT_PUBLIC int const& get_value<int>(const std::string& name);
431 template XBT_PUBLIC double const& get_value<double>(const std::string& name);
432 template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
433 template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
437 void alias(const char* realname, std::initializer_list<const char*> aliases)
439 for (auto const& aliasname : aliases)
440 simgrid_config->alias(realname, aliasname);
443 // ***** declare_flag *****
446 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
447 std::function<void(const T&)> callback)
449 if (simgrid_config == nullptr)
450 simgrid_config = new simgrid::config::Config();
451 simgrid_config->register_option<T>(name, description, std::move(value), std::move(callback));
454 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
455 std::function<void(int const&)> callback);
456 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
457 std::function<void(double const&)> callback);
458 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
459 std::function<void(bool const&)> callback);
460 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
461 std::function<void(std::string const&)> callback);
465 delete simgrid_config;
466 simgrid_config = nullptr;
471 simgrid_config->show_aliases();
476 simgrid_config->help();
481 /*----[ Setting ]---------------------------------------------------------*/
483 /** @brief Set an integer value to \a name within \a cfg
485 * @param key the name of the variable
486 * @param value the value of the variable
488 void sg_cfg_set_int(const char* key, int value)
490 (*simgrid_config)[key].set_value<int>(value);
493 /** @brief Set or add a double value to \a name within \a cfg
495 * @param key the name of the variable
496 * @param value the double to set
498 void sg_cfg_set_double(const char* key, double value)
500 (*simgrid_config)[key].set_value<double>(value);
503 /** @brief Set or add a string value to \a name within \a cfg
505 * @param key the name of the variable
506 * @param value the value to be added
509 void sg_cfg_set_string(const char* key, const char* value)
511 (*simgrid_config)[key].set_value<std::string>(value);
514 /** @brief Set or add a boolean value to \a name within \a cfg
516 * @param key the name of the variable
517 * @param value the value of the variable
519 void sg_cfg_set_boolean(const char* key, const char* value)
521 (*simgrid_config)[key].set_value<bool>(simgrid::config::parse_bool(value));
524 /*----[ Getting ]---------------------------------------------------------*/
525 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
527 * @param key the name of the variable
529 * Returns the first value from the config set under the given name.
531 int sg_cfg_get_int(const char* key)
533 return (*simgrid_config)[key].get_value<int>();
536 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
538 * @param key the name of the variable
540 * Returns the first value from the config set under the given name.
542 double sg_cfg_get_double(const char* key)
544 return (*simgrid_config)[key].get_value<double>();
547 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
549 * @param key the name of the variable
551 * Returns the first value from the config set under the given name.
552 * If there is more than one value, it will issue a warning.
554 int sg_cfg_get_boolean(const char* key)
556 return (*simgrid_config)[key].get_value<bool>() ? 1 : 0;