Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #302 from mpoquet/rename-s4u-synchro-examples
[simgrid.git] / include / xbt / config.hpp
index 42f8fdf..270ee12 100644 (file)
@@ -1,73 +1,30 @@
-/* Copyright (c) 2016. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2016-2018. 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. */
 
-#ifndef _XBT_CONFIG_HPP_
-#define _XBT_CONFIG_HPP_
+#ifndef XBT_CONFIG_HPP
+#define XBT_CONFIG_HPP
+
+#include <xbt/base.h>
 
 #include <cstdlib>
 
 #include <functional>
+#include <initializer_list>
+#include <map>
 #include <stdexcept>
 #include <string>
 #include <type_traits>
 #include <utility>
 
 #include <xbt/base.h>
-#include <xbt/config.h>
+#include <xbt/sysdep.h>
 
 namespace simgrid {
 namespace config {
 
-XBT_PUBLIC(bool) parseBool(const char* value);
-XBT_PUBLIC(double) parseDouble(const char* value);
-XBT_PUBLIC(long int) parseLong(const char* value);
-
-template<class T> struct parse_option {
-  static inline T parse(const char* value)
-  {
-    return T(value);
-  }
-};
-
-template<> struct parse_option<std::string> {
-  static inline std::string parse(const char* value)
-  {
-    return std::string(value);
-  }
-};
-
-template<>
-struct parse_option<double> {
-  static inline double parse(const char* value)
-  {
-    return parseDouble(value);
-  }
-};
-
-template<>
-struct parse_option<int> {
-  static inline double parse(const char* value)
-  {
-    return parseLong(value);
-  }
-};
-
-template<>
-struct parse_option<bool> {
-  static inline bool parse(const char* value)
-  {
-    return parseBool(value);
-  }
-};
-
-template<class T> inline
-T parse(const char* value)
-{
-  return parse_option<T>::parse(value);
-}
+class Config;
 
 template<class T> inline
 std::string to_string(T&& value)
@@ -87,16 +44,63 @@ inline std::string to_string(std::string&& value)
   return std::move(value);
 }
 
+// Set default
+
+template <class T> XBT_PUBLIC void set_default(const char* name, T value);
+
+extern template XBT_PUBLIC void set_default<int>(const char* name, int value);
+extern template XBT_PUBLIC void set_default<double>(const char* name, double value);
+extern template XBT_PUBLIC void set_default<bool>(const char* name, bool value);
+extern template XBT_PUBLIC void set_default<std::string>(const char* name, std::string value);
+
+XBT_PUBLIC bool is_default(const char* name);
+
+// Set config
+
+template <class T> XBT_PUBLIC void set_value(const char* name, T value);
+
+extern template XBT_PUBLIC void set_value<int>(const char* name, int value);
+extern template XBT_PUBLIC void set_value<double>(const char* name, double value);
+extern template XBT_PUBLIC void set_value<bool>(const char* name, bool value);
+extern template XBT_PUBLIC void set_value<std::string>(const char* name, std::string value);
+
+XBT_PUBLIC void set_as_string(const char* name, const std::string& value);
+XBT_PUBLIC void set_parse(std::string options);
+
+// Get config
+
+template <class T> XBT_PUBLIC T const& get_value(std::string name);
+
+extern template XBT_PUBLIC int const& get_value<int>(std::string name);
+extern template XBT_PUBLIC double const& get_value<double>(std::string name);
+extern template XBT_PUBLIC bool const& get_value<bool>(std::string name);
+extern template XBT_PUBLIC std::string const& get_value<std::string>(std::string name);
+
 // Register:
 
 /** Register a configuration flag
  *
  *  @param name        name of the option
  *  @param description Description of the option
+ *  @param value       Initial/default value
  *  @param callback    called with the option value
  */
-XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
-  std::function<void(const char* value)> callback);
+template <class T>
+XBT_PUBLIC void declare_flag(std::string name, std::string description, T value,
+                             std::function<void(const T&)> callback = std::function<void(const T&)>());
+
+extern template XBT_PUBLIC void declare_flag(std::string name, std::string description, int value,
+                                             std::function<void(int const&)> callback);
+extern template XBT_PUBLIC void declare_flag(std::string name, std::string description, double value,
+                                             std::function<void(double const&)> callback);
+extern template XBT_PUBLIC void declare_flag(std::string name, std::string description, bool value,
+                                             std::function<void(bool const&)> callback);
+extern template XBT_PUBLIC void declare_flag(std::string name, 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
  *
@@ -104,105 +108,114 @@ XBT_PUBLIC(void) declareFlag(const char* name, const char* description,
  *  @param name  Flag name
  *  @param description Option description
  */
-template<class T>
-void bindFlag(T& value, const char* name, const char* description)
+template <class T> void bind_flag(T& value, const char* name, const char* description)
 {
-  using namespace std;
-  declareFlag(name, description, [&value](const char* val) {
-    value = simgrid::config::parse<T>(val);
-  });
-  xbt_cfg_setdefault_string(name, simgrid::config::to_string(value).c_str());
+  declare_flag<T>(name, description, value, [&value](T const& val) { value = val; });
 }
 
-/** Bind a variable to configuration flag
- *
- *  <pre><code>
- *  static int x;
- *  simgrid::config::bindFlag(a, "x", [](const char* value) {
- *    return simgrid::config::parse(value);
- *  }
- *  </pre><code>
- */
-// F is a parser, F : const char* -> T
-template<class T, class F>
-typename std::enable_if<std::is_same<
-  T,
-  typename std::remove_cv< decltype(
-    std::declval<F>()(std::declval<const char*>())
-  ) >::type
->::value, void>::type
-bindFlag(T& value, const char* name, const char* description,
-  F callback)
+template <class T>
+void bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description)
 {
-  declareFlag(name, description, [&value,callback](const char* val) {
-    value = callback(val);
-  });
-  xbt_cfg_setdefault_string(name, to_string(value).c_str());
+  bind_flag(value, name, description);
+  alias(name, std::move(aliases));
 }
 
 /** Bind a variable to configuration flag
  *
  *  <pre><code>
  *  static int x;
- *  simgrid::config::bindFlag(a, "x", [](int x) {
+ *  simgrid::config::bind_flag(a, "x", [](int x) {
  *    if (x < x_min || x => x_max)
  *      throw std::range_error("must be in [x_min, x_max)")
  *  });
- *  </pre><code>
+ *  </code></pre>
  */
 // 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
-bindFlag(T& value, const char* name, const char* description,
-  F callback)
+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, F callback)
+{
+  declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
+                 callback(val);
+                 value = std::move(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
+bind_flag(T& value, const char* name, std::initializer_list<const char*> aliases, const char* description, F callback)
 {
-  declareFlag(name, description, [&value,callback](const char* val) {
-    T res = parse<T>(val);
-    callback(res);
-    value = std::move(res);
-  });
-  xbt_cfg_setdefault_string(name, to_string(value).c_str());
+  bind_flag(value, name, description, std::move(callback));
+  alias(name, std::move(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)
+{
+  declare_flag(name, description, value,
+               std::function<void(const T&)>([&value, name, valid_values, callback](const T& val) {
+                 callback(val);
+                 bool found = false;
+                 for (auto kv : valid_values) {
+                   if (kv.first == val)
+                     found = true;
+                 }
+                 if (not found || std::string(val) == "help") {
+                   std::string mesg = std::string("\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);
+               }));
+}
+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)
+{
+  bind_flag(value, name, description, std::move(valid_values), std::move(callback));
+  alias(name, std::move(aliases));
 }
 
 /** Bind a variable to configuration flag
  *
  *  <pre><code>
  *  static int x;
- *  simgrid::config::bindFlag(a, "x", [](int x) { return return x > 0; });
- *  </pre><code>
+ *  simgrid::config::bind_flag(a, "x", [](int x) { 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)
+template <class T, class F>
+typename std::enable_if<std::is_same<bool, decltype(std::declval<F>()(std::declval<const T&>()))>::value, void>::type
+bind_flag(T& value, const char* name, const char* description, F callback)
 {
-  declareFlag(name, description, [&value,callback](const char* val) {
-    T res = parse<T>(val);
-    if (!callback(res))
-      throw std::range_error("invalid value");
-    value = std::move(res);
-  });
-  xbt_cfg_setdefault_string(name, to_string(value).c_str());
+  declare_flag(name, description, value, std::function<void(const T&)>([&value, callback](const T& val) {
+                 if (not callback(val))
+                   throw std::range_error("invalid value.");
+                 value = std::move(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 Prefect", "John Doe");
+ *  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>
 class Flag {
   T value_;
+  std::string name_;
+
 public:
 
   /** Constructor
@@ -211,15 +224,50 @@ 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), 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)
+      : value_(value), name_(name)
+  {
+    simgrid::config::bind_flag(value_, name, std::move(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)
+  {
+    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)
+      : value_(value), name_(name)
+  {
+    simgrid::config::bind_flag(value_, name, std::move(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.
+   */
+  template <class F>
+  Flag(const char* name, const char* desc, T value, std::map<T, std::string> valid_values, F callback)
+      : value_(value), name_(name)
   {
-    simgrid::config::bindFlag(value_, name, desc);
+    simgrid::config::bind_flag(value_, name, desc, std::move(valid_values), std::move(callback));
   }
 
-  template<class F>
-  Flag(const char* name, const char* desc, T value, F callback) : value_(value)
+  /* 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)
+      : value_(value), name_(name)
   {
-    simgrid::config::bindFlag(value_, name, desc, std::move(callback));
+    simgrid::config::bind_flag(value_, name, std::move(aliases), desc, std::move(valid_values), std::move(callback));
   }
 
   // No copy:
@@ -230,22 +278,36 @@ public:
   T& get() { return value_; }
   T const& get() const { return value_; }
 
+  std::string get_name() const { return name_; }
   // Implicit conversion to the underlying type:
   operator T&() { return value_; }
   operator T const&() const{ return value_; }
 
   // Basic interop with T:
-  Flag& operator=(T const& that) { value_ = that; return *this; }
-  Flag& operator=(T && that)     { value_ = that; return *this; }
-  bool operator==(T const& that) const { return value_ == that; }
-  bool operator!=(T const& that) const { return value_ != that; }
-  bool operator<(T const& that) const { return value_ < that; }
-  bool operator>(T const& that) const { return value_ > that; }
-  bool operator<=(T const& that) const { return value_ <= that; }
-  bool operator>=(T const& that) const { return value_ >= that; }
+  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; }
 };
 
+XBT_PUBLIC void finalize();
+XBT_PUBLIC void show_aliases();
+XBT_PUBLIC void help();
 }
 }
+XBT_ATTRIB_DEPRECATED_v323("Please use simgrid::config::get_value<std::string>") XBT_PUBLIC std::string
+    xbt_cfg_get_string(const char* name);
 
 #endif