1 /* Copyright (c) 2016-2019. 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>
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 /** Register a configuration flag
83 * @param name name of the option
84 * @param description Description of the option
85 * @param value Initial/default value
86 * @param callback called with the option value
89 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
90 std::function<void(const T&)> callback = std::function<void(const T&)>());
92 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
93 std::function<void(int const&)> callback);
94 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, double value,
95 std::function<void(double const&)> callback);
96 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, bool value,
97 std::function<void(bool const&)> callback);
98 extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
99 std::function<void(std::string const&)> callback);
103 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
105 /** Bind a variable to configuration flag
107 * @param value Bound variable
108 * @param name Flag name
109 * @param description Option description
111 template <class T> void bind_flag(T& value, const char* name, const char* description)
113 declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
117 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
119 bind_flag(value, name, description);
120 alias(name, std::move(aliases));
123 /** Bind a variable to configuration flag
127 * simgrid::config::bind_flag(a, "x", [](int x) {
128 * if (x < x_min || x => x_max)
129 * throw std::range_error("must be in [x_min, x_max)")
133 // F is a checker, F : T& -> ()
134 template <class T, class F>
135 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
136 bind_flag(T& value, const char* name, const char* description, F callback)
138 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
140 value = std::move(val);
144 template <class T, class F>
145 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
146 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
148 bind_flag(value, name, description, std::move(callback));
149 alias(name, std::move(aliases));
152 template <class T, class F>
153 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
154 bind_flag(T& value, const char* name, const char* description, std::map<T, std::string> valid_values, F callback)
156 declare_flag(name, description, value,
157 std::function<void(const T&)>([&value, name, valid_values, callback](const T& val) {
160 for (auto kv : valid_values) {
164 if (not found || std::string(val) == "help") {
165 std::string mesg = std::string("\n");
166 if (std::string(val) == "help")
167 mesg += std::string("Possible values for option ") + name + ":\n";
169 mesg += std::string("Invalid value '") + val + "' for option " + name + ". Possible values:\n";
170 for (auto kv : valid_values)
171 mesg += " - '" + std::string(kv.first) + "': " + kv.second +
172 (kv.first == value ? " <=== DEFAULT" : "") + "\n";
173 xbt_die("%s", mesg.c_str());
175 value = std::move(val);
178 template <class T, class F>
179 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
180 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
181 std::map<T, std::string> valid_values, F callback)
183 bind_flag(value, name, description, std::move(valid_values), std::move(callback));
184 alias(name, std::move(aliases));
187 /** Bind a variable to configuration flag
191 * simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
194 // F is a predicate, F : T const& -> bool
195 template <class T, class F>
196 typename std::enable_if<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
197 bind_flag(T& value, const char* name, const char* description, F callback)
199 declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
200 if (not callback(val))
201 throw std::range_error("invalid value.");
202 value = std::move(val);
206 /** A variable bound to a CLI option
209 * static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
210 * static simgrid::config::flag<std::string> name("name", "Ford Perfect", "John Doe");
211 * static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
223 * @param name Flag name
224 * @param desc Flag description
225 * @param value Flag initial/default value
227 Flag(const char* name, const char* desc, T value) : value_(value), name_(name)
229 simgrid::config::bind_flag(value_, name, desc);
232 /** Constructor taking also an array of aliases for name */
233 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value)
234 : value_(value), name_(name)
236 simgrid::config::bind_flag(value_, name, std::move(aliases), desc);
239 /* A constructor accepting a callback that will be passed the parameter.
240 * It can either return a boolean (informing whether the parameter is valid), or returning void.
242 template <class F> Flag(const char* name, const char* desc, T value, F callback) : value_(value), name_(name)
244 simgrid::config::bind_flag(value_, name, desc, std::move(callback));
248 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value, F callback)
249 : value_(value), name_(name)
251 simgrid::config::bind_flag(value_, name, std::move(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, T value, std::map<T, std::string> valid_values, F callback)
259 : value_(value), name_(name)
261 simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback));
264 /* A constructor with everything */
266 Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value,
267 std::map<T, std::string> valid_values, F callback)
268 : value_(value), name_(name)
270 simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(valid_values), std::move(callback));
274 Flag(Flag const&) = delete;
275 Flag& operator=(Flag const&) = delete;
277 // Get the underlying value:
278 T& get() { return value_; }
279 T const& get() const { return value_; }
281 const std::string& get_name() const { return name_; }
282 // Implicit conversion to the underlying type:
283 operator T&() { return value_; }
284 operator T const&() const{ return value_; }
286 // Basic interop with T:
288 Flag& operator=(U const& that) { value_ = that; return *this; }
290 Flag& operator=(U && that) { value_ = that; return *this; }
292 bool operator==(U const& that) const { return value_ == that; }
294 bool operator!=(U const& that) const { return value_ != that; }
296 bool operator<(U const& that) const { return value_ < that; }
298 bool operator>(U const& that) const { return value_ > that; }
300 bool operator<=(U const& that) const { return value_ <= that; }
302 bool operator>=(U const& that) const { return value_ >= that; }
305 XBT_PUBLIC void finalize();
306 XBT_PUBLIC void show_aliases();
307 XBT_PUBLIC void help();
310 XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value<std::string>") XBT_PUBLIC std::string
311 xbt_cfg_get_string(const char* name);