Debian Codegraph @

gi/pygi-cache-basic.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-basictype.h"
22 #include "pygi-cache-private.h"
23
24
25 /* pygi_arg_base_setup:
26 * arg_cache: argument cache to initialize
27 * type_info: source for type related attributes to cache
28 * arg_info: (allow-none): source for argument related attributes to cache
29 * transfer: transfer mode to store in the argument cache
30 * direction: marshaling direction to store in the cache
31 *
32 * Initializer for PyGIArgCache
33 */
34 void
35 pygi_arg_base_setup (
36 PyGIArgCache *arg_cache, GITypeInfo *type_info,
37 GIArgInfo *arg_info, /* may be NULL for return arguments */
38 GITransfer transfer, PyGIDirection direction)
39 {
40 arg_cache->direction = direction;
41 arg_cache->transfer = transfer;
42 arg_cache->py_arg_index = -1;
43 arg_cache->c_arg_index = -1;
44
45 if (type_info != NULL) {
46 arg_cache->is_pointer = gi_type_info_is_pointer (type_info);
47 arg_cache->type_tag = gi_type_info_get_tag (type_info);
48 gi_base_info_ref ((GIBaseInfo *)type_info);
49 arg_cache->type_info = type_info;
50 }
51
52 if (arg_info != NULL) {
53 gi_base_info_ref ((GIBaseInfo *)arg_info);
54 arg_cache->arg_info = arg_info;
55 }
56 }
57
58 gboolean
59 pygi_marshal_from_py_basic_type_cache_adapter (
60 PyGIInvokeState *state, PyGICallableCache *callable_cache,
61 PyGIArgCache *arg_cache, PyObject *py_arg, GIArgument *arg,
62 gpointer *cleanup_data)
63 {
64 *arg = pygi_marshal_from_py_basic_type (py_arg, arg_cache->type_tag,
65 arg_cache->transfer, cleanup_data);
66 return !PyErr_Occurred ();
67 }
68
69 PyObject *
70 pygi_marshal_to_py_basic_type_cache_adapter (PyGIInvokeState *state,
71 PyGICallableCache *callable_cache,
72 PyGIArgCache *arg_cache,
73 GIArgument *arg,
74 gpointer *cleanup_data)
75 {
76 return pygi_marshal_to_py_basic_type (*arg, arg_cache->type_tag,
77 arg_cache->transfer);
78 }
79
80 static gboolean
81 marshal_from_py_void (PyGIInvokeState *state,
82 PyGICallableCache *callable_cache,
83 PyGIArgCache *arg_cache, PyObject *py_arg,
84 GIArgument *arg, gpointer *cleanup_data)
85 {
86 g_warn_if_fail (arg_cache->transfer == GI_TRANSFER_NOTHING);
87
88 if (pygi_gpointer_from_py (py_arg, &(arg->v_pointer))) {
89 *cleanup_data = arg->v_pointer;
90 return TRUE;
91 }
92
93 return FALSE;
94 }
95
96 static PyObject *
97 marshal_to_py_void (PyGIInvokeState *state, PyGICallableCache *callable_cache,
98 PyGIArgCache *arg_cache, GIArgument *arg,
99 gpointer *cleanup_data)
100 {
101 if (arg_cache->is_pointer) {
102 return PyLong_FromVoidPtr (arg->v_pointer);
103 }
104 Py_RETURN_NONE;
105 }
106
107 static void
108 marshal_cleanup_from_py_utf8 (PyGIInvokeState *state, PyGIArgCache *arg_cache,
109 PyObject *py_arg, gpointer data,
110 gboolean was_processed)
111 {
112 /* We strdup strings so free unless ownership is transferred to C. */
113 if (was_processed && arg_cache->transfer == GI_TRANSFER_NOTHING)
114 g_free (data);
115 }
116
117 static void
118 marshal_cleanup_to_py_utf8 (PyGIInvokeState *state, PyGIArgCache *arg_cache,
119 gpointer cleanup_data, gpointer data,
120 gboolean was_processed)
121 {
122 /* Python copies the string so we need to free it
123 if the interface is transfering ownership,
124 whether or not it has been processed yet */
125 if (arg_cache->transfer == GI_TRANSFER_EVERYTHING) g_free (data);
126 }
127
128 PyGIArgCache *
129 pygi_arg_void_type_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
130 GITransfer transfer, PyGIDirection direction)
131 {
132 PyGIArgCache *arg_cache = pygi_arg_cache_alloc ();
133
134 pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction);
135
136 if (direction & PYGI_DIRECTION_FROM_PYTHON)
137 arg_cache->from_py_marshaller = marshal_from_py_void;
138
139 if (direction & PYGI_DIRECTION_TO_PYTHON)
140 arg_cache->to_py_marshaller = marshal_to_py_void;
141
142 return arg_cache;
143 }
144
145 PyGIArgCache *
146 pygi_arg_numeric_type_new_from_info (GITypeInfo *type_info,
147 GIArgInfo *arg_info, GITransfer transfer,
148 PyGIDirection direction)
149 {
150 PyGIArgCache *arg_cache = pygi_arg_cache_alloc ();
151
152 pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction);
153
154 if (direction & PYGI_DIRECTION_FROM_PYTHON)
155 arg_cache->from_py_marshaller =
156 pygi_marshal_from_py_basic_type_cache_adapter;
157
158 if (direction & PYGI_DIRECTION_TO_PYTHON)
159 arg_cache->to_py_marshaller =
160 pygi_marshal_to_py_basic_type_cache_adapter;
161
162 return arg_cache;
163 }
164
165
166 PyGIArgCache *
167 pygi_arg_string_type_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
168 GITransfer transfer,
169 PyGIDirection direction)
170 {
171 PyGIArgCache *arg_cache = pygi_arg_cache_alloc ();
172
173 pygi_arg_base_setup (arg_cache, type_info, arg_info, transfer, direction);
174
175 if (direction & PYGI_DIRECTION_FROM_PYTHON) {
176 arg_cache->from_py_marshaller =
177 pygi_marshal_from_py_basic_type_cache_adapter;
178 arg_cache->from_py_cleanup = marshal_cleanup_from_py_utf8;
179 }
180
181 if (direction & PYGI_DIRECTION_TO_PYTHON) {
182 arg_cache->to_py_marshaller =
183 pygi_marshal_to_py_basic_type_cache_adapter;
184 arg_cache->to_py_cleanup = marshal_cleanup_to_py_utf8;
185 }
186
187 return arg_cache;
188 }