X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/9d446af3d297ad838f88db08fc5706f386362ddc..9d0bdd2e04d23fbcc1db6e7e525cf79979c4643b:/include/xbt/config.hpp diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index f87f525c38..af350a5307 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -7,9 +7,13 @@ #ifndef _XBT_CONFIG_HPP_ #define _XBT_CONFIG_HPP_ +#include + #include +#include #include #include +#include #include #include @@ -17,92 +21,70 @@ 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) +bool parseBool(const char* value); +double parseDouble(const char* value); +long int parseLong(const char* value); + +template struct parse_option { + static inline T parse(const char* value) { - return xbt_cfg_get_boolean(name) ? true : false; + return T(value); } }; -template<> struct base_type { - typedef int type; - static inline int get(const char* name) + +template<> struct parse_option { + static inline std::string parse(const char* value) { - return xbt_cfg_get_int(name); + return std::string(value); } }; -template<> struct base_type { - typedef double type; - static inline double get(const char* name) + +template<> +struct parse_option { + static inline double parse(const char* value) { - return xbt_cfg_get_double(name); + return parseDouble(value); } }; -template<> struct base_type { - typedef std::string type; - static inline std::string get(const char* name) + +template<> +struct parse_option { + static inline double parse(const char* value) { - char* value = xbt_cfg_get_string(name); - return value != nullptr ? value : ""; + return parseLong(value); + } +}; + +template<> +struct parse_option { + static inline bool parse(const char* value) + { + return parseBool(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) -{ - return T(base_type::value>::get(name)); -} template inline -T get(std::string const& name) +T parse(const char* value) { - return T(base_type::value>::get(name.c_str())); + return parse_option::parse(value); } -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) +template inline +std::string to_string(T&& value) { - xbt_cfg_setdefault_string(name, value); + return std::to_string(std::forward(value)); } -inline void setDefault(const char* name, std::string const& value) +inline std::string const& to_string(std::string& value) { - xbt_cfg_setdefault_string(name, value.c_str()); + return value; } -inline void setDefault(const char* name, bool value) +inline std::string const& to_string(std::string const& value) { - xbt_cfg_setdefault_boolean(name, value ? "yes" : "no"); + return value; } - -/** Set the default value of a given configuration option */ -template inline -void setDefault(const char* name, T value) +inline std::string to_string(std::string&& value) { - setDefault(name, base_type::value>::type(value)); + return std::move(value); } // Register: @@ -111,12 +93,10 @@ 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 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); +XBT_PUBLIC(void) declareFlag(const char* name, const char* description, + std::function callback); /** Bind a variable to configuration flag * @@ -125,30 +105,91 @@ XBT_PUBLIC(void) registerConfig(const char* name, const char* description, * @param description Option description */ template -void declareFlag(T& value, const char* name, const char* description) +void bindFlag(T& value, const char* name, const char* description) { - registerConfig(name, description, cfgelm_type::value, - [&value](const char* name) { - value = simgrid::config::get(name); - }); - setDefault(name, value); + using namespace std; + declareFlag(name, description, [&value](const char* val) { + value = simgrid::config::parse(val); + }); + xbt_cfg_setdefault_string(name, simgrid::config::to_string(value).c_str()); } /** 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::bindFlag(a, "x", [](const char* value) {
+ *    return simgrid::config::parse(value);
+ *  }
+ *  
+ */ +// F is a parser, F : const char* -> T +template +typename std::enable_if()(std::declval()) + ) >::type +>::value, void>::type +bindFlag(T& value, const char* name, const char* description, + F callback) +{ + declareFlag(name, description, [&value,callback](const char* val) { + value = callback(val); + }); + xbt_cfg_setdefault_string(name, to_string(value).c_str()); +} + +/** Bind a variable to configuration flag + * + *

+ *  static int x;
+ *  simgrid::config::bindFlag(a, "x", [](int x) {
+ *    if (x < x_min || x => x_max)
+ *      throw std::range_error("must be in [x_min, x_max)")
+ *  });
+ *  
*/ +// F is a checker, F : T& -> () template -void declareFlag(T& value, const char* name, const char* description, F f) +typename std::enable_if()(std::declval()) ) +>::value, void>::type +bindFlag(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); + declareFlag(name, description, [&value,callback](const char* val) { + T res = parse(val); + callback(res); + value = std::move(res); + }); + xbt_cfg_setdefault_string(name, to_string(value).c_str()); +} + +/** Bind a variable to configuration flag + * + *

+ *  static int x;
+ *  simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
+ *  
+ */ +// F is a predicate, F : T const& -> bool +template +typename std::enable_if()(std::declval()) ) +>::value, void>::type +bindFlag(T& value, const char* name, const char* description, + F callback) +{ + declareFlag(name, description, [&value,callback](const char* val) { + T res = parse(val); + if (!callback(res)) + throw std::range_error("invalid value"); + value = std::move(res); + }); + xbt_cfg_setdefault_string(name, to_string(value).c_str()); } /** A variable bound to a CLI option @@ -160,7 +201,7 @@ void declareFlag(T& value, const char* name, const char* description, F f) * */ template -class flag { +class Flag { T value_; public: @@ -170,14 +211,20 @@ 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) { - declareFlag(value_, name, desc); + simgrid::config::bindFlag(value_, name, desc); + } + + template + Flag(const char* name, const char* desc, T value, F callback) : value_(value) + { + simgrid::config::bindFlag(value_, name, desc, 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,6 +233,16 @@ public: // Implicit conversion to the underlying type: operator T&() { return value_; } operator T const&() const{ return value_; } + + // Basic interop with T: + Flag& operator=(T const& that) { value_ = that; return *this; } + Flag& operator=(T && that) { value_ = that; return *this; } + bool operator==(T const& that) const { return value_ == that; } + bool operator!=(T const& that) const { return value_ != that; } + bool operator<(T const& that) const { return value_ < that; } + bool operator>(T const& that) const { return value_ > that; } + bool operator<=(T const& that) const { return value_ <= that; } + bool operator>=(T const& that) const { return value_ >= that; } }; }