pygi-cache.c 41.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
21 22

#include "pygi-info.h"
23
#include "pygi-cache.h"
24 25
#include "pygi-marshal-to-py.h"
#include "pygi-marshal-from-py.h"
26
#include "pygi-marshal-cleanup.h"
27
#include "pygi-type.h"
28
#include <girepository.h>
29

30 31 32 33
PyGIArgCache * _arg_cache_new (GITypeInfo *type_info,
                               PyGICallableCache *callable_cache,
                               GIArgInfo *arg_info,
                               GITransfer transfer,
34
                               PyGIDirection direction,
35 36 37 38 39 40 41
                               gssize c_arg_index,
                               gssize py_arg_index);

PyGIArgCache * _arg_cache_new_for_interface (GIInterfaceInfo *iface_info,
                                             PyGICallableCache *callable_cache,
                                             GIArgInfo *arg_info,
                                             GITransfer transfer,
42
                                             PyGIDirection direction,
43 44
                                             gssize c_arg_index,
                                             gssize py_arg_index);
45

46
/* cleanup */
Martin Pitt's avatar
Martin Pitt committed
47
static void
48
_pygi_arg_cache_free (PyGIArgCache *cache)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
49 50 51 52
{
    if (cache == NULL)
        return;

53
    if (cache->type_info != NULL)
54
        g_base_info_unref ( (GIBaseInfo *)cache->type_info);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
55
    if (cache->destroy_notify)
56
        cache->destroy_notify (cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
57
    else
58
        g_slice_free (PyGIArgCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
59 60 61
}

static void
62
_interface_cache_free_func (PyGIInterfaceCache *cache)
63 64
{
    if (cache != NULL) {
65
        Py_XDECREF (cache->py_type);
66
        if (cache->type_name != NULL)
67
            g_free (cache->type_name);
68
        if (cache->interface_info != NULL)
69 70
            g_base_info_unref ( (GIBaseInfo *)cache->interface_info);
        g_slice_free (PyGIInterfaceCache, cache);
71 72 73
    }
}

John (J5) Palmieri's avatar
John (J5) Palmieri committed
74
static void
75
_hash_cache_free_func (PyGIHashCache *cache)
76
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
77
    if (cache != NULL) {
78 79 80
        _pygi_arg_cache_free (cache->key_cache);
        _pygi_arg_cache_free (cache->value_cache);
        g_slice_free (PyGIHashCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
81
    }
82 83
}

84
static void
85
_sequence_cache_free_func (PyGISequenceCache *cache)
86
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
87
    if (cache != NULL) {
88 89
        _pygi_arg_cache_free (cache->item_cache);
        g_slice_free (PyGISequenceCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
90
    }
91 92
}

93
static void
94
_callback_cache_free_func (PyGICallbackCache *cache)
95
{
96 97
    if (cache != NULL) {
        if (cache->interface_info != NULL)
98
            g_base_info_unref ( (GIBaseInfo *)cache->interface_info);
99

100
        g_slice_free (PyGICallbackCache, cache);
101
    }
102 103 104
}

void
105
_pygi_callable_cache_free (PyGICallableCache *cache)
106
{
107 108 109
    if (cache == NULL)
        return;

110
    g_slist_free (cache->to_py_args);
111 112
    g_slist_free (cache->arg_name_list);
    g_hash_table_destroy (cache->arg_name_hash);
113
    g_ptr_array_unref (cache->args_cache);
114

115
    if (cache->return_cache != NULL)
116
        _pygi_arg_cache_free (cache->return_cache);
117

118
    g_slice_free (PyGICallableCache, cache);
119 120 121 122
}

/* cache generation */

123 124
static PyGIInterfaceCache *
_interface_cache_new (GIInterfaceInfo *iface_info)
125 126
{
    PyGIInterfaceCache *ic;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
127

128 129
    ic = g_slice_new0 (PyGIInterfaceCache);
    ( (PyGIArgCache *)ic)->destroy_notify = (GDestroyNotify)_interface_cache_free_func;
130
    ic->g_type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *)iface_info);
131
    ic->py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) iface_info);
132 133 134 135

    if (ic->py_type == NULL)
        return NULL;

136
    ic->type_name = _pygi_g_base_info_get_fullname (iface_info);
137 138 139
    return ic;
}

