X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3c22fdea313dc38d430071450a15015f0746632b..9d0bdd2e04d23fbcc1db6e7e525cf79979c4643b:/src/xbt/config.cpp?ds=sidebyside diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 7c8619f8a3..7578fbdfa5 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -3,7 +3,11 @@ #include +#include +#include +#include #include +#include #include #include @@ -24,7 +28,6 @@ XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL; namespace { -// We could define some range/iterator for the num values. static inline void increment(e_xbt_cfgelm_type_t& type) { @@ -36,24 +39,30 @@ void increment(e_xbt_cfgelm_type_t& type) /* xbt_cfgelm_t: the typedef corresponding to a config variable. */ -typedef struct { +typedef struct s_xbt_cfgelm_t { /* Description */ - char *desc; + std::string desc; /* Allowed type of the variable */ e_xbt_cfgelm_type_t type; - unsigned isdefault:1; + bool isdefault = true; - /* Callbacks */ - xbt_cfg_cb_t cb_set; + /* Callback */ + xbt_cfg_cb_t cb_set = nullptr; - /* Advanced callbacks */ - xbt_cfg_cb_ext_t cb_set_ext; - void* cb_set_data; - xbt_cfg_cb_free_t cb_set_free; + /* Advanced callback (for xbt_cfgelm_string only) */ + std::function callback; /* actual content (could be an union or something) */ - xbt_dynar_t content; + xbt_dynar_t content = nullptr; + + ~s_xbt_cfgelm_t() + { + XBT_DEBUG("Frees cfgelm %p", this); + if (this->type != xbt_cfgelm_alias) + xbt_dynar_free(&(this->content)); + } + } s_xbt_cfgelm_t, *xbt_cfgelm_t; static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" }; @@ -67,7 +76,11 @@ const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = { }; /* Internal stuff used in cache to free a variable */ -static void xbt_cfgelm_free(void *data); +static void xbt_cfgelm_free(void *data) +{ + if (data) + delete (xbt_cfgelm_t) data; +} /* Retrieve the variable we'll modify */ static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type); @@ -158,24 +171,6 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg) xbt_dict_cursor_free(&cursor); } -/* - * free an config element - */ -void xbt_cfgelm_free(void *data) -{ - xbt_cfgelm_t c = (xbt_cfgelm_t) data; - - XBT_DEBUG("Frees cfgelm %p", c); - if (!c) - return; - if (c->cb_set_free) - c->cb_set_free(c->cb_set_data); - xbt_free(c->desc); - if (c->type != xbt_cfgelm_alias) - xbt_dynar_free(&(c->content)); - free(c); -} - /*----[ Registering stuff ]-----------------------------------------------*/ /** @brief Register an element within a config set * @@ -188,7 +183,7 @@ void xbt_cfgelm_free(void *data) static void xbt_cfg_register( xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set, - xbt_cfg_cb_ext_t cb_set_ext, void* cb_set_data, xbt_cfg_cb_free_t cb_set_free) + std::function callback = std::function()) { if (*cfg == NULL) *cfg = xbt_cfg_new(); @@ -199,17 +194,14 @@ static void xbt_cfg_register( xbt_cfgelm_t res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) * cfg, name); xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name); - res = xbt_new(s_xbt_cfgelm_t, 1); + res = new s_xbt_cfgelm_t(); XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)", name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg); - - res->desc = xbt_strdup(desc); res->type = type; + if (desc) + res->desc = desc; res->cb_set = cb_set; - res->cb_set_ext = cb_set_ext; - res->cb_set_data = cb_set_data; - res->cb_set_free = cb_set_free; - res->isdefault = 1; + res->callback = std::move(callback); switch (type) { case xbt_cfgelm_int: @@ -228,43 +220,27 @@ static void xbt_cfg_register( XBT_ERROR("%d is an invalid type code", (int)type); break; } + xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL); } - - void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc){ - xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set, NULL, NULL, NULL); + xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set); xbt_cfg_setdefault_double(name, default_value); } void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) { - xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set, NULL, NULL, NULL); + xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set); xbt_cfg_setdefault_int(name, default_value); } void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc){ - xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set, NULL, NULL, NULL); + xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set); xbt_cfg_setdefault_string(name, default_value); } void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc){ - xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set, NULL, NULL, NULL); + xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set); xbt_cfg_setdefault_boolean(name, default_value); } -/** Register a config with an extended callback - * - * @param name Name of the flag - * @param desc Description of the flag - * @param type Type of the flag - * @param cb Extended callback - * @param data Data associated with the callback - * @param data_free Function used to free the callback data (or NULL) - */ -void xbt_cfg_register_ext(const char *name, const char *desc, e_xbt_cfgelm_type_t type, - xbt_cfg_cb_ext_t cb, void* data, xbt_cfg_cb_free_t data_free) -{ - xbt_cfg_register(&simgrid_config, name, desc, type, NULL, cb, data, data_free); -} - void xbt_cfg_register_alias(const char *newname, const char *oldname) { if (simgrid_config == NULL) @@ -276,12 +252,11 @@ void xbt_cfg_register_alias(const char *newname, const char *oldname) res = (xbt_cfgelm_t) xbt_dict_get_or_null((xbt_dict_t) simgrid_config, newname); xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname); - res = xbt_new0(s_xbt_cfgelm_t, 1); + res = new s_xbt_cfgelm_t(); XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname); res->desc = bprintf("Deprecated alias for %s",newname); res->type = xbt_cfgelm_alias; - res->isdefault = 1; res->content = (xbt_dynar_t)newname; xbt_dict_set((xbt_dict_t) simgrid_config, oldname, res, NULL); @@ -315,7 +290,7 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry) xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry); - xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, NULL, NULL, NULL); + xbt_cfg_register(cfg, entrycpy, NULL, type, NULL); free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */ } @@ -337,7 +312,7 @@ void xbt_cfg_aliases(void) variable = (xbt_cfgelm_t) xbt_dict_get((xbt_dict_t )simgrid_config, name); if (variable->type == xbt_cfgelm_alias) - printf(" %s: %s\n", name, variable->desc); + printf(" %s: %s\n", name, variable->desc.c_str()); } } @@ -360,7 +335,7 @@ void xbt_cfg_help(void) if (variable->type == xbt_cfgelm_alias) continue; - printf(" %s: %s\n", name, variable->desc); + printf(" %s: %s\n", name, variable->desc.c_str()); printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]); size = xbt_dynar_length(variable->content); printf("Current value: "); @@ -648,7 +623,7 @@ void xbt_cfg_setdefault_int(const char *name, int val) if (variable->isdefault){ xbt_cfg_set_int(name, val); - variable->isdefault = 1; + variable->isdefault = true; } else XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val); } @@ -664,7 +639,7 @@ void xbt_cfg_setdefault_double(const char *name, double val) if (variable->isdefault) { xbt_cfg_set_double(name, val); - variable->isdefault = 1; + variable->isdefault = true; } else XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val); } @@ -680,7 +655,7 @@ void xbt_cfg_setdefault_string(const char *name, const char *val) if (variable->isdefault){ xbt_cfg_set_string(name, val); - variable->isdefault = 1; + variable->isdefault = true; } else XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val); } @@ -696,7 +671,7 @@ void xbt_cfg_setdefault_boolean(const char *name, const char *val) if (variable->isdefault){ xbt_cfg_set_boolean(name, val); - variable->isdefault = 1; + variable->isdefault = true; } else XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val); @@ -715,9 +690,7 @@ void xbt_cfg_set_int(const char *name, int val) if (variable->cb_set) variable->cb_set(name); - if (variable->cb_set_ext) - variable->cb_set_ext(name, variable->cb_set_data); - variable->isdefault = 0; + variable->isdefault = false; } /** @brief Set or add a double value to \a name within \a cfg @@ -733,9 +706,7 @@ void xbt_cfg_set_double(const char *name, double val) if (variable->cb_set) variable->cb_set(name); - if (variable->cb_set_ext) - variable->cb_set_ext(name, variable->cb_set_data); - variable->isdefault = 0; + variable->isdefault = false; } /** @brief Set or add a string value to \a name within \a cfg @@ -759,9 +730,23 @@ void xbt_cfg_set_string(const char *name, const char *val) if (variable->cb_set) variable->cb_set(name); - if (variable->cb_set_ext) - variable->cb_set_ext(name, variable->cb_set_data); - variable->isdefault = 0; + + if (variable->callback) { + try { + variable->callback(val); + } + catch(std::range_error& e) { + xbt_die("Invalid flag %s=%s: %s", val, name, e.what()); + } + catch(std::exception& e) { + xbt_die("Error for flag %s=%s: %s", val, name, e.what()); + } + catch(...) { + xbt_die("Error for flag %s=%s", val, name); + } + } + + variable->isdefault = false; } /** @brief Set or add a boolean value to \a name within \a cfg @@ -789,9 +774,7 @@ void xbt_cfg_set_boolean(const char *name, const char *val) if (variable->cb_set) variable->cb_set(name); - if (variable->cb_set_ext) - variable->cb_set_ext(name, variable->cb_set_data); - variable->isdefault = 0; + variable->isdefault = false; } @@ -890,33 +873,69 @@ int xbt_cfg_get_boolean(const char *name) namespace simgrid { namespace config { -static void callCallback(const char* name, void* data) -{ - (*(std::function*) data)(name); -} - -static void freeCallback(void* data) +bool parseBool(const char* value) { - delete (std::function*) data; + for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) { + if (std::strcmp(value, xbt_cfgelm_boolean_values[i].true_val) == 0) + return true; + if (std::strcmp(value, xbt_cfgelm_boolean_values[i].false_val) == 0) + return false; + } + throw std::range_error("not a boolean"); +} + +double parseDouble(const char* value) +{ + 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; +} + +long int parseLong(const char* value) +{ + 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; } -void registerConfig(const char* name, const char* description, - e_xbt_cfgelm_type_t type, - std::function callback) +void declareFlag(const char* name, const char* description, + std::function callback) { - std::function* code - = new std::function(std::move(callback)); - xbt_cfg_register_ext(name, description, type, - callCallback, code, freeCallback); + xbt_cfg_register(&simgrid_config, name, description, xbt_cfgelm_string, NULL, + std::move(callback)); } } } #ifdef SIMGRID_TEST + +#include + #include "xbt.h" #include "xbt/ex.h" +#include + XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg); XBT_TEST_SUITE("config", "Configuration support"); @@ -971,4 +990,27 @@ XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests") } xbt_cfg_free(&simgrid_config); } + +XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags") +{ + 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(!bool_flag2, "Check bool2 flag"); + + xbt_cfg_free(&simgrid_config); +} + #endif /* SIMGRID_TEST */