1 /* Copyright (c) 2016-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include <initializer_list>
18 #include <type_traits>
22 #include <xbt/config.h>
23 #include <xbt/sysdep.h>
28 template<class T> inline
29 std::string to_string(T&& value)
31 return std::to_string(std::forward<T>(value));
33 inline std::string const& to_string(std::string& value)
37 inline std::string const& to_string(std::string const& value)
41 inline std::string to_string(std::string&& value)
43 return std::move(value);
48 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
50 extern template XBT_PUBLIC void set_default<int>(const char* name, int value);
51 extern template XBT_PUBLIC void set_default<double>(const char* name, double value);
52 extern template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
53 extern template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
55 XBT_PUBLIC bool is_default(const char* name);
59 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
61 extern template XBT_PUBLIC void set_value<int>(const char* name, int value);
62 extern template XBT_PUBLIC void set_value<double>(const char* name, double value);
63 extern template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
64 extern template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
68 template <class T> XBT_PUBLIC T const& get_value(const char* name);
70 extern template XBT_PUBLIC int const& get_value<int>(const char* name);
71 extern template XBT_PUBLIC double const& get_value<double>(const char* name);
72 extern template XBT_PUBLIC bool const& get_value<bool>(const char* name);
73 extern template XBT_PUBLIC std::string const& get_value<std::string>(const char* name);
77 /** Register a configuration flag
79 * @param name name of the option
80 * @param description Description of the option
81 * @param value Initial/default value
82 * @param callback called with the option value
85 XBT_PUBLIC void declare_flag(const char* name, const char* description, T value,
86 std::function<void(const T&)> callback = std::function<void(const T&)>());
88 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, int value,
89 std::function<void(int const&)> callback);
90 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, double value,
91 std::function<void(double const&)> callback);
92 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, bool value,
93 std::function<void(bool const&)> callback);
94 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, std::string value,
95 std::function<void(std::string const&)> callback);
99 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
101 /** Bind a variable to configuration flag
103 * @param value Bound variable
104 * @param name Flag name
105 * @param description Option description
107 template <class T> void bind_flag(T& value, const char* name, const char* description)
109 declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
113 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
115 bind_flag(value, name, description);
116 alias(name, std::move(aliases));
119 /** Bind a variable to configuration flag
123 * simgrid::config::bind_flag(a, "x", [](int x) {
124 * if (x < x_min || x => x_max)
125 * throw std::range_error("must be in [x_min, x_max)")
129 // F is a checker, F : T& -> ()
130 template <class T, class F>
131 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
132 bind_flag(T& value, const char* name, const char* description, F callback)
134 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
136 value = std::move(val);
140 template <class T, class F>
141 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
142 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
144 bind_flag(value, name, description, std::move(callback));
145 alias(name, std::move(aliases));
148 template <class T, class F>
149 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
150 bind_flag(T& value, const char* name, const char* description, std::map<T, std::string> valid_values, F callback)
152 declare_flag(name, description, value,
153 std::function<void(const T&)>([&value, name, valid_values, callback](const T& val) {
156 for (auto kv : valid_values) {
160 if (not found || std::string(val) == "help") {
162 if (std::string(val) == "help")
163 mesg = std::string("\nPossible values for option ") + name + ":\n";
165 mesg = std::string("\nInvalid value '") + val + "' for option " + name + ". Possible values:\n";
166 for (auto kv : valid_values)
167 mesg += " - '" + std::string(kv.first) + "': " + kv.second +
168 (kv.first == value ? " <=== DEFAULT" : "") + "\n";
169 xbt_die("%s", mesg.c_str());
171 value = std::move(val);
174 /** Bind a variable to configuration flag
178 * simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
181 // F is a predicate, F : T const& -> bool
182 template <class T, class F>
183 typename std::enable_if<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
184 bind_flag(T& value, const char* name, const char* description, F callback)
186 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
187 if (not callback(val))
188 throw std::range_error("invalid value.");
189 value = std::move(val);
193 /** A variable bound to a CLI option
196 * static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
197 * static simgrid::config::flag<std::string> name("name", "Ford Perfect", "John Doe");
198 * static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
208 * @param name Flag name
209 * @param desc Flag description
210 * @param value Flag initial/default value
212 Flag(const char* name, const char* desc, T value) : value_(value)
214 simgrid::config::bind_flag(value_, name, desc);
217 /** Constructor taking also an array of aliases for name */
218 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value) : value_(value)
220 simgrid::config::bind_flag(value_, name, std::move(aliases), desc);
223 /* A constructor accepting a callback that will be passed the parameter.
224 * It can either return a boolean (informing whether the parameter is valid), or returning void.
227 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
229 simgrid::config::bind_flag(value_, name, desc, std::move(callback));
233 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value, F callback)
236 simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(callback));
239 /* A constructor accepting a map of valid values -> their description,
240 * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
243 Flag(const char* name, const char* desc, T value, std::map<T, std::string> valid_values, F callback) : value_(value)
245 simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback));
249 Flag(Flag const&) = delete;
250 Flag& operator=(Flag const&) = delete;
252 // Get the underlying value:
253 T& get() { return value_; }
254 T const& get() const { return value_; }
256 // Implicit conversion to the underlying type:
257 operator T&() { return value_; }
258 operator T const&() const{ return value_; }
260 // Basic interop with T:
262 Flag& operator=(U const& that) { value_ = that; return *this; }
264 Flag& operator=(U && that) { value_ = that; return *this; }
266 bool operator==(U const& that) const { return value_ == that; }
268 bool operator!=(U const& that) const { return value_ != that; }
270 bool operator<(U const& that) const { return value_ < that; }
272 bool operator>(U const& that) const { return value_ > that; }
274 bool operator<=(U const& that) const { return value_ <= that; }
276 bool operator>=(U const& that) const { return value_ >= that; }
281 XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value<std::string>") XBT_PUBLIC std::string
282 xbt_cfg_get_string(const char* name);