Logo AND Algorithmique Numérique Distribuée

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