X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/c19a107a096f503e67217fb178fa98eb742ceb4d..639c6962daa378323864a53174e0e7fe2fcaef1e:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index 87343a0c19..8071dbd433 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016-2021. 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,6 +20,7 @@ #include #include +#include namespace simgrid { namespace config { @@ -76,6 +77,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 @@ -88,6 +93,13 @@ 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()); +template +void declare_flag(const std::string& name, std::initializer_list aliases, const std::string& description, + T value, std::function callback = std::function()) +{ + 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 +110,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 @@ -166,7 +174,7 @@ bind_flag(std::string& value, const char* name, const char* description, 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"; + 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"; xbt_die("%s", mesg.c_str()); @@ -204,9 +212,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 @@ -221,13 +229,13 @@ public: * @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, aliases, desc); @@ -236,13 +244,15 @@ public: /* 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, aliases, desc, std::move(callback)); @@ -252,8 +262,8 @@ 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, const 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, valid_values, std::move(callback)); @@ -261,7 +271,7 @@ public: /* A constructor with everything */ template - 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, const std::map>& valid_values, F callback) : value_(value), name_(name) { @@ -303,6 +313,7 @@ public: XBT_PUBLIC void finalize(); XBT_PUBLIC void show_aliases(); XBT_PUBLIC void help(); + } // namespace config } // namespace simgrid