Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not test for IEEE since we cannot deal with non-IEEE, and more important since...
[simgrid.git] / include / 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 /* datadesc */
42 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
43
44 /* callbacks prototypes */
45 typedef void (*gras_datadesc_type_cb_void_t)(void                 *vars,
46                                              gras_datadesc_type_t *p_type,
47                                              void                 *data);
48 typedef int (*gras_datadesc_type_cb_int_t)(void                 *vars,
49                                            gras_datadesc_type_t *p_type,
50                                            void                 *data);
51
52 /***********************************************
53  **** Search and retrieve declared datatype ****
54  ***********************************************/
55 gras_datadesc_type_t *gras_datadesc_by_name(const char *name);
56
57 /******************************************
58  **** Declare datadescription yourself ****
59  ******************************************/
60
61 gras_error_t 
62 gras_datadesc_declare_struct(const char            *name,
63                              gras_datadesc_type_t **dst);
64 gras_error_t 
65 gras_datadesc_declare_struct_append(gras_datadesc_type_t          *struct_type,
66                                     const char                    *name,
67                                     gras_datadesc_type_t          *field_type);
68 void
69 gras_datadesc_declare_struct_close(gras_datadesc_type_t          *struct_type);
70 gras_error_t 
71 gras_datadesc_declare_union(const char                      *name,
72                             gras_datadesc_type_cb_int_t      selector,
73                             gras_datadesc_type_t           **dst);
74 gras_error_t 
75 gras_datadesc_declare_union_append(gras_datadesc_type_t          *union_type,
76                                    const char                    *name,
77                                    gras_datadesc_type_t          *field_type);
78 void
79 gras_datadesc_declare_union_close(gras_datadesc_type_t          *union_type);
80 gras_error_t 
81 gras_datadesc_declare_ref(const char                      *name,
82                           gras_datadesc_type_t            *referenced_type,
83                           gras_datadesc_type_t           **dst);
84 gras_error_t
85 gras_datadesc_declare_ref_generic(const char                   *name,
86                                   gras_datadesc_type_cb_int_t   discriminant,
87                                   gras_datadesc_type_t        **dst);
88 gras_error_t 
89 gras_datadesc_declare_array_fixed(const char                    *name,
90                                   gras_datadesc_type_t          *element_type,
91                                   long int                       fixed_size,
92                                   gras_datadesc_type_t         **dst);
93 gras_error_t 
94 gras_datadesc_declare_array_dyn(const char                      *name,
95                                 gras_datadesc_type_t            *element_type,
96                                 gras_datadesc_type_cb_int_t      dynamic_size,
97                                 gras_datadesc_type_t           **dst);
98
99 /*********************************
100  * Change stuff within datadescs *
101  *********************************/
102
103 typedef struct s_gras_dd_cbps gras_dd_cbps_t;
104 void gras_datadesc_cb_set_pre (gras_datadesc_type_t    *type,
105                                gras_datadesc_type_cb_void_t  pre);
106 void gras_datadesc_cb_set_post(gras_datadesc_type_t    *type,
107                                gras_datadesc_type_cb_void_t  post);
108
109 /******************************
110  * Get stuff within datadescs *
111  ******************************/
112 char * gras_datadesc_get_name(gras_datadesc_type_t *ddt);
113 int gras_datadesc_get_id(gras_datadesc_type_t *ddt);
114
115 /********************************************************
116  * Advanced data describing: callback persistent states *
117  ********************************************************/
118
119 void *
120 gras_dd_cbps_pop (gras_dd_cbps_t        *ps, 
121                   const char            *name,
122                   gras_datadesc_type_t **ddt);
123 void
124 gras_dd_cbps_push(gras_dd_cbps_t        *ps,
125                   const char            *name,
126                   void                  *data,
127                   gras_datadesc_type_t  *ddt);
128 void
129 gras_dd_cbps_set (gras_dd_cbps_t        *ps,
130                   const char            *name,
131                   void                  *data,
132                   gras_datadesc_type_t  *ddt);
133
134 void *
135 gras_dd_cbps_get (gras_dd_cbps_t        *ps, 
136                   const char            *name,
137                   gras_datadesc_type_t **ddt);
138
139 void
140 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps);
141 void
142 gras_dd_cbps_block_end(gras_dd_cbps_t *ps);
143
144
145
146
147 /*******************************
148  **** About data convertion ****
149  *******************************/
150 int gras_arch_selfid(void); /* ID of this arch */
151
152 /****************************
153  **** Parse C statements ****
154  ****************************/
155 gras_datadesc_type_t *
156 gras_datadesc_parse(const char *name,
157                     const char *Cdefinition);
158 #define GRAS_DEFINE_TYPE(name,def) \
159   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
160  
161 #define gras_datadesc_by_symbol(name)  \
162   (gras_datadesc_by_name(#name) ?      \
163    gras_datadesc_by_name(#name) :      \
164      gras_datadesc_parse(#name,        \
165                          _gras_this_type_symbol_does_not_exist__##name) \
166   )
167
168 /*****************************
169  **** NWS datadescription ****
170  *****************************/
171
172 /**
173  * Basic types we can embeed in DataDescriptors.
174  */
175 typedef enum
176   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
177    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
178   DataTypes;
179 #define SIMPLE_TYPE_COUNT 9
180
181 /*!  \brief Describe a collection of data.
182  * 
183 ** A description of a collection of #type# data.  #repetitions# is used only
184 ** for arrays; it contains the number of elements.  #offset# is used only for
185 ** struct members in host format; it contains the offset of the member from the
186 ** beginning of the struct, taking into account internal padding added by the
187 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
188 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
189 ** the members of the nested struct, and #tailPadding# indicates how many
190 ** padding bytes the compiler adds to the end of the structure.
191 */
192
193 typedef struct DataDescriptorStruct {
194   DataTypes type;
195   size_t repetitions;
196   size_t offset;
197   /*@null@*/ struct DataDescriptorStruct *members;
198   size_t length;
199   size_t tailPadding;
200 } DataDescriptor;
201 /** DataDescriptor for an array */
202 #define SIMPLE_DATA(type,repetitions) \
203   {type, repetitions, 0, NULL, 0, 0}
204 /** DataDescriptor for an structure member */
205 #define SIMPLE_MEMBER(type,repetitions,offset) \
206   {type, repetitions, offset, NULL, 0, 0}
207 /** DataDescriptor for padding bytes */
208 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
209   sizeof(structType) - offsetof(structType, lastMember) - \
210   sizeof(memberType) * repetitions
211
212 gras_error_t
213 gras_datadesc_import_nws(const char           *name,
214                          const DataDescriptor *desc,
215                          size_t                howmany,
216                          gras_datadesc_type_t **dst);
217
218 END_DECL
219
220 #endif /* GRAS_DATADESC_H */