1 /* Copyright (c) 2004-2020. 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 static 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 static 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 static 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))
185 ~TypedConfigurationElement() override = default;
187 std::string get_string_value() override;
188 const char* get_type_name() override;
189 void set_string_value(const char* value) override;
194 this->callback(this->content);
197 T const& get_value() const { return content; }
199 void set_value(T value)
201 this->content = std::move(value);
203 this->unset_default();
206 void set_default_value(T value)
208 if (this->is_default()) {
209 this->content = std::move(value);
212 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
213 get_key().c_str(), to_string(value).c_str());
218 template <class T> std::string TypedConfigurationElement<T>::get_string_value() // override
220 return to_string(content);
223 template <class T> void TypedConfigurationElement<T>::set_string_value(const char* value) // override
225 this->content = ConfigType<T>::parse(value);
226 this->unset_default();
230 template <class T> const char* TypedConfigurationElement<T>::get_type_name() // override
232 return ConfigType<T>::type_name;
235 } // end of anonymous namespace
241 // name -> ConfigElement:
242 std::map<std::string, std::unique_ptr<ConfigurationElement>> options;
243 // alias -> ConfigElement from options:
244 std::map<std::string, ConfigurationElement*> aliases;
245 bool warn_for_aliases = true;
251 Config(Config const&) = delete;
252 Config& operator=(Config const&) = delete;
254 ConfigurationElement& operator[](const std::string& name);
255 void alias(const std::string& realname, const std::string& aliasname);
257 template <class T, class... A> TypedConfigurationElement<T>* register_option(const std::string& name, A&&... a)
259 xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.",
261 auto* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
262 XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name.c_str(), variable->get_description().c_str(),
263 variable->get_type_name(), variable, this);
264 options[name].reset(variable);
270 void dump(const char* name, const char* indent) const;
271 void show_aliases() const;
275 ConfigurationElement* get_dict_element(const std::string& name);
280 atexit(&sg_config_finalize);
283 inline ConfigurationElement* Config::get_dict_element(const std::string& name)
285 auto opt = options.find(name);
286 if (opt != options.end()) {
287 return opt->second.get();
289 auto als = aliases.find(name);
290 if (als != aliases.end()) {
291 ConfigurationElement* res = als->second;
292 if (warn_for_aliases)
293 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str());
296 std::string msg = "Bad config key: " + name + "\nExisting config keys:\n";
297 for (auto const& elm : options)
298 msg += " " + elm.first + ": (" + elm.second->get_type_name() + ")" + elm.second->get_string_value() + "\n";
299 throw std::out_of_range(msg);
304 inline ConfigurationElement& Config::operator[](const std::string& name)
306 return *(get_dict_element(name));
309 void Config::alias(const std::string& realname, const std::string& aliasname)
311 xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
312 ConfigurationElement* element = this->get_dict_element(realname);
313 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
314 this->aliases.insert({aliasname, element});
317 /** @brief Dump a config set for debugging purpose
319 * @param name The name to give to this config set
320 * @param indent what to write at the beginning of each line (right number of spaces)
322 void Config::dump(const char* name, const char* indent) const
325 XBT_CVERB(xbt_help, "%s>> Dumping of the config set '%s':", indent, name);
327 for (auto const& elm : options)
328 XBT_CVERB(xbt_help, "%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(),
329 elm.second->get_string_value().c_str());
332 XBT_CVERB(xbt_help, "%s<< End of the config set '%s'", indent, name);
335 /** @brief Displays the declared aliases and their replacement */
336 void Config::show_aliases() const
338 for (auto const& elm : aliases)
339 XBT_HELP(" %-40s %s", elm.first.c_str(), elm.second->get_key().c_str());
342 /** @brief Displays the declared options and their description */
343 void Config::help() const
345 for (auto const& elm : options) {
346 simgrid::config::ConfigurationElement* variable = elm.second.get();
347 XBT_HELP(" %s: %s", elm.first.c_str(), variable->get_description().c_str());
348 XBT_HELP(" Type: %s; Current value: %s", variable->get_type_name(), variable->get_string_value().c_str());
352 // ***** set_default *****
354 template <class T> XBT_PUBLIC void set_default(const char* name, T value)
356 (*simgrid_config)[name].set_default_value<T>(std::move(value));
359 template XBT_PUBLIC void set_default<int>(const char* name, int value);
360 template XBT_PUBLIC void set_default<double>(const char* name, double value);
361 template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
362 template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
364 bool is_default(const char* name)
366 return (*simgrid_config)[name].is_default();
369 // ***** set_value *****
371 template <class T> XBT_PUBLIC void set_value(const char* name, T value)
373 (*simgrid_config)[name].set_value<T>(std::move(value));
376 template XBT_PUBLIC void set_value<int>(const char* name, int value);
377 template XBT_PUBLIC void set_value<double>(const char* name, double value);
378 template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
379 template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
381 void set_as_string(const char* name, const std::string& value)
383 (*simgrid_config)[name].set_string_value(value.c_str());
386 void set_parse(const std::string& opt)
388 std::string options(opt);
389 XBT_DEBUG("List to parse and set:'%s'", options.c_str());
390 while (not options.empty()) {
391 XBT_DEBUG("Still to parse and set: '%s'", options.c_str());
394 size_t pos = options.find_first_not_of(" \t\n,");
395 options.erase(0, pos);
397 pos = options.find_first_of(" \t\n,");
398 std::string name = options.substr(0, pos);
399 options.erase(0, pos);
400 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), options.c_str());
405 pos = name.find(':');
406 xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
409 std::string val = name.substr(pos + 1);
412 const std::string path("path");
413 if (name.compare(0, path.length(), path) != 0)
414 XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
416 set_as_string(name.c_str(), val);
420 // ***** get_value *****
422 template <class T> XBT_PUBLIC T const& get_value(const std::string& name)
424 return (*simgrid_config)[name].get_value<T>();
427 template XBT_PUBLIC int const& get_value<int>(const std::string& name);
428 template XBT_PUBLIC double const& get_value<double>(const std::string& name);
429 template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
430 template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
434 void alias(const char* realname, std::initializer_list<const char*> aliases)
436 for (auto const& aliasname : aliases)
437 simgrid_config->alias(realname, aliasname);
440 // ***** declare_flag *****
443 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
444 std::function<void(const T&)> callback)
446 if (simgrid_config == nullptr)
447 simgrid_config = new simgrid::config::Config();
448 simgrid_config->register_option<T>(name, description, std::move(value), std::move(callback));
451 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
452 std::function<void(int const&)> callback);
453 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
454 std::function<void(double const&)> callback);
455 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
456 std::function<void(bool const&)> callback);
457 template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
458 std::function<void(std::string const&)> callback);
462 delete simgrid_config;
463 simgrid_config = nullptr;
468 simgrid_config->show_aliases();
473 simgrid_config->help();
478 /*----[ Setting ]---------------------------------------------------------*/
480 /** @brief Set an integer value to \a name within \a cfg
482 * @param key the name of the variable
483 * @param value the value of the variable
485 void sg_cfg_set_int(const char* key, int value)
487 (*simgrid_config)[key].set_value<int>(value);
490 /** @brief Set or add a double value to \a name within \a cfg
492 * @param key the name of the variable
493 * @param value the double to set
495 void sg_cfg_set_double(const char* key, double value)
497 (*simgrid_config)[key].set_value<double>(value);
500 /** @brief Set or add a string value to \a name within \a cfg
502 * @param key the name of the variable
503 * @param value the value to be added
506 void sg_cfg_set_string(const char* key, const char* value)
508 (*simgrid_config)[key].set_value<std::string>(value);
511 /** @brief Set or add a boolean value to \a name within \a cfg
513 * @param key the name of the variable
514 * @param value the value of the variable
516 void sg_cfg_set_boolean(const char* key, const char* value)
518 (*simgrid_config)[key].set_value<bool>(simgrid::config::parse_bool(value));
521 /*----[ Getting ]---------------------------------------------------------*/
522 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
524 * @param key the name of the variable
526 * Returns the first value from the config set under the given name.
528 int sg_cfg_get_int(const char* key)
530 return (*simgrid_config)[key].get_value<int>();
533 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
535 * @param key the name of the variable
537 * Returns the first value from the config set under the given name.
539 double sg_cfg_get_double(const char* key)
541 return (*simgrid_config)[key].get_value<double>();
544 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
546 * @param key the name of the variable
548 * Returns the first value from the config set under the given name.
549 * If there is more than one value, it will issue a warning.
551 int sg_cfg_get_boolean(const char* key)
553 return (*simgrid_config)[key].get_value<bool>() ? 1 : 0;