Logo AND Algorithmique Numérique Distribuée

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