pygi-cache.c 32.3 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 24
#include "pygi-cache.h"
#include "pygi-argument.h"
25
#include "pygi-type.h"
26
#include <girepository.h>
27

28 29 30 31 32 33 34
PyGIArgCache * _arg_cache_in_new_from_type_info (GITypeInfo *type_info,
                                  PyGIFunctionCache *function_cache,
                                  GITypeTag type_tag,
                                  GITransfer transfer,
                                  GIDirection direction,
                                  gint c_arg_index,
                                  gint py_arg_index);
35 36
/* cleanup */
static inline void
37
_interface_cache_free_func (PyGIInterfaceCache *cache)
38 39 40 41
{
    if (cache != NULL) {
        Py_XDECREF(cache->py_type);
        g_slice_free(PyGIInterfaceCache, cache);
42 43
        if (cache->type_name != NULL)
            g_free(cache->type_name);
44 45 46 47 48 49 50 51 52 53
    }
}

static inline void
_pygi_hash_cache_free (PyGIHashCache *cache)
{
    if (cache != NULL)
        g_slice_free(PyGIHashCache, cache);
}

54 55
static void
_sequence_cache_free_func (PyGISequenceCache *cache)
56 57 58 59 60
{
    if (cache != NULL)
        g_slice_free(PyGISequenceCache, cache);
}

61 62
static void
_callback_cache_free_func (PyGICallbackCache *cache)
63 64 65 66 67 68
{
    if (cache != NULL)
        g_slice_free(PyGICallbackCache, cache);
}

void
69
_pygi_arg_cache_free (PyGIArgCache *cache)
70
{
71 72 73 74 75
    if (cache == NULL)
        return;

    if (cache->arg_info != NULL)
        g_base_info_unref(cache->arg_info);
76 77 78 79
    if (cache->destroy_notify)
        cache->destroy_notify(cache);
    else
        g_slice_free(PyGIArgCache, cache);
80 81 82 83 84 85 86
}

void
_pygi_function_cache_free (PyGIFunctionCache *cache)
{
    int i;

87 88 89
    if (cache == NULL)
        return;

90 91 92 93
    g_slist_free(cache->in_args);
    g_slist_free(cache->out_args);
    for (i = 0; i < cache->n_args; i++) {
        PyGIArgCache *tmp = cache->args_cache[i];
94
        _pygi_arg_cache_free(tmp);
95
    }
96 97 98
    if (cache->return_cache != NULL)
        _pygi_arg_cache_free(cache->return_cache);

99
    g_slice_free1(cache->n_args * sizeof(PyGIArgCache *), cache->args_cache);
100 101 102 103 104
    g_slice_free(PyGIFunctionCache, cache);
}

/* cache generation */
static inline PyGIFunctionCache *
105
_function_cache_new_from_function_info(GIFunctionInfo *function_info)
106 107 108 109 110 111 112 113
{
    PyGIFunctionCache *fc;
    GIFunctionInfoFlags flags;

    fc = g_slice_new0(PyGIFunctionCache);
    flags = g_function_info_get_flags(function_info);
    fc->is_method = flags & GI_FUNCTION_IS_METHOD;
    fc->is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
114

115
    fc->n_args = g_callable_info_get_n_args ( (GICallableInfo *) function_info) + (fc->is_method ? 1: 0);
116 117
    if (fc->n_args > 0)
        fc->args_cache = g_slice_alloc0(fc->n_args * sizeof(PyGIArgCache *));
118 119 120 121

    return fc;
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

static inline PyGIInterfaceCache *
_interface_cache_new_from_interface_info(GIInterfaceInfo *iface_info)
{
    PyGIInterfaceCache *ic;
    
    ic = g_slice_new0(PyGIInterfaceCache);
    ((PyGIArgCache *)ic)->destroy_notify = (GDestroyNotify)_interface_cache_free_func;    
    ic->g_type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *)iface_info);
    if (ic->g_type != G_TYPE_NONE) {
        ic->py_type = _pygi_type_get_from_g_type (ic->g_type);
    } else {
        /* FIXME: avoid passing GI infos to noncache API */
        ic->py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) iface_info);
    }

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

    ic->type_name == _pygi_g_base_info_get_fullname(iface_info);
    return ic;
}

