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,
150 std::function<void(const T&)>([&value,callback](const T& val) {
152 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,
174 std::function<void(const T&)>([&value, callback](const T& val) {
175 if (not callback(val))
176 throw std::range_error("invalid value");
177 value = std::move(val);
182 /** A variable bound to a CLI option
185 * static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
186 * static simgrid::config::flag<std::string> name("name", "Ford Prefect", "John Doe");
187 * static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
197 * @param name Flag name
198 * @param desc Flag description
199 * @param value Flag initial/default value
201 Flag(const char* name, const char* desc, T value) : value_(value)
203 simgrid::config::bindFlag(value_, name, desc);
207 Flag(const char* name, const char* desc, T value, F callback) : value_(value)
209 simgrid::config::bindFlag(value_, name, desc, std::move(callback));
213 Flag(Flag const&) = delete;
214 Flag& operator=(Flag const&) = delete;
216 // Get the underlying value:
217 T& get() { return value_; }
218 T const& get() const { return value_; }
220 // Implicit conversion to the underlying type:
221 operator T&() { return value_; }
222 operator T const&() const{ return value_; }
224 // Basic interop with T:
226 Flag& operator=(U const& that) { value_ = that; return *this; }
228 Flag& operator=(U && that) { value_ = that; return *this; }
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; }
240 bool operator>=(U const& that) const { return value_ >= that; }
245 XBT_PUBLIC(std::string) xbt_cfg_get_string(const char* name);