3 /* cbps - persistant states for callbacks */
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 da GRAS posse. */
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 #include "DataDesc/datadesc_private.h"
14 gras_datadesc_type_t *type;
18 struct s_gras_dd_cbps {
21 gras_dynar_t *globals;
28 * Declare a new element in the PS, and give it a value. If an element of that name already exists,
29 * it becomes masked by the one given here, and will be seable again only after a pop to remove the
30 * value this push adds.
33 gras_dd_cbps_push(gras_dd_cbps_t *ps,
36 gras_datadesc_type_t *ddt) {
38 gras_dynar_t *p_dynar = NULL;
39 gras_dd_cbps_elm_t *p_var = NULL;
41 gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
44 gras_dynar_new(&p_dynar, sizeof (gras_dd_cbps_elm_t *), NULL);
45 gras_dict_insert(ps->space, name, (void **)p_dynar, NULL);
46 /* FIXME: leaking on dynar. Insert in dict with a way to free it */
49 p_var = calloc(1, sizeof(gras_dd_cbps_elm_t));
53 gras_dynar_push(p_dynar, &p_var);
55 gras_dynar_pop(ps->stack, &p_dynar);
56 gras_dynar_push(p_dynar, &name);
57 gras_dynar_push(ps->stack, &p_dynar);
63 * Retrieve an element from the PS, and remove it from the PS. If it's not present in the current block,
64 * it will fail (with abort) and not search in upper blocks since this denotes a programmation error.
67 gras_dd_cbps_pop (gras_dd_cbps_t *ps,
69 gras_datadesc_type_t **ddt) {
70 gras_dynar_t *p_dynar = NULL;
71 gras_dd_cbps_elm_t *p_elm = NULL;
74 /* FIXME: Error handling */
75 gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
76 gras_dynar_pop(p_dynar, &p_elm);
78 if (!gras_dynar_length(p_dynar)) {
79 gras_dict_remove(ps->space, name);
80 gras_dynar_free_container(p_dynar);
91 gras_dynar_pop(ps->stack, &p_dynar);
93 int l = gras_dynar_length(p_dynar);
98 gras_dynar_get(p_dynar, l, &_name);
99 if (!strcmp(name, _name)) {
100 gras_dynar_remove_at(p_dynar, l, &_name);
106 gras_dynar_push(ps->stack, &p_dynar);
115 * Change the value of an element in the PS. If it's not present in the current block, look in the upper ones.
116 * If it's not present in any of them, look in the globals (FIXME: which no function of this API allows to set).
117 * If not present there neither, the code may segfault (Oli?).
119 * Once a reference to an element of that name is found somewhere in the PS, its value is changed.
122 gras_dd_cbps_set (gras_dd_cbps_t *ps,
125 gras_datadesc_type_t *ddt) {
127 gras_dynar_t *p_dynar = NULL;
128 gras_dd_cbps_elm_t *p_elm = NULL;
130 gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
133 gras_dynar_new(&p_dynar, sizeof (gras_dd_cbps_elm_t *), NULL);
134 gras_dict_insert(ps->space, name, (void **)p_dynar, NULL);
136 p_elm = calloc(1, sizeof(gras_dd_cbps_elm_t));
137 gras_dynar_push(ps->globals, &name);
139 gras_dynar_pop(p_dynar, &p_elm);
145 gras_dynar_push(p_dynar, &p_elm);
152 * Get the value of an element in the PS without modifying it.
153 * If it's not present in the current block, look in the upper ones.
154 * If it's not present in any of them, look in the globals (FIXME: which no function of this API allows to set).
155 * If not present there neither, the code may segfault (Oli?).
158 gras_dd_cbps_get (gras_dd_cbps_t *ps,
160 gras_datadesc_type_t **ddt) {
162 gras_dynar_t *p_dynar = NULL;
163 gras_dd_cbps_elm_t *p_elm = NULL;
165 /* FIXME: Error handling */
166 gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
167 gras_dynar_pop(p_dynar, &p_elm);
168 gras_dynar_push(p_dynar, &p_elm);
179 * gras_dd_cbps_block_begin:
181 * Begins a new block.
183 * Blocks are usefull to remove a whole set of declarations you don't even know.
185 * For example, they constitute a very elegent solution to recursive data structures.
186 * push/pop may be used in some cases for that, but if your recursive data struct contains
187 * other structs needing themselves callbacks, you have to use block_{begin,end} to do the trick.
191 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps) {
193 gras_dynar_t *p_dynar = NULL;
195 gras_dynar_new(&p_dynar, sizeof (char *), NULL);
196 gras_dynar_push(ps->stack, &p_dynar);
200 * gras_dd_cbps_block_begin:
202 * End the current block, and go back to the upper one.
205 gras_dd_cbps_block_end(gras_dd_cbps_t *ps) {
207 gras_dynar_t *p_dynar = NULL;
211 gras_dynar_pop(ps->stack, &p_dynar);
213 gras_dynar_foreach(p_dynar, cursor, name) {
214 /* inline the code of _gs_vars_pop(p_vars, name), which was used only once */
216 gras_dynar_t *p_dynar_elm = NULL;
217 gras_dd_cbps_elm_t *p_elm = NULL;
219 gras_dict_retrieve(ps->space, name, (void **)&p_dynar_elm);
220 gras_dynar_pop(p_dynar_elm, &p_elm);
222 if (!gras_dynar_length(p_dynar_elm)) {
223 gras_dict_remove(ps->space, name);
224 gras_dynar_free_container(p_dynar_elm);
231 gras_dynar_free_container(p_dynar);