X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/5ed37babb2fa9097abe82df299c0aa259ed84d5a..bfffef69e1b0554d13eec920f1a32f75b49ceac6:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index 8071dbd433..bb5ee91f36 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -22,8 +22,7 @@ #include #include -namespace simgrid { -namespace config { +namespace simgrid::config { class Config; @@ -92,10 +91,10 @@ XBT_PUBLIC void alias(const char* realname, std::initializer_list a */ 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 = std::function()) + T value, std::function callback = nullptr) { declare_flag(name, description, std::move(value), std::move(callback)); alias(name.c_str(), aliases); @@ -140,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_t()(std::declval()))>::value, void> +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) { @@ -150,7 +149,7 @@ bind_flag(T& value, const char* name, const char* description, F callback) } template -typename std::enable_if_t()(std::declval()))>::value, void> +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)); @@ -158,8 +157,7 @@ bind_flag(T& value, const char* name, std::initializer_list aliases } template -typename std::enable_if_t()(std::declval()))>::value, - void> +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) { @@ -175,14 +173,13 @@ bind_flag(std::string& value, const char* name, const char* description, mesg += std::string("Possible values for option ") + name + ":\n"; else mesg += "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"; + 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_t()(std::declval()))>::value, - void> +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) { @@ -199,7 +196,7 @@ bind_flag(std::string& value, const char* name, std::initializer_list bool template -typename std::enable_if_t()(std::declval()))>::value, void> +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) { @@ -261,6 +258,17 @@ public: /* 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, xbt::type_identity_t value, const std::map>& valid_values, F callback) @@ -314,7 +322,6 @@ XBT_PUBLIC void finalize(); XBT_PUBLIC void show_aliases(); XBT_PUBLIC void help(); -} // namespace config -} // namespace simgrid +} // namespace simgrid::config #endif