140 141 142 143 144
static PyGISequenceCache *
_sequence_cache_new (GITypeInfo *type_info,
                     GIDirection direction,
                     GITransfer transfer,
                     gssize child_offset)
145 146 147
{
    PyGISequenceCache *sc;
    GITypeInfo *item_type_info;
148
    GITransfer item_transfer;
149

150 151
    sc = g_slice_new0 (PyGISequenceCache);
    ( (PyGIArgCache *)sc)->destroy_notify = (GDestroyNotify)_sequence_cache_free_func;
152

153
    sc->is_zero_terminated = g_type_info_is_zero_terminated (type_info);
154 155 156 157
    sc->fixed_size = g_type_info_get_array_fixed_size (type_info);
    sc->len_arg_index = g_type_info_get_array_length (type_info);
    if (sc->len_arg_index >= 0)
        sc->len_arg_index += child_offset;
158

159 160
    item_type_info = g_type_info_get_param_type (type_info, 0);

161 162
    item_transfer =
        transfer == GI_TRANSFER_CONTAINER ? GI_TRANSFER_NOTHING : transfer;
163

164 165 166 167 168 169
    sc->item_cache = _arg_cache_new (item_type_info,
                                     NULL,
                                     NULL,
                                     item_transfer,
                                     direction,
                                     0, 0);
170

171
    if (sc->item_cache == NULL) {
172
        _pygi_arg_cache_free ( (PyGIArgCache *)sc);
173 174
        return NULL;
    }
John (J5) Palmieri's avatar
John (J5) Palmieri committed
175

176 177
    sc->item_size = _pygi_g_type_info_size (item_type_info);
    g_base_info_unref ( (GIBaseInfo *)item_type_info);
178

179
    return sc;
180
}
181 182 183 184
static PyGIHashCache *
_hash_cache_new (GITypeInfo *type_info,
                 GIDirection direction,
                 GITransfer transfer)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
185 186 187 188
{
    PyGIHashCache *hc;
    GITypeInfo *key_type_info;
    GITypeInfo *value_type_info;
189
    GITransfer item_transfer;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
190

191 192
    hc = g_slice_new0 (PyGIHashCache);
    ( (PyGIArgCache *)hc)->destroy_notify = (GDestroyNotify)_hash_cache_free_func;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
193 194 195
    key_type_info = g_type_info_get_param_type (type_info, 0);
    value_type_info = g_type_info_get_param_type (type_info, 1);

196 197
    item_transfer =
        transfer == GI_TRANSFER_CONTAINER ? GI_TRANSFER_NOTHING : transfer;
198

199 200 201 202 203 204
    hc->key_cache = _arg_cache_new (key_type_info,
                                    NULL,
                                    NULL,
                                    item_transfer,
                                    direction,
                                    0, 0);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
205 206

    if (hc->key_cache == NULL) {
207
        _pygi_arg_cache_free ( (PyGIArgCache *)hc);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
208 209 210
        return NULL;
    }

211 212 213 214 215 216
    hc->value_cache = _arg_cache_new (value_type_info,
                                      NULL,
                                      NULL,
                                      item_transfer,
                                      direction,
                                      0, 0);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
217 218

    if (hc->value_cache == NULL) {
219
        _pygi_arg_cache_free ( (PyGIArgCache *)hc);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
220 221 222 223 224 225 226 227
        return NULL;
    }

    g_base_info_unref( (GIBaseInfo *)key_type_info);
    g_base_info_unref( (GIBaseInfo *)value_type_info);

    return hc;
}
228

229 230 231 232
static PyGICallbackCache *
_callback_cache_new (GIArgInfo *arg_info,
                     GIInterfaceInfo *iface_info,
                     gssize child_offset)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
