Logo AND Algorithmique Numérique Distribuée

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