Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
end of simplification of dependencies
[simgrid.git] / include / xbt / config.hpp
1 /* Copyright (c) 2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef _XBT_CONFIG_HPP_
8 #define _XBT_CONFIG_HPP_
9
10 #include <xbt/base.h>
11
12 #include <cstdlib>
13
14 #include <functional>
15 #include <initializer_list>
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
24 namespace simgrid {
25 namespace config {
26
27 XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error {
28 public:
29   explicit missing_key_error(const std::string& what)
30     : std::runtime_error(what) {}
31   explicit missing_key_error(const char* what)
32     : std::runtime_error(what) {}
33   ~missing_key_error() override;
34 };
35
36 template<class T> inline
37 std::string to_string(T&& value)
38 {
39   return std::to_string(std::forward<T>(value));
40 }
41 inline std::string const& to_string(std::string& value)
42 {
43   return value;
44 }
45 inline std::string const& to_string(std::string const& value)
46 {
47   return value;
48 }
49 inline std::string to_string(std::string&& value)
50 {
51   return std::move(value);
52 }
53
54 // Get config
55
56 template<class T>
57 XBT_PUBLIC(T const&) getConfig(const char* name);
58
59 extern template XBT_PUBLIC(int const&) getConfig<int>(const char* name);
60 extern template XBT_PUBLIC(double const&) getConfig<double>(const char* name);
61 extern template XBT_PUBLIC(bool const&) getConfig<bool>(const char* name);
62 extern template XBT_PUBLIC(std::string const&) getConfig<std::string>(const char* name);
63
64 // Register:
65
66 /** Register a configuration flag
67  *
68  *  @param name        name of the option
69  *  @param description Description of the option
70  *  @param value       Initial/default value
71  *  @param callback    called with the option value
72  */
73 template<class T>
74 XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
75   T value, std::function<void(const T&)> callback = std::function<void(const T&)>());
76
77 extern template XBT_PUBLIC(void) declareFlag(const char* name,
78   const char* description, int value, std::function<void(int const &)> callback);
79 extern template XBT_PUBLIC(void) declareFlag(const char* name,
80   const char* description, double value, std::function<void(double const &)> callback);
81 extern template XBT_PUBLIC(void) declareFlag(const char* name,
82   const char* description, bool value, std::function<void(bool const &)> callback);
83 extern template XBT_PUBLIC(void) declareFlag(const char* name,
84   const char* description, std::string value, std::function<void(std::string const &)> callback);
85
86 // ***** alias *****
87
88 XBT_PUBLIC(void) alias(const char* realname, const char* aliasname);
89
90 inline
91 void alias(std::initializer_list<const char*> names)
92 {
93   auto i = names.begin();
94   for (++i; i != names.end(); ++i)
95     alias(*names.begin(), *i);
96 }
97
98 /** Bind a variable to configuration flag
99  *
100  *  @param value Bound variable
101  *  @param name  Flag name
102  *  @param description Option description
103  */
104 template<class T>
105 void bindFlag(T& value, const char* name, const char* description)
106 {
107   declareFlag<T>(name, description, value, [&value](T const& val) {
108     value = val;
109   });
110 }
111
112 template<class T>
113 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
114 {
115   bindFlag(value, *names.begin(), description);
116   alias(names);
117 }
118
119 /** Bind a variable to configuration flag
120  *
121  *  <pre><code>
122  *  static int x;
123  *  simgrid::config::bindFlag(a, "x", [](int x) {
124  *    if (x < x_min || x => x_max)
125  *      throw std::range_error("must be in [x_min, x_max)")
126  *  });
127  *  </code></pre>
128  */
129 // F is a checker, F : T& -> ()
130 template<class T, class F>
131 typename std::enable_if<std::is_same<
132   void,
133   decltype( std::declval<F>()(std::declval<const T&>()) )
134 >::value, void>::type
135 bindFlag(T& value, std::initializer_list<const char*> names, const char* description,
136   F callback)
137 {
138   bindFlag(value, *names.begin(), description);
139   alias(names);
140 }
141
142 template<class T, class F>
143 typename std::enable_if<std::is_same<
144   void,
145   decltype( std::declval<F>()(std::declval<const T&>()) )
146 >::value, void>::type
147 bindFlag(T& value, const char* name, const char* description,
148   F callback)
149 {
150   declareFlag(name, description, value, [&value,callback](const T& val) {
151     callback(val);
152     value = std::move(val);
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 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, [&value,callback](const T& val) {
173     if (!callback(val))
174       throw std::range_error("invalid value");
175     value = std::move(val);
176   });
177 }
178
179 /** A variable bound to a CLI option
180  *
181  *  <pre><code>
182  *  static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
183  *  static simgrid::config::flag<std::string> name("name", "Ford Prefect", "John Doe");
184  *  static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
185  *  </code></pre>
186  */
187 template<class T>
188 class Flag {
189   T value_;
190 public:
191
192   /** Constructor
193    *
194    *  @param name  Flag name
195    *  @param desc  Flag description
196    *  @param value Flag initial/default value
197    */
198   Flag(const char* name, const char* desc, T value) : value_(value)
199   {
200     simgrid::config::bindFlag(value_, name, desc);
201   }
202
203   template<class F>
204   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
205   {
206     simgrid::config::bindFlag(value_, name, desc, std::move(callback));
207   }
208
209   // No copy:
210   Flag(Flag const&) = delete;
211   Flag& operator=(Flag const&) = delete;
212
213   // Get the underlying value:
214   T& get() { return value_; }
215   T const& get() const { return value_; }
216
217   // Implicit conversion to the underlying type:
218   operator T&() { return value_; }
219   operator T const&() const{ return value_; }
220
221   // Basic interop with T:
222   template<class U>
223   Flag& operator=(U const& that) { value_ = that; return *this; }
224   template<class U>
225   Flag& operator=(U && that)     { value_ = that; return *this; }
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   template<class U>
237   bool operator>=(U const& that) const { return value_ >= that; }
238 };
239
240 }
241 }
242
243 #endif