Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[config] some declare_flag to Flag
[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
24 namespace simgrid {
25 namespace 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 // Register:
80
81 /** Register a configuration flag
82  *
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
87  */
88 template <class T>
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&)>());
91
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);
100
101 // ***** alias *****
102
103 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
104
105 /** Bind a variable to configuration flag
106  *
107  *  @param value Bound variable
108  *  @param name  Flag name
109  *  @param description Option description
110  */
111 template <class T> void bind_flag(T& value, const char* name, const char* description)
112 {
113   declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
114 }
115
116 template <class T>
117 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
118 {
119   bind_flag(value, name, description);
120   alias(name, aliases);
121 }
122
123 /** Bind a variable to configuration flag
124  *
125  *  <pre><code>
126  *  static int x;
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)")
130  *  });
131  *  </code></pre>
132  */
133 // F is a checker, F : T& -> ()
134 template <class T, class F>
135 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
136 bind_flag(T& value, const char* name, const char* description, F callback)
137 {
138   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
139                  callback(val);
140                  value = std::move(val);
141                }));
142 }
143
144 template <class T, class F>
145 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
146 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
147 {
148   bind_flag(value, name, description, std::move(callback));
149   alias(name, aliases);
150 }
151
152 template <class F>
153 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
154                           void>
155 bind_flag(std::string& value, const char* name, const char* description,
156           const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
157 {
158   declare_flag(name, description, value,
159                std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
160                  callback(val);
161                  if (valid_values.find(val) != valid_values.end()) {
162                    value = val;
163                    return;
164                  }
165                  std::string mesg = "\n";
166                  if (val == "help")
167                    mesg += std::string("Possible values for option ") + name + ":\n";
168                  else
169                    mesg += std::string("Invalid value '") + val + "' for option " + name + ". Possible values:\n";
170                  for (auto const& kv : valid_values)
171                    mesg += "  - '" + kv.first + "': " + kv.second + (kv.first == value ? "  <=== DEFAULT" : "") + "\n";
172                  xbt_die("%s", mesg.c_str());
173                }));
174 }
175 template <class F>
176 typename std::enable_if_t<std::is_same<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>::value,
177                           void>
178 bind_flag(std::string& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
179           const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
180 {
181   bind_flag(value, name, description, valid_values, std::move(callback));
182   alias(name, aliases);
183 }
184
185 /** Bind a variable to configuration flag
186  *
187  *  <pre><code>
188  *  static int x;
189  *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
190  *  </code></pre>
191  */
192 // F is a predicate, F : T const& -> bool
193 template <class T, class F>
194 typename std::enable_if_t<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>
195 bind_flag(T& value, const char* name, const char* description, F callback)
196 {
197   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
198                  if (not callback(val))
199                    throw std::range_error("invalid value.");
200                  value = std::move(val);
201                }));
202 }
203
204 /** A variable bound to a CLI option
205  *
206  *  <pre><code>
207  *  static simgrid::config::Flag<int> answer("answer", "Expected answer", 42);
208  *  static simgrid::config::Flag<std::string> name("name", "Ford Perfect", "John Doe");
209  *  static simgrid::config::Flag<double> gamma("gamma", "Gamma factor", 1.987);
210  *  </code></pre>
211  */
212 template<class T>
213 class Flag {
214   T value_;
215   std::string name_;
216
217 public:
218   /** Constructor
219    *
220    *  @param name  Flag name
221    *  @param desc  Flag description
222    *  @param value Flag initial/default value
223    */
224   Flag(const char* name, const char* desc, T value) : value_(value), name_(name)
225   {
226     simgrid::config::bind_flag(value_, name, desc);
227   }
228
229   /** Constructor taking also an array of aliases for name */
230   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value)
231       : value_(value), name_(name)
232   {
233     simgrid::config::bind_flag(value_, name, aliases, desc);
234   }
235
236   /* A constructor accepting a callback that will be passed the parameter.
237    * It can either return a boolean (informing whether the parameter is valid), or returning void.
238    */
239   template <class F> Flag(const char* name, const char* desc, T value, F callback) : value_(value), name_(name)
240   {
241     simgrid::config::bind_flag(value_, name, desc, std::move(callback));
242   }
243
244   template <class F>
245   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value, F callback)
246       : value_(value), name_(name)
247   {
248     simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
249   }
250
251   /* A constructor accepting a map of valid values -> their description,
252    * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
253    */
254   template <class F>
255   Flag(const char* name, const char* desc, T value, const std::map<std::string, std::string, std::less<>>& valid_values,
256        F callback)
257       : value_(value), name_(name)
258   {
259     simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
260   }
261
262   /* A constructor with everything */
263   template <class F>
264   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value,
265        const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
266       : value_(value), name_(name)
267   {
268     simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
269   }
270
271   // No copy:
272   Flag(Flag const&) = delete;
273   Flag& operator=(Flag const&) = delete;
274
275   // Get the underlying value:
276   T& get() { return value_; }
277   T const& get() const { return value_; }
278
279   const std::string& get_name() const { return name_; }
280   // Implicit conversion to the underlying type:
281   operator T&() { return value_; }
282   operator T const&() const{ return value_; }
283
284   // Basic interop with T:
285   template<class U>
286   Flag& operator=(U const& that) { value_ = that; return *this; }
287   template<class U>
288   Flag& operator=(U&& that) { value_ = std::forward<U>(that); return *this; }
289   template<class U>
290   bool operator==(U const& that) const { return value_ == that; }
291   template<class U>
292   bool operator!=(U const& that) const { return value_ != that; }
293   template<class U>
294   bool operator<(U const& that) const { return value_ < that; }
295   template<class U>
296   bool operator>(U const& that) const { return value_ > that; }
297   template<class U>
298   bool operator<=(U const& that) const { return value_ <= that; }
299   template<class U>
300   bool operator>=(U const& that) const { return value_ >= that; }
301 };
302
303 XBT_PUBLIC void finalize();
304 XBT_PUBLIC void show_aliases();
305 XBT_PUBLIC void help();
306
307 } // namespace config
308 } // namespace simgrid
309
310 /******************************** Configuration of Simgrid **************************************/
311 extern XBT_PUBLIC simgrid::config::Flag<int> _sg_context_stack_size;
312 extern XBT_PUBLIC simgrid::config::Flag<int> _sg_context_guard_size;
313 extern XBT_PUBLIC simgrid::config::Flag<int> _sg_context_nthreads;
314 extern XBT_PUBLIC simgrid::config::Flag<std::string> _sg_context_synchro;
315
316 extern XBT_PUBLIC simgrid::config::Flag<bool> _sg_cpu_maxmin_selective_update;
317
318 extern XBT_PUBLIC simgrid::config::Flag<bool> _sg_bmf_selective_update;
319
320 extern XBT_PUBLIC simgrid::config::Flag<double> _sg_network_loopback_bandwidth;
321 extern XBT_PUBLIC simgrid::config::Flag<double> _sg_network_loopback_latency;
322 extern XBT_PUBLIC simgrid::config::Flag<bool> _sg_network_maxmin_selective_update;
323
324 extern XBT_PUBLIC simgrid::config::Flag<bool> _sg_execution_cutpath;
325
326 #endif