X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/403af5e6247ce6452b721f418a5b41e4548efac4..88fad0aaff9eb463f048bfdfe4ad6218aba44ddb:/src/xbt/config.cpp diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 0d2265d4f2..16c47dc636 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2004-2017. 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. */ @@ -19,20 +19,18 @@ #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 "xbt/log.h" -#include "xbt/dynar.h" +#include +#include +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support"); -XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = nullptr; -extern "C" { - XBT_PUBLIC(void) sg_config_finalize(); -} +XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr; namespace simgrid { namespace config { @@ -147,8 +145,7 @@ 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) : 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) {} @@ -261,7 +258,7 @@ class Config { private: // name -> ConfigElement: std::map options; - // alias -> xbt_dict_elm_t from options: + // alias -> ConfigElement from options: std::map aliases; bool warn_for_aliases = true; @@ -380,16 +377,15 @@ void Config::help() // ***** getConfig ***** -template -XBT_PUBLIC(T const&) getConfig(const char* name) +template XBT_PUBLIC T const& getConfig(const char* name) { return (*simgrid_config)[name].getValue(); } -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& 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); // ***** alias ***** @@ -400,33 +396,33 @@ void alias(const char* realname, const char* aliasname) // ***** declareFlag ***** -template -XBT_PUBLIC(void) declareFlag(const char* name, const char* description, - T value, std::function callback) +template +XBT_PUBLIC void declareFlag(const char* name, const char* description, T value, std::function callback) { - if (simgrid_config == nullptr) { + if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - atexit(sg_config_finalize); - } simgrid_config->registerOption( name, description, std::move(value), std::move(callback)); } -template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, int value, std::function callback); -template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, double value, std::function callback); -template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, bool value, std::function callback); -template XBT_PUBLIC(void) declareFlag(const char* name, - const char* description, std::string value, std::function callback); - +template XBT_PUBLIC void declareFlag(const char* name, const char* description, int value, + std::function callback); +template XBT_PUBLIC void declareFlag(const char* name, const char* description, double value, + std::function callback); +template XBT_PUBLIC void declareFlag(const char* name, const char* description, bool value, + std::function callback); +template XBT_PUBLIC void declareFlag(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(); } +xbt_cfg_t xbt_cfg_new() +{ + atexit(&sg_config_finalize); + 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) @@ -446,37 +442,29 @@ void xbt_cfg_register_double(const char *name, double default_value, void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) { - if (simgrid_config == nullptr) { + if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - atexit(&sg_config_finalize); - } simgrid_config->registerOption(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) { + if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - atexit(sg_config_finalize); - } simgrid_config->registerOption(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) { + if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - atexit(sg_config_finalize); - } simgrid_config->registerOption(name, desc, simgrid::config::parseBool(default_value), cb_set); } void xbt_cfg_register_alias(const char *realname, const char *aliasname) { - if (simgrid_config == nullptr) { + if (simgrid_config == nullptr) simgrid_config = xbt_cfg_new(); - atexit(sg_config_finalize); - } simgrid_config->alias(realname, aliasname); } @@ -657,13 +645,13 @@ void xbt_cfg_set_double(const char *key, double value) * @param value the value to be added * */ -void xbt_cfg_set_string(const char* key, std::string value) +void xbt_cfg_set_string(const char* key, const char* value) { try { (*simgrid_config)[key].setValue(value); return; } - TRANSLATE_EXCEPTIONS("Could not set variable %s to string %s", key, value.c_str()); + TRANSLATE_EXCEPTIONS("Could not set variable %s to string %s", key, value); } /** @brief Set or add a boolean value to \a name within \a cfg @@ -766,7 +754,7 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg); XBT_TEST_SUITE("config", "Configuration support"); -XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config; +XBT_PUBLIC_DATA xbt_cfg_t simgrid_config; static void make_set() {