145
static inline PyGISequenceCache *
146 147 148 149
_sequence_cache_new_from_type_info(GITypeInfo *type_info)
{
    PyGISequenceCache *sc;
    GITypeInfo *item_type_info;
150
    GITypeTag item_type_tag;
151 152

    sc = g_slice_new0(PyGISequenceCache);
153
    ((PyGIArgCache *)sc)->destroy_notify = (GDestroyNotify)_sequence_cache_free_func;    
154 155 156 157

    sc->fixed_size = -1;
    sc->len_arg_index = -1;
    sc->is_zero_terminated = g_type_info_is_zero_terminated(type_info);
158
    if (!sc->is_zero_terminated)
159 160
        sc->fixed_size = g_type_info_get_array_fixed_size(type_info);
    if (sc->fixed_size < 0)
161 162
        sc->len_arg_index = g_type_info_get_array_length (type_info);

163
    item_type_info = g_type_info_get_param_type (type_info, 0);
164
    item_type_tag = g_type_info_get_tag (item_type_info);
165

166 167 168 169 170 171 172 173
    /* FIXME: support out also */
    sc->item_cache = _arg_cache_in_new_from_type_info(item_type_info,
                                                      NULL,
                                                      item_type_tag,
                                                      GI_TRANSFER_EVERYTHING,
                                                      GI_DIRECTION_IN,
                                                      0, 0);

174 175 176 177 178 179
    if (sc->item_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)sc);
        return NULL;
    }
   
    sc->item_cache->type_tag = item_type_tag;    
180
    g_base_info_unref( (GIBaseInfo *) item_type_info);
181

182
    return sc;
183 184
}

185 186 187 188 189
static inline PyGIArgCache *
_arg_cache_new(void)
{
    return g_slice_new0(PyGIArgCache);
}
190 191
/* process in args */

192 193
static inline PyGIArgCache *
_arg_cache_new_for_in_void(void)
194
{
195
     PyGIArgCache *arg_cache = _arg_cache_new();
196
     arg_cache->in_marshaller = _pygi_marshal_in_void;
197

198
     return arg_cache;
199 200
}

201 202
static inline PyGIArgCache *
_arg_cache_new_for_in_boolean(void)
203
{
204
    PyGIArgCache *arg_cache = _arg_cache_new();
205
    arg_cache->in_marshaller = _pygi_marshal_in_boolean;
206
    return arg_cache;
207 208
}

209 210
static inline PyGIArgCache *
_arg_cache_new_for_in_int8(void)
211
{
212
    PyGIArgCache *arg_cache = _arg_cache_new();
213
    arg_cache->in_marshaller = _pygi_marshal_in_int8;
214
    return arg_cache;
215 216
}

217 218
static inline PyGIArgCache *
_arg_cache_new_for_in_uint8(void)
219
{
220
    PyGIArgCache *arg_cache = _arg_cache_new();
221
    arg_cache->in_marshaller = _pygi_marshal_in_uint8;
222
    return arg_cache;
223 224
}

225 226
static inline PyGIArgCache *
_arg_cache_new_for_in_int16(void)
227
{
228
    PyGIArgCache *arg_cache = _arg_cache_new();
229
    arg_cache->in_marshaller = _pygi_marshal_in_int16;
230
    return arg_cache;
231 232
}

233 234
static inline PyGIArgCache *
_arg_cache_new_for_in_uint16(void)
235
{
236
    PyGIArgCache *arg_cache = _arg_cache_new();
237
    arg_cache->in_marshaller = _pygi_marshal_in_uint16;
238
    return arg_cache;
239 240
}

241 242
static inline PyGIArgCache *
_arg_cache_new_for_in_int32(void)
243
{
244
    PyGIArgCache *arg_cache = _arg_cache_new();
245
    arg_cache->in_marshaller = _pygi_marshal_in_int32;
246
    return arg_cache;
247 248
}

249 250
static inline PyGIArgCache *
_arg_cache_new_for_in_uint32(void)
251
{
252
    PyGIArgCache *arg_cache = _arg_cache_new();
253
    arg_cache->in_marshaller = _pygi_marshal_in_uint32;
254
    return arg_cache;
255 256
}

257 258
static inline PyGIArgCache *
_arg_cache_new_for_in_int64(void)
259
{
260
    PyGIArgCache *arg_cache = _arg_cache_new();
261
    arg_cache->in_marshaller = _pygi_marshal_in_int64;
262
    return arg_cache;
263 264
}

