1 /* Copyright (c) 2004-2014,2016. The SimGrid Team.
2 * All rights reserved. */
12 #include <type_traits>
14 #include <xbt/config.h>
15 #include <xbt/config.hpp>
17 #include "xbt/sysdep.h"
20 #include "xbt/dynar.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
27 XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL;
29 /** @brief possible content of each configuration cell */
31 xbt_cfgelm_int = 0, /**< int */
32 xbt_cfgelm_double, /**< double */
33 xbt_cfgelm_string, /**< char* */
34 xbt_cfgelm_boolean, /**< int */
36 xbt_cfgelm_any, /* not shown to users to prevent errors */
39 } e_xbt_cfgelm_type_t;
45 void increment(e_xbt_cfgelm_type_t& type)
47 typedef std::underlying_type<e_xbt_cfgelm_type_t>::type underlying_type;
48 type = (e_xbt_cfgelm_type_t) ((underlying_type) type + 1);
53 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = {
54 "int", "double", "string", "boolean", "any"
57 /** Boolean possible values **/
58 struct xbt_boolean_couple {
60 const char *false_val;
63 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
73 struct ConfigurationElement ;
78 /* Retrieve the variable we'll modify */
79 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
84 // A configuration variable:
85 struct ConfigurationElement {
89 /* Allowed type of the variable */
90 e_xbt_cfgelm_type_t type;
91 bool isdefault = true;
94 xbt_cfg_cb_t cb_set = nullptr;
96 /* Advanced callback (for xbt_cfgelm_string only) */
97 std::function<void(const char* value)> callback;
101 double double_content;
102 char* string_content;
103 bool boolean_content;
106 ~ConfigurationElement()
108 XBT_DEBUG("Frees cfgelm %p", this);
111 case xbt_cfgelm_double:
112 case xbt_cfgelm_boolean:
114 case xbt_cfgelm_string:
115 free(string_content);
118 xbt_die("Unexpected config type");
125 // name -> ConfigElement:
127 // alias -> xbt_dict_elm_t from options:
135 Config(Config const&) = delete;
136 Config& operator=(Config const&) = delete;
138 xbt_dictelm_t getElement(const char* name);
139 ConfigurationElement* operator[](const char* name);
140 void alias(const char* realname, const char* aliasname);
142 simgrid::config::ConfigurationElement* registerOption(
143 const char *name, const char *desc, e_xbt_cfgelm_type_t type,
144 xbt_cfg_cb_t cb_set);
147 void dump(const char *name, const char *indent);
152 /* Internal stuff used in cache to free a variable */
153 static void xbt_cfgelm_free(void *data)
156 delete (simgrid::config::ConfigurationElement*) data;
160 options(xbt_dict_new_homogeneous(xbt_cfgelm_free)),
161 aliases(xbt_dict_new_homogeneous(NULL))
166 XBT_DEBUG("Frees cfg set %p", this);
167 xbt_dict_free(&this->options);
168 xbt_dict_free(&this->aliases);
172 xbt_dictelm_t Config::getElement(const char* name)
174 // We are interested in the options dictelm:
175 xbt_dictelm_t res = xbt_dict_get_elm_or_null(options, name);
178 // The aliases dict stores pointers to the options dictelm:
179 res = (xbt_dictelm_t) xbt_dict_get_or_null(aliases, name);
181 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, res->key);
186 ConfigurationElement* Config::operator[](const char* name)
188 xbt_dictelm_t elm = getElement(name);
190 return (ConfigurationElement*) elm->content;
195 void Config::alias(const char* realname, const char* aliasname)
197 xbt_assert(this->getElement(aliasname) == nullptr, "Alias '%s' already.", aliasname);
198 xbt_dictelm_t element = this->getElement(realname);
199 xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname);
200 xbt_dict_set(this->aliases, aliasname, element, NULL);
203 /** @brief Dump a config set for debuging purpose
205 * @param name The name to give to this config set
206 * @param indent what to write at the beginning of each line (right number of spaces)
208 void Config::dump(const char *name, const char *indent)
210 xbt_dict_t dict = this->options;
211 xbt_dict_cursor_t cursor = NULL;
212 simgrid::config::ConfigurationElement* variable = NULL;
216 printf("%s>> Dumping of the config set '%s':\n", indent, name);
218 xbt_dict_foreach(dict, cursor, key, variable) {
219 const char* type_name = xbt_cfgelm_type_name[variable->type];
220 switch (variable->type) {
222 printf("%s %s: ()%s) %i", indent, key, type_name, variable->int_content);
224 case xbt_cfgelm_double:
225 printf("%s %s: ()%s) %f", indent, key, type_name, variable->double_content);
227 case xbt_cfgelm_string:
228 printf("%s %s: ()%s) %s", indent, key, type_name, variable->string_content);
230 case xbt_cfgelm_boolean:
231 printf("%s %s: ()%s) %s", indent, key, type_name,
232 variable->boolean_content ? "true" : "false");
235 printf("%s Invalid type!!\n", indent);
241 printf("%s<< End of the config set '%s'\n", indent, name);
244 xbt_dict_cursor_free(&cursor);
247 simgrid::config::ConfigurationElement* Config::registerOption(
248 const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
250 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
251 "type of %s not valid (%d should be between %d and %d)",
252 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
254 simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(this->options, name);
255 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
257 res = new simgrid::config::ConfigurationElement();
258 XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
259 name, desc, xbt_cfgelm_type_name[type], (int)type, res, this);
261 res->cb_set = cb_set;
265 // Set a default default value:
268 res->int_content = 0;
270 case xbt_cfgelm_double:
271 res->double_content = 0.0;
273 case xbt_cfgelm_string:
274 res->string_content = nullptr;
276 case xbt_cfgelm_boolean:
277 res->boolean_content = false;
280 XBT_ERROR("%d is an invalid type code", (int)type);
284 xbt_dict_set(this->options, name, res, NULL);
288 /** @brief Displays the declared aliases and their description */
289 void Config::showAliases()
291 xbt_dict_cursor_t dict_cursor;
292 unsigned int dynar_cursor;
293 xbt_dictelm_t dictel;
295 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
297 xbt_dict_foreach(this->aliases, dict_cursor, name, dictel)
298 xbt_dynar_push(names, &name);
299 xbt_dynar_sort_strings(names);
301 xbt_dynar_foreach(names, dynar_cursor, name) {
302 simgrid::config::ConfigurationElement* variable = (*this)[name];
304 printf(" %s: %s\n", name, variable->desc.c_str());
308 /** @brief Displays the declared options and their description */
311 xbt_dict_cursor_t dict_cursor;
312 unsigned int dynar_cursor;
313 simgrid::config::ConfigurationElement* variable;
315 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
317 xbt_dict_foreach(this->options, dict_cursor, name, variable)
318 xbt_dynar_push(names, &name);
319 xbt_dynar_sort_strings(names);
321 xbt_dynar_foreach(names, dynar_cursor, name) {
322 variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(this->options, name);
324 printf(" %s: %s\n", name, variable->desc.c_str());
325 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
326 printf("Current value: ");
328 switch (variable->type) {
330 printf("%d\n", variable->int_content);
332 case xbt_cfgelm_double:
333 printf("%f\n", variable->double_content);
335 case xbt_cfgelm_string:
336 printf("'%s'\n", variable->string_content);
338 case xbt_cfgelm_boolean:
339 printf("'%s'\n", variable->boolean_content ? "true" : "false");
342 printf("Invalid type!!\n");
347 xbt_dynar_free(&names);
355 /** @brief Constructor */
356 xbt_cfg_t xbt_cfg_new(void) { return new simgrid::config::Config(); }
357 /** @brief Destructor */
358 void xbt_cfg_free(xbt_cfg_t * cfg) { delete *cfg; }
360 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
362 cfg->dump(name, indent);
365 /*----[ Registering stuff ]-----------------------------------------------*/
366 /** @brief Register an element within a config set
368 * @param cfg the config set
369 * @param name the name of the config element
370 * @param desc a description for this item (used by xbt_cfg_help())
371 * @param type the type of the config element
372 * @param cb_set callback function called when a value is set
374 static simgrid::config::ConfigurationElement* xbt_cfg_register(
375 xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
378 *cfg = xbt_cfg_new();
379 return (*cfg)->registerOption(name, desc, type, cb_set);
382 void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc)
384 xbt_cfg_register(&simgrid_config, name, desc, xbt_cfgelm_double, cb_set);
385 xbt_cfg_setdefault_double(name, default_value);
387 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc)
389 xbt_cfg_register(&simgrid_config, name, desc, xbt_cfgelm_int, cb_set);
390 xbt_cfg_setdefault_int(name, default_value);
392 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc)
394 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
395 xbt_cfg_setdefault_string(name, default_value);
397 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc)
399 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
400 xbt_cfg_setdefault_boolean(name, default_value);
403 void xbt_cfg_register_alias(const char *realname, const char *aliasname)
405 if (simgrid_config == NULL)
406 simgrid_config = xbt_cfg_new();
407 simgrid_config->alias(realname, aliasname);
411 * @brief Parse a string and register the stuff described.
413 * @param cfg the config set
414 * @param entry a string describing the element to register
416 * The string may consist in several variable descriptions separated by a space.
417 * Each of them must use the following syntax: \<name\>:\<type\>
418 * with type being one of 'string','int','bool' or 'double'.
420 * Note that this does not allow to set the description, so you should prefer the other interface
422 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
424 char *entrycpy = xbt_strdup(entry);
427 e_xbt_cfgelm_type_t type;
428 XBT_DEBUG("Register string '%s'", entry);
430 tok = strchr(entrycpy, ':');
431 xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
434 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); increment(type));
435 xbt_assert(type < xbt_cfgelm_type_count,
436 "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
438 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
440 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
443 void xbt_cfg_aliases(void) { simgrid_config->showAliases(); }
444 void xbt_cfg_help(void) { simgrid_config->help(); }
446 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
448 simgrid::config::ConfigurationElement* res = (*cfg)[name];
453 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
456 xbt_assert(type == xbt_cfgelm_any || res->type == type,
457 "You tried to access to the config element %s as an %s, but its type is %s.",
458 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
462 /*----[ Setting ]---------------------------------------------------------*/
464 /** @brief Add values parsed from a string into a config set
466 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
467 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
468 * already registered variable, and [value] conforms to the data type under which this variable was registered.
470 * @todo This is a crude manual parser, it should be a proper lexer.
472 void xbt_cfg_set_parse(const char *options)
474 if (!options || !strlen(options)) { /* nothing to do */
477 char *optionlist_cpy = xbt_strdup(options);
479 XBT_DEBUG("List to parse and set:'%s'", options);
480 char *option = optionlist_cpy;
481 while (1) { /* breaks in the code */
485 int len = strlen(name);
486 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
489 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
490 XBT_DEBUG("Take %c.", *option);
493 if (option - name == len) {
494 XBT_DEBUG("Boundary=EOL");
495 option = NULL; /* don't do next iteration */
497 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
498 /* Pass the following blank chars */
500 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
501 /* fprintf(stderr,"Ignore a blank char.\n"); */
504 if (option - name == len - 1)
505 option = NULL; /* don't do next iteration */
507 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
509 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
514 char *val = strchr(name, ':');
515 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
516 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
519 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
520 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
523 xbt_cfg_set_as_string(name,val);
525 free(optionlist_cpy);
529 free(optionlist_cpy);
532 /** @brief Set the value of a variable, using the string representation of that value
534 * @param key name of the variable to modify
535 * @param value string representation of the value to set
537 * @return the first char after the parsed value in val
540 void *xbt_cfg_set_as_string(const char *key, const char *value)
542 simgrid::config::ConfigurationElement* variable = (*simgrid_config)[key];
543 if (variable == nullptr)
544 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
550 switch (variable->type) {
551 case xbt_cfgelm_string:
552 xbt_cfg_set_string(key, value); /* throws */
555 i = strtol(value, &ret, 0);
557 xbt_die("Value of option %s not valid. Should be an integer", key);
559 xbt_cfg_set_int(key, i); /* throws */
561 case xbt_cfgelm_double:
562 d = strtod(value, &ret);
564 xbt_die("Value of option %s not valid. Should be a double", key);
566 xbt_cfg_set_double(key, d); /* throws */
568 case xbt_cfgelm_boolean:
569 xbt_cfg_set_boolean(key, value); /* throws */
570 ret = (char *)value + strlen(value);
573 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
579 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
581 * This is useful to change the default value of a variable while allowing
582 * users to override it with command line arguments
584 void xbt_cfg_setdefault_int(const char *name, int val)
586 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
588 if (variable->isdefault){
589 xbt_cfg_set_int(name, val);
590 variable->isdefault = true;
592 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
595 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
597 * This is useful to change the default value of a variable while allowing
598 * users to override it with command line arguments
600 void xbt_cfg_setdefault_double(const char *name, double val)
602 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
604 if (variable->isdefault) {
605 xbt_cfg_set_double(name, val);
606 variable->isdefault = true;
608 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
611 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
613 * This is useful to change the default value of a variable while allowing
614 * users to override it with command line arguments
616 void xbt_cfg_setdefault_string(const char *name, const char *val)
618 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
620 if (variable->isdefault){
621 xbt_cfg_set_string(name, val);
622 variable->isdefault = true;
624 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
627 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
629 * This is useful to change the default value of a variable while allowing
630 * users to override it with command line arguments
632 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
634 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
636 if (variable->isdefault){
637 xbt_cfg_set_boolean(name, val);
638 variable->isdefault = true;
641 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
644 /** @brief Set an integer value to \a name within \a cfg
646 * @param name the name of the variable
647 * @param val the value of the variable
649 void xbt_cfg_set_int(const char *name, int val)
651 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
652 variable->int_content = val;
653 if (variable->cb_set)
654 variable->cb_set(name);
655 variable->isdefault = false;
658 /** @brief Set or add a double value to \a name within \a cfg
660 * @param name the name of the variable
661 * @param val the double to set
663 void xbt_cfg_set_double(const char *name, double val)
665 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
666 variable->double_content = val;
667 if (variable->cb_set)
668 variable->cb_set(name);
669 variable->isdefault = false;
672 /** @brief Set or add a string value to \a name within \a cfg
674 * @param cfg the config set
675 * @param name the name of the variable
676 * @param val the value to be added
679 void xbt_cfg_set_string(const char *name, const char *val)
681 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
682 free(variable->string_content);
683 variable->string_content = xbt_strdup(val);
684 if (variable->cb_set)
685 variable->cb_set(name);
686 variable->isdefault = false;
688 if (variable->callback) {
690 variable->callback(val);
692 catch(std::range_error& e) {
693 xbt_die("Invalid flag %s=%s: %s", val, name, e.what());
695 catch(std::exception& e) {
696 xbt_die("Error for flag %s=%s: %s", val, name, e.what());
699 xbt_die("Error for flag %s=%s", val, name);
704 /** @brief Set or add a boolean value to \a name within \a cfg
706 * @param name the name of the variable
707 * @param val the value of the variable
709 void xbt_cfg_set_boolean(const char *name, const char *val)
712 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
714 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
715 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
719 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
724 xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
725 variable->boolean_content = bval;
726 if (variable->cb_set)
727 variable->cb_set(name);
728 variable->isdefault = false;
732 /* Say if the value is the default value */
733 int xbt_cfg_is_default_value(const char *name)
735 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
736 return variable->isdefault;
739 /*----[ Getting ]---------------------------------------------------------*/
740 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
742 * @param name the name of the variable
744 * Returns the first value from the config set under the given name.
746 int xbt_cfg_get_int(const char *name)
748 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
749 return variable->int_content;
752 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
754 * @param cfg the config set
755 * @param name the name of the variable
757 * Returns the first value from the config set under the given name.
759 double xbt_cfg_get_double(const char *name)
761 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
762 return variable->double_content;
765 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
767 * @param cfg the config set
768 * @param name the name of the variable
770 * Returns the first value from the config set under the given name.
771 * If there is more than one value, it will issue a warning.
772 * Returns NULL if there is no value.
774 * \warning the returned value is the actual content of the config set
776 char *xbt_cfg_get_string(const char *name)
778 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
779 return variable->string_content;
782 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
784 * @param cfg the config set
785 * @param name the name of the variable
787 * Returns the first value from the config set under the given name.
788 * If there is more than one value, it will issue a warning.
790 int xbt_cfg_get_boolean(const char *name)
792 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
793 return variable->boolean_content;
799 bool parseBool(const char* value)
801 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
802 if (std::strcmp(value, xbt_cfgelm_boolean_values[i].true_val) == 0)
804 if (std::strcmp(value, xbt_cfgelm_boolean_values[i].false_val) == 0)
807 throw std::range_error("not a boolean");
810 double parseDouble(const char* value)
814 double res = std::strtod(value, &end);
816 throw std::range_error("out of range");
818 xbt_die("Unexpected errno");
819 if (end == value || *end != '\0')
820 throw std::range_error("invalid double");
825 long int parseLong(const char* value)
829 long int res = std::strtol(value, &end, 0);
831 if (res == LONG_MIN && errno == ERANGE)
832 throw std::range_error("underflow");
833 else if (res == LONG_MAX && errno == ERANGE)
834 throw std::range_error("overflow");
835 xbt_die("Unexpected errno");
837 if (end == value || *end != '\0')
838 throw std::range_error("invalid integer");
843 void declareFlag(const char* name, const char* description,
844 std::function<void(const char* value)> callback)
846 simgrid::config::ConfigurationElement* e = xbt_cfg_register(
847 &simgrid_config, name, description, xbt_cfgelm_string, NULL);
848 e->callback = std::move(callback);
861 #include <xbt/config.hpp>
863 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
865 XBT_TEST_SUITE("config", "Configuration support");
867 static xbt_cfg_t make_set()
869 xbt_cfg_t set = NULL;
871 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
872 xbt_cfg_register_str(&set, "speed:int");
873 xbt_cfg_register_str(&set, "peername:string");
874 xbt_cfg_register_str(&set, "user:string");
877 } /* end_of_make_set */
879 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
881 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
883 simgrid_config = make_set();
884 xbt_test_add("Alloc and free a config set");
885 xbt_cfg_set_parse("peername:veloce user:bidule");
886 xbt_cfg_free(&simgrid_config);
889 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
891 simgrid_config = make_set();
892 xbt_test_add("Get a single value");
894 /* get_single_value */
897 xbt_cfg_set_parse("peername:toto:42 speed:42");
898 ival = xbt_cfg_get_int("speed");
900 xbt_test_fail("Speed value = %d, I expected 42", ival);
903 xbt_test_add("Access to a non-existant entry");
908 xbt_cfg_set_parse("color:blue");
910 if (e.category != not_found_error)
911 xbt_test_exception(e);
915 xbt_cfg_free(&simgrid_config);
918 XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
920 simgrid_config = make_set();
921 xbt_test_add("C++ declaration of flags");
923 simgrid::config::Flag<int> int_flag("int", "", 0);
924 simgrid::config::Flag<std::string> string_flag("string", "", "foo");
925 simgrid::config::Flag<double> double_flag("double", "", 0.32);
926 simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
927 simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
929 xbt_test_add("Parse values");
930 xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
931 xbt_test_assert(int_flag == 42, "Check int flag");
932 xbt_test_assert(string_flag == "bar", "Check string flag");
933 xbt_test_assert(double_flag == 8.0, "Check double flag");
934 xbt_test_assert(bool_flag1, "Check bool1 flag");
935 xbt_test_assert(!bool_flag2, "Check bool2 flag");
937 xbt_cfg_free(&simgrid_config);
940 #endif /* SIMGRID_TEST */