Logo AND Algorithmique Numérique Distribuée

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