-/* Copyright (c) 2016-2020. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <xbt/base.h>
#include <xbt/sysdep.h>
+#include <xbt/utility.hpp>
-namespace simgrid {
-namespace config {
+namespace simgrid::config {
class Config;
extern template XBT_PUBLIC bool const& get_value<bool>(const std::string& name);
extern template XBT_PUBLIC std::string const& get_value<std::string>(const std::string& name);
+// ***** alias *****
+
+XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
+
// Register:
/** Register a configuration flag
*/
template <class T>
XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, T value,
- std::function<void(const T&)> callback = std::function<void(const T&)>());
+ std::function<void(const T&)> callback = nullptr);
+template <class T>
+void declare_flag(const std::string& name, std::initializer_list<const char*> aliases, const std::string& description,
+ T value, std::function<void(const T&)> callback = nullptr)
+{
+ declare_flag(name, description, std::move(value), std::move(callback));
+ alias(name.c_str(), aliases);
+}
extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, int value,
std::function<void(int const&)> callback);
extern template XBT_PUBLIC void declare_flag(const std::string& name, const std::string& description, std::string value,
std::function<void(std::string const&)> callback);
-// ***** alias *****
-
-XBT_PUBLIC void alias(const char* realname, std::initializer_list<const char*> aliases);
-
/** Bind a variable to configuration flag
*
* @param value Bound variable
void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
{
bind_flag(value, name, description);
- alias(name, std::move(aliases));
+ alias(name, aliases);
}
/** Bind a variable to configuration flag
*/
// F is a checker, F : T& -> ()
template <class T, class F>
-typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
+typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
bind_flag(T& value, const char* name, const char* description, F callback)
{
declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
}
template <class T, class F>
-typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
+typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
{
bind_flag(value, name, description, std::move(callback));
- alias(name, std::move(aliases));
+ alias(name, aliases);
}
-template <class T, class F>
-typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
-bind_flag(T& value, const char* name, const char* description, std::map<T, std::string> valid_values, F callback)
+template <class F>
+typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>, void>
+bind_flag(std::string& value, const char* name, const char* description,
+ const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
{
declare_flag(name, description, value,
- std::function<void(const T&)>([&value, name, valid_values, callback](const T& val) {
+ std::function<void(const std::string&)>([&value, name, valid_values, callback](const std::string& val) {
callback(val);
- bool found = false;
- for (auto kv : valid_values) {
- if (kv.first == val)
- found = true;
+ if (valid_values.find(val) != valid_values.end()) {
+ value = val;
+ return;
}
- if (not found || std::string(val) == "help") {
- std::string mesg = "\n";
- if (std::string(val) == "help")
- mesg += std::string("Possible values for option ") + name + ":\n";
- else
- mesg += std::string("Invalid value '") + val + "' for option " + name + ". Possible values:\n";
- for (auto kv : valid_values)
- mesg += " - '" + std::string(kv.first) + "': " + kv.second +
- (kv.first == value ? " <=== DEFAULT" : "") + "\n";
- xbt_die("%s", mesg.c_str());
- }
- value = std::move(val);
+ std::string mesg = "\n";
+ if (val == "help")
+ mesg += std::string("Possible values for option ") + name + ":\n";
+ else
+ mesg += "Invalid value '" + val + "' for option " + name + ". Possible values:\n";
+ for (auto const& [v, descr] : valid_values)
+ mesg += " - '" + v + "': " + descr + (v == value ? " <=== DEFAULT" : "") + "\n";
+ xbt_die("%s", mesg.c_str());
}));
}
-template <class T, class F>
-typename std::enable_if<std::is_same<void, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
-bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
- std::map<T, std::string> valid_values, F callback)
+template <class F>
+typename std::enable_if_t<std::is_same_v<void, decltype(std::declval<F>()(std::declval<const std::string&>()))>, void>
+bind_flag(std::string& value, const char* name, std::initializer_list<const char*> aliases, const char* description,
+ const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
{
- bind_flag(value, name, description, std::move(valid_values), std::move(callback));
- alias(name, std::move(aliases));
+ bind_flag(value, name, description, valid_values, std::move(callback));
+ alias(name, aliases);
}
/** Bind a variable to configuration flag
*/
// F is a predicate, F : T const& -> bool
template <class T, class F>
-typename std::enable_if<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
+typename std::enable_if_t<std::is_same_v<bool, decltype(std::declval<F>()(std::declval<const T&>()))>, void>
bind_flag(T& value, const char* name, const char* description, F callback)
{
declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
/** A variable bound to a CLI option
*
* <pre><code>
- * static simgrid::config::flag<int> answer("answer", "Expected answer", 42);
- * static simgrid::config::flag<std::string> name("name", "Ford Perfect", "John Doe");
- * static simgrid::config::flag<double> gamma("gamma", "Gamma factor", 1.987);
+ * static simgrid::config::Flag<int> answer("answer", "Expected answer", 42);
+ * static simgrid::config::Flag<std::string> name("name", "Ford Perfect", "John Doe");
+ * static simgrid::config::Flag<double> gamma("gamma", "Gamma factor", 1.987);
* </code></pre>
*/
template<class T>
* @param desc Flag description
* @param value Flag initial/default value
*/
- Flag(const char* name, const char* desc, T value) : value_(value), name_(name)
+ Flag(const char* name, const char* desc, xbt::type_identity_t<T> value) : value_(value), name_(name)
{
simgrid::config::bind_flag(value_, name, desc);
}
/** Constructor taking also an array of aliases for name */
- Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value)
+ Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value)
: value_(value), name_(name)
{
- simgrid::config::bind_flag(value_, name, std::move(aliases), desc);
+ simgrid::config::bind_flag(value_, name, aliases, desc);
}
/* A constructor accepting a callback that will be passed the parameter.
* It can either return a boolean (informing whether the parameter is valid), or returning void.
*/
- template <class F> Flag(const char* name, const char* desc, T value, F callback) : value_(value), name_(name)
+ template <class F>
+ Flag(const char* name, const char* desc, xbt::type_identity_t<T> value, F callback) : value_(value), name_(name)
{
simgrid::config::bind_flag(value_, name, desc, std::move(callback));
}
template <class F>
- Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value, F callback)
+ Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
+ F callback)
: value_(value), name_(name)
{
- simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(callback));
+ simgrid::config::bind_flag(value_, name, aliases, desc, std::move(callback));
}
/* A constructor accepting a map of valid values -> their description,
* and producing an informative error message when an invalid value is passed, or when help is passed as a value.
*/
+ Flag(const char* name, const char* desc, xbt::type_identity_t<T> value,
+ const std::map<std::string, std::string, std::less<>>& valid_values)
+ : value_(value), name_(name)
+ {
+ simgrid::config::bind_flag(value_, name, desc, valid_values, [](const std::string&) {});
+ }
+
+ /* As earlier, a constructor accepting a map of valid values -> their description,
+ * and producing an informative error message when an invalid value is passed, or when help is passed as a value.
+ * But also take a callback that is invoked before the verification of parameter name validity.
+ */
template <class F>
- Flag(const char* name, const char* desc, T value, std::map<T, std::string> valid_values, F callback)
+ Flag(const char* name, const char* desc, xbt::type_identity_t<T> value,
+ const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
: value_(value), name_(name)
{
- simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback));
+ simgrid::config::bind_flag(value_, name, desc, valid_values, std::move(callback));
}
/* A constructor with everything */
template <class F>
- Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, T value,
- std::map<T, std::string> valid_values, F callback)
+ Flag(const char* name, std::initializer_list<const char*> aliases, const char* desc, xbt::type_identity_t<T> value,
+ const std::map<std::string, std::string, std::less<>>& valid_values, F callback)
: value_(value), name_(name)
{
- simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(valid_values), std::move(callback));
+ simgrid::config::bind_flag(value_, name, aliases, desc, valid_values, std::move(callback));
}
// No copy:
XBT_PUBLIC void finalize();
XBT_PUBLIC void show_aliases();
XBT_PUBLIC void help();
-} // namespace config
-} // namespace simgrid
+
+} // namespace simgrid::config
#endif