Logo AND Algorithmique Numérique Distribuée

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