X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a4a0b7a61683f71695edc5b66c554d209311699f..cc2b1c368ba0d6b499b4f65db23d9f0b6d098aac:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index f5b3c95222..397870e61c 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2016-2022. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -12,25 +12,20 @@ #include #include +#include #include #include #include #include #include -#include +#include +#include namespace simgrid { namespace config { -XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error { -public: - explicit missing_key_error(const std::string& what) - : std::runtime_error(what) {} - explicit missing_key_error(const char* what) - : std::runtime_error(what) {} - ~missing_key_error() override; -}; +class Config; template inline std::string to_string(T&& value) @@ -50,15 +45,41 @@ inline std::string to_string(std::string&& value) return std::move(value); } +// Set default + +template XBT_PUBLIC void set_default(const char* name, T value); + +extern template XBT_PUBLIC void set_default(const char* name, int value); +extern template XBT_PUBLIC void set_default(const char* name, double value); +extern template XBT_PUBLIC void set_default(const char* name, bool value); +extern template XBT_PUBLIC void set_default(const char* name, std::string value); + +XBT_PUBLIC bool is_default(const char* name); + +// Set config + +template XBT_PUBLIC void set_value(const char* name, T value); + +extern template XBT_PUBLIC void set_value(const char* name, int value); +extern template XBT_PUBLIC void set_value(const char* name, double value); +extern template XBT_PUBLIC void set_value(const char* name, bool value); +extern template XBT_PUBLIC void set_value(const char* name, std::string value); + +XBT_PUBLIC void set_as_string(const char* name, const std::string& value); +XBT_PUBLIC void set_parse(const std::string& options); + // Get config -template -XBT_PUBLIC(T const&) getConfig(const char* name); +template XBT_PUBLIC T const& get_value(const std::string& name); -extern template XBT_PUBLIC(int const&) getConfig(const char* name); -extern template XBT_PUBLIC(double const&) getConfig(const char* name); -extern template XBT_PUBLIC(bool const&) getConfig(const char* name); -extern template XBT_PUBLIC(std::string const&) getConfig(const char* name); +extern template XBT_PUBLIC int const& get_value(const std::string& name); +extern template XBT_PUBLIC double const& get_value(const std::string& name); +extern template XBT_PUBLIC bool const& get_value(const std::string& name); +extern template XBT_PUBLIC std::string const& get_value(const std::string& name); + +// ***** alias ***** + +XBT_PUBLIC void alias(const char* realname, std::initializer_list aliases); // Register: @@ -69,140 +90,192 @@ extern template XBT_PUBLIC(std::string const&) getConfig(const char * @param value Initial/default value * @param callback called with the option value */ -template -XBT_PUBLIC(void) declareFlag(const char* name, const char* description, - T value, std::function callback = std::function()); - -extern template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, int value, std::function callback); -extern template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, double value, std::function callback); -extern template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, bool value, std::function callback); -extern template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, std::string value, std::function callback); - -// ***** alias ***** - -XBT_PUBLIC(void) alias(const char* realname, const char* aliasname); - -inline -void alias(std::initializer_list names) +template +XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value, + std::function callback = std::function()); +template +void declare_flag(const std::string& name, std::initializer_list aliases, const std::string& description, + T value, std::function callback = std::function()) { - auto i = names.begin(); - for (++i; i != names.end(); ++i) - alias(*names.begin(), *i); + declare_flag(name, description, std::move(value), std::move(callback)); + alias(name.c_str(), aliases); } +extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value, + std::function callback); + /** Bind a variable to configuration flag * * @param value Bound variable * @param name Flag name * @param description Option description */ -template -void bindFlag(T& value, const char* name, const char* description) +template void bind_flag(T& value, const char* name, const char* description) { - declareFlag(name, description, value, [&value](T const& val) { - value = val; - }); + declare_flag(name, description, value, [&value](T const& val) { value = val; }); } -template -void bindFlag(T& value, std::initializer_list names, const char* description) +template +void bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description) { - bindFlag(value, *names.begin(), description); - alias(names); + bind_flag(value, name, description); + alias(name, aliases); } /** Bind a variable to configuration flag * *

  *  static int x;
