Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a4e751728790525ec2eccfb261edf1d9691565c0
[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, [&value,callback](const T& val) {
149     callback(val);
150     value = std::move(val);
151   });
152 }
153
154 /** Bind a variable to configuration flag
155  *
156  *  <pre><code>
157  *  static int x;
158  *  simgrid::config::bindFlag(a, "x", [](int x) { return 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<
164   bool,
165   decltype( std::declval<F>()(std::declval<const T&>()) )
166 >::value, void>::type
167 bindFlag(T& value, const char* name, const char* description,
168   F callback)
169 {
170   declareFlag(name, description, value, [&value, callback](const T& val) {
171     if (not callback(val))
172       throw std::range_error("invalid value");
173     value = std::move(val);
174   });
175 }
176
177 /** A variable bound to a CLI option
178  *
179  *  <pre><code>
180  *  static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
181  *  static simgrid::config::flag<std::string> name("name", "Ford Prefect", "John Doe");
182  *  static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
183  *  </code></pre>
184  */
185 template<class T>
186 class Flag {
187   T value_;
188 public:
189
190   /** Constructor
191    *
192    *  @param name  Flag name
193    *  @param desc  Flag description
194    *  @param value Flag initial/default value
195    */
196   Flag(const char* name, const char* desc, T value) : value_(value)
197   {
198     simgrid::config::bindFlag(value_, name, desc);
199   }
200
201   template<class F>
202   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
203   {
204     simgrid::config::bindFlag(value_, name, desc, std::move(callback));
205   }
206
207   // No copy:
208   Flag(Flag const&) = delete;
209   Flag& operator=(Flag const&) = delete;
210
211   // Get the underlying value:
212   T& get() { return value_; }
213   T const& get() const { return value_; }
214
215   // Implicit conversion to the underlying type:
216   operator T&() { return value_; }
217   operator T const&() const{ return value_; }
218
219   // Basic interop with T:
220   template<class U>
221   Flag& operator=(U const& that) { value_ = that; return *this; }
222   template<class U>
223   Flag& operator=(U && that)     { value_ = that; return *this; }
224   template<class U>
225   bool operator==(U const& that) const { return value_ == that; }
226   template<class U>
227   bool operator!=(U const& that) const { return value_ != that; }
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 };
237
238 }
239 }
240 XBT_PUBLIC std::string xbt_cfg_get_string(const char* name);
241
242 #endif