Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless #includes
[simgrid.git] / src / xbt / config.c
index 1890645..80b4802 100644 (file)
@@ -21,8 +21,7 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
 
-xbt_cfg_t simgrid_config = NULL;
-static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set);
+XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL;
 
 /* xbt_cfgelm_t: the typedef corresponding to a config variable. */
 
@@ -37,6 +36,11 @@ typedef struct {
   /* Callbacks */
   xbt_cfg_cb_t cb_set;
 
+  /* Advanced callbacks */
+  xbt_cfg_cb_ext_t cb_set_ext;
+  void* cb_set_data;
+  xbt_cfg_cb_free_t cb_set_free;
+
   /* actual content (could be an union or something) */
   xbt_dynar_t content;
 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
@@ -67,27 +71,6 @@ xbt_cfg_t xbt_cfg_new(void)
   return (xbt_cfg_t) xbt_dict_new_homogeneous(&xbt_cfgelm_free);
 }
 
-/** \brief Copy an existing configuration set
- *
- * @param whereto the config set to be created
- * @param tocopy the source data
- *
- * This only copy the registrations, not the actual content
- */
-void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
-{
-  xbt_dict_cursor_t cursor = NULL;
-  xbt_cfgelm_t variable = NULL;
-  char *name = NULL;
-
-  XBT_DEBUG("Copy cfg set %p", tocopy);
-  *whereto = NULL;
-  xbt_assert(tocopy, "cannot copy NULL config");
-
-  xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable)
-    xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->cb_set);
-}
-
 /** @brief Destructor */
 void xbt_cfg_free(xbt_cfg_t * cfg)
 {
@@ -120,7 +103,7 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
     printf("%s  %s:", indent, key);
 
     size = xbt_dynar_length(variable->content);
-    printf ("%s. Actual size=%d. postset=%p, List of values:\n",
+    printf ("%s. Actual size=%d. postset=%p\n",
             xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
 
     switch (variable->type) {
@@ -174,6 +157,8 @@ void xbt_cfgelm_free(void *data)
   XBT_DEBUG("Frees cfgelm %p", c);
   if (!c)
     return;
+  if (c->cb_set_free)
+    c->cb_set_free(c->cb_set_data);
   xbt_free(c->desc);
   if (c->type != xbt_cfgelm_alias)
     xbt_dynar_free(&(c->content));
@@ -189,7 +174,10 @@ void xbt_cfgelm_free(void *data)
  *  @param type the type of the config element
  *  @param cb_set callback function called when a value is set
  */
-static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
+static void xbt_cfg_register(
+  xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type,
+  xbt_cfg_cb_t cb_set,
+  xbt_cfg_cb_ext_t cb_set_ext, void* cb_set_data, xbt_cfg_cb_free_t cb_set_free)
 {
   if (*cfg == NULL)
     *cfg = xbt_cfg_new();
@@ -207,6 +195,9 @@ static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc
   res->desc = xbt_strdup(desc);
   res->type = type;
   res->cb_set = cb_set;
+  res->cb_set_ext = cb_set_ext;
+  res->cb_set_data = cb_set_data;
+  res->cb_set_free = cb_set_free;
   res->isdefault = 1;
 
   switch (type) {
@@ -229,23 +220,40 @@ static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc
   xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
 }
 
-void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set);
+
+
+void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc){
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set, NULL, NULL, NULL);
   xbt_cfg_setdefault_double(name, default_value);
 }
-void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set);
+void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc) {
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set, NULL, NULL, NULL);
   xbt_cfg_setdefault_int(name, default_value);
 }
-void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
+void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc){
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set, NULL, NULL, NULL);
   xbt_cfg_setdefault_string(name, default_value);
 }
-void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
+void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc){
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set, NULL, NULL, NULL);
   xbt_cfg_setdefault_boolean(name, default_value);
 }
 
+/** Register a config with an extended callback
+ *
+ *  @param name      Name of the flag
+ *  @param desc      Description of the flag
+ *  @param type      Type of the flag
+ *  @param cb        Extended callback
+ *  @param data      Data associated with the callback
+ *  @param data_free Function used to free the callback data (or NULL)
+ */
+void xbt_cfg_register_ext(const char *name, const char *desc, e_xbt_cfgelm_type_t type,
+  xbt_cfg_cb_ext_t cb, void* data, xbt_cfg_cb_free_t data_free)
+{
+  xbt_cfg_register(&simgrid_config, name, desc, type, NULL, cb, data, data_free);
+}
+
 void xbt_cfg_register_alias(const char *newname, const char *oldname)
 {
   if (simgrid_config == NULL)
@@ -296,7 +304,7 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
   xbt_assert(type < xbt_cfgelm_type_count,
       "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
 
-  xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
+  xbt_cfg_register(cfg, entrycpy, NULL, type, NULL, NULL, NULL, NULL);
 
   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
 }
@@ -683,9 +691,8 @@ void xbt_cfg_setdefault_boolean(const char *name, const char *val)
     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
 }
 