- *  simgrid::config::bindFlag(a, "x", [](int x) {
+ *  simgrid::config::bind_flag(a, "x", [](int x) {
  *    if (x < x_min || x => x_max)
  *      throw std::range_error("must be in [x_min, x_max)")
  *  });
  *  
*/ // F is a checker, F : T& -> () -template -typename std::enable_if()(std::declval()) ) ->::value, void>::type -bindFlag(T& value, std::initializer_list names, const char* description, - F callback) +template +typename std::enable_if_t()(std::declval()))>::value, void> +bind_flag(T& value, const char* name, const char* description, F callback) +{ + declare_flag(name, description, value, std::function([&value, callback](const T& val) { + callback(val); + value = std::move(val); + })); +} + +template +typename std::enable_if_t()(std::declval()))>::value, void> +bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description, F callback) { - bindFlag(value, *names.begin(), description); - alias(names); + bind_flag(value, name, description, std::move(callback)); + alias(name, aliases); } -template -typename std::enable_if()(std::declval()) ) ->::value, void>::type -bindFlag(T& value, const char* name, const char* description, - F callback) +template +typename std::enable_if_t()(std::declval()))>::value, + void> +bind_flag(std::string& value, const char* name, const char* description, + const std::map>& valid_values, F callback) +{ + declare_flag(name, description, value, + std::function([&value, name, valid_values, callback](const std::string& val) { + callback(val); + if (valid_values.find(val) != valid_values.end()) { + value = val; + return; + } + std::string mesg = "\n"; + if (val == "help") + mesg += std::string("Possible values for option ") + name + ":\n"; + else + mesg += std::string("Invalid value '") + val + "' for option " + name + ". Possible values:\n"; + for (auto const& kv : valid_values) + mesg += " - '" + kv.first + "': " + kv.second + (kv.first == value ? " <=== DEFAULT" : "") + "\n"; + xbt_die("%s", mesg.c_str()); + })); +} +template +typename std::enable_if_t()(std::declval()))>::value, + void> +bind_flag(std::string& value, const char* name, std::initializer_list aliases, const char* description, + const std::map>& valid_values, F callback) { - declareFlag(name, description, value, [&value,callback](const T& val) { - callback(val); - value = std::move(val); - }); + bind_flag(value, name, description, valid_values, std::move(callback)); + alias(name, aliases); } /** Bind a variable to configuration flag * *

  *  static int x;
- *  simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
+ *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
  *  
*/ // F is a predicate, F : T const& -> bool -template -typename std::enable_if()(std::declval()) ) ->::value, void>::type -bindFlag(T& value, const char* name, const char* description, - F callback) +template +typename std::enable_if_t()(std::declval()))>::value, void> +bind_flag(T& value, const char* name, const char* description, F callback) { - declareFlag(name, description, value, [&value, callback](const T& val) { - if (not callback(val)) - throw std::range_error("invalid value"); - value = std::move(val); - }); + declare_flag(name, description, value, std::function([&value, callback](const T& val) { + if (not callback(val)) + throw std::range_error("invalid value."); + value = std::move(val); + })); } /** A variable bound to a CLI option * *

- *  static simgrid::config::flag answer("answer", "Expected answer", 42);
- *  static simgrid::config::flag name("name", "Ford Prefect", "John Doe");
- *  static simgrid::config::flag gamma("gamma", "Gamma factor", 1.987);
+ *  static simgrid::config::Flag answer("answer", "Expected answer", 42);
+ *  static simgrid::config::Flag name("name", "Ford Perfect", "John Doe");
+ *  static simgrid::config::Flag gamma("gamma", "Gamma factor", 1.987);
  *  
*/ template class Flag { T value_; -public: + std::string name_; +public: /** Constructor * * @param name Flag name * @param desc Flag description * @param value Flag initial/default value */ - Flag(const char* name, const char* desc, T value) : value_(value) + Flag(const char* name, const char* desc, xbt::type_identity_t value) : value_(value), name_(name) { - simgrid::config::bindFlag(value_, name, desc); + simgrid::config::bind_flag(value_, name, desc); } - template - Flag(const char* name, const char* desc, T value, F callback) : value_(value) + /** Constructor taking also an array of aliases for name */ + Flag(const char* name, std::initializer_list aliases, const char* desc, xbt::type_identity_t value) + : value_(value), name_(name) { - simgrid::config::bindFlag(value_, name, desc, std::move(callback)); + simgrid::config::bind_flag(value_, name, aliases, desc); + } + + /* A constructor accepting a callback that will be passed the parameter. + * It can either return a boolean (informing whether the parameter is valid), or returning void. + */ + template + Flag(const char* name, const char* desc, xbt::type_identity_t value, F callback) : value_(value), name_(name) + { + simgrid::config::bind_flag(value_, name, desc, std::move(callback)); + } + + template + Flag(const char* name, std::initializer_list aliases, const char* desc, xbt::type_identity_t value, + F callback) + : value_(value), name_(name) + { + simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback)); + } + + /* A constructor accepting a map of valid values -> their description, + * and producing an informative error message when an invalid value is passed, or when help is passed as a value. + */ + template + Flag(const char* name, const char* desc, xbt::type_identity_t value, + const std::map>& valid_values, F callback) + : value_(value), name_(name) + { + simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback)); + } + + /* A constructor with everything */ + template + Flag(const char* name, std::initializer_list aliases, const char* desc, xbt::type_identity_t value, + const std::map>& valid_values, F callback) + : value_(value), name_(name) + { + simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback)); } // No copy: @@ -213,6 +286,7 @@ public: T& get() { return value_; } T const& get() const { return value_; } + const std::string& get_name() const { return name_; } // Implicit conversion to the underlying type: operator T&() { return value_; } operator T const&() const{ return value_; } @@ -221,7 +295,7 @@ public: template Flag& operator=(U const& that) { value_ = that; return *this; } template - Flag& operator=(U && that) { value_ = that; return *this; } + Flag& operator=(U&& that) { value_ = std::forward(that); return *this; } template bool operator==(U const& that) const { return value_ == that; } template @@ -236,7 +310,11 @@ public: bool operator>=(U const& that) const { return value_ >= that; } }; -} -} +XBT_PUBLIC void finalize(); +XBT_PUBLIC void show_aliases(); +XBT_PUBLIC void help(); + +} // namespace config +} // namespace simgrid #endif