Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove "using namespace"
[simgrid.git] / include / xbt / config.hpp
index f87f525..8200bf0 100644 (file)
@@ -7,9 +7,16 @@
 #ifndef _XBT_CONFIG_HPP_
 #define _XBT_CONFIG_HPP_
 
+#include <xbt/base.h>
+
+#include <cstdlib>
+
 #include <functional>
+#include <initializer_list>
+#include <stdexcept>
 #include <string>
 #include <type_traits>
+#include <utility>
 
 #include <xbt/base.h>
 #include <xbt/config.h>
 namespace simgrid {
 namespace config {
 
-/** Get the base type of a e_xbt_cfgelm_type_t
- *
- *  * `type` is the type used in the config framework to store
- *     the values of this type;
- *
- *  * `get()` is used to get such a value from the configuration.
- */
-template<e_xbt_cfgelm_type_t type>
-struct base_type;
-template<> struct base_type<xbt_cfgelm_boolean> {
-  typedef bool type;
-  static inline bool get(const char* name)
-  {
-    return xbt_cfg_get_boolean(name) ? true : false;
-  }
-};
-template<> struct base_type<xbt_cfgelm_int> {
-  typedef int type;
-  static inline int get(const char* name)
-  {
-    return xbt_cfg_get_int(name);
-  }
-};
-template<> struct base_type<xbt_cfgelm_double> {
-  typedef double type;
-  static inline double get(const char* name)
-  {
-    return xbt_cfg_get_double(name);
-  }
-};
-template<> struct base_type<xbt_cfgelm_string> {
-  typedef std::string type;
-  static inline std::string get(const char* name)
-  {
-    char* value = xbt_cfg_get_string(name);
-    return value != nullptr ? value : "";
-  }
+XBT_PUBLIC_CLASS missing_key_error : public std::runtime_error {
+public:
+  explicit missing_key_error(const std::string& what)
+    : std::runtime_error(what) {}
+  explicit missing_key_error(const char* what)
+    : std::runtime_error(what) {}
+  ~missing_key_error() override;
 };
 
-/** Associate the e_xbt_cfgelm_type_t to use for a given type */
-template<class T>
-struct cfgelm_type : public std::integral_constant<e_xbt_cfgelm_type_t,
-  std::is_same<T, bool>::value ? xbt_cfgelm_boolean :
-  std::is_integral<T>::value ? xbt_cfgelm_int :
-  std::is_floating_point<T>::value ? xbt_cfgelm_double :
-  xbt_cfgelm_string>
-{};
-
-/** Get a value of a given type from the configuration */
-template<class T> inline
-T get(const char* name)
-{
-  return T(base_type<cfgelm_type<T>::value>::get(name));
-}
 template<class T> inline
-T get(std::string const& name)
-{
-  return T(base_type<cfgelm_type<T>::value>::get(name.c_str()));
-}
-
-inline void setDefault(const char* name, int value)
+std::string to_string(T&& value)
 {
-  xbt_cfg_setdefault_int(name, value);
+  return std::to_string(std::forward<T>(value));
 }
-inline void setDefault(const char* name, double value)
+inline std::string const& to_string(std::string& value)
 {
-  xbt_cfg_setdefault_double(name, value);
+  return value;
 }
-inline void setDefault(const char* name, const char* value)
+inline std::string const& to_string(std::string const& value)
 {
-  xbt_cfg_setdefault_string(name, value);
+  return value;
 }
-inline void setDefault(const char* name, std::string const& value)
+inline std::string to_string(std::string&& value)
 {
-  xbt_cfg_setdefault_string(name, value.c_str());
-}
-inline void setDefault(const char* name, bool value)
-{
-  xbt_cfg_setdefault_boolean(name, value ? "yes" : "no");
+  return std::move(value);
 }
 
-/** Set the default value of a given configuration option */
-template<class T> inline
-void setDefault(const char* name, T value)
-{
-  setDefault(name, base_type<cfgelm_type<T>::value>::type(value));
-}
+// Get config
+
+template<class T>
+XBT_PUBLIC(T const&) getConfig(const char* name);
+
+extern template XBT_PUBLIC(int const&) getConfig<int>(const char* name);
+extern template XBT_PUBLIC(double const&) getConfig<double>(const char* name);
+extern template XBT_PUBLIC(bool const&) getConfig<bool>(const char* name);
+extern template XBT_PUBLIC(std::string const&) getConfig<std::string>(const char* name);
 
 // Register:
 
@@ -111,12 +67,33 @@ void setDefault(const char* name, T value)
  *
  *  @param name        name of the option
  *  @param description Description of the option
- *  @param type        config storage type for the option
- *  @param callback    called with the option name (expected to use `simgrid::config::get`)
+ *  @param value       Initial/default value
+ *  @param callback    called with the option value
  */
-XBT_PUBLIC(void) registerConfig(const char* name, const char* description,
-  e_xbt_cfgelm_type_t type,
-  std::function<void(const char*)> callback);
+template<class T>
+XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
+  T value, std::function<void(const T&)> callback = std::function<void(const T&)>());
+
+extern template XBT_PUBLIC(void) declareFlag(const char* name,
+  const char* description, int value, std::function<void(int const &)> callback);
+extern template XBT_PUBLIC(void) declareFlag(const char* name,
+  const char* description, double value, std::function<void(double const &)> callback);
+extern template XBT_PUBLIC(void) declareFlag(const char* name,
+  const char* description, bool value, std::function<void(bool const &)> callback);
+extern template XBT_PUBLIC(void) declareFlag(const char* name,
+  const char* description, std::string value, std::function<void(std::string const &)> callback);
+
+// ***** alias *****
+
+XBT_PUBLIC(void) alias(const char* realname, const char* aliasname);
+
+inline
+void alias(std::initializer_list<const char*> names)
+{
+  auto i = names.begin();
+  for (++i; i != names.end(); ++i)
+    alias(*names.begin(), *i);
+}
 
 /** Bind a variable to configuration flag
  *
@@ -125,30 +102,78 @@ XBT_PUBLIC(void) registerConfig(const char* name, const char* description,
  *  @param description Option description
  */
 template<class T>
-void declareFlag(T& value, const char* name, const char* description)
+void bindFlag(T& value, const char* name, const char* description)
 {
-  registerConfig(name, description, cfgelm_type<T>::value,
-    [&value](const char* name) {
-      value = simgrid::config::get<T>(name);
-    });
-  setDefault(name, value);
+  declareFlag<T>(name, description, value, [&value](T const& val) {
+    value = val;
+  });
+}
+
+template<class T>
+void bindFlag(T& value, std::initializer_list<const char*> names, const char* description)
+{
+  bindFlag(value, *names.begin(), description);
+  alias(names);
 }
 
 /** Bind a variable to configuration flag
  *
- *  @param value Bound variable
- *  @param name  Flag name
- *  @param description Option description
- *  @f     Callback (called with the option name) providing the value
+ *  <pre><code>
+ *  static int x;
+ *  simgrid::config::bindFlag(a, "x", [](int x) {
+ *    if (x < x_min || x => x_max)
+ *      throw std::range_error("must be in [x_min, x_max)")
+ *  });
+ *  </code></pre>
  */
+// F is a checker, F : T& -> ()
 template<class T, class F>
-void declareFlag(T& value, const char* name, const char* description, F f)
+typename std::enable_if<std::is_same<
+  void,
+  decltype( std::declval<F>()(std::declval<const T&>()) )
+>::value, void>::type
+bindFlag(T& value, std::initializer_list<const char*> names, const char* description,
+  F callback)
 {
-  registerConfig(name, description, cfgelm_type<T>::value,
-    [&value,f](const char* name) {
-      value = f(name);
-    });
-  setDefault(name, value);
+  bindFlag(value, *names.begin(), description);
+  alias(names);
+}
+
+template<class T, class F>
+typename std::enable_if<std::is_same<
+  void,
+  decltype( std::declval<F>()(std::declval<const T&>()) )
+>::value, void>::type
+bindFlag(T& value, const char* name, const char* description,
+  F callback)
+{
+  declareFlag(name, description, value, [&value,callback](const T& val) {
+    callback(val);
+    value = std::move(val);
+  });
+}
+
+/** Bind a variable to configuration flag
+ *
+ *  <pre><code>
+ *  static int x;
+ *  simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
+ *  </code></pre>
+ */
+// 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
+bindFlag(T& value, const char* name, const char* description,
+  F callback)
+{
+  declareFlag(name, description, value, [&value,callback](const T& val) {
+    if (!callback(val))
+      throw std::range_error("invalid value");
+    value = std::move(val);
+  });
 }
 
 /** A variable bound to a CLI option
@@ -160,7 +185,7 @@ void declareFlag(T& value, const char* name, const char* description, F f)
  *  </code></pre>
  */
 template<class T>
-class flag {
+class Flag {
   T value_;
 public:
 
@@ -170,14 +195,20 @@ public:
    *  @param desc  Flag description
    *  @param value Flag initial/default value
    */
-  flag(const char* name, const char* desc, T value) : value_(value)
+  Flag(const char* name, const char* desc, T value) : value_(value)
   {
-    declareFlag(value_, name, desc);
+    simgrid::config::bindFlag(value_, name, desc);
+  }
+
+  template<class F>
+  Flag(const char* name, const char* desc, T value, F callback) : value_(value)
+  {
+    simgrid::config::bindFlag(value_, name, desc, std::move(callback));
   }
 
   // No copy:
-  flag(flag const&) = delete;
-  flag& operator=(flag const&) = delete;
+  Flag(Flag const&) = delete;
+  Flag& operator=(Flag const&) = delete;
 
   // Get the underlying value:
   T& get() { return value_; }
@@ -186,6 +217,24 @@ public:
   // Implicit conversion to the underlying type:
   operator T&() { return value_; }
   operator T const&() const{ return value_; }
+
+  // Basic interop with T:
+  template<class U>
+  Flag& operator=(U const& that) { value_ = that; return *this; }
+  template<class U>
+  Flag& operator=(U && that)     { value_ = that; return *this; }
+  template<class U>
+  bool operator==(U const& that) const { return value_ == that; }
+  template<class U>
+  bool operator!=(U const& that) const { return value_ != that; }
+  template<class U>
+  bool operator<(U const& that) const { return value_ < that; }
+  template<class U>
+  bool operator>(U const& that) const { return value_ > that; }
+  template<class U>
+  bool operator<=(U const& that) const { return value_ <= that; }
+  template<class U>
+  bool operator>=(U const& that) const { return value_ >= that; }
 };
 
 }