Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5ba907f6995017b762eb52fcac4b6a73d1dd1d68
[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     this->unsetDefault();
220   }
221
222   void setDefaultValue(T value)
223   {
224     if (this->isDefault()) {
225       this->content = std::move(value);
226       this->update();
227     } else {
228       XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.",
229                 getKey().c_str(), to_string(value).c_str());
230     }
231   }
232 };
233
234 template<class T>
235 std::string TypedConfigurationElement<T>::getStringValue() // override
236 {
237   return to_string(content);
238 }
239
240 template<class T>
241 void TypedConfigurationElement<T>::setStringValue(const char* value) // override
242 {
243   this->content = ConfigType<T>::parse(value);
244   this->unsetDefault();
245   this->update();
246 }
247
248 template<class T>
249 const char* TypedConfigurationElement<T>::getTypeName() // override
250 {
251   return ConfigType<T>::type_name;
252 }
253
254 } // end of anonymous namespace
255
256 // **** Config ****
257
258 class Config {
259 private:
260   // name -> ConfigElement:
261   std::map<std::string, simgrid::config::ConfigurationElement*> options;
262   // alias -> ConfigElement from options:
263   std::map<std::string, simgrid::config::ConfigurationElement*> aliases;
264   bool warn_for_aliases = true;
265
266 public:
267   Config();
268   ~Config();
269
270   // No copy:
271   Config(Config const&) = delete;
272   Config& operator=(Config const&) = delete;
273
274   ConfigurationElement& operator[](const char* name);
275   template<class T>
276   TypedConfigurationElement<T>& getTyped(const char* name);
277   void alias(const char* realname, const char* aliasname);
278
279   template<class T, class... A>
280   simgrid::config::TypedConfigurationElement<T>*
281     registerOption(const char* name, A&&... a)
282   {
283     xbt_assert(options.find(name) == options.end(), "Refusing to register the config element '%s' twice.", name);
284     TypedConfigurationElement<T>* variable = new TypedConfigurationElement<T>(name, std::forward<A>(a)...);
285     XBT_DEBUG("Register cfg elm %s (%s) of type %s @%p in set %p)", name, variable->getDescription().c_str(),
286               variable->getTypeName(), variable, this);
287     options.insert({name, variable});
288     variable->update();
289     return variable;
290   }
291
292   // Debug:
293   void dump(const char *name, const char *indent);
294   void showAliases();
295   void help();
296
297 protected:
298   ConfigurationElement* getDictElement(const char* name);
299 };
300
301 Config::Config()
302 {
303   atexit(&sg_config_finalize);
304 }
305 Config::~Config()
306 {
307   XBT_DEBUG("Frees cfg set %p", this);
308   for (auto const& elm : options)
309     delete elm.second;
310 }
311
312 inline ConfigurationElement* Config::getDictElement(const char* name)
313 {
314   auto opt = options.find(name);
315   if (opt != options.end()) {
316     return opt->second;
317   } else {
318     auto als = aliases.find(name);
319     if (als != aliases.end()) {
320       ConfigurationElement* res = als->second;
321       if (warn_for_aliases)
322         XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, res->getKey().c_str());
323       return res;
324     } else {
325       throw simgrid::config::missing_key_error(std::string("Bad config key: ") + name);
326     }
327   }
328 }
329
330 inline ConfigurationElement& Config::operator[](const char* name)
331 {
332   return *(getDictElement(name));
333 }
334
335 void Config::alias(const char* realname, const char* aliasname)
336 {
337   xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname);
338   ConfigurationElement* element = this->getDictElement(realname);
339   xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname);
340   this->aliases.insert({aliasname, element});
341 }
342
343 /** @brief Dump a config set for debuging purpose
344  *
345  * @param name The name to give to this config set
346  * @param indent what to write at the beginning of each line (right number of spaces)
347  */
348 void Config::dump(const char *name, const char *indent)
349 {
350   if (name)
351     printf("%s>> Dumping of the config set '%s':\n", indent, name);
352
353   for (auto const& elm : options)
354     printf("%s  %s: ()%s) %s", indent, elm.first.c_str(), elm.second->getTypeName(),
355            elm.second->getStringValue().c_str());
356
357   if (name)
358     printf("%s<< End of the config set '%s'\n", indent, name);
359   fflush(stdout);
360 }
361
362 /** @brief Displays the declared aliases and their description */
363 void Config::showAliases()
364 {
365   bool old_warn_for_aliases = false;
366   std::swap(warn_for_aliases, old_warn_for_aliases);
367   for (auto const& elm : aliases)
368     printf("   %s: %s\n", elm.first.c_str(), (*this)[elm.first.c_str()].getDescription().c_str());
369   std::swap(warn_for_aliases, old_warn_for_aliases);
370 }
371
372 /** @brief Displays the declared options and their description */
373 void Config::help()
374 {
375   for (auto const& elm : options) {
376     simgrid::config::ConfigurationElement* variable = this->options.at(elm.first);
377     printf("   %s: %s\n", elm.first.c_str(), variable->getDescription().c_str());
378     printf("       Type: %s; ", variable->getTypeName());
379     printf("Current value: %s\n", variable->getStringValue().c_str());
380   }
381 }
382
383 // ***** getConfig *****
384
385 template <class T> XBT_PUBLIC T const& getConfig(const char* name)
386 {
387   return (*simgrid_config)[name].getValue<T>();
388 }
389
390 template XBT_PUBLIC int const& getConfig<int>(const char* name);
391 template XBT_PUBLIC double const& getConfig<double>(const char* name);
392 template XBT_PUBLIC bool const& getConfig<bool>(const char* name);
393 template XBT_PUBLIC std::string const& getConfig<std::string>(const char* name);
394
395 // ***** alias *****
396
397 void alias(const char* realname, const char* aliasname)
398 {
399   simgrid_config->alias(realname, aliasname);
400 }
401
402 // ***** declareFlag *****
403
404 template <class T>
405 XBT_PUBLIC void declareFlag(const char* name, const char* description, T value, std::function<void(const T&)> callback)
406 {
407   if (simgrid_config == nullptr)
408     simgrid_config = xbt_cfg_new();
409   simgrid_config->registerOption<T>(
410     name, description, std::move(value), std::move(callback));
411 }
412
413 template XBT_PUBLIC void declareFlag(const char* name, const char* description, int value,
414                                      std::function<void(int const&)> callback);
415 template XBT_PUBLIC void declareFlag(const char* name, const char* description, double value,
416                                      std::function<void(double const&)> callback);
417 template XBT_PUBLIC void declareFlag(const char* name, const char* description, bool value,
418                                      std::function<void(bool const&)> callback);
419 template XBT_PUBLIC void declareFlag(const char* name, const char* description, std::string value,
420                                      std::function<void(std::string const&)> callback);
421 }
422 }
423
424 // ***** C bindings *****
425
426 xbt_cfg_t xbt_cfg_new()
427 {
428   return new simgrid::config::Config();
429 }
430 void xbt_cfg_free(xbt_cfg_t * cfg) { delete *cfg; }
431
432 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
433 {
434   cfg->dump(name, indent);
435 }
436
437 /*----[ Registering stuff ]-----------------------------------------------*/
438
439 void xbt_cfg_register_double(const char *name, double default_value,
440   xbt_cfg_cb_t cb_set, const char *desc)
441 {
442   if (simgrid_config == nullptr)
443     simgrid_config = xbt_cfg_new();
444   simgrid_config->registerOption<double>(name, desc, default_value, cb_set);
445 }
446
447 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc)
448 {
449   if (simgrid_config == nullptr)
450     simgrid_config = xbt_cfg_new();
451   simgrid_config->registerOption<int>(name, desc, default_value, cb_set);
452 }
453
454 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc)
455 {
456   if (simgrid_config == nullptr)
457     simgrid_config = xbt_cfg_new();
458   simgrid_config->registerOption<std::string>(name, desc, default_value ? default_value : "", cb_set);
459 }
460
461 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc)
462 {
463   if (simgrid_config == nullptr)
464     simgrid_config = xbt_cfg_new();
465   simgrid_config->registerOption<bool>(name, desc, simgrid::config::parseBool(default_value), cb_set);
466 }
467
468 void xbt_cfg_register_alias(const char *realname, const char *aliasname)
469 {
470   if (simgrid_config == nullptr)
471     simgrid_config = xbt_cfg_new();
472   simgrid_config->alias(realname, aliasname);
473 }
474
475 void xbt_cfg_aliases() { simgrid_config->showAliases(); }
476 void xbt_cfg_help()    { simgrid_config->help(); }
477
478 /*----[ Setting ]---------------------------------------------------------*/
479
480 /** @brief Add values parsed from a string into a config set
481  *
482  * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
483  * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
484  * already registered variable, and [value] conforms to the data type under which this variable was registered.
485  *
486  * @todo This is a crude manual parser, it should be a proper lexer.
487  */
488 void xbt_cfg_set_parse(const char *options)
489 {
490   if (not options || not strlen(options)) { /* nothing to do */
491     return;
492   }
493
494   XBT_DEBUG("List to parse and set:'%s'", options);
495   std::string optionlist(options);
496   while (not optionlist.empty()) {
497     XBT_DEBUG("Still to parse and set: '%s'", optionlist.c_str());
498
499     // skip separators
500     size_t pos = optionlist.find_first_not_of(" \t\n,");
501     optionlist.erase(0, pos);
502     // find option
503     pos              = optionlist.find_first_of(" \t\n,");
504     std::string name = optionlist.substr(0, pos);
505     optionlist.erase(0, pos);
506     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name.c_str(), optionlist.c_str());
507
508     if (name.empty())
509       continue;
510
511     pos = name.find(':');
512     xbt_assert(pos != std::string::npos, "Option '%s' badly formatted. Should be of the form 'name:value'",
513                name.c_str());
514
515     std::string val = name.substr(pos + 1);
516     name.erase(pos);
517
518     const std::string path("path");
519     if (name.compare(0, path.length(), path) != 0)
520       XBT_INFO("Configuration change: Set '%s' to '%s'", name.c_str(), val.c_str());
521
522     try {
523       (*simgrid_config)[name.c_str()].setStringValue(val.c_str());
524     }
525     catch (simgrid::config::missing_key_error& e) {
526       goto on_missing_key;
527     }
528     catch (...) {
529       goto on_exception;
530     }
531   }
532   return;
533
534   /* Do not THROWF from a C++ exception catching context, or some cleanups will be missing */
535 on_missing_key:
536   THROWF(not_found_error, 0, "Could not set variables %s", options);
537 on_exception:
538   THROWF(unknown_error, 0, "Could not set variables %s", options);
539 }
540
541 // Horrible mess to translate C++ exceptions to C exceptions:
542 // Exit from the catch block (and do the correct exception cleaning) before attempting to THROWF.
543 #define TRANSLATE_EXCEPTIONS(...) \
544   catch(simgrid::config::missing_key_error& e) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); } \
545   catch(...) { THROWF(not_found_error, 0, __VA_ARGS__); abort(); }
546
547 /** @brief Set the value of a variable, using the string representation of that value
548  *
549  * @param key name of the variable to modify
550  * @param value string representation of the value to set
551  */
552
553 void xbt_cfg_set_as_string(const char *key, const char *value)
554 {
555   try {
556     (*simgrid_config)[key].setStringValue(value);
557     return;
558   }
559   TRANSLATE_EXCEPTIONS("Could not set variable %s as string %s", key, value);
560 }
561
562 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
563  *
564  * This is useful to change the default value of a variable while allowing
565  * users to override it with command line arguments
566  */
567 void xbt_cfg_setdefault_int(const char *key, int value)
568 {
569   try {
570     (*simgrid_config)[key].setDefaultValue<int>(value);
571     return;
572   }
573   TRANSLATE_EXCEPTIONS("Could not set variable %s to default integer %i", key, value);
574 }
575
576 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
577  *
578  * This is useful to change the default value of a variable while allowing
579  * users to override it with command line arguments
580  */
581 void xbt_cfg_setdefault_double(const char *key, double value)
582 {
583   try {
584     (*simgrid_config)[key].setDefaultValue<double>(value);
585     return;
586   }
587   TRANSLATE_EXCEPTIONS("Could not set variable %s to default double %f", key, value);
588 }
589
590 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
591  *
592  * This is useful to change the default value of a variable while allowing
593  * users to override it with command line arguments
594  */
595 void xbt_cfg_setdefault_string(const char *key, const char *value)
596 {
597   try {
598     (*simgrid_config)[key].setDefaultValue<std::string>(value ? value : "");
599     return;
600   }
601   TRANSLATE_EXCEPTIONS("Could not set variable %s to default string %s", key, value);
602 }
603
604 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
605  *
606  * This is useful to change the default value of a variable while allowing
607  * users to override it with command line arguments
608  */
609 void xbt_cfg_setdefault_boolean(const char *key, const char *value)
610 {
611   try {
612     (*simgrid_config)[key].setDefaultValue<bool>(simgrid::config::parseBool(value));
613     return;
614   }
615   TRANSLATE_EXCEPTIONS("Could not set variable %s to default boolean %s", key, value);
616 }
617
618 /** @brief Set an integer value to \a name within \a cfg
619  *
620  * @param key the name of the variable
621  * @param value the value of the variable
622  */
623 void xbt_cfg_set_int(const char *key, int value)
624 {
625   try {
626     (*simgrid_config)[key].setValue<int>(value);
627     return;
628   }
629   TRANSLATE_EXCEPTIONS("Could not set variable %s to integer %i", key, value);
630 }
631
632 /** @brief Set or add a double value to \a name within \a cfg
633  *
634  * @param key the name of the variable
635  * @param value the double to set
636  */
637 void xbt_cfg_set_double(const char *key, double value)
638 {
639   try {
640     (*simgrid_config)[key].setValue<double>(value);
641     return;
642   }
643   TRANSLATE_EXCEPTIONS("Could not set variable %s to double %f", key, value);
644 }
645
646 /** @brief Set or add a string value to \a name within \a cfg
647  *
648  * @param key the name of the variable
649  * @param value the value to be added
650  *
651  */
652 void xbt_cfg_set_string(const char* key, const char* value)
653 {
654   try {
655     (*simgrid_config)[key].setValue<std::string>(value);
656     return;
657   }
658   TRANSLATE_EXCEPTIONS("Could not set variable %s to string %s", key, value);
659 }
660
661 /** @brief Set or add a boolean value to \a name within \a cfg
662  *
663  * @param key the name of the variable
664  * @param value the value of the variable
665  */
666 void xbt_cfg_set_boolean(const char *key, const char *value)
667 {
668   try {
669     (*simgrid_config)[key].setValue<bool>(simgrid::config::parseBool(value));
670     return;
671   }
672   TRANSLATE_EXCEPTIONS("Could not set variable %s to boolean %s", key, value);
673 }
674
675
676 /* Say if the value is the default value */
677 int xbt_cfg_is_default_value(const char *key)
678 {
679   try {
680     return (*simgrid_config)[key].isDefault() ? 1 : 0;
681   }
682   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
683 }
684
685 /*----[ Getting ]---------------------------------------------------------*/
686 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
687  *
688  * @param key the name of the variable
689  *
690  * Returns the first value from the config set under the given name.
691  */
692 int xbt_cfg_get_int(const char *key)
693 {
694   try {
695     return (*simgrid_config)[key].getValue<int>();
696   }
697   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
698 }
699
700 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
701  *
702  * @param key the name of the variable
703  *
704  * Returns the first value from the config set under the given name.
705  */
706 double xbt_cfg_get_double(const char *key)
707 {
708   try {
709     return (*simgrid_config)[key].getValue<double>();
710   }
711   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
712 }
713
714 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
715  *
716  * @param key the name of the variable
717  *
718  * Returns the first value from the config set under the given name.
719  * If there is more than one value, it will issue a warning.
720  * Returns nullptr if there is no value.
721  *
722  * \warning the returned value is the actual content of the config set
723  */
724 std::string xbt_cfg_get_string(const char* key)
725 {
726   try {
727     return (*simgrid_config)[key].getValue<std::string>();
728   }
729   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
730 }
731
732 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
733  *
734  * @param key the name of the variable
735  *
736  * Returns the first value from the config set under the given name.
737  * If there is more than one value, it will issue a warning.
738  */
739 int xbt_cfg_get_boolean(const char *key)
740 {
741   try {
742     return (*simgrid_config)[key].getValue<bool>() ? 1 : 0;
743   }
744   TRANSLATE_EXCEPTIONS("Could not get variable %s", key);
745 }
746
747 #ifdef SIMGRID_TEST
748
749 #include <string>
750
751 #include "xbt.h"
752 #include "xbt/ex.h"
753 #include <xbt/ex.hpp>
754
755 #include <xbt/config.hpp>
756
757 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
758
759 XBT_TEST_SUITE("config", "Configuration support");
760
761 XBT_PUBLIC_DATA xbt_cfg_t simgrid_config;
762
763 static void make_set()
764 {
765   simgrid_config = nullptr;
766   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
767   xbt_cfg_register_int("speed", 0, nullptr, "");
768   xbt_cfg_register_string("peername", "", nullptr, "");
769   xbt_cfg_register_string("user", "", nullptr, "");
770 }                               /* end_of_make_set */
771
772 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
773 {
774   auto temp = simgrid_config;
775   make_set();
776   xbt_test_add("Alloc and free a config set");
777   xbt_cfg_set_parse("peername:veloce user:bidule");
778   xbt_cfg_free(&simgrid_config);
779   simgrid_config = temp;
780 }
781
782 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
783 {
784   auto temp = simgrid_config;
785   make_set();
786   xbt_test_add("Get a single value");
787   {
788     /* get_single_value */
789     xbt_cfg_set_parse("peername:toto:42 speed:42");
790     int ival = xbt_cfg_get_int("speed");
791     if (ival != 42)
792       xbt_test_fail("Speed value = %d, I expected 42", ival);
793   }
794
795   xbt_test_add("Access to a non-existant entry");
796   {
797     try {
798       xbt_cfg_set_parse("color:blue");
799     } catch(xbt_ex& e) {
800       if (e.category != not_found_error)
801         xbt_test_exception(e);
802     }
803   }
804   xbt_cfg_free(&simgrid_config);
805   simgrid_config = temp;
806 }
807
808 XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
809 {
810   auto temp = simgrid_config;
811   make_set();
812   xbt_test_add("C++ declaration of flags");
813
814   simgrid::config::Flag<int> int_flag("int", "", 0);
815   simgrid::config::Flag<std::string> string_flag("string", "", "foo");
816   simgrid::config::Flag<double> double_flag("double", "", 0.32);
817   simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
818   simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
819
820   xbt_test_add("Parse values");
821   xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
822   xbt_test_assert(int_flag == 42, "Check int flag");
823   xbt_test_assert(string_flag == "bar", "Check string flag");
824   xbt_test_assert(double_flag == 8.0, "Check double flag");
825   xbt_test_assert(bool_flag1, "Check bool1 flag");
826   xbt_test_assert(not bool_flag2, "Check bool2 flag");
827
828   xbt_cfg_free(&simgrid_config);
829   simgrid_config = temp;
830 }
831
832 #endif                          /* SIMGRID_TEST */