1 /* Copyright (c) 2016-2022. 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/sysdep.h>
23 #include <xbt/utility.hpp>
30 template<class T> inline
31 std::string to_string(T&& value)
33 return std::to_string(std::forward<T>(value));
35 inline std::string const& to_string(std::string& value)
39 inline std::string const& to_string(std::string const& value)
43 inline std::string to_string(std::string&& value)
45 return std::move(value);
50 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
52 extern template XBT_PUBLIC void set_default<int>(const char* name, int value);
53 extern template XBT_PUBLIC void set_default<double>(const char* name, double value);
54 extern template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
55 extern template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
57 XBT_PUBLIC bool is_default(const char* name);
61 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
63 extern template XBT_PUBLIC void set_value<int>(const char* name, int value);
64 extern template XBT_PUBLIC void set_value<double>(const char* name, double value);
65 extern template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
66 extern template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
68 XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
69 XBT_PUBLIC void set_parse(const std::string& options);
73 template <class T> XBT_PUBLIC T const& get_value(const std::string& name);
75 extern template XBT_PUBLIC int const& get_value<int>(const std::string& name);
76 extern template XBT_PUBLIC double const& get_value<double>(const std::string& name);
77 extern template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
78 extern template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
82 /** Register a configuration flag
84 * @param name name of the option
85 * @param description Description of the option
86 * @param value Initial/default value
87 * @param callback called with the option value
90 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
91 std::function<void(const T&)> callback = std::function<void(const T&)>());
93 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
94 std::function<void(int const&)> callback);
95 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
96 std::function<void(double const&)> callback);
97 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
98 std::function<void(bool const&)> callback);
99 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
100 std::function<void(std::string const&)> callback);
104 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
106 /** Bind a variable to configuration flag
108 * @param value Bound variable
109 * @param name Flag name
110 * @param description Option description
112 template <class T> void bind_flag(T& value, const char* name, const char* description)
114 declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
118 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
120 bind_flag(value, name, description);
121 alias(name, aliases);
124 /** Bind a variable to configuration flag
128 * simgrid::config::bind_flag(a, "x", [](int x) {
129 * if (x < x_min || x => x_max)
130 * throw std::range_error("must be in [x_min, x_max)")
134 // F is a checker, F : T& -> ()
135 template <class T, class F>
136 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
137 bind_flag(T& value, const char* name, const char* description, F callback)
139 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
141 value = std::move(val);
145 template <class T, class F>
146 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
147 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
149 bind_flag(value, name, description, std::move(callback));
150 alias(name, aliases);
154 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
156 bind_flag(std::string& value, const char* name, const char* description,
157 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
159 declare_flag(name, description, value,
160 std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
162 if (valid_values.find(val) != valid_values.end()) {
166 std::string mesg = "\n";
168 mesg += std::string("Possible values for option ") + name + ":\n";
170 mesg += std::string("Invalid value '") + val + "' for option " + name + ". Possible values:\n";
171 for (auto const& kv : valid_values)
172 mesg += " - '" + kv.first + "': " + kv.second + (kv.first == value ? " <=== DEFAULT" : "") + "\n";
173 xbt_die("%s", mesg.c_str());
177 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
179 bind_flag(std::string& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
180 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
182 bind_flag(value, name, description, valid_values, std::move(callback));
183 alias(name, aliases);
186 /** Bind a variable to configuration flag
190 * simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
193 // F is a predicate, F : T const& -> bool
194 template <class T, class F>
195 typename std::enable_if_t<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
196 bind_flag(T& value, const char* name, const char* description, F callback)
198 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
199 if (not callback(val))
200 throw std::range_error("invalid value.");
201 value = std::move(val);
205 /** A variable bound to a CLI option
208 * static simgrid::config::Flag<int> answer("answer", "Expected answer", 42);
209 * static simgrid::config::Flag<std::string> name("name", "Ford Perfect", "John Doe");
210 * static simgrid::config::Flag<double> gamma("gamma", "Gamma factor", 1.987);
221 * @param name Flag name
222 * @param desc Flag description
223 * @param value Flag initial/default value
225 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value) : value_(value), name_(name)
227 simgrid::config::bind_flag(value_, name, desc);
230 /** Constructor taking also an array of aliases for name */
231 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value)
232 : value_(value), name_(name)
234 simgrid::config::bind_flag(value_, name, aliases, desc);
237 /* A constructor accepting a callback that will be passed the parameter.
238 * It can either return a boolean (informing whether the parameter is valid), or returning void.
241 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value, F callback) : value_(value), name_(name)
243 simgrid::config::bind_flag(value_, name, desc, std::move(callback));
247 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
249 : value_(value), name_(name)
251 simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
254 /* A constructor accepting a map of valid values -> their description,
255 * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
258 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value,
259 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
260 : value_(value), name_(name)
262 simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
265 /* A constructor with everything */
267 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
268 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
269 : value_(value), name_(name)
271 simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
275 Flag(Flag const&) = delete;
276 Flag& operator=(Flag const&) = delete;
278 // Get the underlying value:
279 T& get() { return value_; }
280 T const& get() const { return value_; }
282 const std::string& get_name() const { return name_; }
283 // Implicit conversion to the underlying type:
284 operator T&() { return value_; }
285 operator T const&() const{ return value_; }
287 // Basic interop with T:
289 Flag& operator=(U const& that) { value_ = that; return *this; }
291 Flag& operator=(U&& that) { value_ = std::forward<U>(that); return *this; }
293 bool operator==(U const& that) const { return value_ == that; }
295 bool operator!=(U const& that) const { return value_ != that; }
297 bool operator<(U const& that) const { return value_ < that; }
299 bool operator>(U const& that) const { return value_ > that; }
301 bool operator<=(U const& that) const { return value_ <= that; }
303 bool operator>=(U const& that) const { return value_ >= that; }
306 XBT_PUBLIC void finalize();
307 XBT_PUBLIC void show_aliases();
308 XBT_PUBLIC void help();
310 } // namespace config
311 } // namespace simgrid