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)
108 declareFlag<T>(name, description, value, [&value](T const& val) {
114 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
116 bindFlag(value, *names.begin(), description);
120 /** Bind a variable to configuration flag
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)")
130 // F is a checker, F : T& -> ()
131 template<class T, class F>
132 typename std::enable_if<std::is_same<
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,
139 bindFlag(value, *names.begin(), description);
143 template<class T, class F>
144 typename std::enable_if<std::is_same<
146 decltype( std::declval<F>()(std::declval<const T&>()) )
147 >::value, void>::type
148 bindFlag(T& value, const char* name, const char* description,
151 declareFlag(name, description, value, [&value,callback](const T& val) {
153 value = std::move(val);
157 /** Bind a variable to configuration flag
161 * simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
164 // F is a predicate, F : T const& -> bool
165 template<class T, class F>
166 typename std::enable_if<std::is_same<
168 decltype( std::declval<F>()(std::declval<const T&>()) )
169 >::value, void>::type
170 bindFlag(T& value, const char* name, const char* description,
173 declareFlag(name, description, value, [&value,callback](const T& val) {
175 throw std::range_error("invalid value");
176 value = std::move(val);
180 /** A variable bound to a CLI option
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);
195 * @param name Flag name
196 * @param desc Flag description
197 * @param value Flag initial/default value
199 Flag(const char* name, const char* desc, T value) : value_(value)
201 simgrid::config::bindFlag(value_, name, desc);
205 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
207 simgrid::config::bindFlag(value_, name, desc, std::move(callback));
211 Flag(Flag const&) = delete;
212 Flag& operator=(Flag const&) = delete;
214 // Get the underlying value:
215 T& get() { return value_; }
216 T const& get() const { return value_; }
218 // Implicit conversion to the underlying type:
219 operator T&() { return value_; }
220 operator T const&() const{ return value_; }
222 // Basic interop with T:
224 Flag& operator=(U const& that) { value_ = that; return *this; }
226 Flag& operator=(U && that) { value_ = that; return *this; }
228 bool operator==(U const& that) const { return value_ == that; }
230 bool operator!=(U const& that) const { return value_ != that; }
232 bool operator<(U const& that) const { return value_ < that; }
234 bool operator>(U const& that) const { return value_ > that; }
236 bool operator<=(U const& that) const { return value_ <= that; }
238 bool operator>=(U const& that) const { return value_ >= that; }