3 /* gras/datadesc.h - Describing the data you want to exchange */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003, 2004 Martin Quinson. */
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. */
11 #ifndef GRAS_DATADESC_H
12 #define GRAS_DATADESC_H
14 #include <stddef.h> /* offsetof() */
15 #include <sys/types.h> /* size_t */
19 /*! C++ users need love */
22 # define BEGIN_DECL extern "C" {
28 /*! C++ users need love */
36 /* End of cruft for C++ */
42 * gras_datadesc_type_t:
44 * Opaque type describing a type description you don't want to open.
46 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
48 typedef struct s_gras_cbps gras_cbps_t;
50 /* callbacks prototypes */
51 typedef void (*gras_datadesc_type_cb_void_t)(gras_cbps_t *vars, void *data);
52 typedef int (*gras_datadesc_type_cb_int_t)(gras_cbps_t *vars, void *data);
53 typedef gras_datadesc_type_t *(*gras_datadesc_selector_t)(gras_cbps_t *vars, void *data);
55 /***********************************************
56 **** Search and retrieve declared datatype ****
57 ***********************************************/
58 gras_datadesc_type_t *gras_datadesc_by_name(const char *name);
60 /******************************************
61 **** Declare datadescription yourself ****
62 ******************************************/
65 gras_datadesc_struct(const char *name,
66 gras_datadesc_type_t **dst);
68 gras_datadesc_struct_append(gras_datadesc_type_t *struct_type,
70 gras_datadesc_type_t *field_type);
72 gras_datadesc_struct_close(gras_datadesc_type_t *struct_type);
74 gras_datadesc_union(const char *name,
75 gras_datadesc_type_cb_int_t selector,
76 gras_datadesc_type_t **dst);
78 gras_datadesc_union_append(gras_datadesc_type_t *union_type,
80 gras_datadesc_type_t *field_type);
82 gras_datadesc_union_close(gras_datadesc_type_t *union_type);
84 gras_datadesc_ref(const char *name,
85 gras_datadesc_type_t *referenced_type,
86 gras_datadesc_type_t **dst);
88 gras_datadesc_ref_generic(const char *name,
89 gras_datadesc_selector_t selector,
90 gras_datadesc_type_t **dst);
92 gras_datadesc_array_fixed(const char *name,
93 gras_datadesc_type_t *element_type,
95 gras_datadesc_type_t **dst);
97 gras_datadesc_array_dyn(const char *name,
98 gras_datadesc_type_t *element_type,
99 gras_datadesc_type_cb_int_t dynamic_size,
100 gras_datadesc_type_t **dst);
102 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
103 gras_datadesc_type_t **dst);
105 /*********************************
106 * Change stuff within datadescs *
107 *********************************/
109 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
110 gras_datadesc_type_cb_void_t pre);
111 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
112 gras_datadesc_type_cb_void_t post);
114 /******************************
115 * Get stuff within datadescs *
116 ******************************/
117 char * gras_datadesc_get_name(gras_datadesc_type_t *ddt);
118 int gras_datadesc_get_id(gras_datadesc_type_t *ddt);
120 /********************************************************
121 * Advanced data describing: callback persistent states *
122 ********************************************************/
123 /* simple one: push/pop sizes of arrays */
125 gras_cbps_i_push(gras_cbps_t *ps, int val);
127 gras_cbps_i_pop(gras_cbps_t *ps);
128 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data);
130 /* complex one: complete variable environment support */
132 gras_cbps_v_pop (gras_cbps_t *ps,
134 gras_datadesc_type_t **ddt,
137 gras_cbps_v_push(gras_cbps_t *ps,
140 gras_datadesc_type_t *ddt);
142 gras_cbps_v_set (gras_cbps_t *ps,
145 gras_datadesc_type_t *ddt);
148 gras_cbps_v_get (gras_cbps_t *ps,
150 gras_datadesc_type_t **ddt);
153 gras_cbps_block_begin(gras_cbps_t *ps);
155 gras_cbps_block_end(gras_cbps_t *ps);
160 /*******************************
161 **** About data convertion ****
162 *******************************/
163 int gras_arch_selfid(void); /* ID of this arch */
165 /****************************
166 **** Parse C statements ****
167 ****************************/
168 gras_datadesc_type_t *
169 gras_datadesc_parse(const char *name,
170 const char *Cdefinition);
171 #define GRAS_DEFINE_TYPE(name,def) \
172 static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
174 #define gras_datadesc_by_symbol(name) \
175 (gras_datadesc_by_name(#name) ? \
176 gras_datadesc_by_name(#name) : \
177 gras_datadesc_parse(#name, \
178 _gras_this_type_symbol_does_not_exist__##name) \
181 /*****************************
182 **** NWS datadescription ****
183 *****************************/
186 * Basic types we can embeed in DataDescriptors.
189 {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
190 UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
192 #define SIMPLE_TYPE_COUNT 9
194 /*! \brief Describe a collection of data.
196 ** A description of a collection of #type# data. #repetitions# is used only
197 ** for arrays; it contains the number of elements. #offset# is used only for
198 ** struct members in host format; it contains the offset of the member from the
199 ** beginning of the struct, taking into account internal padding added by the
200 ** compiler for alignment purposes. #members#, #length#, and #tailPadding# are
201 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
202 ** the members of the nested struct, and #tailPadding# indicates how many
203 ** padding bytes the compiler adds to the end of the structure.
206 typedef struct DataDescriptorStruct {
210 /*@null@*/ struct DataDescriptorStruct *members;
214 /** DataDescriptor for an array */
215 #define SIMPLE_DATA(type,repetitions) \
216 {type, repetitions, 0, NULL, 0, 0}
217 /** DataDescriptor for an structure member */
218 #define SIMPLE_MEMBER(type,repetitions,offset) \
219 {type, repetitions, offset, NULL, 0, 0}
220 /** DataDescriptor for padding bytes */
221 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
222 sizeof(structType) - offsetof(structType, lastMember) - \
223 sizeof(memberType) * repetitions
226 gras_datadesc_import_nws(const char *name,
227 const DataDescriptor *desc,
229 gras_datadesc_type_t **dst);
233 #endif /* GRAS_DATADESC_H */