1 /* Copyright (c) 2016-2017. The SimGrid Team. All rights reserved. */
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. */
14 #include <initializer_list>
17 #include <type_traits>
21 #include <xbt/config.h>
26 XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error {
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;
35 template<class T> inline
36 std::string to_string(T&& value)
38 return std::to_string(std::forward<T>(value));
40 inline std::string const& to_string(std::string& value)
44 inline std::string const& to_string(std::string const& value)
48 inline std::string to_string(std::string&& value)
50 return std::move(value);
56 XBT_PUBLIC(T const&) getConfig(const char* name);
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);
65 /** Register a configuration flag
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
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&)>());
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);
87 XBT_PUBLIC(void) alias(const char* realname, const char* aliasname);
90 void alias(std::initializer_list<const char*> names)
92 auto i = names.begin();
93 for (++i; i != names.end(); ++i)
94 alias(*names.begin(), *i);
97 /** Bind a variable to configuration flag
99 * @param value Bound variable
100 * @param name Flag name
101 * @param description Option description
104 void bindFlag(T& value, const char* name, const char* description)
106 declareFlag<T>(name, description, value, [&value](T const& val) {
112 void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
114 bindFlag(value, *names.begin(), description);
118 /** Bind a variable to configuration flag
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)")
128 // F is a checker, F : T& -> ()
129 template<class T, class F>
130 typename std::enable_if<std::is_same<
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,
137 bindFlag(value, *names.begin(), description);
141 template<class T, class F>
142 typename std::enable_if<std::is_same<
144 decltype( std::declval<F>()(std::declval<const T&>()) )
145 >::value, void>::type
146 bindFlag(T& value, const char* name, const char* description,
149 declareFlag(name, description, value, [&value,callback](const T& val) {
151 value = std::move(val);
155 /** Bind a variable to configuration flag
159 * simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
162 // F is a predicate, F : T const& -> bool
163 template<class T, class F>
164 typename std::enable_if<std::is_same<
166 decltype( std::declval<F>()(std::declval<const T&>()) )
167 >::value, void>::type
168 bindFlag(T& value, const char* name, const char* description,
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);
178 /** A variable bound to a CLI option
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);
193 * @param name Flag name
194 * @param desc Flag description
195 * @param value Flag initial/default value
197 Flag(const char* name, const char* desc, T value) : value_(value)
199 simgrid::config::bindFlag(value_, name, desc);
203 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
205 simgrid::config::bindFlag(value_, name, desc, std::move(callback));
209 Flag(Flag const&) = delete;
210 Flag& operator=(Flag const&) = delete;
212 // Get the underlying value:
213 T& get() { return value_; }
214 T const& get() const { return value_; }
216 // Implicit conversion to the underlying type:
217 operator T&() { return value_; }
218 operator T const&() const{ return value_; }
220 // Basic interop with T:
222 Flag& operator=(U const& that) { value_ = that; return *this; }
224 Flag& operator=(U && that) { value_ = that; return *this; }
226 bool operator==(U const& that) const { return value_ == that; }
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; }