aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-marshal-utils.c
blob: a743f155d5163f3a79d6ad138e0650387899efb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-marshal-utils.c : marshal utils */

/* 
 *
 * Author : 
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 *
 * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */




#include "config.h"
#include "camel-marshal-utils.h"
#include "camel-arg-collector.c"


#define NB_OP_CHUNKS 20
static GMemChunk *op_chunk=NULL;
static GStaticMutex op_chunk_mutex = G_STATIC_MUTEX_INIT;

CamelFuncDef *
camel_func_def_new (CamelMarshal marshal, guint n_params, ...)
{   
    CamelFuncDef *func_def;
    va_list args;
    GtkType type;
    int i;
    
    func_def = g_new (CamelFuncDef, 1);
    func_def->marshal = marshal;
    func_def->n_params = n_params;
    func_def->params_type = g_new (GtkType, n_params);

    va_start (args, n_params);  
    for (i=0; i<n_params; i++) {
        type = va_arg (args, GtkType);
        func_def->params_type [i] = type; 
    }
    va_end (args);

    return func_def;
}




static gboolean
_collect_params (GtkArg *params,
         CamelFuncDef *func_def,
         va_list var_args)
{
  int i;
  gboolean failed = FALSE;
  

  for (i=0; 
       i<func_def->n_params; 
       i++, params++)
    {
      gchar *error;

      params->name = NULL;
      params->type = (func_def->params_type) [i];
      CAMEL_ARG_COLLECT_VALUE (params,
                   var_args,
                   error);
      if (error)
    {
      failed = TRUE;
      g_free (error);
    }
    }
  return (failed);
}



/**
 * camel_marshal_create_op: create an operation 
 * @func_def: function definition object
 * @func: function to call
 * 
 * create a function ready to be executed. The 
 * vari
 * 
 * 
 * Return value: operation ready to be executed
 **/
CamelOp *
camel_marshal_create_op (CamelFuncDef *func_def, CamelFunc func, ...)
{
    gboolean error;
    CamelOp *op;
    va_list args;

    g_assert (func_def);

    op = camel_op_new (func_def);
    op->func = func;

    va_start (args, func);
    error = _collect_params (op->params, func_def, args);
    va_end (args);
     
    if (error) {
        camel_op_free (op);
        return NULL;
    } else 
        return (op);
}




/**
 * camel_op_new: return a new CamelOp object 
 * 
 * The obtained object must be destroyed with 
 * camel_op_free ()
 * 
 * Return value: the newly allocated CamelOp object
 **/
CamelOp *
camel_op_new (CamelFuncDef *func_def)
{
    CamelOp *op;

    g_static_mutex_lock (&op_chunk_mutex);
    if (!op_chunk)
        op_chunk = g_mem_chunk_create (CamelOp, 
                           NB_OP_CHUNKS,
                           G_ALLOC_AND_FREE);
    g_static_mutex_unlock (&op_chunk_mutex);

    op = g_chunk_new (CamelOp, op_chunk);
    op->func_def = func_def;
    op->params = g_new (GtkArg, func_def->n_params);
    
    return op;  
}

/**
 * camel_op_free: free a CamelOp object allocated with camel_op_new
 * @op: CamelOp object to free
 * 
 * Free a CamelOp object allocated with camel_op_new ()
 * this routine won't work with CamelOp objects allocated 
 * with other allocators.
 **/
void 
camel_op_free (CamelOp *op)
{
    g_free (op->params);
    g_chunk_free (op, op_chunk);
}


/**
 * camel_op_run: run an operation 
 * @op: the operation object
 * 
 * run an operation 
 * 
 **/
void
camel_op_run (CamelOp *op)
{
    g_assert (op);
    g_assert (op->func_def);
    g_assert (op->params);

    op->func_def->marshal (op->func, op->params);
}




/**
 * camel_op_set_user_data: set the private field
 * @op: operation 
 * @user_data: private field
 * 
 * associate a field to an operation object
 **/
