Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / xbt / config.hpp
1 /* Copyright (c) 2016-2023. 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::config {
26
27 class Config;
28
29 template<class T> inline
30 std::string to_string(T&& value)
31 {
32   return std::to_string(std::forward<T>(value));
33 }
34 inline std::string const& to_string(std::string& value)
35 {
36   return value;
37 }
38 inline std::string const& to_string(std::string const& value)
39 {
40   return value;
41 }
42 inline std::string to_string(std::string&& value)
43 {
44   return std::move(value);
45 }
46
47 // Set default
48
49 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
50
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);
55
56 XBT_PUBLIC bool is_default(const char* name);
57
58 // Set config
59
60 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
61
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);
66
67 XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
68 XBT_PUBLIC void set_parse(const std::string& options);
69
70 // Get config
71
72 template <class T> XBT_PUBLIC T const& get_value(const std::string& name);
73
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);
78
79 // ***** alias *****
80
81 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
82
83 // Register:
84
85 /** Register a configuration flag
86  *
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
91  */
92 template <class T>
93 XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
94                              std::function<void(const T&)> callback = nullptr);
95 template <class T>
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)
98 {
99   declare_flag(name, description, std::move(value), std::move(callback));
100   alias(name.c_str(), aliases);
101 }
102
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);
111
112 /** Bind a variable to configuration flag
113  *
114  *  @param value Bound variable
115  *  @param name  Flag name
116  *  @param description Option description
117  */
118 template <class T> void bind_flag(T& value, const char* name, const char* description)
119 {
120   declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
121 }
122
123 template <class T>
124 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
125 {
126   bind_flag(value, name, description);
127   alias(name, aliases);
128 }
129
130 /** Bind a variable to configuration flag
131  *
132  *  <pre><code>
133  *  static int x;
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)")
137  *  });
138  *  </code></pre>
139  */
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)
144 {
145   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
146                  callback(val);
147                  value = std::move(val);
148                }));
149 }
150
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)
154 {
155   bind_flag(value, name, description, std::move(callback));
156   alias(name, aliases);
157 }
158
159 template <class F>
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)
163 {
164   declare_flag(name, description, value,
165                std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
166                  callback(val);
167                  if (valid_values.find(val) != valid_values.end()) {
168                    value = val;
169                    return;
170                  }
171                  std::string mesg = "\n";
172                  if (val == "help")
173                    mesg += std::string("Possible values for option ") + name + ":\n";
174                  else
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());
179                }));
180 }
181 template <class F>
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)
185 {
186   bind_flag(value, name, description, valid_values, std::move(callback));
187   alias(name, aliases);
188 }
189
190 /** Bind a variable to configuration flag
191  *
192  *  <pre><code>
193  *  static int x;
194  *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
195  *  </code></pre>
196  */
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)
201 {
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);
206                }));
207 }
208
209 /** A variable bound to a CLI option
210  *
211  *  <pre><code>
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);
215  *  </code></pre>
216  */
217 template<class T>
218 class Flag {
219   T value_;
220   std::string name_;
221
222 public:
223   /** Constructor
224    *
225    *  @param name  Flag name
226    *  @param desc  Flag description
227    *  @param value Flag initial/default value
228    */
229   Flag(const char* name, const char* desc, xbt::type_identity_t<T> value) : value_(value), name_(name)
230   {
231     simgrid::config::bind_flag(value_, name, desc);
232   }
233
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)
237   {
238     simgrid::config::bind_flag(value_, name, aliases, desc);
239   }
240
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.
243    */
244   template <class F>
245   Flag(const char* name, const char* desc, xbt::type_identity_t<T> value, F callback) : value_(value), name_(name)
246   {
247     simgrid::config::bind_flag(value_, name, desc, std::move(callback));
248   }
249
250   template <class F>
251   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
252        F callback)
253       : value_(value), name_(name)
254   {
255     simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
256   }
257
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.
260    */
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)
264   {
265     simgrid::config::bind_flag(value_, name, desc, valid_values, [](const std::string&) {});
266   }
267
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.
271    */
272   template <class F>
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)
276   {
277     simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
278   }
279
280   /* A constructor with everything */
281   template <class F>
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)
285   {
286     simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
287   }
288
289   // No copy:
290   Flag(Flag const&) = delete;
291   Flag& operator=(Flag const&) = delete;
292
293   // Get the underlying value:
294   T& get() { return value_; }
295   T const& get() const { return value_; }
296
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_; }
301
302   // Basic interop with T:
303   template<class U>
304   Flag& operator=(U const& that) { value_ = that; return *this; }
305   template<class U>
306   Flag& operator=(U&& that) { value_ = std::forward<U>(that); return *this; }
307   template<class U>
308   bool operator==(U const& that) const { return value_ == that; }
309   template<class U>
310   bool operator!=(U const& that) const { return value_ != that; }
311   template<class U>
312   bool operator<(U const& that) const { return value_ < that; }
313   template<class U>
314   bool operator>(U const& that) const { return value_ > that; }
315   template<class U>
316   bool operator<=(U const& that) const { return value_ <= that; }
317   template<class U>
318   bool operator>=(U const& that) const { return value_ >= that; }
319 };
320
321 XBT_PUBLIC void finalize();
322 XBT_PUBLIC void show_aliases();
323 XBT_PUBLIC void help();
324
325 } // namespace simgrid::config
326
327 #endif