Debian Codegraph @

gi/pygi-cache-interface.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2011 John (J5) Palmieri <[email protected]>
5 * Copyright (C) 2014 Simon Feltman <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "pygi-info.h"
22 #include "pygi-type.h"
23 #include "pygi-cache-private.h"
24
25 static void
26 _interface_cache_free_func (PyGIInterfaceCache *cache)
27 {
28 if (cache != NULL) {
29 Py_XDECREF (cache->py_type);
30 if (cache->type_name != NULL) g_free (cache->type_name);
31 if (cache->interface_info != NULL)
32 gi_base_info_unref ((GIBaseInfo *)cache->interface_info);
33 g_slice_free (PyGIInterfaceCache, cache);
34 }
35 }
36
37 /* pygi_arg_interface_setup:
38 * arg_cache: argument cache to initialize
39 * type_info: source for type related attributes to cache
40 * arg_info: (allow-none): source for argument related attributes to cache
41 * transfer: transfer mode to store in the argument cache
42 * direction: marshaling direction to store in the cache
43 * iface_info: interface info to cache
44 *
45 * Initializer for PyGIInterfaceCache
46 *
47 * Returns: TRUE on success and FALSE on failure
48 */
49 static gboolean
50 pygi_arg_interface_setup (
51 PyGIInterfaceCache *iface_cache, GITypeInfo *type_info,
52 GIArgInfo *arg_info, /* may be NULL for return arguments */
53 GITransfer transfer, PyGIDirection direction,
54 GIRegisteredTypeInfo *iface_info)
55 {
56 GIBaseInfo *base_info = GI_BASE_INFO (iface_info);
57
58 pygi_arg_base_setup ((PyGIArgCache *)iface_cache, type_info, arg_info,
59 transfer, direction);
60
61 ((PyGIArgCache *)iface_cache)->destroy_notify =
62 (GDestroyNotify)_interface_cache_free_func;
63
64 gi_base_info_ref (base_info);
65 iface_cache->interface_info = iface_info;
66 iface_cache->arg_cache.type_tag = GI_TYPE_TAG_INTERFACE;
67 iface_cache->type_name = _pygi_gi_base_info_get_fullname (base_info);
68 iface_cache->g_type = gi_registered_type_info_get_g_type (iface_info);
69 iface_cache->py_type = pygi_type_import_by_gi_info (base_info);
70
71 if (g_type_is_a (iface_cache->g_type, G_TYPE_OBJECT)) {
72 if (g_str_equal (g_type_name (iface_cache->g_type), "GCancellable"))
73 iface_cache->arg_cache.async_context =
74 PYGI_ASYNC_CONTEXT_CANCELLABLE;
75 }
76
77 if (iface_cache->py_type == NULL) {
78 return FALSE;
79 }
80
81 return TRUE;
82 }
83
84 PyGIArgCache *
85 pygi_arg_interface_new_from_info (
86 GITypeInfo *type_info,
87 GIArgInfo *arg_info, /* may be NULL for return arguments */
88 GITransfer transfer, PyGIDirection direction,
89 GIRegisteredTypeInfo *iface_info)
90 {
91 PyGIInterfaceCache *ic;
92
93 ic = g_slice_new0 (PyGIInterfaceCache);
94 if (!pygi_arg_interface_setup (ic, type_info, arg_info, transfer,
95 direction, iface_info)) {
96 pygi_arg_cache_free ((PyGIArgCache *)ic);
97 return NULL;
98 }
99
100 return (PyGIArgCache *)ic;
101 }