Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to select on socket 0
[simgrid.git] / include / xbt / config.h
1 /* $Id$ */
2
3 /* config - Dictionnary 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 uniq 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  *  Moreover,  of 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.
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 mecanism is not used in SimGrid yet...
47  *
48  *  \todo We need a callback mecanism so that the configurable code get
49  *  notified of configuration changes.
50  *
51  *  \section XBT_cfg_ex Example
52  *
53  *  \dontinclude config_usage.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
78 /** @name 1. Type declaration and memory management
79  *
80  *  
81  *
82  *  @{
83  */
84   /** @brief Configuration set are only special dynars. But don't rely on it, it may change. */
85   typedef xbt_dynar_t xbt_cfg_t;
86
87   /** @brief possible content of each configuration cell */
88   typedef enum {
89     xbt_cfgelm_int=0,  /**< int */
90     xbt_cfgelm_double, /**< double */
91     xbt_cfgelm_string, /**< char* */
92     xbt_cfgelm_host,   /**< both a char* (representing the hostname) and an integer (representing the port) */
93     xbt_cfgelm_type_count 
94   } e_xbt_cfgelm_type_t;
95
96   xbt_cfg_t xbt_cfg_new (void);
97   void xbt_cfg_cpy(xbt_cfg_t tocopy, /* OUT */ xbt_cfg_t *whereto);
98   void xbt_cfg_free(xbt_cfg_t *cfg);
99   void xbt_cfg_dump(const char *name,const char*indent,xbt_cfg_t cfg);
100
101  /** @} */
102
103 /** @name 2. User interface: changing values
104  *
105  * This is the only interface you should use unless you want to let your 
106  * own code become configurable with this.
107  *
108  * If the variable accept at most one value, those functions replace the 
109  * current value with the provided one. If max>1, the provided value is 
110  * appended to the list.
111  *
112  * string values are strdup'ed before use, so you can (and should) free
113  * your copy  
114  *
115  * @{
116  */
117
118   xbt_error_t xbt_cfg_set(xbt_cfg_t cfg, ...);
119   xbt_error_t xbt_cfg_set_vargs(xbt_cfg_t cfg, va_list pa);
120   xbt_error_t xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options);
121
122
123 /*
124   Set the value of the cell \a name in \a cfg with the provided value.
125  */
126 xbt_error_t xbt_cfg_set_int   (xbt_cfg_t cfg, const char *name, 
127                                  int val);
128 xbt_error_t xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, 
129                                  double val);
130 xbt_error_t xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, 
131                                  const char *val);
132 xbt_error_t xbt_cfg_set_host  (xbt_cfg_t cfg, const char *name, 
133                                  const char *host,int port);
134
135 /*
136  Remove the provided value from the cell @name in @cfg.
137  */
138 xbt_error_t xbt_cfg_rm_int   (xbt_cfg_t cfg, const char *name, 
139                                 int val);
140 xbt_error_t xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, 
141                                 double val);
142 xbt_error_t xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, 
143                                 const char *val);
144 xbt_error_t xbt_cfg_rm_host  (xbt_cfg_t cfg, const char *name, 
145                                 const char *host,int port);
146
147 /* rm every values */
148 xbt_error_t xbt_cfg_empty(xbt_cfg_t cfg, const char *name);     
149
150 /* @} */
151 /** @name 3.  Registering stuff
152  *
153  *  This how to add new variables to an existing configuration set. Use it to make your code 
154  *  configurable.
155  *
156  *  @{
157  */
158   void xbt_cfg_register(xbt_cfg_t cfg,
159                         const char *name, e_xbt_cfgelm_type_t type,
160                         int min, int max);
161   xbt_error_t xbt_cfg_unregister(xbt_cfg_t cfg, const char *name);
162   xbt_error_t xbt_cfg_register_str(xbt_cfg_t cfg, const char *entry);
163   xbt_error_t xbt_cfg_check(xbt_cfg_t cfg);
164   xbt_error_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name, 
165                                /* OUT */ e_xbt_cfgelm_type_t *type);
166 /*  @} */
167 /** @name 4. Getting the stored values
168  *
169  * This is how to retrieve the values stored in the configuration set. This is only 
170  * intended to configurable code, naturally.
171  *
172  * Note that those function return a pointer to the values actually stored 
173  * in the set. Do not modify them unless you really know what you're doing.                  
174  *
175  *  @{
176  */
177
178   xbt_error_t xbt_cfg_get_int   (xbt_cfg_t cfg, const char *name, int    *val);
179   xbt_error_t xbt_cfg_get_double(xbt_cfg_t cfg, const char *name, double *val);
180   xbt_error_t xbt_cfg_get_string(xbt_cfg_t cfg, const char *name, char  **val);
181   xbt_error_t xbt_cfg_get_host  (xbt_cfg_t cfg, const char *name, char  **host, int *port);
182   xbt_error_t xbt_cfg_get_dynar (xbt_cfg_t cfg, const char *name, xbt_dynar_t *dynar);
183
184 /** @} */
185 /** @} */
186 END_DECL()
187   
188 #endif /* _XBT_CONFIG_H_ */