1 /* Copyright (c) 2016-2023. 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>
25 namespace simgrid::config {
29 template<class T> inline
30 std::string to_string(T&& value)
32 return std::to_string(std::forward<T>(value));
34 inline std::string const& to_string(std::string& value)
38 inline std::string const& to_string(std::string const& value)
42 inline std::string to_string(std::string&& value)
44 return std::move(value);
49 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
51 extern template XBT_PUBLIC void set_default<int>(const char* name, int value);
52 extern template XBT_PUBLIC void set_default<double>(const char* name, double value);
53 extern template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
54 extern template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
56 XBT_PUBLIC bool is_default(const char* name);
60 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
62 extern template XBT_PUBLIC void set_value<int>(const char* name, int value);
63 extern template XBT_PUBLIC void set_value<double>(const char* name, double value);
64 extern template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
65 extern template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
67 XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
68 XBT_PUBLIC void set_parse(const std::string& options);
72 template <class T> XBT_PUBLIC T const& get_value(const std::string& name);
74 extern template XBT_PUBLIC int const& get_value<int>(const std::string& name);
75 extern template XBT_PUBLIC double const& get_value<double>(const std::string& name);
76 extern template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
77 extern template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
81 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
85 /** Register a configuration flag
87 * @param name name of the option
88 * @param description Description of the option
89 * @param value Initial/default value
90 * @param callback called with the option value
93 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
94 std::function<void(const T&)> callback = nullptr);
96 void declare_flag(const std::string& name, std::initializer_list<const char*> aliases, const std::string& description,
97 T value, std::function<void(const T&)> callback = nullptr)
99 declare_flag(name, description, std::move(value), std::move(callback));
100 alias(name.c_str(), aliases);
103 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
104 std::function<void(int const&)> callback);
105 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
106 std::function<void(double const&)> callback);
107 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
108 std::function<void(bool const&)> callback);
109 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
110 std::function<void(std::string const&)> callback);
112 /** Bind a variable to configuration flag
114 * @param value Bound variable
115 * @param name Flag name
116 * @param description Option description
118 template <class T> void bind_flag(T& value, const char* name, const char* description)
120 declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
124 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
126 bind_flag(value, name, description);
127 alias(name, aliases);
130 /** Bind a variable to configuration flag
134 * simgrid::config::bind_flag(a, "x", [](int x) {
135 * if (x < x_min || x => x_max)
136 * throw std::range_error("must be in [x_min, x_max)")
140 // F is a checker, F : T& -> ()
141 template <class T, class F>
142 typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
143 bind_flag(T& value, const char* name, const char* description, F callback)
145 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
147 value = std::move(val);
151 template <class T, class F>
152 typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
153 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
155 bind_flag(value, name, description, std::move(callback));
156 alias(name, aliases);
160 typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>, void>
161 bind_flag(std::string& value, const char* name, const char* description,
162 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
164 declare_flag(name, description, value,
165 std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
167 if (valid_values.find(val) != valid_values.end()) {
171 std::string mesg = "\n";
173 mesg += std::string("Possible values for option ") + name + ":\n";
175 mesg += "Invalid value '" + val + "' for option " + name + ". Possible values:\n";
176 for (auto const& [v, descr] : valid_values)
177 mesg += " - '" + v + "': " + descr + (v == value ? " <=== DEFAULT" : "") + "\n";
178 xbt_die("%s", mesg.c_str());
182 typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>, void>
183 bind_flag(std::string& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
184 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
186 bind_flag(value, name, description, valid_values, std::move(callback));
187 alias(name, aliases);
190 /** Bind a variable to configuration flag
194 * simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
197 // F is a predicate, F : T const& -> bool
198 template <class T, class F>
199 typename std::enable_if_t<std::is_same_v<bool, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
200 bind_flag(T& value, const char* name, const char* description, F callback)
202 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
203 if (not callback(val))
204 throw std::range_error("invalid value.");
205 value = std::move(val);
209 /** A variable bound to a CLI option
212 * static simgrid::config::Flag<int> answer("answer", "Expected answer", 42);
213 * static simgrid::config::Flag<std::string> name("name", "Ford Perfect", "John Doe");
214 * static simgrid::config::Flag<double> gamma("gamma", "Gamma factor", 1.987);
225 * @param name Flag name
226 * @param desc Flag description
227 * @param value Flag initial/default value
229 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value) : value_(value), name_(name)
231 simgrid::config::bind_flag(value_, name, desc);
234 /** Constructor taking also an array of aliases for name */
235 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value)
236 : value_(value), name_(name)
238 simgrid::config::bind_flag(value_, name, aliases, desc);
241 /* A constructor accepting a callback that will be passed the parameter.
242 * It can either return a boolean (informing whether the parameter is valid), or returning void.
245 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value, F callback) : value_(value), name_(name)
247 simgrid::config::bind_flag(value_, name, desc, std::move(callback));
251 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
253 : value_(value), name_(name)
255 simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
258 /* A constructor accepting a map of valid values -> their description,
259 * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
261 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value,
262 const std::map<std::string, std::string, std::less<>>& valid_values)
263 : value_(value), name_(name)
265 simgrid::config::bind_flag(value_, name, desc, valid_values, [](const std::string&) {});
268 /* As earlier, a constructor accepting a map of valid values -> their description,
269 * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
270 * But also take a callback that is invoked before the verification of parameter name validity.
273 Flag(const char* name, const char* desc, xbt::type_identity_t<T> value,
274 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
275 : value_(value), name_(name)
277 simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
280 /* A constructor with everything */
282 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
283 const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
284 : value_(value), name_(name)
286 simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
290 Flag(Flag const&) = delete;
291 Flag& operator=(Flag const&) = delete;
293 // Get the underlying value:
294 T& get() { return value_; }
295 T const& get() const { return value_; }
297 const std::string& get_name() const { return name_; }
298 // Implicit conversion to the underlying type:
299 operator T&() { return value_; }
300 operator T const&() const{ return value_; }
302 // Basic interop with T:
304 Flag& operator=(U const& that) { value_ = that; return *this; }
306 Flag& operator=(U&& that) { value_ = std::forward<U>(that); return *this; }
308 bool operator==(U const& that) const { return value_ == that; }
310 bool operator!=(U const& that) const { return value_ != that; }
312 bool operator<(U const& that) const { return value_ < that; }
314 bool operator>(U const& that) const { return value_ > that; }
316 bool operator<=(U const& that) const { return value_ <= that; }
318 bool operator>=(U const& that) const { return value_ >= that; }
321 XBT_PUBLIC void finalize();
322 XBT_PUBLIC void show_aliases();
323 XBT_PUBLIC void help();
325 } // namespace simgrid::config