Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[xbt] Fix 'control reaches end of non-void function' in Result::get()
[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() noexcept 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   using namespace std;
108   declareFlag<T>(name, description, value, [&value](T const& val) {
109     value = val;
110   });
111 }
112
113 template<class T>
114 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
115 {
116   bindFlag(value, *names.begin(), description);
117   alias(names);
118 }
119
120 /** Bind a variable to configuration flag
121  *
122  *  <pre><code>
123  *  static int x;
124  *  simgrid::config::bindFlag(a, "x", [](int x) {
125  *    if (x < x_min || x => x_max)
126  *      throw std::range_error("must be in [x_min, x_max)")
127  *  });
128  *  </code></pre>
129  */
130 // F is a checker, F : T& -> ()
131 template<class T, class F>
132 typename std::enable_if<std::is_same<
133   void,
134   decltype( std::declval<F>()(std::declval<const T&>()) )
135 >::value, void>::type
136 bindFlag(T& value, std::initializer_list<const char*> names, const char* description,
137   F callback)
138 {
139   bindFlag(value, *names.begin(), description);
140   alias(names);
141 }
142
143 template<class T, class F>
144 typename std::enable_if<std::is_same<
145   void,
146   decltype( std::declval<F>()(std::declval<const T&>()) )
147 >::value, void>::type
148 bindFlag(T& value, const char* name, const char* description,
149   F callback)
150 {
151   declareFlag(name, description, value, [&value,callback](const T& val) {
152     callback(val);
153     value = std::move(val);
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, [&value,callback](const T& val) {
174     if (!callback(val))
175       throw std::range_error("invalid value");
176     value = std::move(val);
177   });
178 }
179
180 /** A variable bound to a CLI option
181  *
182  *  <pre><code>
183  *  static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
184  *  static simgrid::config::flag<std::string> name("name", "Ford Prefect", "John Doe");
185  *  static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
186  *  </code></pre>
187  */
188 template<class T>
189 class Flag {
190   T value_;
191 public:
192
193   /** Constructor
194    *
195    *  @param name  Flag name
196    *  @param desc  Flag description
197    *  @param value Flag initial/default value
198    */
199   Flag(const char* name, const char* desc, T value) : value_(value)
200   {
201     simgrid::config::bindFlag(value_, name, desc);
202   }
203
204   template<class F>
205   Flag(const char* name, const char* desc, T value, F callback) : value_(value)
206   {
207     simgrid::config::bindFlag(value_, name, desc, std::move(callback));
208   }
209
210   // No copy:
211   Flag(Flag const&) = delete;
212   Flag& operator=(Flag const&) = delete;
213
214   // Get the underlying value:
215   T& get() { return value_; }
216   T const& get() const { return value_; }
217
218   // Implicit conversion to the underlying type:
219   operator T&() { return value_; }
220   operator T const&() const{ return value_; }
221
222   // Basic interop with T:
223   template<class U>
224   Flag& operator=(U const& that) { value_ = that; return *this; }
225   template<class U>
226   Flag& operator=(U && that)     { value_ = that; return *this; }
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   template<class U>
238   bool operator>=(U const& that) const { return value_ >= that; }
239 };
240
241 }
242 }
243
244 #endif