265 266
static inline PyGIArgCache *
_arg_cache_new_for_in_uint64(void)
267
{
268
    PyGIArgCache *arg_cache = _arg_cache_new();
269
    arg_cache->in_marshaller = _pygi_marshal_in_uint64;
270
    return arg_cache;
271 272
}

273 274
static inline PyGIArgCache *
_arg_cache_new_for_in_float(void)
275
{
276
    PyGIArgCache *arg_cache = _arg_cache_new();
277
    arg_cache->in_marshaller = _pygi_marshal_in_float;
278
    return arg_cache;
279 280
}

281 282
static inline PyGIArgCache *
_arg_cache_new_for_in_double(void)
283
{
284
    PyGIArgCache *arg_cache = _arg_cache_new();
285
    arg_cache->in_marshaller = _pygi_marshal_in_double;
286
    return arg_cache;
287 288
}

289 290
static inline PyGIArgCache *
_arg_cache_new_for_in_unichar(void)
291
{
292
    PyGIArgCache *arg_cache = _arg_cache_new();
293
    arg_cache->in_marshaller = _pygi_marshal_in_unichar;
294
    return arg_cache;
295 296
}

297 298
static inline PyGIArgCache *
_arg_cache_new_for_in_gtype(void)
299
{
300
    PyGIArgCache *arg_cache = _arg_cache_new();
301
    arg_cache->in_marshaller = _pygi_marshal_in_gtype;
302
    return arg_cache;
303 304
}

305 306
static inline PyGIArgCache *
_arg_cache_new_for_in_utf8(GITransfer transfer)
307
{
308
    PyGIArgCache *arg_cache = _arg_cache_new();
309
    arg_cache->in_marshaller = _pygi_marshal_in_utf8;
310 311 312
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

313
    return arg_cache;
314 315
}

316 317
static inline PyGIArgCache *
_arg_cache_new_for_in_filename(GITransfer transfer)
318
{
319
    PyGIArgCache *arg_cache = _arg_cache_new();
320
    arg_cache->in_marshaller = _pygi_marshal_in_filename;
321 322 323
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

324
    return arg_cache;
325 326
}

327 328
static inline PyGIArgCache *
_arg_cache_new_for_in_array(GITypeInfo *type_info,
329
                            GITransfer transfer)
330
{
331
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
332
    arg_cache->in_marshaller = _pygi_marshal_in_array;
333 334

    /* arg_cache->cleanup = _pygi_cleanup_array; */
335
    return arg_cache;
336 337
}

338 339 340
static inline PyGIArgCache *
_arg_cache_new_for_in_glist(GITypeInfo *type_info,
                                      GITransfer transfer)
341
{
342
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
343
    arg_cache->in_marshaller = _pygi_marshal_in_glist;
344 345
    /* arg_cache->cleanup = */

346
    return arg_cache;
347 348
}

349 350 351
static inline PyGIArgCache *
_arg_cache_new_for_in_gslist(GITypeInfo *type_info,
                                       GITransfer transfer)
352
{
353
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
354
    arg_cache->in_marshaller = _pygi_marshal_in_gslist;
355 356
    /* arg_cache->cleanup = */

357
    return arg_cache;
358 359
}

360 361
static inline PyGIArgCache *
_arg_cache_new_for_in_ghash(GITypeInfo *type_info)
362
{
363 364
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_ghash;*/
365 366
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
367
    return arg_cache;
368 369
}

370 371
static inline PyGIArgCache *
_arg_cache_new_for_in_gerror(void)
372
{
373
    PyGIArgCache *arg_cache = _arg_cache_new();
374
    arg_cache->in_marshaller = _pygi_marshal_in_gerror;
375
    arg_cache->is_aux = TRUE;
376
    return arg_cache;
377 378
}

