X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1b89437671ba572a199dd4ff5766ba82720cdf03..e71a2a302d28430dc1bfee906f842f5f3d0fa3ce:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index 28a83ecfa6..87343a0c19 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2020. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2016-2021. 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. */ @@ -117,7 +117,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 +132,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()))>::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) { @@ -142,46 +142,44 @@ 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()))>::value, 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()))>::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 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 = "\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 += 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()(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()))>::value, + 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 +191,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()))>::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) { @@ -232,7 +230,7 @@ public: Flag(const char* name, std::initializer_list aliases, const char* desc, 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. @@ -247,26 +245,27 @@ public: Flag(const char* name, std::initializer_list aliases, const char* desc, 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. */ template - Flag(const char* name, const char* desc, T value, std::map valid_values, F callback) + Flag(const char* name, const char* desc, 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) + 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: