X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/0cfb40d124549f4dde6f00095847de0d04828adf..a8760c75c04d098c2c100f94be5b99ceb83b0c17:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index f87f525c38..202bb4ab17 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -1,109 +1,74 @@ -/* Copyright (c) 2016. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2016-2018. 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. */ -#ifndef _XBT_CONFIG_HPP_ -#define _XBT_CONFIG_HPP_ +#ifndef XBT_CONFIG_HPP +#define XBT_CONFIG_HPP + +#include + +#include #include +#include +#include +#include #include #include +#include #include #include +#include namespace simgrid { namespace config { -/** Get the base type of a e_xbt_cfgelm_type_t - * - * * `type` is the type used in the config framework to store - * the values of this type; - * - * * `get()` is used to get such a value from the configuration. - */ -template -struct base_type; -template<> struct base_type { - typedef bool type; - static inline bool get(const char* name) - { - return xbt_cfg_get_boolean(name) ? true : false; - } -}; -template<> struct base_type { - typedef int type; - static inline int get(const char* name) - { - return xbt_cfg_get_int(name); - } -}; -template<> struct base_type { - typedef double type; - static inline double get(const char* name) - { - return xbt_cfg_get_double(name); - } -}; -template<> struct base_type { - typedef std::string type; - static inline std::string get(const char* name) - { - char* value = xbt_cfg_get_string(name); - return value != nullptr ? value : ""; - } -}; - -/** Associate the e_xbt_cfgelm_type_t to use for a given type */ -template -struct cfgelm_type : public std::integral_constant::value ? xbt_cfgelm_boolean : - std::is_integral::value ? xbt_cfgelm_int : - std::is_floating_point::value ? xbt_cfgelm_double : - xbt_cfgelm_string> -{}; - -/** Get a value of a given type from the configuration */ template inline -T get(const char* name) +std::string to_string(T&& value) { - return T(base_type::value>::get(name)); + return std::to_string(std::forward(value)); } -template inline -T get(std::string const& name) -{ - return T(base_type::value>::get(name.c_str())); -} - -inline void setDefault(const char* name, int value) -{ - xbt_cfg_setdefault_int(name, value); -} -inline void setDefault(const char* name, double value) -{ - xbt_cfg_setdefault_double(name, value); -} -inline void setDefault(const char* name, const char* value) +inline std::string const& to_string(std::string& value) { - xbt_cfg_setdefault_string(name, value); + return value; } -inline void setDefault(const char* name, std::string const& value) +inline std::string const& to_string(std::string const& value) { - xbt_cfg_setdefault_string(name, value.c_str()); + return value; } -inline void setDefault(const char* name, bool value) +inline std::string to_string(std::string&& value) { - xbt_cfg_setdefault_boolean(name, value ? "yes" : "no"); + return std::move(value); } -/** Set the default value of a given configuration option */ -template inline -void setDefault(const char* name, T value) -{ - setDefault(name, base_type::value>::type(value)); -} +// Set default + +template XBT_PUBLIC void set_default(const char* name, T value); + +extern template XBT_PUBLIC void set_default(const char* name, int value); +extern template XBT_PUBLIC void set_default(const char* name, double value); +extern template XBT_PUBLIC void set_default(const char* name, bool value); +extern template XBT_PUBLIC void set_default(const char* name, std::string value); + +// Set config + +template XBT_PUBLIC void set_value(const char* name, T value); + +extern template XBT_PUBLIC void set_value(const char* name, int value); +extern template XBT_PUBLIC void set_value(const char* name, double value); +extern template XBT_PUBLIC void set_value(const char* name, bool value); +extern template XBT_PUBLIC void set_value(const char* name, std::string value); + +// Get config + +template XBT_PUBLIC T const& get_value(const char* name); + +extern template XBT_PUBLIC int const& get_value(const char* name); +extern template XBT_PUBLIC double const& get_value(const char* name); +extern template XBT_PUBLIC bool const& get_value(const char* name); +extern template XBT_PUBLIC std::string const& get_value(const char* name); // Register: @@ -111,12 +76,25 @@ void setDefault(const char* name, T value) * * @param name name of the option * @param description Description of the option - * @param type config storage type for the option - * @param callback called with the option name (expected to use `simgrid::config::get`) + * @param value Initial/default value + * @param callback called with the option value */ -XBT_PUBLIC(void) registerConfig(const char* name, const char* description, - e_xbt_cfgelm_type_t type, - std::function callback); +template +XBT_PUBLIC void declare_flag(const char* name, const char* description, T value, + std::function callback = std::function()); + +extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, int value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, double value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, bool value, + std::function callback); +extern template XBT_PUBLIC void declare_flag(const char* name, const char* 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 * @@ -124,43 +102,102 @@ XBT_PUBLIC(void) registerConfig(const char* name, const char* description, * @param name Flag name * @param description Option description */ -template -void declareFlag(T& value, const char* name, const char* description) +template void bind_flag(T& value, const char* name, const char* description) +{ + declare_flag(name, description, value, [&value](T const& val) { value = val; }); +} + +template +void bind_flag(T& value, const char* name, std::initializer_list aliases, const char* description) { - registerConfig(name, description, cfgelm_type::value, - [&value](const char* name) { - value = simgrid::config::get(name); - }); - setDefault(name, value); + bind_flag(value, name, description); + alias(name, std::move(aliases)); } /** Bind a variable to configuration flag * - * @param value Bound variable - * @param name Flag name - * @param description Option description - * @f Callback (called with the option name) providing the value + *

