Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change the prototype of datadesc_get_name to return a const char*
[simgrid.git] / include / gras / datadesc.h
1 /* $Id$ */
2
3 /* gras/datadesc.h - Describing the data you want to exchange               */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef GRAS_DATADESC_H
11 #define GRAS_DATADESC_H
12
13 #include "xbt/misc.h" /* BEGIN_DECL */
14 #include "xbt/dynar.h" /* void_f_pvoid_t */
15
16 BEGIN_DECL()
17
18 /** @addtogroup GRAS_dd Data description
19  *  @brief Describing data to be exchanged (Communication facility)
20  *
21  * @section Overview
22  *
23  * Since GRAS takes care of potential representation conversion when the platform is heterogeneous, 
24  * any data which transits on the network must be described beforehand.
25  * 
26  * There is several possible interfaces for this, ranging from the really completely automatic parsing to 
27  * completely manual. Let's study each of them from the simplest to the more advanced.
28  * 
29  * \warning At least, I would like to present those sections in the right order, but doxygen prevents me 
30  * from doing so. There is a weird bug I fail to circumvent here. The right order is naturally:
31  *   -# basic operations
32  *   -# Automatic parsing
33  *   -# Simple manual definitions
34  *   -# Callback Persistant State: Simple push/pop mechanism
35  *   -# Callback Persistant State: Full featured mechanism
36  */
37 /* @{*/
38
39 /** @name 1. basic operations
40  *
41  * If you only want to send pre-existing types, simply retrieve the pre-defined description with 
42  * the \ref gras_datadesc_by_name function. Existing types entail:
43  *  - char (both signed and unsigned)
44  *  - int (short, regular, long and long long, both signed and unsigned)
45  *  - float and double
46  *  - string (which is indeed a reference to a dynamically sized array of char, strlen being used to retrive the size)
47  * 
48  * Example:\verbatim gras_datadesc_type_t i = gras_datadesc_by_name("int");
49  gras_datadesc_type_t uc = gras_datadesc_by_name("unsigned char");
50  gras_datadesc_type_t str = gras_datadesc_by_name("string");\endverbatim
51  */
52 /* @{ */
53   
54 /** @brief Opaque type describing a type description. */
55 typedef struct s_gras_datadesc_type *gras_datadesc_type_t;
56
57 /** \brief Search a type description from its name */
58 gras_datadesc_type_t gras_datadesc_by_name(const char *name);
59
60 /* @} */
61     
62 /** @name 2. Automatic parsing
63  * 
64  *  If you need to declare a new datatype, this is the simplest way to describe it to GRAS. Simply
65  *  enclose its type definition  into a \ref GRAS_DEFINE_TYPE macro call, and you're set. Here is 
66  *  an type declaration  example: \verbatim GRAS_DEFINE_TYPE(mytype,struct mytype {
67    int myfirstfield;
68    char mysecondfield;
69  });\endverbatim
70  *  The type is then both copied verbatim into your source file and stored for further parsing. This allows
71  *  you to let GRAS parse the exact version you are actually using in your program.
72  *  You can then retrieve the corresponding type description with \ref gras_datadesc_by_symbol.
73  *  Don't worry too much for the performances, the type is only parsed once and a binary representation 
74  *  is stored and used in any subsequent calls.
75  * 
76  *  If your structure contains any pointer, you have to explain GRAS the size of the pointed array. This
77  *  can be 1 in the case of simple references, or more in the case of regular arrays. For that, use the 
78  *  \ref GRAS_ANNOTE macro within the type declaration you are passing to \ref GRAS_DEFINE_TYPE. This macro
79  *  rewrites itself to nothing in the declaration (so they won't pollute the type definition copied verbatim
80  *  into your code), and give some information to GRAS about your pointer. 
81  
82  *  GRAS_ANNOTE takes two arguments being the key name and the key value. For now, the only accepted key name 
83  *  is "size", to specify the length of the pointed array. It can either be the string "1" (without the quote)
84  *  or the name of another field of the structure.
85  *  
86  *  Here is an example:\verbatim GRAS_DEFINE_TYPE(s_clause,
87   struct s_array {
88     int length;
89     int *data GRAS_ANNOTE(size,length);
90     struct s_array *father GRAS_ANNOTE(size,1);
91  }
92 ;)\endverbatim
93  * It specifies that the structure s_array contains two fields, and that the size of the array pointed 
94  * by \a data is the \a length field, and that the \a father field is a simple reference.
95  * 
96  * If you cannot express your datadescs with this mechanism, you'll have to use the more advanced 
97  * (and somehow complex) one described below.
98  * 
99  *  \warning Since GRAS_DEFINE_TYPE is a macro, you shouldn't  put any comma in your type definition 
100  *  (comma separates macro args). 
101  * 
102  *  For example, change \verbatim int a, b;\endverbatim to \verbatim int a;
103  int b;\endverbatim
104  */
105 /** @{ */
106
107  
108 /**   @brief Automatically parse C code
109  *    @hideinitializer
110  */
111 #define GRAS_DEFINE_TYPE(name,def) \
112   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
113  
114 /** @brief Retrieve a datadesc which was previously parsed 
115  *  @hideinitializer
116  */
117 #define gras_datadesc_by_symbol(name)  \
118   (gras_datadesc_by_name(#name) ?      \
119    gras_datadesc_by_name(#name) :      \
120      gras_datadesc_parse(#name,        \
121                          _gras_this_type_symbol_does_not_exist__##name) \
122   )
123
124 /** @def GRAS_ANNOTE
125  *  @brief Add an annotation to a type to be automatically parsed
126  */
127 #define GRAS_ANNOTE(key,val)
128
129 /* @} */
130
131 gras_datadesc_type_t 
132 gras_datadesc_parse(const char *name, const char *C_statement);
133
134 /** @name 3. Simple manual definitions
135  * 
136  * Here are the functions to use if you want to declare your description manually. 
137  * The function names should be self-explanatory in most cases.
138  * 
139  * You can add callbacks to the datatypes doing any kind of action you may want. Usually, 
140  * pre-send callbacks are used to prepare the type expedition while post-receive callbacks 
141  * are used to fix any issue after the receive.
142  * 
143  * If your types are dynamic, you'll need to add some extra callback. For example, there is a
144  * specific callback for the string type which is in charge of computing the length of the char
145  * array. This is done with the cbps mechanism, explained in next section.
146  * 
147  * If your types may contain pointer cycle, you must specify it to GRAS using the @ref gras_datadesc_cycle_set. 
148  * 
149  * Example:\verbatim
150  typedef struct {
151    unsigned char c1;
152    unsigned long int l1;
153    unsigned char c2;
154    unsigned long int l2;
155  } mystruct;
156  [...]
157   my_type=gras_datadesc_struct("mystruct");
158   gras_datadesc_struct_append(my_type,"c1", gras_datadesc_by_name("unsigned char"));
159   gras_datadesc_struct_append(my_type,"l1", gras_datadesc_by_name("unsigned long"));
160   gras_datadesc_struct_append(my_type,"c2", gras_datadesc_by_name("unsigned char"));
161   gras_datadesc_struct_append(my_type,"l2", gras_datadesc_by_name("unsigned long int"));
162   gras_datadesc_struct_close(my_type);
163
164   my_type=gras_datadesc_ref("mystruct*", gras_datadesc_by_name("mystruct"));
165   
166   [Use my_type to send pointers to mystruct data]\endverbatim
167  */
168 /* @{ */
169
170
171 /** \brief Opaque type describing a type description callback persistant state. */
172 typedef struct s_gras_cbps *gras_cbps_t;
173
174 /* callbacks prototypes */
175 /** \brief Prototype of type callbacks returning nothing. */
176 typedef void (*gras_datadesc_type_cb_void_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
177 /** \brief Prototype of type callbacks returning an int. */
178 typedef int (*gras_datadesc_type_cb_int_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
179 /** \brief Prototype of type callbacks selecting a type. */
180 typedef gras_datadesc_type_t (*gras_datadesc_selector_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
181
182
183 /******************************************
184  **** Declare datadescription yourself ****
185  ******************************************/
186
187 gras_datadesc_type_t gras_datadesc_struct(const char *name);
188 void gras_datadesc_struct_append(gras_datadesc_type_t  struct_type,
189                                  const char           *name,
190                                  gras_datadesc_type_t  field_type);
191 void gras_datadesc_struct_close(gras_datadesc_type_t   struct_type);
192
193
194 gras_datadesc_type_t gras_datadesc_union(const char                 *name,
195                                          gras_datadesc_type_cb_int_t selector);
196 void gras_datadesc_union_append(gras_datadesc_type_t   union_type,
197                                 const char            *name,
198                                 gras_datadesc_type_t   field_type);
199 void gras_datadesc_union_close(gras_datadesc_type_t    union_type);
200
201
202 gras_datadesc_type_t 
203   gras_datadesc_ref(const char          *name,
204                     gras_datadesc_type_t referenced_type);
205 gras_datadesc_type_t 
206   gras_datadesc_ref_generic(const char              *name,
207                             gras_datadesc_selector_t selector);
208
209 gras_datadesc_type_t 
210   gras_datadesc_array_fixed(const char          *name,
211                             gras_datadesc_type_t element_type,
212                             long int             fixed_size);
213 gras_datadesc_type_t 
214   gras_datadesc_array_dyn(const char                 *name,
215                           gras_datadesc_type_t        element_type,
216                           gras_datadesc_type_cb_int_t dynamic_size);
217 gras_datadesc_type_t 
218   gras_datadesc_ref_pop_arr(gras_datadesc_type_t  element_type);
219
220 gras_datadesc_type_t 
221   gras_datadesc_dynar(gras_datadesc_type_t elm_t,
222                       void_f_pvoid_t *free_func);
223
224 /*********************************
225  * Change stuff within datadescs *
226  *********************************/
227
228 /** \brief Specify that this type may contain cycles */
229 void gras_datadesc_cycle_set(gras_datadesc_type_t type);
230 /** \brief Specify that this type do not contain any cycles (default) */
231 void gras_datadesc_cycle_unset(gras_datadesc_type_t type);
232 /** \brief Add a pre-send callback to this datadesc. */
233 void gras_datadesc_cb_send (gras_datadesc_type_t         type,
234                             gras_datadesc_type_cb_void_t pre);
235 /** \brief Add a post-receive callback to this datadesc.*/
236 void gras_datadesc_cb_recv(gras_datadesc_type_t          type,
237                            gras_datadesc_type_cb_void_t  post);
238 /** \brief Add a pre-send callback to the given field of the datadesc */
239 void gras_datadesc_cb_field_send (gras_datadesc_type_t   type,
240                                   const char            *field_name,
241                                   gras_datadesc_type_cb_void_t  pre);
242 /** \brief Add a post-receive callback to the given field of the datadesc */
243 void gras_datadesc_cb_field_recv(gras_datadesc_type_t    type,
244                                  const char             *field_name,
245                                  gras_datadesc_type_cb_void_t  post);
246 /** \brief Add a pre-send callback to the given field resulting in its value to be pushed */
247 void gras_datadesc_cb_field_push (gras_datadesc_type_t   type,
248                                   const char            *field_name);
249
250 /******************************
251  * Get stuff within datadescs *
252  ******************************/
253 /** \brief Returns the name of a datadescription */
254 const char * gras_datadesc_get_name(gras_datadesc_type_t ddt);
255 /** \brief Returns the identifier of a datadescription */
256 int gras_datadesc_get_id(gras_datadesc_type_t ddt);
257
258 /* @} */
259
260 /** @name 4. Callback Persistant State: Simple push/pop mechanism
261  * 
262  * Sometimes, one of the callbacks need to leave information for the next ones. If this is a simple integer (such as
263  * an array size), you can use the functions described here. If not, you'll have to play with the complete cbps interface.
264  * 
265  * Here is an example:\verbatim
266 struct s_array {
267   int length;
268   int *data;
269 }
270 [...]
271 my_type=gras_datadesc_struct("s_array");
272 gras_datadesc_struct_append(my_type,"length", gras_datadesc_by_name("int"));
273 gras_datadesc_cb_field_send (my_type, "length", gras_datadesc_cb_push_int);
274
275 gras_datadesc_struct_append(my_type,"data",
276                             gras_datadesc_array_dyn ("s_array::data",gras_datadesc_by_name("int"), gras_datadesc_cb_pop));
277 gras_datadesc_struct_close(my_type);
278 \endverbatim
279
280  */
281 /* @{ */
282
283 void
284 gras_cbps_i_push(gras_cbps_t ps, int val);
285 int 
286 gras_cbps_i_pop(gras_cbps_t ps);
287
288 int gras_datadesc_cb_pop(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
289 void gras_datadesc_cb_push_int(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
290 void gras_datadesc_cb_push_uint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
291 void gras_datadesc_cb_push_lint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
292 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
293
294
295 /* @} */
296
297 /** @name 5. Callback Persistant State: Full featured mechanism
298  * 
299  * Sometimes, one of the callbacks need to leave information for the next ones. If the simple push/pop mechanism
300  * introduced in previous section isn't enough, you can always use this full featured one.
301  */
302
303 /* @{ */
304
305 xbt_error_t
306   gras_cbps_v_pop (gras_cbps_t            ps, 
307                    const char            *name,
308          /* OUT */ gras_datadesc_type_t  *ddt,
309          /* OUT */ void                 **res);
310 xbt_error_t
311 gras_cbps_v_push(gras_cbps_t            ps,
312                  const char            *name,
313                  void                  *data,
314                  gras_datadesc_type_t   ddt);
315 void
316 gras_cbps_v_set (gras_cbps_t            ps,
317                  const char            *name,
318                  void                  *data,
319                  gras_datadesc_type_t   ddt);
320
321 void *
322 gras_cbps_v_get (gras_cbps_t            ps, 
323                  const char            *name,
324        /* OUT */ gras_datadesc_type_t  *ddt);
325
326 void
327 gras_cbps_block_begin(gras_cbps_t ps);
328 void
329 gras_cbps_block_end(gras_cbps_t ps);
330
331 /* @} */
332 /* @} */
333
334
335 /*******************************
336  **** About data convertion ****
337  *******************************/
338 int gras_arch_selfid(void); /* ID of this arch */
339
340
341 /*****************************
342  **** NWS datadescription * FIXME: obsolete?
343  *****************************/
344
345 /**
346  * Basic types we can embeed in DataDescriptors.
347  */
348 typedef enum
349   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
350    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
351   DataTypes;
352 #define SIMPLE_TYPE_COUNT 9
353
354 /*!  \brief Describe a collection of data.
355  * 
356 ** A description of a collection of #type# data.  #repetitions# is used only
357 ** for arrays; it contains the number of elements.  #offset# is used only for
358 ** struct members in host format; it contains the offset of the member from the
359 ** beginning of the struct, taking into account internal padding added by the
360 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
361 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
362 ** the members of the nested struct, and #tailPadding# indicates how many
363 ** padding bytes the compiler adds to the end of the structure.
364 */
365
366 typedef struct DataDescriptorStruct {
367   DataTypes type;
368   size_t repetitions;
369   size_t offset;
370   /*@null@*/ struct DataDescriptorStruct *members;
371   size_t length;
372   size_t tailPadding;
373 } DataDescriptor;
374 /** DataDescriptor for an array */
375 #define SIMPLE_DATA(type,repetitions) \
376   {type, repetitions, 0, NULL, 0, 0}
377 /** DataDescriptor for an structure member */
378 #define SIMPLE_MEMBER(type,repetitions,offset) \
379   {type, repetitions, offset, NULL, 0, 0}
380 /** DataDescriptor for padding bytes */
381 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
382   sizeof(structType) - offsetof(structType, lastMember) - \
383   sizeof(memberType) * repetitions
384
385 xbt_error_t
386 gras_datadesc_import_nws(const char           *name,
387                          const DataDescriptor *desc,
388                          unsigned long         howmany,
389                /* OUT */ gras_datadesc_type_t *dst);
390
391
392 END_DECL()
393
394 #endif /* GRAS_DATADESC_H */