Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fa317a41e40daca2db0f939b05869fed9f6aae74
[simgrid.git] / include / xbt / config.h
1 /* $Id$ */
2
3 /* config - Dictionary where the type of each cell is provided.            */
4
5 /* This is useful to build named structs, like option or property sets.     */
6
7 /* Copyright (c) 2001,2002,2003,2004 Martin Quinson. All rights reserved.   */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #ifndef _XBT_CONFIG_H_
13 #define _XBT_CONFIG_H_
14
15 #include "xbt/dynar.h"
16
17 BEGIN_DECL()
18
19 /** @addtogroup XBT_config
20  * 
21  *  All modules of the SimGrid toolkit can be configured with this API. 
22  *  User modules and libraries can also use these facilities to handle 
23  *  their own configuration.
24  *
25  *  A configuration set contain several \e variables which have a unique name
26  *  in the set and can take a given type of value. For example, it may 
27  *  contain a \a size variable, accepting \e int values. 
28  *
29  *  It is impossible to set a value to a variable which has not been registered before.
30  *  Usually, the module registers all the options it accepts in the configuration set,
31  *  during its initialization and user code then set and unset values.
32  *
33  *  The easiest way to register a variable is to use the xbt_str_register_str function, 
34  *  which accepts a string representation of the config element descriptor. The syntax 
35  *  is the following: \verbatim <name>:<min nb>_to_<max nb>_<type>\endverbatim
36  *
37  *  For example, <tt>size:1_to_1_int</tt> describes a variable called \e size which 
38  *  must take exactly one value, and the value being an integer. Set the maximum to 0 to 
39  *  disable the upper bound on data count.
40  *
41  *  Another example could be <tt>outputfiles:0_to_10_string</tt> which describes a variable
42  *  called \e outputfiles and which can take between 0 and 10 strings as value.
43  *
44  *  To some extend, configuration sets can be seen as typed hash structures.
45  * 
46  *  \todo This great mechanism is not used in SimGrid yet...
47  *
48  *  \section XBT_cfg_ex Example
49  *
50  *  \dontinclude config_usage.c
51  *
52  *  First, let's create a configuration set with some registered variables.
53  *  This must be done by the configurable library before the user interactions.
54  *
55  *  \skip make_set
56  *  \until end_of_make_set
57  *
58  *  Now, set and get a single value
59  *  \skip get_single_value
60  *  \skip int
61  *  \until cfg_free
62  *
63  *  And now, set and get a multiple value
64  *  \skip get_multiple_value
65  *  \skip dyn
66  *  \until cfg_free
67  *
68  *  All those functions throws mismatch_error if asked to deal with an 
69  *  unregistered variable.
70  *  \skip myset
71  *  \until cfg_free
72  *  @{
73  */
74
75 /** @name 1. Type declaration and memory management
76  *
77  *  
78  *
79  *  @{
80  */
81   /** @brief Configuration set are only special dynars. But don't rely on it, it may change. */
82   typedef xbt_dynar_t xbt_cfg_t;
83
84   /** @brief possible content of each configuration cell */
85   typedef enum {
86     xbt_cfgelm_int=0,  /**< int */
87     xbt_cfgelm_double, /**< double */
88     xbt_cfgelm_string, /**< char* */
89     xbt_cfgelm_host,   /**< both a char* (representing the hostname) and an integer (representing the port) */
90     
91     xbt_cfgelm_any,    /* not shown to users to prevent errors */
92     xbt_cfgelm_type_count 
93   } e_xbt_cfgelm_type_t;
94   
95   /** \brief Callback types. They get the name of the modified entry, and the position of the changed value */
96   typedef void (*xbt_cfg_cb_t)(const char*, int);
97
98   xbt_cfg_t xbt_cfg_new (void);
99   void xbt_cfg_cpy(xbt_cfg_t tocopy, /* OUT */ xbt_cfg_t *whereto);
100   void xbt_cfg_free(xbt_cfg_t *cfg);
101   void xbt_cfg_dump(const char *name,const char*indent,xbt_cfg_t cfg);
102
103  /** @} */
104
105 /** @name 2. User interface: changing values
106  *
107  * This is the only interface you should use unless you want to let your 
108  * own code become configurable with this.
109  *
110  * If the variable accept at most one value, those functions replace the 
111  * current value with the provided one. If max>1, the provided value is 
112  * appended to the list.
113  *
114  * string values are strdup'ed before use, so you can (and should) free
115  * your copy  
116  *
117  * @{
118  */
119
120   void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...);
121   void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa);
122   void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options);
123
124
125 /*
126   Set the value of the cell \a name in \a cfg with the provided value.
127  */
128 void xbt_cfg_set_int   (xbt_cfg_t cfg, const char *name, 
129                         int val);
130 void xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, 
131                         double val);
132 void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, 
133                         const char *val);
134 void xbt_cfg_set_host  (xbt_cfg_t cfg, const char *name, 
135                         const char *host,int port);
136
137 /*
138  Remove the provided value from the cell @name in @cfg.
139  */
140 void xbt_cfg_rm_int   (xbt_cfg_t cfg, const char *name, 
141                        int val);
142 void xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, 
143                        double val);
144 void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, 
145                        const char *val);
146 void xbt_cfg_rm_host  (xbt_cfg_t cfg, const char *name, 
147                        const char *host,int port);
148                                   
149 /*
150  Remove the value at position \e pos from the config \e cfg
151  */
152 void xbt_cfg_rm_at   (xbt_cfg_t cfg, const char *name, int pos);
153
154 /* rm every values */
155 void xbt_cfg_empty(xbt_cfg_t cfg, const char *name);    
156
157 /* @} */
158 /** @name 3.  Registering stuff
159  *
160  *  This how to add new variables to an existing configuration set. Use it to make your code 
161  *  configurable.
162  *
163  *  @{
164  */
165   void xbt_cfg_register(xbt_cfg_t cfg,
166                         const char *name, e_xbt_cfgelm_type_t type,
167                         int min, int max,
168                         xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm);
169   void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name);
170   void xbt_cfg_register_str(xbt_cfg_t cfg, const char *entry);
171   void xbt_cfg_check(xbt_cfg_t cfg);
172   e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name);
173 /*  @} */
174 /** @name 4. Getting the stored values
175  *
176  * This is how to retrieve the values stored in the configuration set. This is only 
177  * intended to configurable code, naturally.
178  *
179  * Note that those function return a pointer to the values actually stored 
180  * in the set. Do not modify them unless you really know what you're doing. 
181  * Likewise, do not free the strings after use, they are not copy of the data,
182  * but the data themselves.
183  *
184  *  @{
185  */
186
187   int         xbt_cfg_get_int   (xbt_cfg_t cfg, const char *name);
188   double      xbt_cfg_get_double(xbt_cfg_t cfg, const char *name);
189   char*       xbt_cfg_get_string(xbt_cfg_t cfg, const char *name);
190   void        xbt_cfg_get_host  (xbt_cfg_t cfg, const char *name, char  **host, int *port);
191   xbt_dynar_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name);
192
193   int    xbt_cfg_get_int_at   (xbt_cfg_t cfg, const char *name, int pos);
194   double xbt_cfg_get_double_at(xbt_cfg_t cfg, const char *name, int pos);
195   char*  xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos);
196   void   xbt_cfg_get_host_at  (xbt_cfg_t cfg, const char *name, int pos, char  **host, int *port);
197
198 /** @} */
199 /** @} */
200 END_DECL()
201   
202 #endif /* _XBT_CONFIG_H_ */