Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
empty a C->C++ wrapper that will soon die
[simgrid.git] / src / xbt / config.cpp
1 /* Copyright (c) 2004-2018. The SimGrid Team. All rights reserved.     */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstdio>
7
8 #include <algorithm>
9 #include <cerrno>
10 #include <cstring>
11 #include <climits>
12
13 #include <functional>
14 #include <map>
15 #include <stdexcept>
16 #include <string>
17 #include <string>
18 #include <type_traits>
19 #include <typeinfo>
20 #include <vector>
21
22 #include "simgrid/sg_config.hpp"
23 #include "xbt/dynar.h"
24 #include "xbt/log.h"
25 #include "xbt/misc.h"
26 #include "xbt/sysdep.h"
27 #include <xbt/config.h>
28 #include <xbt/config.hpp>
29 #include <xbt/ex.hpp>
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
32
33 XBT_EXPORT_NO_IMPORT xbt_cfg_t simgrid_config = nullptr;
34
35 namespace simgrid {
36 namespace config {
37
38 missing_key_error::~missing_key_error() = default;
39
40 namespace {
41
42 const char* true_values[] = {
43   "yes", "on", "true", "1"
44 };
45 const char* false_values[] = {
46   "no", "off", "false", "0"
47 };
48
49 static bool parseBool(const char* value)
50 {
51   for (const char* const& true_value : true_values)
52     if (std::strcmp(true_value, value) == 0)
53       return true;
54   for (const char* const& false_value : false_values)
55     if (std::strcmp(false_value, value) == 0)
56       return false;
57   throw std::range_error("not a boolean");
58 }
59
60 static double parseDouble(const char* value)
61 {
62   char* end;
63   errno = 0;
64   double res = std::strtod(value, &end);
65   if (errno == ERANGE)
66     throw std::range_error("out of range");
67   else if (errno)
68     xbt_die("Unexpected errno");
69   if (end == value || *end != '\0')
70     throw std::range_error("invalid double");
71   else
72     return res;
73 }
74
75 static long int parseLong(const char* value)
76 {
77   char* end;
78   errno = 0;
79   long int res = std::strtol(value, &end, 0);
80   if (errno) {
81     if (res == LONG_MIN && errno == ERANGE)
82       throw std::range_error("underflow");
83     else if (res == LONG_MAX && errno == ERANGE)
84       throw std::range_error("overflow");
85     xbt_die("Unexpected errno");
86   }
87   if (end == value || *end != '\0')
88     throw std::range_error("invalid integer");
89   else
90     return res;
91 }
92
93 // ***** ConfigType *****
94
95 /// A trait which define possible options types:
96 template <class T> class ConfigType;
97
98 template <> class ConfigType<int> {
99 public:
100   static constexpr const char* type_name = "int";
101   static inline double parse(const char* value)
102   {
103     return parseLong(value);
104   }
105 };
106 template <> class ConfigType<double> {
107 public:
108   static constexpr const char* type_name = "double";
109   static inline double parse(const char* value)
110   {
111     return parseDouble(value);
112   }
113 };
114 template <> class ConfigType<std::string> {
115 public:
116   static constexpr const char* type_name = "string";
117   static inline std::string parse(const char* value)
118   {
119     return std::string(value);
120   }
121 };
122 template <> class ConfigType<bool> {
123 public:
124   static constexpr const char* type_name = "boolean";
125   static inline bool parse(const char* value)
126   {
127     return parseBool(value);
128   }
129 };
130
131 // **** Forward declarations ****
132
133 class ConfigurationElement ;
134 template<class T> class TypedConfigurationElement;
135
136 // **** ConfigurationElement ****
137
138 class ConfigurationElement {
139 private:
140   std::string key;
141   std::string desc;
142   bool isdefault = true;
143
144 public:
145   /* Callback */
146   xbt_cfg_cb_t old_callback = nullptr;
147
148   ConfigurationElement(const char* key, const char* desc) : key(key ? key : ""), desc(desc ? desc : "") {}
149   ConfigurationElement(const char* key, const char* desc, xbt_cfg_cb_t cb)
150     : key(key ? key : ""), desc(desc ? desc : ""), old_callback(cb) {}
151
152   virtual ~ConfigurationElement()=default;
153
154   virtual std::string getStringValue() = 0;
155   virtual void setStringValue(const char* value) = 0;
156   virtual const char* getTypeName() = 0;
157
158   template<class T>
159   T const& getValue() const
160   {
161     return dynamic_cast<const TypedConfigurationElement<T>&>(*this).getValue();
162   }
163   template<class T>
164   void setValue(T value)
165   {
166     dynamic_cast<TypedConfigurationElement<T>&>(*this).setValue(std::move(value));
167   }
168   template<class T>
169   void setDefaultValue(T value)
170   {
171     dynamic_cast<TypedConfigurationElement<T>&>(*this).setDefaultValue(std::move(value));
172   }
173   void unsetDefault() { isdefault = false; }
174   bool isDefault() const { return isdefault; }
175
176   std::string const& getDescription() const { return desc; }
177   std::string const& getKey() const { return key; }
178 };
179
180 // **** TypedConfigurationElement<T> ****
181
182 // TODO, could we use boost::any with some Type* reference?
183 template<class T>
184 class TypedConfigurationElement : public ConfigurationElement {
185 private:
186   T content;
187   std::function<void(T&)> callback;
188
189 public:
190   TypedConfigurationElement(const char* key, const char* desc, T value = T())
191     : ConfigurationElement(key, desc), content(std::move(value))
192   {}
193   TypedConfigurationElement(const char* key, const char* desc, T value, xbt_cfg_cb_t cb)
194       : ConfigurationElement(key, desc, cb), content(std::move(value))
195   {}
196   TypedConfigurationElement(const char* key, const char* desc, T value, std::function<void(T&)> callback)
197       : ConfigurationElement(key, desc), content(std::move(value)), callback(std::move(callback))
198   {}
199   ~TypedConfigurationElement() = default;
200
201   std::string getStringValue() override;
202   const char* getTypeName() override;
203   void setStringValue(const char* value) override;
204
205   void update()
206   {
207     if (old_callback)
208       this->old_callback(getKey().c_str());
209     if (this->callback)
210       this->callback(this->content);
211   }
212
213   T const& getValue() const { return content; }
214
215   void setValue(T value)
216   {
217     this->content = std::move(value);
218     this->update();
219   }
220
221   void setDefaultValue(T value)
222   {
223     if (this->isDefault()) {
224       this->content = std::move(value);
225       this->update();
226     } else {
227       XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
228                 getKey().c_str(), to_string(value).c_str());
229     }
230   }
231 };
232
233 template<class T>
234 std::string TypedConfigurationElement<T>::getStringValue() // override
235 {
236   return to_string(content);
237 }
238
239 template<class T>
240 void TypedConfigurationElement<T>::setStringValue(const char* value) // override
241 {
242   this->content = ConfigType<T>::parse(value);
243   this->unsetDefault();
244   this->update();
245 }
246
247 template<class T>
248 const char* TypedConfigurationElement<T>::getTypeName() // override
249 {
250   return ConfigType<T>::type_name;
251 }
252
253 } // end of anonymous namespace
254
255 // **** Config ****
256
257 class Config {
258 private:
259   // name -> ConfigElement:
260   std::map<std::string, simgrid::config::ConfigurationElement*> options;
261   // alias -> ConfigElement from options:
262   std::map<std::string, simgrid::config::ConfigurationElement*> aliases;
263   bool warn_for_aliases = true;
264
265 public:
266   Config();
267   ~Config();
268
269   // No copy:
270   Config(Config const&) = delete;
271   Config& operator=(Config const&) = delete;
272
273   ConfigurationElement& operator[](const char* name);
274   template<class T>
275   TypedConfigurationElement<T>& getTyped(const char* name);
276   void alias(const char* realname, const char* aliasname);
277
278   template<class T, class... A>
279   simgrid::config::TypedConfigurationElement<T>*
280     registerOption(const char* name, A&&... a)
281   {
282     xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.", name);
283     TypedConfigurationElement<T>* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
284     XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name, variable->getDescription().c_str(),
285               variable->getTypeName(), variable, this);
286     options.insert({name, variable});
287     variable->update();
288     return variable;
289   }
290
291   // Debug:
292   void dump(const char *name, const char *indent);
293   void showAliases();
294   void help();
295
296 protected:
297   ConfigurationElement* getDictElement(const char* name);
298 };
299
300 Config::Config()
301 {
302   atexit(&sg_config_finalize);
303 }
304 Config::~Config()
305 {
306   XBT_DEBUG("Frees cfg set %p", this);
307   for (auto const& elm : options)
308     delete elm.second;
309 }
310
311 inline ConfigurationElement* Config::getDictElement(const char* name)
312 {
313   auto opt = options.find(name);
314   if (opt != options.end()) {
315     return opt->second;
316   } else {
317     auto als = aliases.find(name);
318     if (als != aliases.end()) {
319       ConfigurationElement* res = als->second;
320       if (warn_for_aliases)
321         XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, res->getKey().c_str());
322       return res;
323     } else {
324       throw simgrid::config::missing_key_error(std::string("Bad config key: ") + name);
325     }
326   }
327 }
328
329 inline ConfigurationElement& Config::operator[](const char* name)
330 {
331   return *(getDictElement(name));
332 }
333
334 void Config::alias(const char* realname, const char* aliasname)
335 {
336   xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname);
337   ConfigurationElement* element = this->getDictElement(realname);
338   xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname);
339   this->aliases.insert({aliasname, element});
340 }
341
342 /** @brief Dump a config set for debuging purpose
343  *
344  * @param name The name to give to this config set
345  * @param indent what to write at the beginning of each line (right number of spaces)
346  */
347 void Config::dump(const char *name, const char *indent)
348 {
349   if (name)
350     printf("%s>> Dumping of the config set '%s':\n", indent, name);
351
352   for (auto const& elm : options)
353     printf("%s  %s: ()%s) %s", indent, elm.first.c_str(), elm.second->getTypeName(),
354            elm.second->getStringValue().c_str());
355
356   if (name)
357     printf("%s<< End of the config set '%s'\n", indent, name);
358   fflush(stdout);
359 }
360
361 /** @brief Displays the declared aliases and their description */
362 void Config::showAliases()
363 {
364   bool old_warn_for_aliases = false;
365   std::swap(warn_for_aliases, old_warn_for_aliases);
366   for (auto const& elm : aliases)
367     printf("   %s: %s\n", elm.first.c_str(), (*this)[elm.first.c_str()].getDescription().c_str());
368   std::swap(warn_for_aliases, old_warn_for_aliases);
369 }
370
371 /** @brief Displays the declared options and their description */
372 void Config::help()
373 {
374   for (auto const& elm : options) {
375     simgrid::config::ConfigurationElement* variable = this->options.at(elm.first);
376     printf("   %s: %s\n", elm.first.c_str(), variable->getDescription().c_str());
377     printf("       Type: %s; ", variable->getTypeName());
378     printf("Current value: %s\n", variable->getStringValue().c_str());
379   }
380 }
381
382 // ***** getConfig *****
383
384 template <class T> XBT_PUBLIC T const& getConfig(const char* name)
385 {
386   return (*simgrid_config)[name].getValue<T>();
387 }
388
389 template XBT_PUBLIC int const& getConfig<int>(const char* name);
390 template XBT_PUBLIC double const& getConfig<double>(const char* name);
391 template XBT_PUBLIC bool const& getConfig<bool>(const char* name);
392 template XBT_PUBLIC std::string const& getConfig<std::string>(const char* name);
393
394 // ***** alias *****
395
396 void alias(const char* realname, const char* aliasname)
397 {
398   simgrid_config->alias(realname, aliasname);
399 }
400
401 // ***** declareFlag *****
402
403 template <class T>
404 XBT_PUBLIC void declareFlag(const char* name, const char* description, T value, std::function<void(const T&)> callback)
405 {
406   if (simgrid_config == nullptr)
407     simgrid_config = xbt_cfg_new();
408   simgrid_config->registerOption<T>(
409     name, description, std::move(value), std::move(callback));
410 }
411
412 template XBT_PUBLIC void declareFlag(const char* name, const char* description, int value,
413                                      std::function<void(int const&)> callback);
414 template XBT_PUBLIC void declareFlag(const char* name, const char* description, double value,
415                                      std::function<void(double const&)> callback);
416 template XBT_PUBLIC void declareFlag(const char* name, const char* description, bool value,
417                                      std::function<void(bool const&)> callback);
418 template XBT_PUBLIC void declareFlag(const char* name, const char* description, std::string value,
419                                      std::function<void(std::string const&)> callback);
420 }
421 }
422
423 // ***** C bindings *****
424
425 xbt_cfg_t xbt_cfg_new()
426 {
427   return new simgrid::config::Config();
428 }
429 void xbt_cfg_free(xbt_cfg_t * cfg) { delete *cfg; }
430
431 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
432 {
433   cfg->dump(name, indent);
434 }
435
436 /*----[ Registering stuff ]-----------------------------------------------*/
437
438 void xbt_cfg_register_double(const char *name, double default_value,
439   xbt_cfg_cb_t cb_set, const char *desc)
440 {
441   if (simgrid_config == nullptr)
442     simgrid_config = xbt_cfg_new();
443   simgrid_config->registerOption<double>(name, desc, default_value, cb_set);
444 }
445
446 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc)
447 {
448   if (simgrid_config == nullptr)
449     simgrid_config = xbt_cfg_new();
450   simgrid_config->registerOption<int>(name, desc, default_value, cb_set);
451 }
452
453 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc)
454 {
455   if (simgrid_config == nullptr)
456     simgrid_config = xbt_cfg_new();
457   simgrid_config->registerOption<std::string>(name, desc, default_value ? default_value : "", cb_set);
458 }
459
460 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc)
461 {
462   if (simgrid_config == nullptr)
463     simgrid_config = xbt_cfg_new();
464   simgrid_config->registerOption<bool>(name, desc, simgrid::config::parseBool(default_value), cb_set);
465 }
466
467 void xbt_cfg_register_alias(const char *realname, const char *aliasname)
468 {
469   if (simgrid_config == nullptr)
470     simgrid_config = xbt_cfg_new();
471   simgrid_config->alias(realname, aliasname);
472 }
473
474 void xbt_cfg_aliases() { simgrid_config->showAliases(); }
475 void xbt_cfg_help()    { simgrid_config->help(); }
476
477 /*----[ Setting ]---------------------------------------------------------*/
478
479 /** @brief Add values parsed from a string into a config set
480  *
481  * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
482  * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
483  * already registered variable, and [value] conforms to the data type under which this variable was registered.
484  *
485  * @todo This is a crude manual parser, it should be a proper lexer.
486  */
487 void xbt_cfg_set_parse(const char *options)
488 {
489   if (not options || not strlen(options)) { /* nothing to do */
490     return;
491   }
492
493   XBT_DEBUG("List to parse and set:'%s'", options);
494   std::string optionlist(options);
495   while (not optionlist.empty()) {
496     XBT_DEBUG("Still to parse and set: '%s'", optionlist.c_str());
497
498     // skip separators
499     size_t pos = optionlist.find_first_not_of(" \t\n,");
500     optionlist.erase(0, pos);
501     // find option
502     pos              = optionlist.find_first_of(" \t\n,");
503     std::string name = optionlist.substr(0, pos);
504     optionlist.erase(0, pos);
505     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), optionlist.c_str());
506
507     if (name.empty())
508       continue;
509
510     pos = name.find(':');
511     xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
512                name.c_str());
513
514     std::string val = name.substr(pos + 1);
515     name.erase(pos);
516
517     const std::string path("path");
518     if (name.compare(0, path.length(), path) != 0)
519       XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
520
521     try {
522       (*simgrid_config)[name.c_str()].setStringValue(val.c_str());
523     }
524     catch (simgrid::config::missing_key_error& e) {
525       goto on_missing_key;
526     }
527     catch (...) {
528       goto on_exception;
529     }
530   }
531   return;
532
533   /* Do not THROWF from a C++ exception catching context, or some cleanups will be missing */
534 on_missing_key:
535   THROWF(not_found_error, 0, "Could not set variables %s", options);
536 on_exception:
537   THROWF(unknown_error, 0, "Could not set variables %s", options);
538 }
539
540 // Horrible mess to translate C++ exceptions to C exceptions:
541 // Exit from the catch block (and do the correct exception cleaning) before attempting to THROWF.
542 #define TRANSLATE_EXCEPTIONS(...) \
543   catch(simgrid::config::missing_key_error& e) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); } \
544   catch(...) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); }
545
546 /** @brief Set the value of a variable, using the string representation of that value
547  *
548  * @param key name of the variable to modify
549  * @param value string representation of the value to set
550  */
551
552 void xbt_cfg_set_as_string(const char *key, const char *value)
553 {
554   try {
555     (*simgrid_config)[key].setStringValue(value);
556     return;
557   }
558   TRANSLATE_EXCEPTIONS("Could not set variable %s as string %s", key, value);
559 }
560
561 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
562  *
563  * This is useful to change the default value of a variable while allowing
564  * users to override it with command line arguments
565  */
566 void xbt_cfg_setdefault_int(const char *key, int value)
567 {
568   try {
569     (*simgrid_config)[key].setDefaultValue<int>(value);
570     return;
571   }
572   TRANSLATE_EXCEPTIONS("Could not set variable %s to default integer %i", key, value);
573 }
574
575 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
576  *
577  * This is useful to change the default value of a variable while allowing
578  * users to override it with command line arguments
579  */
580 void xbt_cfg_setdefault_double(const char *key, double value)
581 {
582   try {
583     (*simgrid_config)[key].setDefaultValue<double>(value);
584     return;
585   }
586   TRANSLATE_EXCEPTIONS("Could not set variable %s to default double %f", key, value);
587 }
588
589 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
590  *
591  * This is useful to change the default value of a variable while allowing
592  * users to override it with command line arguments
593  */
594 void xbt_cfg_setdefault_string(const char *key, const char *value)
595 {
596   try {
597     (*simgrid_config)[key].setDefaultValue<std::string>(value ? value : "");
598     return;
599   }
600   TRANSLATE_EXCEPTIONS("Could not set variable %s to default string %s", key, value);
601 }
602
603 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
604  *
605  * This is useful to change the default value of a variable while allowing
606  * users to override it with command line arguments
607  */
608 void xbt_cfg_setdefault_boolean(const char *key, const char *value)
609 {
610   try {
611     (*simgrid_config)[key].setDefaultValue<bool>(simgrid::config::parseBool(value));
612     return;
613   }
614   TRANSLATE_EXCEPTIONS("Could not set variable %s to default boolean %s", key, value);
615 }
616
617 /** @brief Set an integer value to \a name within \a cfg
618  *
619  * @param key the name of the variable
620  * @param value the value of the variable
621  */
622 void xbt_cfg_set_int(const char *key, int value)
623 {
624   try {
625     (*simgrid_config)[key].setValue<int>(value);
626     return;
627   }
628   TRANSLATE_EXCEPTIONS("Could not set variable %s to integer %i", key, value);
629 }
630
631 /** @brief Set or add a double value to \a name within \a cfg
632  *
633  * @param key the name of the variable
634  * @param value the double to set
635  */
636 void xbt_cfg_set_double(const char *key, double value)
637 {
638   try {
639     (*simgrid_config)[key].setValue<double>(value);
640     return;
641   }
642   TRANSLATE_EXCEPTIONS("Could not set variable %s to double %f", key, value);
643 }
644
645 /** @brief Set or add a string value to \a name within \a cfg
646  *
647  * @param key the name of the variable
648  * @param value the value to be added
649  *
650  */
651 void xbt_cfg_set_string(const char* key, const char* value)
652 {
653   try {
654     (*simgrid_config)[key].setValue<std::string>(value);
655     return;
656   }
657   TRANSLATE_EXCEPTIONS("Could not set variable %s to string %s", key, value);
658 }
659
660 /** @brief Set or add a boolean value to \a name within \a cfg
661  *
662  * @param key the name of the variable
663  * @param value the value of the variable
664  */
665 void xbt_cfg_set_boolean(const char *key, const char *value)
666 {
667   try {
668     (*simgrid_config)[key].setValue<bool>(simgrid::config::parseBool(value));
669     return;
670   }
671   TRANSLATE_EXCEPTIONS("Could not set variable %s to boolean %s", key, value);
672 }
673
674
675 /* Say if the value is the default value */
676 int xbt_cfg_is_default_value(const char *key)
677 {
678   try {
679     return (*simgrid_config)[key].isDefault() ? 1 : 0;
680   }
681   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
682 }
683
684 /*----[ Getting ]---------------------------------------------------------*/
685 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
686  *
687  * @param key the name of the variable
688  *
689  * Returns the first value from the config set under the given name.
690  */
691 int xbt_cfg_get_int(const char *key)
692 {
693   try {
694     return (*simgrid_config)[key].getValue<int>();
695   }
696   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
697 }
698
699 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
700  *
701  * @param key the name of the variable
702  *
703  * Returns the first value from the config set under the given name.
704  */
705 double xbt_cfg_get_double(const char *key)
706 {
707   try {
708     return (*simgrid_config)[key].getValue<double>();
709   }
710   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
711 }
712
713 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
714  *
715  * @param key the name of the variable
716  *
717  * Returns the first value from the config set under the given name.
718  * If there is more than one value, it will issue a warning.
719  * Returns nullptr if there is no value.
720  *
721  * \warning the returned value is the actual content of the config set
722  */
723 std::string xbt_cfg_get_string(const char* key)
724 {
725   try {
726     return (*simgrid_config)[key].getValue<std::string>();
727   }
728   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
729 }
730
731 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
732  *
733  * @param key the name of the variable
734  *
735  * Returns the first value from the config set under the given name.
736  * If there is more than one value, it will issue a warning.
737  */
738 int xbt_cfg_get_boolean(const char *key)
739 {
740   try {
741     return (*simgrid_config)[key].getValue<bool>() ? 1 : 0;
742   }
743   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
744 }
745
746 #ifdef SIMGRID_TEST
747
748 #include <string>
749
750 #include "xbt.h"
751 #include "xbt/ex.h"
752 #include <xbt/ex.hpp>
753
754 #include <xbt/config.hpp>
755
756 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
757
758 XBT_TEST_SUITE("config", "Configuration support");
759
760 XBT_PUBLIC_DATA xbt_cfg_t simgrid_config;
761
762 static void make_set()
763 {
764   simgrid_config = nullptr;
765   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
766   xbt_cfg_register_int("speed", 0, nullptr, "");
767   xbt_cfg_register_string("peername", "", nullptr, "");
768   xbt_cfg_register_string("user", "", nullptr, "");
769 }                               /* end_of_make_set */
770
771 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
772 {
773   auto temp = simgrid_config;
774   make_set();
775   xbt_test_add("Alloc and free a config set");
776   xbt_cfg_set_parse("peername:veloce user:bidule");
777   xbt_cfg_free(&simgrid_config);
778   simgrid_config = temp;
779 }
780
781 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
782 {
783   auto temp = simgrid_config;
784   make_set();
785   xbt_test_add("Get a single value");
786   {
787     /* get_single_value */
788     xbt_cfg_set_parse("peername:toto:42 speed:42");
789     int ival = xbt_cfg_get_int("speed");
790     if (ival != 42)
791       xbt_test_fail("Speed value = %d, I expected 42", ival);
792   }
793
794   xbt_test_add("Access to a non-existant entry");
795   {
796     try {
797       xbt_cfg_set_parse("color:blue");
798     } catch(xbt_ex& e) {
799       if (e.category != not_found_error)
800         xbt_test_exception(e);
801     }
802   }
803   xbt_cfg_free(&simgrid_config);
804   simgrid_config = temp;
805 }
806
807 XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
808 {
809   auto temp = simgrid_config;
810   make_set();
811   xbt_test_add("C++ declaration of flags");
812
813   simgrid::config::Flag<int> int_flag("int", "", 0);
814   simgrid::config::Flag<std::string> string_flag("string", "", "foo");
815   simgrid::config::Flag<double> double_flag("double", "", 0.32);
816   simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
817   simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
818
819   xbt_test_add("Parse values");
820   xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
821   xbt_test_assert(int_flag == 42, "Check int flag");
822   xbt_test_assert(string_flag == "bar", "Check string flag");
823   xbt_test_assert(double_flag == 8.0, "Check double flag");
824   xbt_test_assert(bool_flag1, "Check bool1 flag");
825   xbt_test_assert(not bool_flag2, "Check bool2 flag");
826
827   xbt_cfg_free(&simgrid_config);
828   simgrid_config = temp;
829 }
830
831 #endif                          /* SIMGRID_TEST */