-
-namespace simgrid {
-namespace config {
-
-bool parseBool(const char* value)
-{
- for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
- if (std::strcmp(value, xbt_cfgelm_boolean_values[i].true_val) == 0)
- return true;
- if (std::strcmp(value, xbt_cfgelm_boolean_values[i].false_val) == 0)
- return false;
- }
- throw std::range_error("not a boolean");
-}
-
-double parseDouble(const char* value)
-{
- char* end;
- errno = 0;
- double res = std::strtod(value, &end);
- if (errno == ERANGE)
- throw std::range_error("out of range");
- else if (errno)
- xbt_die("Unexpected errno");
- if (end == value || *end != '\0')
- throw std::range_error("invalid double");
- else
- return res;
-}
-
-long int parseLong(const char* value)
-{
- char* end;
- errno = 0;
- long int res = std::strtol(value, &end, 0);
- if (errno) {
- if (res == LONG_MIN && errno == ERANGE)
- throw std::range_error("underflow");
- else if (res == LONG_MAX && errno == ERANGE)
- throw std::range_error("overflow");
- xbt_die("Unexpected errno");
- }
- if (end == value || *end != '\0')
- throw std::range_error("invalid integer");
- else
- return res;
-}
-
-void declareFlag(const char* name, const char* description,
- std::function<void(const char* value)> callback)
-{
- simgrid::config::ConfigurationElement* e = xbt_cfg_register(
- &simgrid_config, name, description, xbt_cfgelm_string, NULL);
- e->callback = std::move(callback);
-}
-
-}
-}
-
-#ifdef SIMGRID_TEST
-
-#include <string>
-
-#include "xbt.h"
-#include "xbt/ex.h"
-
-#include <xbt/config.hpp>
-
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
-
-XBT_TEST_SUITE("config", "Configuration support");
-
-static xbt_cfg_t make_set()
-{
- xbt_cfg_t set = NULL;
-
- xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
- xbt_cfg_register_str(&set, "speed:int");
- xbt_cfg_register_str(&set, "peername:string");
- xbt_cfg_register_str(&set, "user:string");
-
- return set;
-} /* end_of_make_set */
-
-XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
-
-XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
-{
- simgrid_config = make_set();
- xbt_test_add("Alloc and free a config set");
- xbt_cfg_set_parse("peername:veloce user:bidule");
- xbt_cfg_free(&simgrid_config);
-}
-
-XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
-{
- simgrid_config = make_set();
- xbt_test_add("Get a single value");
- {
- /* get_single_value */
- int ival;
-
- xbt_cfg_set_parse("peername:toto:42 speed:42");
- ival = xbt_cfg_get_int("speed");
- if (ival != 42)
- xbt_test_fail("Speed value = %d, I expected 42", ival);
- }
-
- xbt_test_add("Access to a non-existant entry");
- {
- xbt_ex_t e;
-
- TRY {
- xbt_cfg_set_parse("color:blue");
- } CATCH(e) {
- if (e.category != not_found_error)
- xbt_test_exception(e);
- xbt_ex_free(e);
- }
- }
- xbt_cfg_free(&simgrid_config);
-}
-
-XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
-{
- simgrid_config = make_set();
- xbt_test_add("C++ declaration of flags");
-
- simgrid::config::Flag<int> int_flag("int", "", 0);
- simgrid::config::Flag<std::string> string_flag("string", "", "foo");
- simgrid::config::Flag<double> double_flag("double", "", 0.32);
- simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
- simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
-
- xbt_test_add("Parse values");
- xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
- xbt_test_assert(int_flag == 42, "Check int flag");
- xbt_test_assert(string_flag == "bar", "Check string flag");
- xbt_test_assert(double_flag == 8.0, "Check double flag");
- xbt_test_assert(bool_flag1, "Check bool1 flag");
- xbt_test_assert(!bool_flag2, "Check bool2 flag");
-
- xbt_cfg_free(&simgrid_config);
-}
-
-#endif /* SIMGRID_TEST */