1 /* Copyright (c) 2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef _XBT_CONFIG_HPP_
8 #define _XBT_CONFIG_HPP_
15 #include <type_traits>
19 #include <xbt/config.h>
24 template<class T> inline
25 std::string to_string(T&& value)
27 return std::to_string(std::forward<T>(value));
29 inline std::string const& to_string(std::string& value)
33 inline std::string const& to_string(std::string const& value)
37 inline std::string to_string(std::string&& value)
39 return std::move(value);
45 XBT_PUBLIC(T const&) getConfig(const char* name);
47 extern template XBT_PUBLIC(int const&) getConfig<int>(const char* name);
48 extern template XBT_PUBLIC(double const&) getConfig<double>(const char* name);
49 extern template XBT_PUBLIC(bool const&) getConfig<bool>(const char* name);
50 extern template XBT_PUBLIC(std::string const&) getConfig<std::string>(const char* name);
54 /** Register a configuration flag
56 * @param name name of the option
57 * @param description Description of the option
58 * @param value Initial/default value
59 * @param callback called with the option value
62 XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
63 T value, std::function<void(const T&)> callback = std::function<void(const T&)>());
65 extern template XBT_PUBLIC(void) declareFlag(const char* name,
66 const char* description, int value, std::function<void(int const &)> callback);
67 extern template XBT_PUBLIC(void) declareFlag(const char* name,
68 const char* description, double value, std::function<void(double const &)> callback);
69 extern template XBT_PUBLIC(void) declareFlag(const char* name,
70 const char* description, bool value, std::function<void(bool const &)> callback);
71 extern template XBT_PUBLIC(void) declareFlag(const char* name,
72 const char* description, std::string value, std::function<void(std::string const &)> callback);
74 /** Bind a variable to configuration flag
76 * @param value Bound variable
77 * @param name Flag name
78 * @param description Option description
81 void bindFlag(T& value, const char* name, const char* description)
84 declareFlag<T>(name, description, value, [&value](T const& val) {
89 /** Bind a variable to configuration flag
93 * simgrid::config::bindFlag(a, "x", [](int x) {
94 * if (x < x_min || x => x_max)
95 * throw std::range_error("must be in [x_min, x_max)")
99 // F is a checker, F : T& -> ()
100 template<class T, class F>
101 typename std::enable_if<std::is_same<
103 decltype( std::declval<F>()(std::declval<const T&>()) )
104 >::value, void>::type
105 bindFlag(T& value, const char* name, const char* description,
108 declareFlag(name, description, value, [&value,callback](const T& val) {
110 value = std::move(val);
114 /** Bind a variable to configuration flag
118 * simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
121 // F is a predicate, F : T const& -> bool
122 template<class T, class F>
123 typename std::enable_if<std::is_same<
125 decltype( std::declval<F>()(std::declval<const T&>()) )
126 >::value, void>::type
127 bindFlag(T& value, const char* name, const char* description,
130 declareFlag(name, description, value, [&value,callback](const T& val) {
132 throw std::range_error("invalid value");
133 value = std::move(val);
137 /** A variable bound to a CLI option
140 * static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
141 * static simgrid::config::flag<std::string> name("name", "Ford Prefect", "John Doe");
142 * static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
152 * @param name Flag name
153 * @param desc Flag description
154 * @param value Flag initial/default value
156 Flag(const char* name, const char* desc, T value) : value_(value)
158 simgrid::config::bindFlag(value_, name, desc);
162 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
164 simgrid::config::bindFlag(value_, name, desc, std::move(callback));
168 Flag(Flag const&) = delete;
169 Flag& operator=(Flag const&) = delete;
171 // Get the underlying value:
172 T& get() { return value_; }
173 T const& get() const { return value_; }
175 // Implicit conversion to the underlying type:
176 operator T&() { return value_; }
177 operator T const&() const{ return value_; }
179 // Basic interop with T:
180 Flag& operator=(T const& that) { value_ = that; return *this; }
181 Flag& operator=(T && that) { value_ = that; return *this; }
182 bool operator==(T const& that) const { return value_ == that; }
183 bool operator!=(T const& that) const { return value_ != that; }
184 bool operator<(T const& that) const { return value_ < that; }
185 bool operator>(T const& that) const { return value_ > that; }
186 bool operator<=(T const& that) const { return value_ <= that; }
187 bool operator>=(T const& that) const { return value_ >= that; }