Logo AND Algorithmique Numérique Distribuée

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