Logo AND Algorithmique Numérique Distribuée

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