Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[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 <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 class XBT_PUBLIC 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> XBT_PUBLIC T const& getConfig(const char* name);
56
57 extern template XBT_PUBLIC int const& getConfig<int>(const char* name);
58 extern template XBT_PUBLIC double const& getConfig<double>(const char* name);
59 extern template XBT_PUBLIC bool const& getConfig<bool>(const char* name);
60 extern template XBT_PUBLIC std::string const& getConfig<std::string>(const char* name);
61
62 // Register:
63
64 /** Register a configuration flag
65  *
66  *  @param name        name of the option
67  *  @param description Description of the option
68  *  @param value       Initial/default value
69  *  @param callback    called with the option value
70  */
71 template <class T>
72 XBT_PUBLIC void declareFlag(const char* name, const char* description, T value,
73                             std::function<void(const T&)> callback = std::function<void(const T&)>());
74
75 extern template XBT_PUBLIC void declareFlag(const char* name, const char* description, int value,
76                                             std::function<void(int const&)> callback);
77 extern template XBT_PUBLIC void declareFlag(const char* name, const char* description, double value,
78                                             std::function<void(double const&)> callback);
79 extern template XBT_PUBLIC void declareFlag(const char* name, const char* description, bool value,
80                                             std::function<void(bool const&)> callback);
81 extern template XBT_PUBLIC void declareFlag(const char* name, const char* description, std::string value,
82                                             std::function<void(std::string const&)> callback);
83
84 // ***** alias *****
85
86 XBT_PUBLIC void alias(const char* realname, const char* aliasname);
87
88 inline
89 void alias(std::initializer_list<const char*> names)
90 {
91   auto i = names.begin();
92   for (++i; i != names.end(); ++i)
93     alias(*names.begin(), *i);
94 }
95
96 /** Bind a variable to configuration flag
97  *
98  *  @param value Bound variable
99  *  @param name  Flag name
100  *  @param description Option description
101  */
102 template<class T>
103 void bindFlag(T& value, const char* name, const char* description)
104 {
105   declareFlag<T>(name, description, value, [&value](T const& val) {
106     value = val;
107   });
108 }
109
110 template<class T>
111 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
112 {
113   bindFlag(value, *names.begin(), description);
114   alias(names);
115 }
116
117 /** Bind a variable to configuration flag
118  *
119  *  <pre><code>
120  *  static int x;
121  *  simgrid::config::bindFlag(a, "x", [](int x) {
122  *    if (x < x_min || x => x_max)
123  *      throw std::range_error("must be in [x_min, x_max)")
124  *  });
125  *  </code></pre>
126  */
127 // F is a checker, F : T& -> ()
128 template<class T, class F>
129 typename std::enable_if<std::is_same<
130   void,
131   decltype( std::declval<F>()(std::declval<const T&>()) )
132 >::value, void>::type
133 bindFlag(T& value, std::initializer_list<const char*> names, const char* description,
134   F callback)
135 {
136   bindFlag(value, *names.begin(), description);
137   alias(names);
138 }
139
140 template<class T, class F>
141 typename std::enable_if<std::is_same<
142   void,
143   decltype( std::declval<F>()(std::declval<const T&>()) )
144 >::value, void>::type
145 bindFlag(T& value, const char* name, const char* description,
146   F callback)
147 {
148   declareFlag(name, description, value,
149     std::function<void(const T&)>([&value,callback](const T& val) {
150       callback(val);
151       value = std::move(val);
152     }
153   ));
154 }
155
156 /** Bind a variable to configuration flag
157  *
158  *  <pre><code>
159  *  static int x;
160  *  simgrid::config::bindFlag(a, "x", [](int x) { return x > 0; });
161  *  </code></pre>
162  */
163 // F is a predicate, F : T const& -> bool
164 template<class T, class F>
165 typename std::enable_if<std::is_same<
166   bool,
167   decltype( std::declval<F>()(std::declval<const T&>()) )
168 >::value, void>::type
169 bindFlag(T& value, const char* name, const char* description,
170   F callback)
171 {
172   declareFlag(name, description, value,
173     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
181 /** A variable bound to a CLI option
182  *
183  *  <pre><code>
184  *  static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
185  *  static simgrid::config::flag<std::string> name("name", "Ford Perfect", "John Doe");
186  *  static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
187  *  </code></pre>
188  */
189 template<class T>
190 class Flag {
191   T value_;
192 public:
193
194   /** Constructor
195    *
196    *  @param name  Flag name
197    *  @param desc  Flag description
198    *  @param value Flag initial/default value
199    */
200   Flag(const char* name, const char* desc, T value) : value_(value)
201   {
202     simgrid::config::bindFlag(value_, name, desc);
203   }
204
205   template<class F>
206   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
207   {
208     simgrid::config::bindFlag(value_, name, desc, std::move(callback));
209   }
210
211   // No copy:
212   Flag(Flag const&) = delete;
213   Flag& operator=(Flag const&) = delete;
214
215   // Get the underlying value:
216   T& get() { return value_; }
217   T const& get() const { return value_; }
218
219   // Implicit conversion to the underlying type:
220   operator T&() { return value_; }
221   operator T const&() const{ return value_; }
222
223   // Basic interop with T:
224   template<class U>
225   Flag& operator=(U const& that) { value_ = that; return *this; }
226   template<class U>
227   Flag& operator=(U && that)     { value_ = that; return *this; }
228   template<class U>
229   bool operator==(U const& that) const { return value_ == that; }
230   template<class U>
231   bool operator!=(U const& that) const { return value_ != that; }
232   template<class U>
233   bool operator<(U const& that) const { return value_ < that; }
234   template<class U>
235   bool operator>(U const& that) const { return value_ > that; }
236   template<class U>
237   bool operator<=(U const& that) const { return value_ <= that; }
238   template<class U>
239   bool operator>=(U const& that) const { return value_ >= that; }
240 };
241
242 }
243 }
244 XBT_PUBLIC std::string xbt_cfg_get_string(const char* name);
245
246 #endif