From 0ac754a3a1133fcb39c1a1ecb1a68649939c76ce Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 19 Apr 2018 12:05:30 +0200 Subject: [PATCH] Snake_case in xbt/config. --- include/xbt/config.hpp | 10 +-- src/xbt/config.cpp | 174 ++++++++++++++++++++--------------------- 2 files changed, 91 insertions(+), 93 deletions(-) diff --git a/include/xbt/config.hpp b/include/xbt/config.hpp index 5c2f0988e3..852bdaded0 100644 --- a/include/xbt/config.hpp +++ b/include/xbt/config.hpp @@ -45,12 +45,12 @@ inline std::string to_string(std::string&& value) // Get config -template XBT_PUBLIC T const& getConfig(const char* name); +template XBT_PUBLIC T const& get_config(const char* name); -extern template XBT_PUBLIC int const& getConfig(const char* name); -extern template XBT_PUBLIC double const& getConfig(const char* name); -extern template XBT_PUBLIC bool const& getConfig(const char* name); -extern template XBT_PUBLIC std::string const& getConfig(const char* name); +extern template XBT_PUBLIC int const& get_config(const char* name); +extern template XBT_PUBLIC double const& get_config(const char* name); +extern template XBT_PUBLIC bool const& get_config(const char* name); +extern template XBT_PUBLIC std::string const& get_config(const char* name); // Register: diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 2caa254d31..a172a49155 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -44,7 +44,7 @@ const char* false_values[] = { "no", "off", "false", "0" }; -static bool parseBool(const char* value) +static bool parse_bool(const char* value) { for (const char* const& true_value : true_values) if (std::strcmp(true_value, value) == 0) @@ -55,7 +55,7 @@ static bool parseBool(const char* value) throw std::range_error("not a boolean"); } -static double parseDouble(const char* value) +static double parse_double(const char* value) { char* end; errno = 0; @@ -70,7 +70,7 @@ static double parseDouble(const char* value) return res; } -static long int parseLong(const char* value) +static long int parse_long(const char* value) { char* end; errno = 0; @@ -98,7 +98,7 @@ public: static constexpr const char* type_name = "int"; static inline double parse(const char* value) { - return parseLong(value); + return parse_long(value); } }; template <> class ConfigType { @@ -106,7 +106,7 @@ public: static constexpr const char* type_name = "double"; static inline double parse(const char* value) { - return parseDouble(value); + return parse_double(value); } }; template <> class ConfigType { @@ -122,7 +122,7 @@ public: static constexpr const char* type_name = "boolean"; static inline bool parse(const char* value) { - return parseBool(value); + return parse_bool(value); } }; @@ -147,32 +147,29 @@ public: 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 ~ConfigurationElement() = default; - virtual std::string getStringValue() = 0; - virtual void setStringValue(const char* value) = 0; - virtual const char* getTypeName() = 0; + 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& getValue() const + template T const& get_value() const { - return dynamic_cast&>(*this).getValue(); + return dynamic_cast&>(*this).get_value(); } - template - void setValue(T value) + template void set_value(T value) { - dynamic_cast&>(*this).setValue(std::move(value)); + dynamic_cast&>(*this).set_value(std::move(value)); } - template - void setDefaultValue(T value) + template void set_default_value(T value) { - dynamic_cast&>(*this).setDefaultValue(std::move(value)); + dynamic_cast&>(*this).set_default_value(std::move(value)); } - void unsetDefault() { isdefault = false; } - bool isDefault() const { return isdefault; } + void unset_default() { isdefault = false; } + bool is_default() const { return isdefault; } - std::string const& getDescription() const { return desc; } - std::string const& getKey() const { return key; } + std::string const& get_description() const { return desc; } + std::string const& get_key() const { return key; } }; // **** TypedConfigurationElement **** @@ -196,55 +193,52 @@ public: {} ~TypedConfigurationElement() = default; - std::string getStringValue() override; - const char* getTypeName() override; - void setStringValue(const char* value) override; + 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(getKey().c_str()); + this->old_callback(get_key().c_str()); if (this->callback) this->callback(this->content); } - T const& getValue() const { return content; } + T const& get_value() const { return content; } - void setValue(T value) + void set_value(T value) { this->content = std::move(value); this->update(); - this->unsetDefault(); + this->unset_default(); } - void setDefaultValue(T value) + void set_default_value(T value) { - if (this->isDefault()) { + 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.", - getKey().c_str(), to_string(value).c_str()); + get_key().c_str(), to_string(value).c_str()); } } }; -template -std::string TypedConfigurationElement::getStringValue() // override +template std::string TypedConfigurationElement::get_string_value() // override { return to_string(content); } -template -void TypedConfigurationElement::setStringValue(const char* value) // override +template void TypedConfigurationElement::set_string_value(const char* value) // override { this->content = ConfigType::parse(value); - this->unsetDefault(); + this->unset_default(); this->update(); } -template -const char* TypedConfigurationElement::getTypeName() // override +template const char* TypedConfigurationElement::get_type_name() // override { return ConfigType::type_name; } @@ -272,14 +266,13 @@ public: ConfigurationElement& operator[](const char* name); void alias(const char* realname, const char* aliasname); - template - simgrid::config::TypedConfigurationElement* - registerOption(const char* name, A&&... a) + 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->getDescription().c_str(), - variable->getTypeName(), variable, this); + 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; @@ -287,11 +280,11 @@ public: // Debug: void dump(const char *name, const char *indent); - void showAliases(); + void show_aliases(); void help(); protected: - ConfigurationElement* getDictElement(const char* name); + ConfigurationElement* get_dict_element(const char* name); }; Config::Config() @@ -305,7 +298,7 @@ Config::~Config() delete elm.second; } -inline ConfigurationElement* Config::getDictElement(const char* name) +inline ConfigurationElement* Config::get_dict_element(const char* name) { auto opt = options.find(name); if (opt != options.end()) { @@ -315,7 +308,7 @@ inline ConfigurationElement* Config::getDictElement(const char* 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->getKey().c_str()); + 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); @@ -325,13 +318,13 @@ inline ConfigurationElement* Config::getDictElement(const char* name) inline ConfigurationElement& Config::operator[](const char* name) { - return *(getDictElement(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->getDictElement(realname); + 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}); } @@ -347,8 +340,8 @@ void Config::dump(const char *name, const char *indent) 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->getTypeName(), - elm.second->getStringValue().c_str()); + 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); @@ -356,10 +349,10 @@ void Config::dump(const char *name, const char *indent) } /** @brief Displays the declared aliases and their replacement */ -void Config::showAliases() +void Config::show_aliases() { for (auto const& elm : aliases) - printf(" %-40s %s\n", elm.first.c_str(), elm.second->getKey().c_str()); + printf(" %-40s %s\n", elm.first.c_str(), elm.second->get_key().c_str()); } /** @brief Displays the declared options and their description */ @@ -367,23 +360,23 @@ 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->getDescription().c_str()); - printf(" Type: %s; ", variable->getTypeName()); - printf("Current value: %s\n", variable->getStringValue().c_str()); + 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()); } } -// ***** getConfig ***** +// ***** get_config ***** -template XBT_PUBLIC T const& getConfig(const char* name) +template XBT_PUBLIC T const& get_config(const char* name) { - return (*simgrid_config)[name].getValue(); + return (*simgrid_config)[name].get_value(); } -template XBT_PUBLIC int const& getConfig(const char* name); -template XBT_PUBLIC double const& getConfig(const char* name); -template XBT_PUBLIC bool const& getConfig(const char* name); -template XBT_PUBLIC std::string const& getConfig(const char* name); +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 ***** @@ -400,8 +393,7 @@ XBT_PUBLIC void declareFlag(const char* name, const char* description, T value, { if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - simgrid_config->registerOption( - name, description, std::move(value), std::move(callback)); + simgrid_config->register_option(name, description, std::move(value), std::move(callback)); } template XBT_PUBLIC void declareFlag(const char* name, const char* description, int value, @@ -435,28 +427,28 @@ void xbt_cfg_register_double(const char *name, double default_value, { if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - simgrid_config->registerOption(name, desc, default_value, cb_set); + 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->registerOption(name, desc, default_value, cb_set); + 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->registerOption(name, desc, default_value ? default_value : "", cb_set); + 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->registerOption(name, desc, simgrid::config::parseBool(default_value), cb_set); + 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) @@ -466,8 +458,14 @@ void xbt_cfg_register_alias(const char *realname, const char *aliasname) simgrid_config->alias(realname, aliasname); } -void xbt_cfg_aliases() { simgrid_config->showAliases(); } -void xbt_cfg_help() { simgrid_config->help(); } +void xbt_cfg_aliases() +{ + simgrid_config->show_aliases(); +} +void xbt_cfg_help() +{ + simgrid_config->help(); +} /*----[ Setting ]---------------------------------------------------------*/ @@ -513,7 +511,7 @@ void xbt_cfg_set_parse(const char *options) 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()].setStringValue(val.c_str()); + (*simgrid_config)[name.c_str()].set_string_value(val.c_str()); } } @@ -525,7 +523,7 @@ void xbt_cfg_set_parse(const char *options) void xbt_cfg_set_as_string(const char *key, const char *value) { - (*simgrid_config)[key].setStringValue(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 @@ -535,7 +533,7 @@ void xbt_cfg_set_as_string(const char *key, const char *value) */ void xbt_cfg_setdefault_int(const char *key, int value) { - (*simgrid_config)[key].setDefaultValue(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 @@ -545,7 +543,7 @@ void xbt_cfg_setdefault_int(const char *key, int value) */ void xbt_cfg_setdefault_double(const char *key, double value) { - (*simgrid_config)[key].setDefaultValue(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 @@ -555,7 +553,7 @@ void xbt_cfg_setdefault_double(const char *key, double value) */ void xbt_cfg_setdefault_string(const char *key, const char *value) { - (*simgrid_config)[key].setDefaultValue(value ? 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 @@ -565,7 +563,7 @@ void xbt_cfg_setdefault_string(const char *key, const char *value) */ void xbt_cfg_setdefault_boolean(const char *key, const char *value) { - (*simgrid_config)[key].setDefaultValue(simgrid::config::parseBool(value)); + (*simgrid_config)[key].set_default_value(simgrid::config::parse_bool(value)); } /** @brief Set an integer value to \a name within \a cfg @@ -575,7 +573,7 @@ void xbt_cfg_setdefault_boolean(const char *key, const char *value) */ void xbt_cfg_set_int(const char *key, int value) { - (*simgrid_config)[key].setValue(value); + (*simgrid_config)[key].set_value(value); } /** @brief Set or add a double value to \a name within \a cfg @@ -585,7 +583,7 @@ void xbt_cfg_set_int(const char *key, int value) */ void xbt_cfg_set_double(const char *key, double value) { - (*simgrid_config)[key].setValue(value); + (*simgrid_config)[key].set_value(value); } /** @brief Set or add a string value to \a name within \a cfg @@ -596,7 +594,7 @@ void xbt_cfg_set_double(const char *key, double value) */ void xbt_cfg_set_string(const char* key, const char* value) { - (*simgrid_config)[key].setValue(value); + (*simgrid_config)[key].set_value(value); } /** @brief Set or add a boolean value to \a name within \a cfg @@ -606,14 +604,14 @@ void xbt_cfg_set_string(const char* key, const char* value) */ void xbt_cfg_set_boolean(const char *key, const char *value) { - (*simgrid_config)[key].setValue(simgrid::config::parseBool(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].isDefault() ? 1 : 0; + return (*simgrid_config)[key].is_default() ? 1 : 0; } /*----[ Getting ]---------------------------------------------------------*/ @@ -625,7 +623,7 @@ int xbt_cfg_is_default_value(const char *key) */ int xbt_cfg_get_int(const char *key) { - return (*simgrid_config)[key].getValue(); + return (*simgrid_config)[key].get_value(); } /** @brief Retrieve a double value of a variable (get a warning if not uniq) @@ -636,7 +634,7 @@ int xbt_cfg_get_int(const char *key) */ double xbt_cfg_get_double(const char *key) { - return (*simgrid_config)[key].getValue(); + return (*simgrid_config)[key].get_value(); } /** @brief Retrieve a string value of a variable (get a warning if not uniq) @@ -651,7 +649,7 @@ double xbt_cfg_get_double(const char *key) */ std::string xbt_cfg_get_string(const char* key) { - return (*simgrid_config)[key].getValue(); + return (*simgrid_config)[key].get_value(); } /** @brief Retrieve a boolean value of a variable (get a warning if not uniq) @@ -663,7 +661,7 @@ std::string xbt_cfg_get_string(const char* key) */ int xbt_cfg_get_boolean(const char *key) { - return (*simgrid_config)[key].getValue() ? 1 : 0; + return (*simgrid_config)[key].get_value() ? 1 : 0; } #ifdef SIMGRID_TEST -- 2.20.1