Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take the gras.h from the current dir, not the installed one
[simgrid.git] / include / gras / datadesc.h
1 /* $Id$ */
2
3 /* gras/datadesc.h - Describing the data you want to exchange               */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003, 2004 Martin Quinson.                                 */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #ifndef GRAS_DATADESC_H
12 #define GRAS_DATADESC_H
13
14 #include <stddef.h>    /* offsetof() */
15 #include <sys/types.h>  /* size_t */
16 #include <stdarg.h>
17
18
19 /*! C++ users need love */
20 #ifndef BEGIN_DECL
21 # ifdef __cplusplus
22 #  define BEGIN_DECL extern "C" {
23 # else
24 #  define BEGIN_DECL 
25 # endif
26 #endif
27
28 /*! C++ users need love */
29 #ifndef END_DECL
30 # ifdef __cplusplus
31 #  define END_DECL }
32 # else
33 #  define END_DECL 
34 # endif
35 #endif
36 /* End of cruft for C++ */
37
38 BEGIN_DECL
39
40
41 /**
42  * gras_datadesc_type_t:
43  * 
44  * Opaque type describing a type description you don't want to open.
45  */
46 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
47
48 /* callbacks prototypes */
49 typedef void (*gras_datadesc_type_cb_void_t)(void                 *vars,
50                                              gras_datadesc_type_t *p_type,
51                                              void                 *data);
52 typedef int (*gras_datadesc_type_cb_int_t)(void                 *vars,
53                                            gras_datadesc_type_t *p_type,
54                                            void                 *data);
55
56 /***********************************************
57  **** Search and retrieve declared datatype ****
58  ***********************************************/
59 gras_datadesc_type_t *gras_datadesc_by_name(const char *name);
60
61 /******************************************
62  **** Declare datadescription yourself ****
63  ******************************************/
64
65 gras_error_t 
66 gras_datadesc_declare_struct(const char            *name,
67                              gras_datadesc_type_t **dst);
68 gras_error_t 
69 gras_datadesc_declare_struct_append(gras_datadesc_type_t          *struct_type,
70                                     const char                    *name,
71                                     gras_datadesc_type_t          *field_type);
72 void
73 gras_datadesc_declare_struct_close(gras_datadesc_type_t          *struct_type);
74 gras_error_t 
75 gras_datadesc_declare_union(const char                      *name,
76                             gras_datadesc_type_cb_int_t      selector,
77                             gras_datadesc_type_t           **dst);
78 gras_error_t 
79 gras_datadesc_declare_union_append(gras_datadesc_type_t          *union_type,
80                                    const char                    *name,
81                                    gras_datadesc_type_t          *field_type);
82 void
83 gras_datadesc_declare_union_close(gras_datadesc_type_t          *union_type);
84 gras_error_t 
85 gras_datadesc_declare_ref(const char                      *name,
86                           gras_datadesc_type_t            *referenced_type,
87                           gras_datadesc_type_t           **dst);
88 gras_error_t
89 gras_datadesc_declare_ref_generic(const char                   *name,
90                                   gras_datadesc_type_cb_int_t   discriminant,
91                                   gras_datadesc_type_t        **dst);
92 gras_error_t 
93 gras_datadesc_declare_array_fixed(const char                    *name,
94                                   gras_datadesc_type_t          *element_type,
95                                   long int                       fixed_size,
96                                   gras_datadesc_type_t         **dst);
97 gras_error_t 
98 gras_datadesc_declare_array_dyn(const char                      *name,
99                                 gras_datadesc_type_t            *element_type,
100                                 gras_datadesc_type_cb_int_t      dynamic_size,
101                                 gras_datadesc_type_t           **dst);
102
103 /*********************************
104  * Change stuff within datadescs *
105  *********************************/
106
107 typedef struct s_gras_dd_cbps gras_dd_cbps_t;
108 void gras_datadesc_cb_set_pre (gras_datadesc_type_t    *type,
109                                gras_datadesc_type_cb_void_t  pre);
110 void gras_datadesc_cb_set_post(gras_datadesc_type_t    *type,
111                                gras_datadesc_type_cb_void_t  post);
112
113 /******************************
114  * Get stuff within datadescs *
115  ******************************/
116 char * gras_datadesc_get_name(gras_datadesc_type_t *ddt);
117 int gras_datadesc_get_id(gras_datadesc_type_t *ddt);
118
119 /********************************************************
120  * Advanced data describing: callback persistent states *
121  ********************************************************/
122
123 gras_error_t
124 gras_dd_cbps_pop (gras_dd_cbps_t        *ps, 
125                   const char            *name,
126                   gras_datadesc_type_t **ddt,
127                   void                 **res);
128 gras_error_t
129 gras_dd_cbps_push(gras_dd_cbps_t        *ps,
130                   const char            *name,
131                   void                  *data,
132                   gras_datadesc_type_t  *ddt);
133 void
134 gras_dd_cbps_set (gras_dd_cbps_t        *ps,
135                   const char            *name,
136                   void                  *data,
137                   gras_datadesc_type_t  *ddt);
138
139 void *
140 gras_dd_cbps_get (gras_dd_cbps_t        *ps, 
141                   const char            *name,
142                   gras_datadesc_type_t **ddt);
143
144 void
145 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps);
146 void
147 gras_dd_cbps_block_end(gras_dd_cbps_t *ps);
148
149
150
151
152 /*******************************
153  **** About data convertion ****
154  *******************************/
155 int gras_arch_selfid(void); /* ID of this arch */
156
157 /****************************
158  **** Parse C statements ****
159  ****************************/
160 gras_datadesc_type_t *
161 gras_datadesc_parse(const char *name,
162                     const char *Cdefinition);
163 #define GRAS_DEFINE_TYPE(name,def) \
164   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
165  
166 #define gras_datadesc_by_symbol(name)  \
167   (gras_datadesc_by_name(#name) ?      \
168    gras_datadesc_by_name(#name) :      \
169      gras_datadesc_parse(#name,        \
170                          _gras_this_type_symbol_does_not_exist__##name) \
171   )
172
173 /*****************************
174  **** NWS datadescription ****
175  *****************************/
176
177 /**
178  * Basic types we can embeed in DataDescriptors.
179  */
180 typedef enum
181   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
182    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
183   DataTypes;
184 #define SIMPLE_TYPE_COUNT 9
185
186 /*!  \brief Describe a collection of data.
187  * 
188 ** A description of a collection of #type# data.  #repetitions# is used only
189 ** for arrays; it contains the number of elements.  #offset# is used only for
190 ** struct members in host format; it contains the offset of the member from the
191 ** beginning of the struct, taking into account internal padding added by the
192 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
193 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
194 ** the members of the nested struct, and #tailPadding# indicates how many
195 ** padding bytes the compiler adds to the end of the structure.
196 */
197
198 typedef struct DataDescriptorStruct {
199   DataTypes type;
200   size_t repetitions;
201   size_t offset;
202   /*@null@*/ struct DataDescriptorStruct *members;
203   size_t length;
204   size_t tailPadding;
205 } DataDescriptor;
206 /** DataDescriptor for an array */
207 #define SIMPLE_DATA(type,repetitions) \
208   {type, repetitions, 0, NULL, 0, 0}
209 /** DataDescriptor for an structure member */
210 #define SIMPLE_MEMBER(type,repetitions,offset) \
211   {type, repetitions, offset, NULL, 0, 0}
212 /** DataDescriptor for padding bytes */
213 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
214   sizeof(structType) - offsetof(structType, lastMember) - \
215   sizeof(memberType) * repetitions
216
217 gras_error_t
218 gras_datadesc_import_nws(const char           *name,
219                          const DataDescriptor *desc,
220                          size_t                howmany,
221                          gras_datadesc_type_t **dst);
222
223 END_DECL
224
225 #endif /* GRAS_DATADESC_H */