Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev-add_comm_fault_scenario' into 'master'
[simgrid.git] / include / xbt / config.hpp
1 /* Copyright (c) 2016-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef XBT_CONFIG_HPP
7 #define XBT_CONFIG_HPP
8
9 #include <xbt/base.h>
10
11 #include <cstdlib>
12
13 #include <functional>
14 #include <initializer_list>
15 #include <map>
16 #include <stdexcept>
17 #include <string>
18 #include <type_traits>
19 #include <utility>
20
21 #include <xbt/base.h>
22 #include <xbt/sysdep.h>
23 #include <xbt/utility.hpp>
24
25 namespace simgrid {
26 namespace config {
27
28 class Config;
29
30 template<class T> inline
31 std::string to_string(T&& value)
32 {
33   return std::to_string(std::forward<T>(value));
34 }
35 inline std::string const& to_string(std::string& value)
36 {
37   return value;
38 }
39 inline std::string const& to_string(std::string const& value)
40 {
41   return value;
42 }
43 inline std::string to_string(std::string&& value)
44 {
45   return std::move(value);
46 }
47
48 // Set default
49
50 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
51
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);
56
57 XBT_PUBLIC bool is_default(const char* name);
58
59 // Set config
60
61 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
62
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);
67
68 XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
69 XBT_PUBLIC void set_parse(const std::string& options);
70
71 // Get config
72
73 template <class T> XBT_PUBLIC T const& get_value(const std::string& name);
74
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);
79
80 // Register:
81
82 /** Register a configuration flag
83  *
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
88  */
89 template <class T>
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&)>());
92
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);
101
102 // ***** alias *****
103
104 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
105
106 /** Bind a variable to configuration flag
107  *
108  *  @param value Bound variable
109  *  @param name  Flag name
110  *  @param description Option description
111  */
112 template <class T> void bind_flag(T& value, const char* name, const char* description)
113 {
114   declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
115 }
116
117 template <class T>
118 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
119 {
120   bind_flag(value, name, description);
121   alias(name, aliases);
122 }
123
124 /** Bind a variable to configuration flag
125  *
126  *  <pre><code>
127  *  static int x;
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)")
131  *  });
132  *  </code></pre>
133  */
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)
138 {
139   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
140                  callback(val);
141                  value = std::move(val);
142                }));
143 }
144
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)
148 {
149   bind_flag(value, name, description, std::move(callback));
150   alias(name, aliases);
151 }
152
153 template <class F>
154 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
155                           void>
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)
158 {
159   declare_flag(name, description, value,
160                std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
161                  callback(val);
162                  if (valid_values.find(val) != valid_values.end()) {
163                    value = val;
164                    return;
165                  }
166                  std::string mesg = "\n";
167                  if (val == "help")
168                    mesg += std::string("Possible values for option ") + name + ":\n";
169                  else
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());
174                }));
175 }
176 template <class F>
177 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
178                           void>
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)
181 {
182   bind_flag(value, name, description, valid_values, std::move(callback));
183   alias(name, aliases);
184 }
185
186 /** Bind a variable to configuration flag
187  *
188  *  <pre><code>
189  *  static int x;
190  *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
191  *  </code></pre>
192  */
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)
197 {
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);
202                }));
203 }
204
205 /** A variable bound to a CLI option
206  *
207  *  <pre><code>
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);
211  *  </code></pre>
212  */
213 template<class T>
214 class Flag {
215   T value_;
216   std::string name_;
217
218 public:
219   /** Constructor
220    *
221    *  @param name  Flag name
222    *  @param desc  Flag description
223    *  @param value Flag initial/default value
224    */
225   Flag(const char* name, const char* desc, xbt::type_identity_t<T> value) : value_(value), name_(name)
226   {
227     simgrid::config::bind_flag(value_, name, desc);
228   }
229
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)
233   {
234     simgrid::config::bind_flag(value_, name, aliases, desc);
235   }
236
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.
239    */
240   template <class F>
241   Flag(const char* name, const char* desc, xbt::type_identity_t<T> value, F callback) : value_(value), name_(name)
242   {
243     simgrid::config::bind_flag(value_, name, desc, std::move(callback));
244   }
245
246   template <class F>
247   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
248        F callback)
249       : value_(value), name_(name)
250   {
251     simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
252   }
253
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.
256    */
257   template <class F>
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)
261   {
262     simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
263   }
264
265   /* A constructor with everything */
266   template <class F>
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)
270   {
271     simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
272   }
273
274   // No copy:
275   Flag(Flag const&) = delete;
276   Flag& operator=(Flag const&) = delete;
277
278   // Get the underlying value:
279   T& get() { return value_; }
280   T const& get() const { return value_; }
281
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_; }
286
287   // Basic interop with T:
288   template<class U>
289   Flag& operator=(U const& that) { value_ = that; return *this; }
290   template<class U>
291   Flag& operator=(U&& that) { value_ = std::forward<U>(that); return *this; }
292   template<class U>
293   bool operator==(U const& that) const { return value_ == that; }
294   template<class U>
295   bool operator!=(U const& that) const { return value_ != that; }
296   template<class U>
297   bool operator<(U const& that) const { return value_ < that; }
298   template<class U>
299   bool operator>(U const& that) const { return value_ > that; }
300   template<class U>
301   bool operator<=(U const& that) const { return value_ <= that; }
302   template<class U>
303   bool operator>=(U const& that) const { return value_ >= that; }
304 };
305
306 XBT_PUBLIC void finalize();
307 XBT_PUBLIC void show_aliases();
308 XBT_PUBLIC void help();
309
310 } // namespace config
311 } // namespace simgrid
312
313 #endif