Logo AND Algorithmique Numérique Distribuée

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