void 
camel_op_set_user_data (CamelOp *op, gpointer user_data)
{
    g_assert (op);
    op->user_data = user_data;
}


/**
 * camel_op_get_user_data: return the private field
 * @op: operation object
 * 
 * return the private field associated to 
 * an operation object.
 * 
 * Return value: 
 **/
gpointer 
camel_op_get_user_data (CamelOp *op)
{
    g_assert (op);
    return op->user_data;
}



/* misc marshaller */


typedef void (*CamelMarshal_NONE__POINTER_INT) (gpointer arg1,
                        gint arg2);
void camel_marshal_NONE__POINTER_INT (CamelFunc func, 
                      GtkArg *args)
{
    CamelMarshal_NONE__POINTER_INT rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_INT) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_INT(args[1]));
}





typedef void (*CamelMarshal_NONE__POINTER_INT_POINTER) (gpointer arg1,
                            gint arg2,
                            gpointer arg3);
void camel_marshal_NONE__POINTER_INT_POINTER (CamelFunc func, 
                          GtkArg *args)
{
    CamelMarshal_NONE__POINTER_INT_POINTER rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_INT_POINTER) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_INT(args[1]),
           GTK_VALUE_POINTER(args[2]));
}


typedef void (*CamelMarshal_NONE__POINTER_BOOL_POINTER) (gpointer arg1,
                             gboolean arg2,
                             gpointer arg3);
void camel_marshal_NONE__POINTER_BOOL_POINTER (CamelFunc func, 
                          GtkArg *args)
{
    CamelMarshal_NONE__POINTER_BOOL_POINTER rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_BOOL_POINTER) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_BOOL(args[1]),
           GTK_VALUE_POINTER(args[2]));
}


typedef void (*CamelMarshal_NONE__POINTER_INT_POINTER_POINTER) (gpointer arg1,
                                gint arg2,
                                gpointer arg3,
                                gpointer arg4);
void camel_marshal_NONE__POINTER_INT_POINTER_POINTER (CamelFunc func, 
                              GtkArg *args)
{
    CamelMarshal_NONE__POINTER_INT_POINTER_POINTER rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_INT_POINTER_POINTER) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_INT(args[1]),
           GTK_VALUE_POINTER(args[2]),
           GTK_VALUE_POINTER(args[3]));
}



typedef void (*CamelMarshal_NONE__POINTER_BOOL_POINTER_POINTER) (gpointer arg1,
                                 gboolean arg2,
                                 gpointer arg3,
                                 gpointer arg4);
void camel_marshal_NONE__POINTER_BOOL_POINTER_POINTER (CamelFunc func, 
                               GtkArg *args)
{
    CamelMarshal_NONE__POINTER_BOOL_POINTER_POINTER rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_BOOL_POINTER_POINTER) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_BOOL(args[1]),
           GTK_VALUE_POINTER(args[2]),
           GTK_VALUE_POINTER(args[3]));
}



typedef void (*CamelMarshal_NONE__POINTER_POINTER_POINTER) (gpointer arg1,
                                gpointer arg2,
                                gpointer arg3);
void camel_marshal_NONE__POINTER_POINTER_POINTER (CamelFunc func, 
                          GtkArg *args)
{
    CamelMarshal_NONE__POINTER_POINTER_POINTER rfunc;

    rfunc = (CamelMarshal_NONE__POINTER_POINTER_POINTER) func;
    (* rfunc) (GTK_VALUE_POINTER(args[0]),
           GTK_VALUE_POINTER(args[1]),
           GTK_VALUE_POINTER(args[2]));
}


typedef void (*CamelMarshal_NONE__INT) (gint arg1);
void camel_marshal_NONE__INT (CamelFunc func, 
                  GtkArg *args)
{
    CamelMarshal_NONE__INT rfunc;

    rfunc = (CamelMarshal_NONE__INT) func;
    (* rfunc) (GTK_VALUE_INT (args[0]));
}