233 234 235
{
   PyGICallbackCache *cc;

236
   cc = g_slice_new0 (PyGICallbackCache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
237 238
   ( (PyGIArgCache *)cc)->destroy_notify = (GDestroyNotify)_callback_cache_free_func;

239
   cc->user_data_index = g_arg_info_get_closure (arg_info);
240
   if (cc->user_data_index != -1)
241
       cc->user_data_index += child_offset;
242
   cc->destroy_notify_index = g_arg_info_get_destroy (arg_info);
243
   if (cc->destroy_notify_index != -1)
244
       cc->destroy_notify_index += child_offset;
245
   cc->scope = g_arg_info_get_scope (arg_info);
246 247
   g_base_info_ref( (GIBaseInfo *)iface_info);
   cc->interface_info = iface_info;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
248 249 250
   return cc;
}

251 252
static PyGIArgCache *
_arg_cache_alloc (void)
253
{
254
    return g_slice_new0 (PyGIArgCache);
255
}
256

257 258 259 260 261 262
static void
_arg_cache_from_py_basic_type_setup (PyGIArgCache *arg_cache)
{
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
}

263 264 265
static void
_arg_cache_to_py_basic_type_setup (PyGIArgCache *arg_cache)
{
266
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
267 268
}

269
static void
270
_arg_cache_from_py_void_setup (PyGIArgCache *arg_cache)
271
{
272
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_void;
273
}
274

275
static void
276
_arg_cache_to_py_void_setup (PyGIArgCache *arg_cache)
277
{
278
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_void;
279 280
}

281
static void
282 283
_arg_cache_from_py_utf8_setup (PyGIArgCache *arg_cache,
                               GITransfer transfer)
284
{
285
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_basic_type_cache_adapter;
286
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_utf8;
287
}
288

289
static void
290 291
_arg_cache_to_py_utf8_setup (PyGIArgCache *arg_cache,
                               GITransfer transfer)
292
{
293
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_basic_type_cache_adapter;
294
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_utf8;
295 296
}

297
static gboolean
298 299 300 301
_arg_cache_from_py_array_setup (PyGIArgCache *arg_cache,
                                PyGICallableCache *callable_cache,
                                GITypeInfo *type_info,
                                GITransfer transfer,
302 303
                                PyGIDirection direction,
                                gssize arg_index)
304
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
305
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
306
    seq_cache->array_type = g_type_info_get_array_type (type_info);
307

308
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_array;
309

310
    if (seq_cache->len_arg_index >= 0) {
311
        PyGIArgCache *child_cache = 
312
            _pygi_callable_cache_get_arg (callable_cache, seq_cache->len_arg_index);
313

314
        if (child_cache == NULL) {
315
            child_cache = _arg_cache_alloc ();
316 317
        } else if (child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD ||
                   child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD_NEEDS_UPDATE) {
318
            arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_array;
319 320 321
            return TRUE;
        }

322 323 324 325 326
        if (seq_cache->len_arg_index < arg_index)
            child_cache->meta_type = PYGI_META_ARG_TYPE_CHILD_NEEDS_UPDATE;
        else
            child_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;

327
        child_cache->direction = direction;
328 329
        child_cache->to_py_marshaller = NULL;
        child_cache->from_py_marshaller = NULL;
330

331
        _pygi_callable_cache_set_arg (callable_cache, seq_cache->len_arg_index, child_cache);
332 333
    }

334
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_array;
335 336

    return TRUE;
337 338
}

339
static gboolean
340 341 342 343 344 345
_arg_cache_to_py_array_setup (PyGIArgCache *arg_cache,
                              PyGICallableCache *callable_cache,
                              GITypeInfo *type_info,
                              GITransfer transfer,
                              PyGIDirection direction,
                              gssize arg_index)
346
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
347
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache;
348 349
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_array;
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_array;
350

351
    seq_cache->array_type = g_type_info_get_array_type (type_info);
352 353

    if (seq_cache->len_arg_index >= 0) {
354 355
        PyGIArgCache *child_cache = _pygi_callable_cache_get_arg (callable_cache,
                                                                  seq_cache->len_arg_index);
356
        if (seq_cache->len_arg_index < arg_index)
357
             callable_cache->n_to_py_child_args++;
358

359
        if (child_cache != NULL) {
360 361
            callable_cache->to_py_args =
                g_slist_remove (callable_cache->to_py_args, child_cache);
362 363 364 365

            if (child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD ||
                child_cache->meta_type == PYGI_META_ARG_TYPE_CHILD_NEEDS_UPDATE)
                return TRUE;
366
        } else {
367
            child_cache = _arg_cache_alloc ();
368
        }
369

370 371
        child_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
        child_cache->direction = direction;
372 373
        child_cache->to_py_marshaller = NULL;
        child_cache->from_py_marshaller = NULL;
374

375
        _pygi_callable_cache_set_arg (callable_cache, seq_cache->len_arg_index, child_cache);
376 377 378 379 380
    }

    return TRUE;
}