379 380 381 382 383 384 385 386 387 388
static inline PyGIArgCache *
_arg_cache_new_for_in_interface_union(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_inteface_union;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

389 390 391 392 393 394

static void 
_g_slice_free_gvalue_func(GValue *value) {
    g_slice_free(GValue, value);
}

395
static inline PyGIArgCache *
396 397
_arg_cache_new_for_in_interface_struct(GIInterfaceInfo *iface_info,
                                       GITransfer transfer)
398
{
399
    PyGIInterfaceCache *iface_cache = _interface_cache_new_from_interface_info(iface_info);
400
    PyGIArgCache *arg_cache = (PyGIArgCache *)iface_cache;
401 402 403 404 405 406 407
    iface_cache->is_foreign = g_struct_info_is_foreign( (GIStructInfo*)iface_info);
    arg_cache->in_marshaller = _pygi_marshal_in_interface_struct;
    if (iface_cache->g_type == G_TYPE_VALUE)
        arg_cache->cleanup = _g_slice_free_gvalue_func;
    if (iface_cache->g_type == G_TYPE_CLOSURE)
        arg_cache->cleanup = g_closure_unref;

408 409 410 411
    return arg_cache;
}

static inline PyGIArgCache *
412 413
_arg_cache_new_for_in_interface_object(GIInterfaceInfo *iface_info,
                                       GITransfer transfer)
414
{
415
    PyGIArgCache *arg_cache = (PyGIArgCache *)_interface_cache_new_from_interface_info(iface_info);
416 417 418 419 420 421 422 423
    arg_cache->in_marshaller = _pygi_marshal_in_interface_object;
    if (transfer == GI_TRANSFER_EVERYTHING)
        arg_cache->cleanup = (GDestroyNotify)g_object_unref;

    return arg_cache;
}

static inline PyGIArgCache *
424 425
_arg_cache_new_for_in_interface_boxed(GIInterfaceInfo *iface_info,
                                      GITransfer transfer)
426
{
427 428
    PyGIArgCache *arg_cache = (PyGIArgCache *)_interface_cache_new_from_interface_info(iface_info);
    arg_cache->in_marshaller = _pygi_marshal_in_interface_boxed;
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_in_interface_callback(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_callback;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_in_interface_enum(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_enum;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_in_interface_flags(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_flags;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

PyGIArgCache *
_arg_cache_in_new_from_interface_info (GIInterfaceInfo *iface_info,
                                       PyGIFunctionCache *function_cache,
                                       GIInfoType info_type,
                                       GITransfer transfer,
                                       GIDirection direction,
                                       gint c_arg_index,
                                       gint py_arg_index)
{
    PyGIArgCache *arg_cache = NULL;

    switch (info_type) {
        case GI_INFO_TYPE_UNION:
            arg_cache = _arg_cache_new_for_in_interface_union();
            break;
        case GI_INFO_TYPE_STRUCT:
478 479
            arg_cache = _arg_cache_new_for_in_interface_struct(iface_info,
                                                               transfer);
480 481 482
            break;
        case GI_INFO_TYPE_OBJECT:
        case GI_INFO_TYPE_INTERFACE:
483 484
            arg_cache = _arg_cache_new_for_in_interface_object(iface_info,
                                                               transfer);
485 486
            break;
        case GI_INFO_TYPE_BOXED:
487 488
            arg_cache = _arg_cache_new_for_in_interface_boxed(iface_info,
                                                              transfer);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
            break;
        case GI_INFO_TYPE_CALLBACK:
            arg_cache = _arg_cache_new_for_in_interface_callback();
            break;
        case GI_INFO_TYPE_ENUM:
            arg_cache = _arg_cache_new_for_in_interface_enum();
            break;
        case GI_INFO_TYPE_FLAGS:
            arg_cache = _arg_cache_new_for_in_interface_flags();
            break;
        default:
            g_assert_not_reached();
    }

    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;
    }
510 511

    return arg_cache;
512 513
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/* process out args */

static inline PyGIArgCache *
_arg_cache_new_for_out_void(void)
{
     PyGIArgCache *arg_cache = _arg_cache_new();
     arg_cache->out_marshaller = _pygi_marshal_out_void;

     return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_boolean(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_boolean;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int8(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int8;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint8(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint8;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int16(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int16;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint16(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint16;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int32(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int32;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint32(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint32;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int64(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int64;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint64(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint64;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_float(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_float;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_double(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_double;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_unichar(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_unichar;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gtype(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_gtype;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_utf8(GITransfer transfer)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_utf8;

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_filename(GITransfer transfer)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_filename;

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_array(GITypeInfo *type_info,
                            GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_array;

    /* arg_cache->cleanup = _pygi_cleanup_array; */
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_glist(GITypeInfo *type_info,
                                      GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_glist;
    /* arg_cache->cleanup = */

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gslist(GITypeInfo *type_info,
                                       GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_gslist;
    /* arg_cache->cleanup = */

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_ghash(GITypeInfo *type_info)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->out_marshaller = _pygi_marshal_out_ghash;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gerror(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_gerror;
    arg_cache->is_aux = TRUE;
    return arg_cache;
}


PyGIArgCache *
_arg_cache_out_new_from_type_info (GITypeInfo *type_info,
                                   PyGIFunctionCache *function_cache,
                                   GITypeTag type_tag,
                                   GITransfer transfer,
                                   GIDirection direction,
                                   gint c_arg_index)
{
    PyGIArgCache *arg_cache = NULL;

    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
           arg_cache = _arg_cache_new_for_out_void();
           break;
       case GI_TYPE_TAG_BOOLEAN:
           arg_cache = _arg_cache_new_for_out_boolean();
           break;
       case GI_TYPE_TAG_INT8:
           arg_cache = _arg_cache_new_for_out_int8();
           break;
       case GI_TYPE_TAG_UINT8:
           arg_cache = _arg_cache_new_for_out_uint8();
           break;
       case GI_TYPE_TAG_INT16:
           arg_cache = _arg_cache_new_for_out_uint16();
           break;
       case GI_TYPE_TAG_UINT16:
           arg_cache = _arg_cache_new_for_out_uint16();
           break;
       case GI_TYPE_TAG_INT32:
           arg_cache = _arg_cache_new_for_out_int32();
           break;
       case GI_TYPE_TAG_UINT32:
           arg_cache = _arg_cache_new_for_out_uint32();
           break;
       case GI_TYPE_TAG_INT64:
           arg_cache = _arg_cache_new_for_out_int64();
           break;
       case GI_TYPE_TAG_UINT64:
           arg_cache = _arg_cache_new_for_out_uint64();
           break;
       case GI_TYPE_TAG_FLOAT:
           arg_cache = _arg_cache_new_for_out_float();
           break;
       case GI_TYPE_TAG_DOUBLE:
           arg_cache = _arg_cache_new_for_out_double();
           break;
       case GI_TYPE_TAG_UNICHAR:
           arg_cache = _arg_cache_new_for_out_unichar();
           break;
       case GI_TYPE_TAG_GTYPE:
           arg_cache = _arg_cache_new_for_out_gtype();
           break;
       case GI_TYPE_TAG_UTF8:
           arg_cache = _arg_cache_new_for_out_utf8(transfer);
           break;
       case GI_TYPE_TAG_FILENAME:
           arg_cache = _arg_cache_new_for_out_filename(transfer);
           break;
       case GI_TYPE_TAG_ARRAY:
           arg_cache = _arg_cache_new_for_out_array(type_info,
                                                   transfer);
           break;
       case GI_TYPE_TAG_INTERFACE:
           /*
           {
               GIInterfaceInfo *interface_info = g_type_info_get_interface(type_info);
               GIInfoType info_type = g_base_info_get_type( (GIBaseInfo *) interface_info);
               arg_cache = _arg_cache_out_new_from_interface_info(interface_info,
                                                                 function_cache,
                                                                 info_type,
                                                                 transfer,
                                                                 direction,
                                                                 c_arg_index,
                                                                 py_arg_index);

               g_base_info_unref( (GIBaseInfo *) interface_info);
               return arg_cache;
           }
           */
       case GI_TYPE_TAG_GLIST:
           arg_cache = _arg_cache_new_for_out_glist(type_info,
                                                   transfer);
           break;
       case GI_TYPE_TAG_GSLIST:
           arg_cache = _arg_cache_new_for_out_gslist(type_info,
                                                    transfer);
           break;
       case GI_TYPE_TAG_GHASH:
           arg_cache = _arg_cache_new_for_out_ghash(type_info);
           break;
       case GI_TYPE_TAG_ERROR:
           arg_cache = _arg_cache_new_for_out_gerror();
           break;
    }

    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = type_tag;
        arg_cache->c_arg_index = c_arg_index;
    }

    return arg_cache;
}
805

806 807 808 809 810 811 812 813
PyGIArgCache *
_arg_cache_in_new_from_type_info (GITypeInfo *type_info,
                                  PyGIFunctionCache *function_cache,
                                  GITypeTag type_tag,
                                  GITransfer transfer,
                                  GIDirection direction,
                                  gint c_arg_index,
                                  gint py_arg_index)
814
{
815
    PyGIArgCache *arg_cache = NULL;
816 817 818

    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
819
           arg_cache = _arg_cache_new_for_in_void();
820 821
           break;
       case GI_TYPE_TAG_BOOLEAN:
822
           arg_cache = _arg_cache_new_for_in_boolean();
823 824
           break;
       case GI_TYPE_TAG_INT8:
825
           arg_cache = _arg_cache_new_for_in_int8();
826 827
           break;
       case GI_TYPE_TAG_UINT8:
828
           arg_cache = _arg_cache_new_for_in_uint8();
829 830
           break;
       case GI_TYPE_TAG_INT16:
831
           arg_cache = _arg_cache_new_for_in_uint16();
832 833
           break;
       case GI_TYPE_TAG_UINT16:
834
           arg_cache = _arg_cache_new_for_in_uint16();
835 836
           break;
       case GI_TYPE_TAG_INT32:
837
           arg_cache = _arg_cache_new_for_in_int32();
838 839
           break;
       case GI_TYPE_TAG_UINT32:
840
           arg_cache = _arg_cache_new_for_in_uint32();
841 842
           break;
       case GI_TYPE_TAG_INT64:
843
           arg_cache = _arg_cache_new_for_in_int64();
844 845
           break;
       case GI_TYPE_TAG_UINT64:
846
           arg_cache = _arg_cache_new_for_in_uint64();
847 848
           break;
       case GI_TYPE_TAG_FLOAT:
849
           arg_cache = _arg_cache_new_for_in_float();
850 851
           break;
       case GI_TYPE_TAG_DOUBLE:
852
           arg_cache = _arg_cache_new_for_in_double();
853 854
           break;
       case GI_TYPE_TAG_UNICHAR:
855
           arg_cache = _arg_cache_new_for_in_unichar();
856 857
           break;
       case GI_TYPE_TAG_GTYPE:
858
           arg_cache = _arg_cache_new_for_in_gtype();
859 860
           break;
       case GI_TYPE_TAG_UTF8:
861
           arg_cache = _arg_cache_new_for_in_utf8(transfer);
862 863
           break;
       case GI_TYPE_TAG_FILENAME:
864
           arg_cache = _arg_cache_new_for_in_filename(transfer);
865 866
           break;
       case GI_TYPE_TAG_ARRAY:
867 868
           arg_cache = _arg_cache_new_for_in_array(type_info,
                                                   transfer);
869 870
           break;
       case GI_TYPE_TAG_INTERFACE:
871 872 873 874 875 876 877 878 879 880 881 882 883 884
           {
               GIInterfaceInfo *interface_info = g_type_info_get_interface(type_info);
               GIInfoType info_type = g_base_info_get_type( (GIBaseInfo *) interface_info);
               arg_cache = _arg_cache_in_new_from_interface_info(interface_info,
                                                                 function_cache,
                                                                 info_type,
                                                                 transfer,
                                                                 direction,
                                                                 c_arg_index,
                                                                 py_arg_index);

               g_base_info_unref( (GIBaseInfo *) interface_info);
               return arg_cache;
           }
885
       case GI_TYPE_TAG_GLIST:
886 887
           arg_cache = _arg_cache_new_for_in_glist(type_info,
                                                   transfer);
888 889
           break;
       case GI_TYPE_TAG_GSLIST:
890 891
           arg_cache = _arg_cache_new_for_in_gslist(type_info,
                                                    transfer);
892 893
           break;
       case GI_TYPE_TAG_GHASH:
894
           arg_cache = _arg_cache_new_for_in_ghash(type_info);
895 896
           break;
       case GI_TYPE_TAG_ERROR:
897
           arg_cache = _arg_cache_new_for_in_gerror();
898 899 900
           break;
    }

901 902 903 904 905 906 907 908 909
    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = type_tag;
        arg_cache->py_arg_index = py_arg_index;
        arg_cache->c_arg_index = c_arg_index;
    }

    return arg_cache;
910 911
}

912
static inline gboolean
John (J5) Palmieri's avatar
John (J5) Palmieri committed
913
_args_cache_generate(GIFunctionInfo *function_info,
914 915
                     PyGIFunctionCache *function_cache)
{
916
    int arg_index = 0;
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
    GITypeInfo *return_info;
    GITypeTag return_type_tag;
    PyGIArgCache *return_cache;
    /* cache the return arg */
    return_info = g_callable_info_get_return_type( (GICallableInfo *)function_info);
    return_type_tag = g_type_info_get_tag(return_info);
    return_cache = 
        _arg_cache_out_new_from_type_info(return_info,
                                          function_cache,
                                          return_type_tag,
                                          GI_TRANSFER_EVERYTHING,
                                          GI_DIRECTION_OUT,
                                          -1);

    function_cache->return_cache = return_cache;
    g_base_info_unref(return_info);

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    /* first arg is the instance */
    if (function_cache->is_method) {
        GIInterfaceInfo *interface_info;
        PyGIArgCache *instance_cache;
        GIInfoType info_type;

        interface_info = g_base_info_get_container ( (GIBaseInfo *) function_info);
        info_type = g_base_info_get_type(interface_info);

        instance_cache =
            _arg_cache_in_new_from_interface_info(interface_info,
                                                  function_cache,
                                                  info_type,
                                                  GI_TRANSFER_NOTHING,
                                                  GI_DIRECTION_IN,
                                                  arg_index,
                                                  0);

        g_base_info_unref( (GIBaseInfo *) interface_info);

        if (instance_cache == NULL)
            return FALSE;

        function_cache->args_cache[arg_index] = instance_cache;
        
        arg_index++;
        function_cache->n_in_args++;
    }

963 964

    for (; arg_index < function_cache->n_args; arg_index++) {
965
        PyGIArgCache *arg_cache = NULL;
966
        GIArgInfo *arg_info;
967
        GITypeInfo *type_info;
968 969 970 971
        GIDirection direction;
        GITransfer transfer;
        GITypeTag type_tag;
        gint py_arg_index;
972 973

        /* must be an aux arg filled in by its owner so skip */
974
        if (function_cache->args_cache[arg_index] != NULL)
975 976
            continue;

John (J5) Palmieri's avatar
John (J5) Palmieri committed
977
        arg_info =
978
            g_callable_info_get_arg( (GICallableInfo *) function_info, arg_index);
979

980 981
        direction = g_arg_info_get_direction(arg_info);
        transfer = g_arg_info_get_ownership_transfer(arg_info);
982
        type_info = g_arg_info_get_type(arg_info);
983
        type_tag = g_type_info_get_tag(type_info);
984

985
        switch(direction) {
986
            case GI_DIRECTION_IN:
987
                py_arg_index = function_cache->n_in_args;
988
                function_cache->n_in_args++;
989

990 991 992 993 994 995 996 997
                arg_cache =
                    _arg_cache_in_new_from_type_info(type_info,
                                                     function_cache,
                                                     type_tag,
                                                     transfer,
                                                     direction,
                                                     arg_index,
                                                     py_arg_index);
998

999 1000 1001
                if (arg_cache == NULL)
                    goto arg_err;

1002 1003
                function_cache->in_args =
                    g_slist_append(function_cache->in_args, arg_cache);
1004
                break;
1005 1006

            case GI_DIRECTION_OUT:
1007
                function_cache->n_out_args++;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
                PyErr_Format(PyExc_NotImplementedError,
                             "Out caching is not fully implemented yet");

                goto arg_err;

            case GI_DIRECTION_INOUT:
                PyErr_Format(PyExc_NotImplementedError,
                             "In/Out caching is not fully implemented yet");
                goto arg_err;

1018
        }
1019 1020

        function_cache->args_cache[arg_index] = arg_cache;
1021
        g_base_info_unref( (GIBaseInfo *) type_info);
1022 1023 1024 1025
        continue;
arg_err:
        g_base_info_unref( (GIBaseInfo *) type_info);
        return FALSE;
1026
    }
1027
    return TRUE;
1028 1029 1030 1031 1032
}

PyGIFunctionCache *
_pygi_function_cache_new (GIFunctionInfo *function_info)
{
1033
    PyGIFunctionCache *fc = _function_cache_new_from_function_info(function_info);
1034 1035 1036
    if (fc == NULL)
        return NULL;

1037 1038 1039
    if (!_args_cache_generate(function_info, fc))
        goto err;

1040
    return fc;
1041 1042
err:
    _pygi_function_cache_free(fc);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1043
    return NULL;
1044
}