X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/905948d05c644e30242007ee7fea0f0e1254452e..HEAD:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index b6f60f00f6..bb5ee91f36 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2016-2023. 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. */ @@ -20,9 +20,9 @@ #include #include +#include -namespace simgrid { -namespace config { +namespace simgrid::config { class Config; @@ -76,6 +76,10 @@ extern template XBT_PUBLIC double const& get_value(const std::string& na 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: /** Register a configuration flag @@ -87,7 +91,14 @@ extern template XBT_PUBLIC std::string const& get_value(const std:: */ template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value, - std::function callback = std::function()); + std::function callback = nullptr); +template +void declare_flag(const std::string& name, std::initializer_list aliases, const std::string& description, + T value, std::function callback = nullptr) +{ + 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); @@ -98,10 +109,6 @@ extern template XBT_PUBLIC void declare_flag(const std::string& name, const std: extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value, std::function callback); -// ***** alias ***** - -XBT_PUBLIC void alias(const char* realname, std::initializer_list aliases); - /** Bind a variable to configuration flag * * @param value Bound variable @@ -117,7 +124,7 @@ template void bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description) { bind_flag(value, name, description); - alias(name, std::move(aliases)); + alias(name, aliases); } /** Bind a variable to configuration flag @@ -132,7 +139,7 @@ void bind_flag(T& value, const char* name, std::initializer_list al */ // F is a checker, F : T& -> () template -typename std::enable_if()(std::declval()))>::value, void>::type +typename std::enable_if_t()(std::declval()))>, 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) { @@ -142,46 +149,42 @@ bind_flag(T& value, const char* name, const char* description, F callback) } template -typename std::enable_if()(std::declval()))>::value, void>::type +typename std::enable_if_t()(std::declval()))>, void> bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description, F callback) { bind_flag(value, name, description, std::move(callback)); - alias(name, std::move(aliases)); + alias(name, aliases); } -template -typename std::enable_if()(std::declval()))>::value, void>::type -bind_flag(T& value, const char* name, const char* description, std::map valid_values, F callback) +template +typename std::enable_if_t()(std::declval()))>, 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 T& val) { + std::function([&value, name, valid_values, callback](const std::string& val) { callback(val); - bool found = false; - for (auto kv : valid_values) { - if (kv.first == val) - found = true; - } - if (not found || std::string(val) == "help") { - std::string mesg = std::string("\n"); - if (std::string(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 kv : valid_values) - mesg += " - '" + std::string(kv.first) + "': " + kv.second + - (kv.first == value ? " <=== DEFAULT" : "") + "\n"; - xbt_die("%s", mesg.c_str()); + if (valid_values.find(val) != valid_values.end()) { + value = val; + return; } - value = std::move(val); + std::string mesg = "\n"; + if (val == "help") + mesg += std::string("Possible values for option ") + name + ":\n"; + else + mesg += "Invalid value '" + val + "' for option " + name + ". Possible values:\n"; + for (auto const& [v, descr] : valid_values) + mesg += " - '" + v + "': " + descr + (v == value ? " <=== DEFAULT" : "") + "\n"; + xbt_die("%s", mesg.c_str()); })); } -template -typename std::enable_if()(std::declval()))>::value, void>::type -bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description, - std::map valid_values, F callback) +template +typename std::enable_if_t()(std::declval()))>, void> +bind_flag(std::string& value, const char* name, std::initializer_list aliases, const char* description, + const std::map>& valid_values, F callback) { - bind_flag(value, name, description, std::move(valid_values), std::move(callback)); - alias(name, std::move(aliases)); + bind_flag(value, name, description, valid_values, std::move(callback)); + alias(name, aliases); } /** Bind a variable to configuration flag @@ -193,7 +196,7 @@ bind_flag(T& value, const char* name, std::initializer_list aliases */ // F is a predicate, F : T const& -> bool template -typename std::enable_if()(std::declval()))>::value, void>::type +typename std::enable_if_t()(std::declval()))>, 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) { @@ -206,9 +209,9 @@ bind_flag(T& value, const char* name, const char* description, F callback) /** A variable bound to a CLI option * *

- *  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);
+ *  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 @@ -217,57 +220,70 @@ class Flag { 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), name_(name) + Flag(const char* name, const char* desc, xbt::type_identity_t value) : value_(value), name_(name) { simgrid::config::bind_flag(value_, name, desc); } /** Constructor taking also an array of aliases for name */ - Flag(const char* name, std::initializer_list aliases, const char* desc, T value) + Flag(const char* name, std::initializer_list aliases, const char* desc, xbt::type_identity_t value) : value_(value), name_(name) { - simgrid::config::bind_flag(value_, name, std::move(aliases), desc); + 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, T value, F callback) : value_(value), name_(name) + 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, T value, F callback) + 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, std::move(aliases), desc, std::move(callback)); + 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. */ + Flag(const char* name, const char* desc, xbt::type_identity_t value, + const std::map>& valid_values) + : value_(value), name_(name) + { + simgrid::config::bind_flag(value_, name, desc, valid_values, [](const std::string&) {}); + } + + /* As earlier, 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. + * But also take a callback that is invoked before the verification of parameter name validity. + */ template - Flag(const char* name, const char* desc, T value, std::map valid_values, F callback) + 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, std::move(valid_values), std::move(callback)); + 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, T value, - std::map valid_values, F callback) + 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, std::move(aliases), desc, std::move(valid_values), std::move(callback)); + simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback)); } // No copy: @@ -287,7 +303,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 @@ -305,9 +321,7 @@ public: XBT_PUBLIC void finalize(); XBT_PUBLIC void show_aliases(); XBT_PUBLIC void help(); -} -} -XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value") XBT_PUBLIC std::string - xbt_cfg_get_string(const char* name); + +} // namespace simgrid::config #endif