381
static void
382 383
_arg_cache_from_py_glist_setup (PyGIArgCache *arg_cache,
                                GITransfer transfer)
384
{
385 386
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_glist;
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_glist;
387
}
388

389
static void
390 391
_arg_cache_to_py_glist_setup (PyGIArgCache *arg_cache,
                              GITransfer transfer)
392
{
393 394
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_glist;
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_glist;
395 396
}

397
static void
398 399
_arg_cache_from_py_gslist_setup (PyGIArgCache *arg_cache,
                                 GITransfer transfer)
400
{
401 402
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_gslist;
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_glist;
403
}
404

405
static void
406 407
_arg_cache_to_py_gslist_setup (PyGIArgCache *arg_cache,
                                 GITransfer transfer)
408
{
409 410
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_gslist;
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_glist;
411 412
}

413
static void
414
_arg_cache_from_py_ghash_setup (PyGIArgCache *arg_cache)
415
{
416 417
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_ghash;
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_ghash;
418 419
}

420
static void
421
_arg_cache_to_py_ghash_setup (PyGIArgCache *arg_cache)
422
{
423 424
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_ghash;
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_ghash;
425 426
}

427
static void
428
_arg_cache_from_py_gerror_setup (PyGIArgCache *arg_cache)
429
{
430
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_gerror;
431
    arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
432 433
}

434
static void
435
_arg_cache_to_py_gerror_setup (PyGIArgCache *arg_cache)
436
{
437
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_gerror;
438
    arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
439 440
}

441
static void
442 443
_arg_cache_from_py_interface_union_setup (PyGIArgCache *arg_cache,
                                          GITransfer transfer)
444
{
445
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_struct_cache_adapter;
446 447
}

448
static void
449 450
_arg_cache_to_py_interface_union_setup (PyGIArgCache *arg_cache,
                                        GITransfer transfer)
451
{
452
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_struct_cache_adapter;
453 454
}

455
static void
456 457 458
_arg_cache_from_py_interface_struct_setup (PyGIArgCache *arg_cache,
                                           GIInterfaceInfo *iface_info,
                                           GITransfer transfer)
459
{
460
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
461
    iface_cache->is_foreign = g_struct_info_is_foreign ( (GIStructInfo*)iface_info);
462
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_struct_cache_adapter;
463 464

    if (iface_cache->g_type == G_TYPE_VALUE)
465
        arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_struct_gvalue;
466
    else if (iface_cache->is_foreign)
467
        arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_struct_foreign;
468
}
469

470
static void
471 472 473
_arg_cache_to_py_interface_struct_setup (PyGIArgCache *arg_cache,
                                         GIInterfaceInfo *iface_info,
                                         GITransfer transfer)
474 475
{
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
476
    iface_cache->is_foreign = g_struct_info_is_foreign ( (GIStructInfo*)iface_info);
477
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_struct_cache_adapter;
478 479

    if (iface_cache->is_foreign)
480
        arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_interface_struct_foreign;
481 482
}

483
static void
484 485
_arg_cache_from_py_interface_object_setup (PyGIArgCache *arg_cache,
                                           GITransfer transfer)
486
{
487 488
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_object;
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_object;
489 490
}

491
static void
492 493
_arg_cache_to_py_interface_object_setup (PyGIArgCache *arg_cache,
                                         GITransfer transfer)
494
{
495
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_object_cache_adapter;
496
    arg_cache->to_py_cleanup = _pygi_marshal_cleanup_to_py_interface_object;
497 498
}

499
static void
500 501
_arg_cache_from_py_interface_callback_setup (PyGIArgCache *arg_cache,
                                             PyGICallableCache *callable_cache)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