-/** @brief Set or add an integer value to \a name within \a cfg
+/** @brief Set an integer value to \a name within \a cfg
  *
- * @param cfg the config set
  * @param name the name of the variable
  * @param val the value of the variable
  */
@@ -696,15 +703,16 @@ void xbt_cfg_set_int(const char *name, int val)
   xbt_dynar_set(variable->content, 0, &val);
 
   if (variable->cb_set)
-    variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
+    variable->cb_set(name);
+  if (variable->cb_set_ext)
+    variable->cb_set_ext(name, variable->cb_set_data);
   variable->isdefault = 0;
 }
 
 /** @brief Set or add a double value to \a name within \a cfg
  *
- * @param cfg the config set
  * @param name the name of the variable
- * @param val the doule to set
+ * @param val the double to set
  */
 void xbt_cfg_set_double(const char *name, double val)
 {
@@ -713,7 +721,9 @@ void xbt_cfg_set_double(const char *name, double val)
   xbt_dynar_set(variable->content, 0, &val);
 
   if (variable->cb_set)
-    variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
+    variable->cb_set(name);
+  if (variable->cb_set_ext)
+    variable->cb_set_ext(name, variable->cb_set_data);
   variable->isdefault = 0;
 }
 
@@ -737,7 +747,9 @@ void xbt_cfg_set_string(const char *name, const char *val)
   xbt_dynar_set(variable->content, 0, &newval);
 
   if (variable->cb_set)
-    variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
+    variable->cb_set(name);
+  if (variable->cb_set_ext)
+    variable->cb_set_ext(name, variable->cb_set_data);
   variable->isdefault = 0;
 }
 
@@ -748,10 +760,10 @@ void xbt_cfg_set_string(const char *name, const char *val)
  */
 void xbt_cfg_set_boolean(const char *name, const char *val)
 {
-  int i, bval;
+  int bval=-1;
   xbt_cfgelm_t variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
 
-  for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
+  for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
     if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
       bval = 1;
       break;
@@ -761,14 +773,13 @@ void xbt_cfg_set_boolean(const char *name, const char *val)
       break;
     }
   }
-  if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
-    xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
-  }
-
+  xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
   xbt_dynar_set(variable->content, 0, &bval);
 
   if (variable->cb_set)
-    variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
+    variable->cb_set(name);
+  if (variable->cb_set_ext)
+    variable->cb_set_ext(name, variable->cb_set_data);
   variable->isdefault = 0;
 }
 
@@ -865,61 +876,6 @@ int xbt_cfg_get_boolean(const char *name)
   return xbt_dynar_get_as(variable->content, 0, int);
 }
 
-/** @brief Retrieve the dynar of all the values stored in a variable
- *
- * @param cfg where to search in
- * @param name what to search for
- *
- * Get the data stored in the config set.
- *
- * \warning the returned value is the actual content of the config set
- */
-xbt_dynar_t xbt_cfg_get_dynar(const char *name)
-{
-  xbt_cfgelm_t variable = NULL;
-  xbt_ex_t e;
-
-  TRY {
-    variable = xbt_dict_get((xbt_dict_t) simgrid_config, name);
-  } CATCH(e) {
-    if (e.category == not_found_error) {
-      xbt_ex_free(e);
-      THROWF(not_found_error, 0, "No registered variable %s in this config set", name);
-    }
-    RETHROW;
-  }
-
-  return variable->content;
-}
-
-/** @brief Retrieve one of the integer value of a variable */
-int xbt_cfg_get_int_at(xbt_cfg_t cfg, const char *name, int pos)
-{
-  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_int);
-  return xbt_dynar_get_as(variable->content, pos, int);
-}
-
-/** @brief Retrieve one of the double value of a variable */
-double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos)
-{
-  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_double);
-  return xbt_dynar_get_as(variable->content, pos, double);
-}
-
-/** @brief Retrieve one of the string value of a variable */
-char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
-{
-  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_string);
-  return xbt_dynar_get_as(variable->content, pos, char *);
-}
-
-/** @brief Retrieve one of the boolean value of a variable */
-int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
-{
-  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
-  return xbt_dynar_get_as(variable->content, pos, int);
-}
-
 #ifdef SIMGRID_TEST
 #include "xbt.h"
 #include "xbt/ex.h"