Logo AND Algorithmique Numérique Distribuée

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