Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
config: fix declareFlag template
[simgrid.git] / include / xbt / config.hpp
1 /* Copyright (c) 2016-2017. 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 <stdexcept>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19
20 #include <xbt/base.h>
21 #include <xbt/config.h>
22
23 namespace simgrid {
24 namespace config {
25
26 XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error {
27 public:
28   explicit missing_key_error(const std::string& what)
29     : std::runtime_error(what) {}
30   explicit missing_key_error(const char* what)
31     : std::runtime_error(what) {}
32   ~missing_key_error() override;
33 };
34
35 template<class T> inline
36 std::string to_string(T&& value)
37 {
38   return std::to_string(std::forward<T>(value));
39 }
40 inline std::string const& to_string(std::string& value)
41 {
42   return value;
43 }
44 inline std::string const& to_string(std::string const& value)
45 {
46   return value;
47 }
48 inline std::string to_string(std::string&& value)
49 {
50   return std::move(value);
51 }
52
53 // Get config
54
55 template<class T>
56 XBT_PUBLIC(T const&) getConfig(const char* name);
57
58 extern template XBT_PUBLIC(int const&) getConfig<int>(const char* name);
59 extern template XBT_PUBLIC(double const&) getConfig<double>(const char* name);
60 extern template XBT_PUBLIC(bool const&) getConfig<bool>(const char* name);
61 extern template XBT_PUBLIC(std::string const&) getConfig<std::string>(const char* name);
62
63 // Register:
64
65 /** Register a configuration flag
66  *
67  *  @param name        name of the option
68  *  @param description Description of the option
69  *  @param value       Initial/default value
70  *  @param callback    called with the option value
71  */
72 template<class T>
73 XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
74   T value, std::function<void(const T&)> callback = std::function<void(const T&)>());
75
76 extern template XBT_PUBLIC(void) declareFlag(const char* name,
77   const char* description, int value, std::function<void(int const &)> callback);
78 extern template XBT_PUBLIC(void) declareFlag(const char* name,
79   const char* description, double value, std::function<void(double const &)> callback);
80 extern template XBT_PUBLIC(void) declareFlag(const char* name,
81   const char* description, bool value, std::function<void(bool const &)> callback);
82 extern template XBT_PUBLIC(void) declareFlag(const char* name,
83   const char* description, std::string value, std::function<void(std::string const &)> callback);
84
85 // ***** alias *****
86
87 XBT_PUBLIC(void) alias(const char* realname, const char* aliasname);
88
89 inline
90 void alias(std::initializer_list<const char*> names)
91 {
92   auto i = names.begin();
93   for (++i; i != names.end(); ++i)
94     alias(*names.begin(), *i);
95 }
96
97 /** Bind a variable to configuration flag
98  *
99  *  @param value Bound variable
100  *  @param name  Flag name
101  *  @param description Option description
102  */
103 template<class T>
104 void bindFlag(T& value, const char* name, const char* description)
105 {
106   declareFlag<T>(name, description, value, [&value](T const& val) {
107     value = val;
108   });
109 }
110
111 template<class T>
112 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
113 {
114   bindFlag(value, *names.begin(), description);
115   alias(names);
116 }
117
118 /** Bind a variable to configuration flag
119  *
120  *  <pre><code>
121  *  static int x;
122  *  simgrid::config::bindFlag(a, "x", [](int x) {
123  *    if (x < x_min || x => x_max)
124  *      throw std::range_error("must be in [x_min, x_max)")
125  *  });
126  *  </code></pre>
127  */
128 // F is a checker, F : T& -> ()
129 template<class T, class F>
130 typename std::enable_if<std::is_same<
131   void,
132   decltype( std::declval<F>()(std::declval<const T&>()) )
133 >::value, void>::type
134 bindFlag(T& value, std::initializer_list<const char*> names, const char* description,
135   F callback)
136 {
137   bindFlag(value, *names.begin(), description);
138   alias(names);
139 }
140
141 template<class T, class F>
142 typename std::enable_if<std::is_same<
143   void,
144   decltype( std::declval<F>()(std::declval<const T&>()) )
145 >::value, void>::type
146 bindFlag(T& value, const char* name, const char* description,
147   F callback)
148 {
149   declareFlag(name, description, value,
150     std::function<void(const T&)>([&value,callback](const T& val) {
151       callback(val);
152       value = std::move(val);
153     }
154   ));
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 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,
174     std::function<void(const T&)>([&value, callback](const T& val) {
175       if (not callback(val))
176         throw std::range_error("invalid value");
177         value = std::move(val);
178     })
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 Prefect", "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::bindFlag(value_, name, desc);
204   }
205
206   template<class F>
207   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
208   {
209     simgrid::config::bindFlag(value_, name, desc, std::move(callback));
210   }
211
212   // No copy:
213   Flag(Flag const&) = delete;
214   Flag& operator=(Flag const&) = delete;
215
216   // Get the underlying value:
217   T& get() { return value_; }
218   T const& get() const { return value_; }
219
220   // Implicit conversion to the underlying type:
221   operator T&() { return value_; }
222   operator T const&() const{ return value_; }
223
224   // Basic interop with T:
225   template<class U>
226   Flag& operator=(U const& that) { value_ = that; return *this; }
227   template<class U>
228   Flag& operator=(U && that)     { value_ = that; return *this; }
229   template<class U>
230   bool operator==(U const& that) const { return value_ == that; }
231   template<class U>
232   bool operator!=(U const& that) const { return value_ != that; }
233   template<class U>
234   bool operator<(U const& that) const { return value_ < that; }
235   template<class U>
236   bool operator>(U const& that) const { return value_ > that; }
237   template<class U>
238   bool operator<=(U const& that) const { return value_ <= that; }
239   template<class U>
240   bool operator>=(U const& that) const { return value_ >= that; }
241 };
242
243 }
244 }
245 XBT_PUBLIC(std::string) xbt_cfg_get_string(const char* name);
246
247 #endif