Logo AND Algorithmique Numérique Distribuée

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