Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
48fd59fec75c4ccbe3ddc17d33ab2712e4ea8760
[simgrid.git] / include / xbt / config.hpp
1 /* Copyright (c) 2016-2018. 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/config.h>
23 #include <xbt/sysdep.h>
24
25 namespace simgrid {
26 namespace config {
27
28 template<class T> inline
29 std::string to_string(T&& value)
30 {
31   return std::to_string(std::forward<T>(value));
32 }
33 inline std::string const& to_string(std::string& value)
34 {
35   return value;
36 }
37 inline std::string const& to_string(std::string const& value)
38 {
39   return value;
40 }
41 inline std::string to_string(std::string&& value)
42 {
43   return std::move(value);
44 }
45
46 // Set default
47
48 template <class T> XBT_PUBLIC void set_default(const char* name, T value);
49
50 extern template XBT_PUBLIC void set_default<int>(const char* name, int value);
51 extern template XBT_PUBLIC void set_default<double>(const char* name, double value);
52 extern template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
53 extern template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
54
55 XBT_PUBLIC bool is_default(const char* name);
56
57 // Set config
58
59 template <class T> XBT_PUBLIC void set_value(const char* name, T value);
60
61 extern template XBT_PUBLIC void set_value<int>(const char* name, int value);
62 extern template XBT_PUBLIC void set_value<double>(const char* name, double value);
63 extern template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
64 extern template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
65
66 XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
67 XBT_PUBLIC void set_parse(std::string options);
68
69 // Get config
70
71 template <class T> XBT_PUBLIC T const& get_value(const char* name);
72
73 extern template XBT_PUBLIC int const& get_value<int>(const char* name);
74 extern template XBT_PUBLIC double const& get_value<double>(const char* name);
75 extern template XBT_PUBLIC bool const& get_value<bool>(const char* name);
76 extern template XBT_PUBLIC std::string const& get_value<std::string>(const char* name);
77
78 // Register:
79
80 /** Register a configuration flag
81  *
82  *  @param name        name of the option
83  *  @param description Description of the option
84  *  @param value       Initial/default value
85  *  @param callback    called with the option value
86  */
87 template <class T>
88 XBT_PUBLIC void declare_flag(const char* name, const char* description, T value,
89                              std::function<void(const T&)> callback = std::function<void(const T&)>());
90
91 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, int value,
92                                              std::function<void(int const&)> callback);
93 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, double value,
94                                              std::function<void(double const&)> callback);
95 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, bool value,
96                                              std::function<void(bool const&)> callback);
97 extern template XBT_PUBLIC void declare_flag(const char* name, const char* description, std::string value,
98                                              std::function<void(std::string const&)> callback);
99
100 // ***** alias *****
101
102 XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
103
104 /** Bind a variable to configuration flag
105  *
106  *  @param value Bound variable
107  *  @param name  Flag name
108  *  @param description Option description
109  */
110 template <class T> void bind_flag(T& value, const char* name, const char* description)
111 {
112   declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
113 }
114
115 template <class T>
116 void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
117 {
118   bind_flag(value, name, description);
119   alias(name, std::move(aliases));
120 }
121
122 /** Bind a variable to configuration flag
123  *
124  *  <pre><code>
125  *  static int x;
126  *  simgrid::config::bind_flag(a, "x", [](int x) {
127  *    if (x < x_min || x => x_max)
128  *      throw std::range_error("must be in [x_min, x_max)")
129  *  });
130  *  </code></pre>
131  */
132 // F is a checker, F : T& -> ()
133 template <class T, class F>
134 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
135 bind_flag(T& value, const char* name, const char* description, F callback)
136 {
137   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
138                  callback(val);
139                  value = std::move(val);
140                }));
141 }
142
143 template <class T, class F>
144 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
145 bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
146 {
147   bind_flag(value, name, description, std::move(callback));
148   alias(name, std::move(aliases));
149 }
150
151 template <class T, class F>
152 typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
153 bind_flag(T& value, const char* name, const char* description, std::map<T, std::string> valid_values, F callback)
154 {
155   declare_flag(name, description, value,
156                std::function<void(const T&)>([&value, name, valid_values, callback](const T& val) {
157                  callback(val);
158                  bool found = false;
159                  for (auto kv : valid_values) {
160                    if (kv.first == val)
161                      found = true;
162                  }
163                  if (not found || std::string(val) == "help") {
164                    std::string mesg;
165                    if (std::string(val) == "help")
166                      mesg = std::string("\nPossible values for option ") + name + ":\n";
167                    else
168                      mesg = std::string("\nInvalid value '") + val + "' for option " + name + ". Possible values:\n";
169                    for (auto kv : valid_values)
170                      mesg += "  - '" + std::string(kv.first) + "': " + kv.second +
171                              (kv.first == value ? "  <=== DEFAULT" : "") + "\n";
172                    xbt_die("%s", mesg.c_str());
173                  }
174                  value = std::move(val);
175                }));
176 }
177 /** Bind a variable to configuration flag
178  *
179  *  <pre><code>
180  *  static int x;
181  *  simgrid::config::bind_flag(a, "x", [](int x) { return x > 0; });
182  *  </code></pre>
183  */
184 // F is a predicate, F : T const& -> bool
185 template <class T, class F>
186 typename std::enable_if<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
187 bind_flag(T& value, const char* name, const char* description, F callback)
188 {
189   declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
190                  if (not callback(val))
191                    throw std::range_error("invalid value.");
192                  value = std::move(val);
193                }));
194 }
195
196 /** A variable bound to a CLI option
197  *
198  *  <pre><code>
199  *  static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
200  *  static simgrid::config::flag<std::string> name("name", "Ford Perfect", "John Doe");
201  *  static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
202  *  </code></pre>
203  */
204 template<class T>
205 class Flag {
206   T value_;
207 public:
208
209   /** Constructor
210    *
211    *  @param name  Flag name
212    *  @param desc  Flag description
213    *  @param value Flag initial/default value
214    */
215   Flag(const char* name, const char* desc, T value) : value_(value)
216   {
217     simgrid::config::bind_flag(value_, name, desc);
218   }
219
220   /** Constructor taking also an array of aliases for name */
221   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value) : value_(value)
222   {
223     simgrid::config::bind_flag(value_, name, std::move(aliases), desc);
224   }
225
226   /* A constructor accepting a callback that will be passed the parameter.
227    * It can either return a boolean (informing whether the parameter is valid), or returning void.
228    */
229   template<class F>
230   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
231   {
232     simgrid::config::bind_flag(value_, name, desc, std::move(callback));
233   }
234
235   template <class F>
236   Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value, F callback)
237       : value_(value)
238   {
239     simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(callback));
240   }
241
242   /* A constructor accepting a map of valid values -> their description,
243    * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
244    */
245   template <class F>
246   Flag(const char* name, const char* desc, T value, std::map<T, std::string> valid_values, F callback) : value_(value)
247   {
248     simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback));
249   }
250
251   // No copy:
252   Flag(Flag const&) = delete;
253   Flag& operator=(Flag const&) = delete;
254
255   // Get the underlying value:
256   T& get() { return value_; }
257   T const& get() const { return value_; }
258
259   // Implicit conversion to the underlying type:
260   operator T&() { return value_; }
261   operator T const&() const{ return value_; }
262
263   // Basic interop with T:
264   template<class U>
265   Flag& operator=(U const& that) { value_ = that; return *this; }
266   template<class U>
267   Flag& operator=(U && that)     { value_ = that; return *this; }
268   template<class U>
269   bool operator==(U const& that) const { return value_ == that; }
270   template<class U>
271   bool operator!=(U const& that) const { return value_ != that; }
272   template<class U>
273   bool operator<(U const& that) const { return value_ < that; }
274   template<class U>
275   bool operator>(U const& that) const { return value_ > that; }
276   template<class U>
277   bool operator<=(U const& that) const { return value_ <= that; }
278   template<class U>
279   bool operator>=(U const& that) const { return value_ >= that; }
280 };
281
282 }
283 }
284 XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value<std::string>") XBT_PUBLIC std::string
285     xbt_cfg_get_string(const char* name);
286
287 #endif