+ *  static int x;
+ *  simgrid::config::bind_flag(a, "x", [](int x) {
+ *    if (x < x_min || x => x_max)
+ *      throw std::range_error("must be in [x_min, x_max)")
+ *  });
+ *  
*/ -template -void declareFlag(T& value, const char* name, const char* description, F f) +// F is a checker, F : T& -> () +template +typename std::enable_if()(std::declval()))>::value, void>::type +bind_flag(T& value, const char* name, const char* description, F callback) { - registerConfig(name, description, cfgelm_type::value, - [&value,f](const char* name) { - value = f(name); - }); - setDefault(name, value); + declare_flag(name, description, value, std::function([&value, callback](const T& val) { + callback(val); + value = std::move(val); + })); +} + +template +typename std::enable_if()(std::declval()))>::value, void>::type +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)); +} + +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) +{ + 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 (not found || std::string(val) == "help") { + std::string mesg; + if (std::string(val) == "help") + mesg = std::string("\nPossible values for option ") + name + ":\n"; + else + mesg = std::string("\nInvalid 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); + })); +} +/** Bind a variable to configuration flag + * + *

+ *  static int x;
+ *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
+ *  
+ */ +// F is a predicate, F : T const& -> bool +template +typename std::enable_if()(std::declval()))>::value, void>::type +bind_flag(T& value, const char* name, const char* description, F callback) +{ + declare_flag(name, description, value, std::function([&value, callback](const T& val) { + if (not callback(val)) + throw std::range_error("invalid value."); + value = std::move(val); + })); } /** A variable bound to a CLI option * *

  *  static simgrid::config::flag answer("answer", "Expected answer", 42);
- *  static simgrid::config::flag name("name", "Ford Prefect", "John Doe");
+ *  static simgrid::config::flag name("name", "Ford Perfect", "John Doe");
  *  static simgrid::config::flag gamma("gamma", "Gamma factor", 1.987);
  *  
*/ template -class flag { +class Flag { T value_; public: @@ -170,14 +207,45 @@ public: * @param desc Flag description * @param value Flag initial/default value */ - flag(const char* name, const char* desc, T value) : value_(value) + Flag(const char* name, const char* desc, T value) : value_(value) + { + 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) : value_(value) { - declareFlag(value_, name, desc); + simgrid::config::bind_flag(value_, name, std::move(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) + { + 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) + : value_(value) + { + simgrid::config::bind_flag(value_, name, std::move(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) : value_(value) + { + simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback)); } // No copy: - flag(flag const&) = delete; - flag& operator=(flag const&) = delete; + Flag(Flag const&) = delete; + Flag& operator=(Flag const&) = delete; // Get the underlying value: T& get() { return value_; } @@ -186,9 +254,29 @@ public: // Implicit conversion to the underlying type: operator T&() { return value_; } operator T const&() const{ return value_; } + + // Basic interop with T: + template + Flag& operator=(U const& that) { value_ = that; return *this; } + template + Flag& operator=(U && that) { value_ = that; return *this; } + template + bool operator==(U const& that) const { return value_ == that; } + template + bool operator!=(U const& that) const { return value_ != that; } + template + bool operator<(U const& that) const { return value_ < that; } + template + bool operator>(U const& that) const { return value_ > that; } + template + bool operator<=(U const& that) const { return value_ <= that; } + template + bool operator>=(U const& that) const { return value_ >= that; } }; } } +XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value") XBT_PUBLIC std::string + xbt_cfg_get_string(const char* name); #endif