1 /* Copyright (c) 2016. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef _XBT_CONFIG_HPP_
8 #define _XBT_CONFIG_HPP_
15 #include <initializer_list>
18 #include <type_traits>
22 #include <xbt/config.h>
27 XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error {
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;
36 template<class T> inline
37 std::string to_string(T&& value)
39 return std::to_string(std::forward<T>(value));
41 inline std::string const& to_string(std::string& value)
45 inline std::string const& to_string(std::string const& value)
49 inline std::string to_string(std::string&& value)
51 return std::move(value);
57 XBT_PUBLIC(T const&) getConfig(const char* name);
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);
66 /** Register a configuration flag
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
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&)>());
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);
88 XBT_PUBLIC(void) alias(const char* realname, const char* aliasname);
91 void alias(std::initializer_list<const char*> names)
93 auto i = names.begin();
94 for (++i; i != names.end(); ++i)
95 alias(*names.begin(), *i);
98 /** Bind a variable to configuration flag
100 * @param value Bound variable
101 * @param name Flag name
102 * @param description Option description
105 void bindFlag(T& value, const char* name, const char* description)
107 declareFlag<T>(name, description, value, [&value](T const& val) {
113 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
115 bindFlag(value, *names.begin(), description);
119 /** Bind a variable to configuration flag
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)")
129 // F is a checker, F : T& -> ()
130 template<class T, class F>
131 typename std::enable_if<std::is_same<
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,
138 bindFlag(value, *names.begin(), description);
142 template<class T, class F>
143 typename std::enable_if<std::is_same<
145 decltype( std::declval<F>()(std::declval<const T&>()) )
146 >::value, void>::type
147 bindFlag(T& value, const char* name, const char* description,
150 declareFlag(name, description, value, [&value,callback](const T& val) {
152 value = std::move(val);
156 /** Bind a variable to configuration flag
160 * simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
163 // F is a predicate, F : T const& -> bool
164 template<class T, class F>
165 typename std::enable_if<std::is_same<
167 decltype( std::declval<F>()(std::declval<const T&>()) )
168 >::value, void>::type
169 bindFlag(T& value, const char* name, const char* description,
172 declareFlag(name, description, value, [&value,callback](const T& val) {
174 throw std::range_error("invalid value");
175 value = std::move(val);
179 /** A variable bound to a CLI option
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);
194 * @param name Flag name
195 * @param desc Flag description
196 * @param value Flag initial/default value
198 Flag(const char* name, const char* desc, T value) : value_(value)
200 simgrid::config::bindFlag(value_, name, desc);
204 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
206 simgrid::config::bindFlag(value_, name, desc, std::move(callback));
210 Flag(Flag const&) = delete;
211 Flag& operator=(Flag const&) = delete;
213 // Get the underlying value:
214 T& get() { return value_; }
215 T const& get() const { return value_; }
217 // Implicit conversion to the underlying type:
218 operator T&() { return value_; }
219 operator T const&() const{ return value_; }
221 // Basic interop with T:
223 Flag& operator=(U const& that) { value_ = that; return *this; }
225 Flag& operator=(U && that) { value_ = that; return *this; }
227 bool operator==(U const& that) const { return value_ == that; }
229 bool operator!=(U const& that) const { return value_ != that; }
231 bool operator<(U const& that) const { return value_ < that; }
233 bool operator>(U const& that) const { return value_ > that; }
235 bool operator<=(U const& that) const { return value_ <= that; }
237 bool operator>=(U const& that) const { return value_ >= that; }