Logo AND Algorithmique Numérique Distribuée

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