502
{
503
    PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
504
    if (callback_cache->user_data_index >= 0) {
505
        PyGIArgCache *user_data_arg_cache = _arg_cache_alloc ();
506
        user_data_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD_WITH_PYARG;
507
        user_data_arg_cache->direction = PYGI_DIRECTION_FROM_PYTHON;
508 509
        _pygi_callable_cache_set_arg (callable_cache, callback_cache->user_data_index,
                                      user_data_arg_cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
510
    }
511 512

    if (callback_cache->destroy_notify_index >= 0) {
513
        PyGIArgCache *destroy_arg_cache = _arg_cache_alloc ();
514
        destroy_arg_cache->meta_type = PYGI_META_ARG_TYPE_CHILD;
515
        destroy_arg_cache->direction = PYGI_DIRECTION_FROM_PYTHON;
516 517
        _pygi_callable_cache_set_arg (callable_cache, callback_cache->destroy_notify_index,
                                      destroy_arg_cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
518
    }
519
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_callback;
520
    arg_cache->from_py_cleanup = _pygi_marshal_cleanup_from_py_interface_callback;
521 522
}

523
static void
524
_arg_cache_to_py_interface_callback_setup (void)
525
{
526 527 528
    PyErr_Format(PyExc_NotImplementedError,
                 "Callback returns are not supported");
}
529

530
static void
531 532
_arg_cache_from_py_interface_enum_setup (PyGIArgCache *arg_cache,
                                         GITransfer transfer)
533
{
534
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_enum;
535 536
}

537
static void
538 539
_arg_cache_to_py_interface_enum_setup (PyGIArgCache *arg_cache,
                                       GITransfer transfer)
540
{
541
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_enum;
542
}
543

544
static void
545 546
_arg_cache_from_py_interface_flags_setup (PyGIArgCache *arg_cache,
                                          GITransfer transfer)
547
{
548
    arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_flags;
549 550
}

551
static void
552 553
_arg_cache_to_py_interface_flags_setup (PyGIArgCache *arg_cache,
                                        GITransfer transfer)
554
{
555
    arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_flags;
556 557
}

558 559 560 561 562
PyGIArgCache *
_arg_cache_new_for_interface (GIInterfaceInfo *iface_info,
                              PyGICallableCache *callable_cache,
                              GIArgInfo *arg_info,
                              GITransfer transfer,
563
                              PyGIDirection direction,
564 565
                              gssize c_arg_index,
                              gssize py_arg_index)
566 567
{
    PyGIInterfaceCache *iface_cache = NULL;
568
    PyGIArgCache *arg_cache = NULL;
569
    gssize child_offset = 0;
570
    GIInfoType info_type;
571 572 573 574 575

    if (callable_cache != NULL)
        child_offset =
            (callable_cache->function_type == PYGI_FUNCTION_TYPE_METHOD ||
                 callable_cache->function_type == PYGI_FUNCTION_TYPE_VFUNC) ? 1: 0;
576

577 578
    info_type = g_base_info_get_type ( (GIBaseInfo *)iface_info);

579 580
    /* Callbacks are special cased */
    if (info_type != GI_INFO_TYPE_CALLBACK) {
581
        iface_cache = _interface_cache_new (iface_info);
582 583 584 585 586 587

        arg_cache = (PyGIArgCache *)iface_cache;
        if (arg_cache == NULL)
            return NULL;
    }

588 589
    switch (info_type) {
        case GI_INFO_TYPE_UNION:
590
            if (direction & PYGI_DIRECTION_FROM_PYTHON)
591
               _arg_cache_from_py_interface_union_setup (arg_cache, transfer);
592

593
            if (direction & PYGI_DIRECTION_TO_PYTHON)
594
               _arg_cache_to_py_interface_union_setup (arg_cache, transfer);
595

596
            break;
597
        case GI_INFO_TYPE_BOXED:
598
        case GI_INFO_TYPE_STRUCT:
599
            if (direction & PYGI_DIRECTION_FROM_PYTHON)
600 601 602 603
               _arg_cache_from_py_interface_struct_setup (arg_cache,
                                                          iface_info,
                                                          transfer);

604
            if (direction & PYGI_DIRECTION_TO_PYTHON)
605 606 607
               _arg_cache_to_py_interface_struct_setup (arg_cache,
                                                        iface_info,
                                                        transfer);
608 609 610
            break;
        case GI_INFO_TYPE_OBJECT:
        case GI_INFO_TYPE_INTERFACE:
611
            if (direction & PYGI_DIRECTION_FROM_PYTHON)
612
               _arg_cache_from_py_interface_object_setup (arg_cache, transfer);
613

614
            if (direction & PYGI_DIRECTION_TO_PYTHON)
615
               _arg_cache_to_py_interface_object_setup (arg_cache, transfer);
616

617 618
            break;
        case GI_INFO_TYPE_CALLBACK:
619 620 621
            {
                PyGICallbackCache *callback_cache;

622
                if (direction & PYGI_DIRECTION_TO_PYTHON) {
623
                    _arg_cache_to_py_interface_callback_setup ();
624 625 626
                    return NULL;
                }

John (J5) Palmieri's avatar
John (J5) Palmieri committed
627
                callback_cache =
628 629 630
                    _callback_cache_new (arg_info,
                                         iface_info,
                                         child_offset);
631 632 633 634 635

                arg_cache = (PyGIArgCache *)callback_cache;
                if (arg_cache == NULL)
                    return NULL;

636
                if (direction & PYGI_DIRECTION_FROM_PYTHON)
637
                    _arg_cache_from_py_interface_callback_setup (arg_cache, callable_cache);
638 639 640

                break;
            }
641
        case GI_INFO_TYPE_ENUM:
642
            if (direction & PYGI_DIRECTION_FROM_PYTHON)
643
               _arg_cache_from_py_interface_enum_setup (arg_cache, transfer);
644

645
            if (direction & PYGI_DIRECTION_TO_PYTHON)
646
               _arg_cache_to_py_interface_enum_setup (arg_cache, transfer);
647

648 649
            break;
        case GI_INFO_TYPE_FLAGS:
650
            if (direction & PYGI_DIRECTION_FROM_PYTHON)
651
               _arg_cache_from_py_interface_flags_setup (arg_cache, transfer);
652

653
            if (direction & PYGI_DIRECTION_TO_PYTHON)
654
               _arg_cache_to_py_interface_flags_setup (arg_cache, transfer);
655

656 657
            break;
        default:
658
            g_assert_not_reached ();
659 660 661 662 663 664 665 666
    }

    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = GI_TYPE_TAG_INTERFACE;
        arg_cache->py_arg_index = py_arg_index;
        arg_cache->c_arg_index = c_arg_index;
667

668
        if (iface_cache != NULL) {
669
            g_base_info_ref ( (GIBaseInfo *)iface_info);
670 671
            iface_cache->interface_info = iface_info;
        }
672
    }
673 674

    return arg_cache;
675 676
}

677
PyGIArgCache *
678 679 680 681
_arg_cache_new (GITypeInfo *type_info,
                PyGICallableCache *callable_cache,
                GIArgInfo *arg_info,
                GITransfer transfer,
682
                PyGIDirection direction,
683 684
                gssize c_arg_index,
                gssize py_arg_index)
685
{
686
    PyGIArgCache *arg_cache = NULL;
687
    gssize child_offset = 0;
688
    GITypeTag type_tag;
689

690 691
    type_tag = g_type_info_get_tag (type_info);

692 693 694 695
    if (callable_cache != NULL)
        child_offset =
            (callable_cache->function_type == PYGI_FUNCTION_TYPE_METHOD ||
                callable_cache->function_type == PYGI_FUNCTION_TYPE_VFUNC) ? 1: 0;
696

697 698
    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
699
           arg_cache = _arg_cache_alloc ();
700 701
           if (arg_cache == NULL)
               break;
702

703
           if (direction & PYGI_DIRECTION_FROM_PYTHON)
704 705
               _arg_cache_from_py_void_setup (arg_cache);

706
           if (direction & PYGI_DIRECTION_TO_PYTHON)
707
               _arg_cache_to_py_void_setup (arg_cache);
708

709 710 711 712 713 714 715 716 717 718 719 720 721 722
           break;
       case GI_TYPE_TAG_BOOLEAN:
       case GI_TYPE_TAG_INT8:
       case GI_TYPE_TAG_UINT8:
       case GI_TYPE_TAG_INT16:
       case GI_TYPE_TAG_UINT16:
       case GI_TYPE_TAG_INT32:
       case GI_TYPE_TAG_UINT32:
       case GI_TYPE_TAG_INT64:
       case GI_TYPE_TAG_UINT64:
       case GI_TYPE_TAG_FLOAT:
       case GI_TYPE_TAG_DOUBLE:
       case GI_TYPE_TAG_UNICHAR:
       case GI_TYPE_TAG_GTYPE:
723
           arg_cache = _arg_cache_alloc ();
724 725
           if (arg_cache == NULL)
               break;
726