X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/65beede857f93e33c96e544f76b93793c973dc10..1361e008419329de33e4c92a6ad96621d40a9c40:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index a0406db96d..10440094fd 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-2020. 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. */ @@ -151,36 +151,32 @@ bind_flag(T& value, const char* name, std::initializer_list 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) +bind_flag(T& 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) { callback(val); - bool found = false; - for (auto kv : valid_values) { - if (kv.first == val) - found = true; + if (valid_values.find(val) != valid_values.end()) { + value = std::move(val); + return; } - 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()); - } - value = std::move(val); + 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 const& kv : valid_values) + mesg += " - '" + std::string(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) + const std::map& valid_values, F callback) { - bind_flag(value, name, description, std::move(valid_values), std::move(callback)); + bind_flag(value, name, description, valid_values, std::move(callback)); alias(name, std::move(aliases)); } @@ -217,7 +213,6 @@ class Flag { std::string name_; public: - /** Constructor * * @param name Flag name @@ -255,7 +250,7 @@ public: * 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)); @@ -264,10 +259,10 @@ public: /* 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, std::move(aliases), desc, valid_values, std::move(callback)); } // No copy: @@ -287,7 +282,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,7 +300,7 @@ public: XBT_PUBLIC void finalize(); XBT_PUBLIC void show_aliases(); XBT_PUBLIC void help(); -} -} +} // namespace config +} // namespace simgrid #endif