X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/0cfb40d124549f4dde6f00095847de0d04828adf..ab4d43a39df33bb5f24c3b2cc6d576130b16a649:/src/xbt/config.cpp diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 964343c37d..84c6b7b10a 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -1,37 +1,752 @@ -/* Copyright (c) 2016. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2004-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. */ +#include + +#include +#include +#include +#include + #include +#include +#include #include +#include +#include +#include +#include +#include "simgrid/sg_config.hpp" +#include "xbt/dynar.h" +#include "xbt/log.h" +#include "xbt/misc.h" +#include "xbt/sysdep.h" #include #include +#include + +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support"); + +XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr; namespace simgrid { namespace config { -static void callCallback(const char* name, void* data) +namespace { + +const char* true_values[] = { + "yes", "on", "true", "1" +}; +const char* false_values[] = { + "no", "off", "false", "0" +}; + +static bool parse_bool(const char* value) { - (*(std::function*) data)(name); + for (const char* const& true_value : true_values) + if (std::strcmp(true_value, value) == 0) + return true; + for (const char* const& false_value : false_values) + if (std::strcmp(false_value, value) == 0) + return false; + throw std::range_error("not a boolean"); } -static void freeCallback(void* data) +static double parse_double(const char* value) { - delete (std::function*) data; + char* end; + errno = 0; + double res = std::strtod(value, &end); + if (errno == ERANGE) + throw std::range_error("out of range"); + else if (errno) + xbt_die("Unexpected errno"); + if (end == value || *end != '\0') + throw std::range_error("invalid double"); + else + return res; } -void registerConfig(const char* name, const char* description, - e_xbt_cfgelm_type_t type, - std::function callback) +static long int parse_long(const char* value) { - std::function* code - = new std::function(std::move(callback)); - xbt_cfg_register_ext(name, description, type, - callCallback, code, freeCallback); + char* end; + errno = 0; + long int res = std::strtol(value, &end, 0); + if (errno) { + if (res == LONG_MIN && errno == ERANGE) + throw std::range_error("underflow"); + else if (res == LONG_MAX && errno == ERANGE) + throw std::range_error("overflow"); + xbt_die("Unexpected errno"); + } + if (end == value || *end != '\0') + throw std::range_error("invalid integer"); + else + return res; } +// ***** ConfigType ***** + +/// A trait which define possible options types: +template class ConfigType; + +template <> class ConfigType { +public: + static constexpr const char* type_name = "int"; + static inline double parse(const char* value) + { + return parse_long(value); + } +}; +template <> class ConfigType { +public: + static constexpr const char* type_name = "double"; + static inline double parse(const char* value) + { + return parse_double(value); + } +}; +template <> class ConfigType { +public: + static constexpr const char* type_name = "string"; + static inline std::string parse(const char* value) + { + return std::string(value); + } +}; +template <> class ConfigType { +public: + static constexpr const char* type_name = "boolean"; + static inline bool parse(const char* value) + { + return parse_bool(value); + } +}; + +// **** Forward declarations **** + +class ConfigurationElement ; +template class TypedConfigurationElement; + +// **** ConfigurationElement **** + +class ConfigurationElement { +private: + std::string key; + std::string desc; + bool isdefault = true; + +public: + /* Callback */ + xbt_cfg_cb_t old_callback = nullptr; + + ConfigurationElement(const char* key, const char* desc) : key(key ? key : ""), desc(desc ? desc : "") {} + ConfigurationElement(const char* key, const char* desc, xbt_cfg_cb_t cb) + : key(key ? key : ""), desc(desc ? desc : ""), old_callback(cb) {} + + virtual ~ConfigurationElement() = default; + + virtual std::string get_string_value() = 0; + virtual void set_string_value(const char* value) = 0; + virtual const char* get_type_name() = 0; + + template T const& get_value() const + { + return dynamic_cast&>(*this).get_value(); + } + template void set_value(T value) + { + dynamic_cast&>(*this).set_value(std::move(value)); + } + template void set_default_value(T value) + { + dynamic_cast&>(*this).set_default_value(std::move(value)); + } + void unset_default() { isdefault = false; } + bool is_default() const { return isdefault; } + + std::string const& get_description() const { return desc; } + std::string const& get_key() const { return key; } +}; + +// **** TypedConfigurationElement **** + +// TODO, could we use boost::any with some Type* reference? +template +class TypedConfigurationElement : public ConfigurationElement { +private: + T content; + std::function callback; + +public: + TypedConfigurationElement(const char* key, const char* desc, T value = T()) + : ConfigurationElement(key, desc), content(std::move(value)) + {} + TypedConfigurationElement(const char* key, const char* desc, T value, xbt_cfg_cb_t cb) + : ConfigurationElement(key, desc, cb), content(std::move(value)) + {} + TypedConfigurationElement(const char* key, const char* desc, T value, std::function callback) + : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback)) + {} + ~TypedConfigurationElement() = default; + + std::string get_string_value() override; + const char* get_type_name() override; + void set_string_value(const char* value) override; + + void update() + { + if (old_callback) + this->old_callback(get_key().c_str()); + if (this->callback) + this->callback(this->content); + } + + T const& get_value() const { return content; } + + void set_value(T value) + { + this->content = std::move(value); + this->update(); + this->unset_default(); + } + + void set_default_value(T value) + { + if (this->is_default()) { + this->content = std::move(value); + this->update(); + } else { + XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", + get_key().c_str(), to_string(value).c_str()); + } + } +}; + +template std::string TypedConfigurationElement::get_string_value() // override +{ + return to_string(content); +} + +template void TypedConfigurationElement::set_string_value(const char* value) // override +{ + this->content = ConfigType::parse(value); + this->unset_default(); + this->update(); +} + +template const char* TypedConfigurationElement::get_type_name() // override +{ + return ConfigType::type_name; +} + +} // end of anonymous namespace + +// **** Config **** + +class Config { +private: + // name -> ConfigElement: + std::map options; + // alias -> ConfigElement from options: + std::map aliases; + bool warn_for_aliases = true; + +public: + Config(); + ~Config(); + + // No copy: + Config(Config const&) = delete; + Config& operator=(Config const&) = delete; + + ConfigurationElement& operator[](const char* name); + void alias(const char* realname, const char* aliasname); + + template + simgrid::config::TypedConfigurationElement* register_option(const char* name, A&&... a) + { + xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.", name); + TypedConfigurationElement* variable = new TypedConfigurationElement(name, std::forward(a)...); + XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name, variable->get_description().c_str(), + variable->get_type_name(), variable, this); + options.insert({name, variable}); + variable->update(); + return variable; + } + + // Debug: + void dump(const char *name, const char *indent); + void show_aliases(); + void help(); + +protected: + ConfigurationElement* get_dict_element(const char* name); +}; + +Config::Config() +{ + atexit(&sg_config_finalize); +} +Config::~Config() +{ + XBT_DEBUG("Frees cfg set %p", this); + for (auto const& elm : options) + delete elm.second; } -} \ No newline at end of file + +inline ConfigurationElement* Config::get_dict_element(const char* name) +{ + auto opt = options.find(name); + if (opt != options.end()) { + return opt->second; + } else { + auto als = aliases.find(name); + if (als != aliases.end()) { + ConfigurationElement* res = als->second; + if (warn_for_aliases) + XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, res->get_key().c_str()); + return res; + } else { + THROWF(not_found_error, 0, "Bad config key: %s", name); + } + } +} + +inline ConfigurationElement& Config::operator[](const char* name) +{ + return *(get_dict_element(name)); +} + +void Config::alias(const char* realname, const char* aliasname) +{ + xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname); + ConfigurationElement* element = this->get_dict_element(realname); + xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname); + this->aliases.insert({aliasname, element}); +} + +/** @brief Dump a config set for debuging purpose + * + * @param name The name to give to this config set + * @param indent what to write at the beginning of each line (right number of spaces) + */ +void Config::dump(const char *name, const char *indent) +{ + if (name) + printf("%s>> Dumping of the config set '%s':\n", indent, name); + + for (auto const& elm : options) + printf("%s %s: ()%s) %s", indent, elm.first.c_str(), elm.second->get_type_name(), + elm.second->get_string_value().c_str()); + + if (name) + printf("%s<< End of the config set '%s'\n", indent, name); + fflush(stdout); +} + +/** @brief Displays the declared aliases and their replacement */ +void Config::show_aliases() +{ + for (auto const& elm : aliases) + printf(" %-40s %s\n", elm.first.c_str(), elm.second->get_key().c_str()); +} + +/** @brief Displays the declared options and their description */ +void Config::help() +{ + for (auto const& elm : options) { + simgrid::config::ConfigurationElement* variable = this->options.at(elm.first); + printf(" %s: %s\n", elm.first.c_str(), variable->get_description().c_str()); + printf(" Type: %s; ", variable->get_type_name()); + printf("Current value: %s\n", variable->get_string_value().c_str()); + } +} + +// ***** get_config ***** + +template XBT_PUBLIC T const& get_config(const char* name) +{ + return (*simgrid_config)[name].get_value(); +} + +template XBT_PUBLIC int const& get_config(const char* name); +template XBT_PUBLIC double const& get_config(const char* name); +template XBT_PUBLIC bool const& get_config(const char* name); +template XBT_PUBLIC std::string const& get_config(const char* name); + +// ***** alias ***** + +void alias(const char* realname, std::initializer_list aliases) +{ + for (auto const& aliasname : aliases) + simgrid_config->alias(realname, aliasname); +} + +// ***** declare_flag ***** + +template +XBT_PUBLIC void declare_flag(const char* name, const char* description, T value, std::function callback) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->register_option(name, description, std::move(value), std::move(callback)); +} + +template XBT_PUBLIC void declare_flag(const char* name, const char* description, int value, + std::function callback); +template XBT_PUBLIC void declare_flag(const char* name, const char* description, double value, + std::function callback); +template XBT_PUBLIC void declare_flag(const char* name, const char* description, bool value, + std::function callback); +template XBT_PUBLIC void declare_flag(const char* name, const char* description, std::string value, + std::function callback); +} +} + +// ***** C bindings ***** + +xbt_cfg_t xbt_cfg_new() +{ + return new simgrid::config::Config(); +} +void xbt_cfg_free(xbt_cfg_t * cfg) { delete *cfg; } + +void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg) +{ + cfg->dump(name, indent); +} + +/*----[ Registering stuff ]-----------------------------------------------*/ + +void xbt_cfg_register_double(const char *name, double default_value, + xbt_cfg_cb_t cb_set, const char *desc) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->register_option(name, desc, default_value, cb_set); +} + +void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->register_option(name, desc, default_value, cb_set); +} + +void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->register_option(name, desc, default_value ? default_value : "", cb_set); +} + +void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->register_option(name, desc, simgrid::config::parse_bool(default_value), cb_set); +} + +void xbt_cfg_register_alias(const char *realname, const char *aliasname) +{ + if (simgrid_config == nullptr) + simgrid_config = xbt_cfg_new(); + simgrid_config->alias(realname, aliasname); +} + +void xbt_cfg_aliases() +{ + simgrid_config->show_aliases(); +} +void xbt_cfg_help() +{ + simgrid_config->help(); +} + +/*----[ Setting ]---------------------------------------------------------*/ + +/** @brief Add values parsed from a string into a config set + * + * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ',' + * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an + * already registered variable, and [value] conforms to the data type under which this variable was registered. + * + * @todo This is a crude manual parser, it should be a proper lexer. + */ +void xbt_cfg_set_parse(const char *options) +{ + if (not options || not strlen(options)) { /* nothing to do */ + return; + } + + XBT_DEBUG("List to parse and set:'%s'", options); + std::string optionlist(options); + while (not optionlist.empty()) { + XBT_DEBUG("Still to parse and set: '%s'", optionlist.c_str()); + + // skip separators + size_t pos = optionlist.find_first_not_of(" \t\n,"); + optionlist.erase(0, pos); + // find option + pos = optionlist.find_first_of(" \t\n,"); + std::string name = optionlist.substr(0, pos); + optionlist.erase(0, pos); + XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), optionlist.c_str()); + + if (name.empty()) + continue; + + pos = name.find(':'); + xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'", + name.c_str()); + + std::string val = name.substr(pos + 1); + name.erase(pos); + + const std::string path("path"); + if (name.compare(0, path.length(), path) != 0) + XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str()); + + (*simgrid_config)[name.c_str()].set_string_value(val.c_str()); + } +} + +/** @brief Set the value of a variable, using the string representation of that value + * + * @param key name of the variable to modify + * @param value string representation of the value to set + */ + +void xbt_cfg_set_as_string(const char *key, const char *value) +{ + (*simgrid_config)[key].set_string_value(value); +} + +/** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet + * + * This is useful to change the default value of a variable while allowing + * users to override it with command line arguments + */ +void xbt_cfg_setdefault_int(const char *key, int value) +{ + (*simgrid_config)[key].set_default_value(value); +} + +/** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet + * + * This is useful to change the default value of a variable while allowing + * users to override it with command line arguments + */ +void xbt_cfg_setdefault_double(const char *key, double value) +{ + (*simgrid_config)[key].set_default_value(value); +} + +/** @brief Set a string value to \a name within \a cfg if it wasn't changed yet + * + * This is useful to change the default value of a variable while allowing + * users to override it with command line arguments + */ +void xbt_cfg_setdefault_string(const char *key, const char *value) +{ + (*simgrid_config)[key].set_default_value(value ? value : ""); +} + +/** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet + * + * This is useful to change the default value of a variable while allowing + * users to override it with command line arguments + */ +void xbt_cfg_setdefault_boolean(const char *key, const char *value) +{ + (*simgrid_config)[key].set_default_value(simgrid::config::parse_bool(value)); +} + +/** @brief Set an integer value to \a name within \a cfg + * + * @param key the name of the variable + * @param value the value of the variable + */ +void xbt_cfg_set_int(const char *key, int value) +{ + (*simgrid_config)[key].set_value(value); +} + +/** @brief Set or add a double value to \a name within \a cfg + * + * @param key the name of the variable + * @param value the double to set + */ +void xbt_cfg_set_double(const char *key, double value) +{ + (*simgrid_config)[key].set_value(value); +} + +/** @brief Set or add a string value to \a name within \a cfg + * + * @param key the name of the variable + * @param value the value to be added + * + */ +void xbt_cfg_set_string(const char* key, const char* value) +{ + (*simgrid_config)[key].set_value(value); +} + +/** @brief Set or add a boolean value to \a name within \a cfg + * + * @param key the name of the variable + * @param value the value of the variable + */ +void xbt_cfg_set_boolean(const char *key, const char *value) +{ + (*simgrid_config)[key].set_value(simgrid::config::parse_bool(value)); +} + + +/* Say if the value is the default value */ +int xbt_cfg_is_default_value(const char *key) +{ + return (*simgrid_config)[key].is_default() ? 1 : 0; +} + +/*----[ Getting ]---------------------------------------------------------*/ +/** @brief Retrieve an integer value of a variable (get a warning if not uniq) + * + * @param key the name of the variable + * + * Returns the first value from the config set under the given name. + */ +int xbt_cfg_get_int(const char *key) +{ + return (*simgrid_config)[key].get_value(); +} + +/** @brief Retrieve a double value of a variable (get a warning if not uniq) + * + * @param key the name of the variable + * + * Returns the first value from the config set under the given name. + */ +double xbt_cfg_get_double(const char *key) +{ + return (*simgrid_config)[key].get_value(); +} + +/** @brief Retrieve a string value of a variable (get a warning if not uniq) + * + * @param key the name of the variable + * + * Returns the first value from the config set under the given name. + * If there is more than one value, it will issue a warning. + * Returns nullptr if there is no value. + * + * \warning the returned value is the actual content of the config set + */ +std::string xbt_cfg_get_string(const char* key) +{ + return (*simgrid_config)[key].get_value(); +} + +/** @brief Retrieve a boolean value of a variable (get a warning if not uniq) + * + * @param key the name of the variable + * + * Returns the first value from the config set under the given name. + * If there is more than one value, it will issue a warning. + */ +int xbt_cfg_get_boolean(const char *key) +{ + return (*simgrid_config)[key].get_value() ? 1 : 0; +} + +#ifdef SIMGRID_TEST + +#include + +#include "xbt.h" +#include "xbt/ex.h" +#include + +#include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg); + +XBT_TEST_SUITE("config", "Configuration support"); + +XBT_PUBLIC_DATA xbt_cfg_t simgrid_config; + +static void make_set() +{ + simgrid_config = nullptr; + xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical); + simgrid::config::declare_flag("speed", "description", 0); + simgrid::config::declare_flag("peername", "description", ""); + simgrid::config::declare_flag("user", "description", ""); +} /* end_of_make_set */ + +XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set") +{ + auto temp = simgrid_config; + make_set(); + xbt_test_add("Alloc and free a config set"); + xbt_cfg_set_parse("peername:veloce user:bidule"); + xbt_cfg_free(&simgrid_config); + simgrid_config = temp; +} + +XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests") +{ + auto temp = simgrid_config; + make_set(); + xbt_test_add("Get a single value"); + { + /* get_single_value */ + xbt_cfg_set_parse("peername:toto:42 speed:42"); + int ival = simgrid::config::get_config("speed"); + if (ival != 42) + xbt_test_fail("Speed value = %d, I expected 42", ival); + } + + xbt_test_add("Access to a non-existant entry"); + { + try { + xbt_cfg_set_parse("color:blue"); + } catch(xbt_ex& e) { + if (e.category != not_found_error) + xbt_test_exception(e); + } + } + xbt_cfg_free(&simgrid_config); + simgrid_config = temp; +} + +XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags") +{ + auto temp = simgrid_config; + make_set(); + xbt_test_add("C++ declaration of flags"); + + simgrid::config::Flag int_flag("int", "", 0); + simgrid::config::Flag string_flag("string", "", "foo"); + simgrid::config::Flag double_flag("double", "", 0.32); + simgrid::config::Flag bool_flag1("bool1", "", false); + simgrid::config::Flag bool_flag2("bool2", "", true); + + xbt_test_add("Parse values"); + xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false"); + xbt_test_assert(int_flag == 42, "Check int flag"); + xbt_test_assert(string_flag == "bar", "Check string flag"); + xbt_test_assert(double_flag == 8.0, "Check double flag"); + xbt_test_assert(bool_flag1, "Check bool1 flag"); + xbt_test_assert(not bool_flag2, "Check bool2 flag"); + + xbt_cfg_free(&simgrid_config); + simgrid_config = temp; +} + +#endif /* SIMGRID_TEST */