Logo AND Algorithmique Numérique Distribuée

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