Logo AND Algorithmique Numérique Distribuée

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