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;
32 void increment(e_xbt_cfgelm_type_t& type)
34 typedef std::underlying_type<e_xbt_cfgelm_type_t>::type underlying_type;
35 type = (e_xbt_cfgelm_type_t) ((underlying_type) type + 1);
43 // A configuration variable:
44 struct ConfigurationElement {
48 /* Allowed type of the variable */
49 e_xbt_cfgelm_type_t type;
50 bool isdefault = true;
53 xbt_cfg_cb_t cb_set = nullptr;
55 /* Advanced callback (for xbt_cfgelm_string only) */
56 std::function<void(const char* value)> callback;
58 /* actual content (could be an union or something) */
59 xbt_dynar_t content = nullptr;
61 ~ConfigurationElement()
63 XBT_DEBUG("Frees cfgelm %p", this);
64 if (this->type != xbt_cfgelm_alias)
65 xbt_dynar_free(&(this->content));
77 Config(Config const&) = delete;
78 Config& operator=(Config const&) = delete;
81 /* Internal stuff used in cache to free a variable */
82 static void xbt_cfgelm_free(void *data)
85 delete (simgrid::config::ConfigurationElement*) data;
89 options(xbt_dict_new_homogeneous(xbt_cfgelm_free))
94 XBT_DEBUG("Frees cfg set %p", this);
95 xbt_dict_free(&this->options);
101 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
103 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
111 /* Retrieve the variable we'll modify */
112 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
114 /*----[ Memory management ]-----------------------------------------------*/
115 /** @brief Constructor
117 * Initialise a config set
119 xbt_cfg_t xbt_cfg_new(void)
121 return new simgrid::config::Config();
124 /** @brief Destructor */
125 void xbt_cfg_free(xbt_cfg_t * cfg)
130 /** @brief Dump a config set for debuging purpose
132 * @param name The name to give to this config set
133 * @param indent what to write at the beginning of each line (right number of spaces)
134 * @param cfg the config set
136 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
138 xbt_dict_t dict = cfg->options;
139 xbt_dict_cursor_t cursor = NULL;
140 simgrid::config::ConfigurationElement* variable = NULL;
149 printf("%s>> Dumping of the config set '%s':\n", indent, name);
151 xbt_dict_foreach(dict, cursor, key, variable) {
152 printf("%s %s:", indent, key);
154 size = xbt_dynar_length(variable->content);
155 printf ("%s. Actual size=%d. postset=%p\n",
156 xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
158 switch (variable->type) {
160 for (i = 0; i < size; i++) {
161 ival = xbt_dynar_get_as(variable->content, i, int);
162 printf("%s %d\n", indent, ival);
165 case xbt_cfgelm_double:
166 for (i = 0; i < size; i++) {
167 dval = xbt_dynar_get_as(variable->content, i, double);
168 printf("%s %f\n", indent, dval);
171 case xbt_cfgelm_string:
172 for (i = 0; i < size; i++) {
173 sval = xbt_dynar_get_as(variable->content, i, char *);
174 printf("%s %s\n", indent, sval);
177 case xbt_cfgelm_boolean:
178 for (i = 0; i < size; i++) {
179 ival = xbt_dynar_get_as(variable->content, i, int);
180 printf("%s %d\n", indent, ival);
183 case xbt_cfgelm_alias:
187 printf("%s Invalid type!!\n", indent);
193 printf("%s<< End of the config set '%s'\n", indent, name);
196 xbt_dict_cursor_free(&cursor);
199 /*----[ Registering stuff ]-----------------------------------------------*/
200 /** @brief Register an element within a config set
202 * @param cfg the config set
203 * @param name the name of the config element
204 * @param desc a description for this item (used by xbt_cfg_help())
205 * @param type the type of the config element
206 * @param cb_set callback function called when a value is set
208 static void xbt_cfg_register(
209 xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type,
211 std::function<void(const char* value)> callback = std::function<void(const char* value)>())
214 *cfg = xbt_cfg_new();
215 xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
216 "type of %s not valid (%d should be between %d and %d)",
217 name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
219 simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null((*cfg)->options, name);
220 xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
222 res = new simgrid::config::ConfigurationElement();
223 XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
224 name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
228 res->cb_set = cb_set;
229 res->callback = std::move(callback);
233 res->content = xbt_dynar_new(sizeof(int), NULL);
235 case xbt_cfgelm_double:
236 res->content = xbt_dynar_new(sizeof(double), NULL);
238 case xbt_cfgelm_string:
239 res->content = xbt_dynar_new(sizeof(char *), xbt_free_ref);
241 case xbt_cfgelm_boolean:
242 res->content = xbt_dynar_new(sizeof(int), NULL);
245 XBT_ERROR("%d is an invalid type code", (int)type);
249 xbt_dict_set((*cfg)->options, name, res, NULL);
252 void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc){
253 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set);
254 xbt_cfg_setdefault_double(name, default_value);
256 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) {
257 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set);
258 xbt_cfg_setdefault_int(name, default_value);
260 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc){
261 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
262 xbt_cfg_setdefault_string(name, default_value);
264 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc){
265 xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
266 xbt_cfg_setdefault_boolean(name, default_value);
269 void xbt_cfg_register_alias(const char *newname, const char *oldname)
271 if (simgrid_config == NULL)
272 simgrid_config = xbt_cfg_new();
274 simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(simgrid_config->options, oldname);
275 xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
277 res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(simgrid_config->options, newname);
278 xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
280 res = new simgrid::config::ConfigurationElement();
281 XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
283 res->desc = std::string("Deprecated alias for ")+std::string(newname);
284 res->type = xbt_cfgelm_alias;
285 res->content = (xbt_dynar_t)newname;
287 xbt_dict_set(simgrid_config->options, oldname, res, NULL);
291 * @brief Parse a string and register the stuff described.
293 * @param cfg the config set
294 * @param entry a string describing the element to register
296 * The string may consist in several variable descriptions separated by a space.
297 * Each of them must use the following syntax: \<name\>:\<type\>
298 * with type being one of 'string','int','bool' or 'double'.
300 * Note that this does not allow to set the description, so you should prefer the other interface
302 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
304 char *entrycpy = xbt_strdup(entry);
307 e_xbt_cfgelm_type_t type;
308 XBT_DEBUG("Register string '%s'", entry);
310 tok = strchr(entrycpy, ':');
311 xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
314 for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); increment(type));
315 xbt_assert(type < xbt_cfgelm_type_count,
316 "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
318 xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
320 free(entrycpy); /* strdup'ed by dict mechanism, but cannot be const */
323 /** @brief Displays the declared aliases and their description */
324 void xbt_cfg_aliases(void)
326 xbt_dict_cursor_t dict_cursor;
327 unsigned int dynar_cursor;
328 simgrid::config::ConfigurationElement* variable;
330 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
332 xbt_dict_foreach(simgrid_config->options, dict_cursor, name, variable)
333 xbt_dynar_push(names, &name);
334 xbt_dynar_sort_strings(names);
336 xbt_dynar_foreach(names, dynar_cursor, name) {
337 variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, name);
339 if (variable->type == xbt_cfgelm_alias)
340 printf(" %s: %s\n", name, variable->desc.c_str());
344 /** @brief Displays the declared options and their description */
345 void xbt_cfg_help(void)
347 xbt_dict_cursor_t dict_cursor;
348 unsigned int dynar_cursor;
349 simgrid::config::ConfigurationElement* variable;
351 xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
353 xbt_dict_foreach(simgrid_config->options, dict_cursor, name, variable)
354 xbt_dynar_push(names, &name);
355 xbt_dynar_sort_strings(names);
357 xbt_dynar_foreach(names, dynar_cursor, name) {
359 variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, name);
360 if (variable->type == xbt_cfgelm_alias)
363 printf(" %s: %s\n", name, variable->desc.c_str());
364 printf(" Type: %s; ", xbt_cfgelm_type_name[variable->type]);
365 size = xbt_dynar_length(variable->content);
366 printf("Current value: ");
369 printf(size == 0 ? "n/a\n" : "{ ");
370 for (int i = 0; i < size; i++) {
371 const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
373 switch (variable->type) {
375 printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
377 case xbt_cfgelm_double:
378 printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
380 case xbt_cfgelm_string:
381 printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
383 case xbt_cfgelm_boolean: {
384 int b = xbt_dynar_get_as(variable->content, i, int);
385 const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val: xbt_cfgelm_boolean_values[0].false_val;
386 if (b == 0 || b == 1)
387 printf("'%s'%s", bs, sep);
389 printf("'%s/%d'%s", bs, b, sep);
393 printf("Invalid type!!%s", sep);
398 xbt_dynar_free(&names);
401 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
403 simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(cfg->options, name);
405 // The user used the old name. Switch to the new one after a short warning
406 while (res && res->type == xbt_cfgelm_alias) {
407 const char* newname = (const char *)res->content;
408 XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
409 res = xbt_cfgelm_get(cfg, newname, type);
415 THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
418 xbt_assert(type == xbt_cfgelm_any || res->type == type,
419 "You tried to access to the config element %s as an %s, but its type is %s.",
420 name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
424 /** @brief Get the type of this variable in that configuration set
426 * @param cfg the config set
427 * @param name the name of the element
429 * @return the type of the given element
431 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
433 simgrid::config::ConfigurationElement* variable = NULL;
435 variable = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(cfg->options, name);
437 THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
439 XBT_DEBUG("type in variable = %d", (int)variable->type);
440 return variable->type;
443 /*----[ Setting ]---------------------------------------------------------*/
444 /** @brief va_args version of xbt_cfg_set
446 * @param cfg config set to fill
447 * @param name variable name
448 * @param pa variable value
450 * Add some values to the config set.
452 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
457 e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
462 type = xbt_cfg_get_type(cfg, name);
465 if (e.category == not_found_error) {
467 THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
473 case xbt_cfgelm_string:
474 str = va_arg(pa, char *);
475 xbt_cfg_set_string(name, str);
479 xbt_cfg_set_int(name, i);
481 case xbt_cfgelm_double:
482 d = va_arg(pa, double);
483 xbt_cfg_set_double(name, d);
485 case xbt_cfgelm_boolean:
486 str = va_arg(pa, char *);
487 xbt_cfg_set_boolean(name, str);
490 xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
494 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
496 * @param cfg config set to fill
497 * @param name variable name
498 * @param ... variable value
500 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
505 xbt_cfg_set_vargs(cfg, name, pa);
509 /** @brief Add values parsed from a string into a config set
511 * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
512 * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
513 * already registered variable, and [value] conforms to the data type under which this variable was registered.
515 * @todo This is a crude manual parser, it should be a proper lexer.
517 void xbt_cfg_set_parse(const char *options)
519 if (!options || !strlen(options)) { /* nothing to do */
522 char *optionlist_cpy = xbt_strdup(options);
524 XBT_DEBUG("List to parse and set:'%s'", options);
525 char *option = optionlist_cpy;
526 while (1) { /* breaks in the code */
530 int len = strlen(name);
531 XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
534 while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
535 XBT_DEBUG("Take %c.", *option);
538 if (option - name == len) {
539 XBT_DEBUG("Boundary=EOL");
540 option = NULL; /* don't do next iteration */
542 XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
543 /* Pass the following blank chars */
545 while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
546 /* fprintf(stderr,"Ignore a blank char.\n"); */
549 if (option - name == len - 1)
550 option = NULL; /* don't do next iteration */
552 XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
554 if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
559 char *val = strchr(name, ':');
560 xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
561 /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
564 if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
565 XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
568 xbt_cfg_set_as_string(name,val);
570 free(optionlist_cpy);
574 free(optionlist_cpy);
577 /** @brief Set the value of a variable, using the string representation of that value
579 * @param key name of the variable to modify
580 * @param value string representation of the value to set
582 * @return the first char after the parsed value in val
585 void *xbt_cfg_set_as_string(const char *key, const char *value) {
589 volatile simgrid::config::ConfigurationElement* variable = NULL;
594 while (variable == NULL) {
595 variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, key);
596 if (variable->type == xbt_cfgelm_alias) {
597 const char *newname = (const char*)variable->content;
598 XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
604 if (e.category == not_found_error) {
606 THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
611 switch (variable->type) {
612 case xbt_cfgelm_string:
613 xbt_cfg_set_string(key, value); /* throws */
616 i = strtol(value, &ret, 0);
618 xbt_die("Value of option %s not valid. Should be an integer", key);
620 xbt_cfg_set_int(key, i); /* throws */
622 case xbt_cfgelm_double:
623 d = strtod(value, &ret);
625 xbt_die("Value of option %s not valid. Should be a double", key);
627 xbt_cfg_set_double(key, d); /* throws */
629 case xbt_cfgelm_boolean:
630 xbt_cfg_set_boolean(key, value); /* throws */
631 ret = (char *)value + strlen(value);
634 THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
640 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
642 * This is useful to change the default value of a variable while allowing
643 * users to override it with command line arguments
645 void xbt_cfg_setdefault_int(const char *name, int val)
647 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
649 if (variable->isdefault){
650 xbt_cfg_set_int(name, val);
651 variable->isdefault = true;
653 XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
656 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
658 * This is useful to change the default value of a variable while allowing
659 * users to override it with command line arguments
661 void xbt_cfg_setdefault_double(const char *name, double val)
663 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
665 if (variable->isdefault) {
666 xbt_cfg_set_double(name, val);
667 variable->isdefault = true;
669 XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
672 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
674 * This is useful to change the default value of a variable while allowing
675 * users to override it with command line arguments
677 void xbt_cfg_setdefault_string(const char *name, const char *val)
679 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
681 if (variable->isdefault){
682 xbt_cfg_set_string(name, val);
683 variable->isdefault = true;
685 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
688 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
690 * This is useful to change the default value of a variable while allowing
691 * users to override it with command line arguments
693 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
695 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
697 if (variable->isdefault){
698 xbt_cfg_set_boolean(name, val);
699 variable->isdefault = true;
702 XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
705 /** @brief Set an integer value to \a name within \a cfg
707 * @param name the name of the variable
708 * @param val the value of the variable
710 void xbt_cfg_set_int(const char *name, int val)
712 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
714 xbt_dynar_set(variable->content, 0, &val);
716 if (variable->cb_set)
717 variable->cb_set(name);
718 variable->isdefault = false;
721 /** @brief Set or add a double value to \a name within \a cfg
723 * @param name the name of the variable
724 * @param val the double to set
726 void xbt_cfg_set_double(const char *name, double val)
728 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
730 xbt_dynar_set(variable->content, 0, &val);
732 if (variable->cb_set)
733 variable->cb_set(name);
734 variable->isdefault = false;
737 /** @brief Set or add a string value to \a name within \a cfg
739 * @param cfg the config set
740 * @param name the name of the variable
741 * @param val the value to be added
744 void xbt_cfg_set_string(const char *name, const char *val)
746 char *newval = xbt_strdup(val);
747 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
749 if (!xbt_dynar_is_empty(variable->content)) {
750 char *sval = xbt_dynar_get_as(variable->content, 0, char *);
754 xbt_dynar_set(variable->content, 0, &newval);
756 if (variable->cb_set)
757 variable->cb_set(name);
759 if (variable->callback) {
761 variable->callback(val);
763 catch(std::range_error& e) {
764 xbt_die("Invalid flag %s=%s: %s", val, name, e.what());
766 catch(std::exception& e) {
767 xbt_die("Error for flag %s=%s: %s", val, name, e.what());
770 xbt_die("Error for flag %s=%s", val, name);
774 variable->isdefault = false;
777 /** @brief Set or add a boolean value to \a name within \a cfg
779 * @param name the name of the variable
780 * @param val the value of the variable
782 void xbt_cfg_set_boolean(const char *name, const char *val)
785 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
787 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
788 if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
792 if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
797 xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
798 xbt_dynar_set(variable->content, 0, &bval);
800 if (variable->cb_set)
801 variable->cb_set(name);
802 variable->isdefault = false;
806 /* Say if the value is the default value */
807 int xbt_cfg_is_default_value(const char *name)
809 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
810 return variable->isdefault;
813 /*----[ Getting ]---------------------------------------------------------*/
814 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
816 * @param name the name of the variable
818 * Returns the first value from the config set under the given name.
819 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
821 int xbt_cfg_get_int(const char *name)
823 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
825 if (xbt_dynar_length(variable->content) > 1) {
826 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
827 name, xbt_dynar_length(variable->content));
830 return xbt_dynar_get_as(variable->content, 0, int);
833 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
835 * @param cfg the config set
836 * @param name the name of the variable
838 * Returns the first value from the config set under the given name.
839 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
841 double xbt_cfg_get_double(const char *name)
843 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
845 if (xbt_dynar_length(variable->content) > 1) {
846 XBT_WARN ("You asked for the first value of the config element '%s', but there is %lu values\n",
847 name, xbt_dynar_length(variable->content));
850 return xbt_dynar_get_as(variable->content, 0, double);
853 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
855 * @param cfg the config set
856 * @param name the name of the variable
858 * Returns the first value from the config set under the given name.
859 * If there is more than one value, it will issue a warning. Consider using
860 * xbt_cfg_get_dynar() instead. Returns NULL if there is no value.
862 * \warning the returned value is the actual content of the config set
864 char *xbt_cfg_get_string(const char *name)
866 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
868 if (xbt_dynar_length(variable->content) > 1) {
869 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values\n",
870 name, xbt_dynar_length(variable->content));
871 } else if (xbt_dynar_is_empty(variable->content)) {
875 return xbt_dynar_get_as(variable->content, 0, char *);
878 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
880 * @param cfg the config set
881 * @param name the name of the variable
883 * Returns the first value from the config set under the given name.
884 * If there is more than one value, it will issue a warning. Consider using xbt_cfg_get_dynar() instead.
886 int xbt_cfg_get_boolean(const char *name)
888 simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
890 if (xbt_dynar_length(variable->content) > 1) {
891 XBT_WARN("You asked for the first value of the config element '%s', but there is %lu values",
892 name, xbt_dynar_length(variable->content));
895 return xbt_dynar_get_as(variable->content, 0, int);
901 bool parseBool(const char* value)
903 for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
904 if (std::strcmp(value, xbt_cfgelm_boolean_values[i].true_val) == 0)
906 if (std::strcmp(value, xbt_cfgelm_boolean_values[i].false_val) == 0)
909 throw std::range_error("not a boolean");
912 double parseDouble(const char* value)
916 double res = std::strtod(value, &end);
918 throw std::range_error("out of range");
920 xbt_die("Unexpected errno");
921 if (end == value || *end != '\0')
922 throw std::range_error("invalid double");
927 long int parseLong(const char* value)
931 long int res = std::strtol(value, &end, 0);
933 if (res == LONG_MIN && errno == ERANGE)
934 throw std::range_error("underflow");
935 else if (res == LONG_MAX && errno == ERANGE)
936 throw std::range_error("overflow");
937 xbt_die("Unexpected errno");
939 if (end == value || *end != '\0')
940 throw std::range_error("invalid integer");
945 void declareFlag(const char* name, const char* description,
946 std::function<void(const char* value)> callback)
948 xbt_cfg_register(&simgrid_config, name, description, xbt_cfgelm_string, NULL,
949 std::move(callback));
962 #include <xbt/config.hpp>
964 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
966 XBT_TEST_SUITE("config", "Configuration support");
968 static xbt_cfg_t make_set()
970 xbt_cfg_t set = NULL;
972 xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
973 xbt_cfg_register_str(&set, "speed:int");
974 xbt_cfg_register_str(&set, "peername:string");
975 xbt_cfg_register_str(&set, "user:string");
978 } /* end_of_make_set */
980 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
982 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
984 simgrid_config = make_set();
985 xbt_test_add("Alloc and free a config set");
986 xbt_cfg_set_parse("peername:veloce user:bidule");
987 xbt_cfg_free(&simgrid_config);
990 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
992 simgrid_config = make_set();
993 xbt_test_add("Get a single value");
995 /* get_single_value */
998 xbt_cfg_set_parse("peername:toto:42 speed:42");
999 ival = xbt_cfg_get_int("speed");
1001 xbt_test_fail("Speed value = %d, I expected 42", ival);
1004 xbt_test_add("Access to a non-existant entry");
1009 xbt_cfg_set_parse("color:blue");
1011 if (e.category != not_found_error)
1012 xbt_test_exception(e);
1016 xbt_cfg_free(&simgrid_config);
1019 XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
1021 simgrid_config = make_set();
1022 xbt_test_add("C++ declaration of flags");
1024 simgrid::config::Flag<int> int_flag("int", "", 0);
1025 simgrid::config::Flag<std::string> string_flag("string", "", "foo");
1026 simgrid::config::Flag<double> double_flag("double", "", 0.32);
1027 simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
1028 simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
1030 xbt_test_add("Parse values");
1031 xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
1032 xbt_test_assert(int_flag == 42, "Check int flag");
1033 xbt_test_assert(string_flag == "bar", "Check string flag");
1034 xbt_test_assert(double_flag == 8.0, "Check double flag");
1035 xbt_test_assert(bool_flag1, "Check bool1 flag");
1036 xbt_test_assert(!bool_flag2, "Check bool2 flag");
1038 xbt_cfg_free(&simgrid_config);
1041 #endif /* SIMGRID_TEST */