Debian Codegraph @

gi/pygi-cache-enum.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-type.h"
22 #include "pygenum.h"
23 #include "pygflags.h"
24 #include "pygi-cache-private.h"
25
26 static gboolean
27 gi_argument_from_c_long (GIArgument *arg_out, long c_long_in,
28 GITypeTag type_tag)
29 {
30 switch (type_tag) {
31 case GI_TYPE_TAG_INT8:
32 arg_out->v_int8 = (gint8)c_long_in;
33 return TRUE;
34 case GI_TYPE_TAG_UINT8:
35 arg_out->v_uint8 = (guint8)c_long_in;
36 return TRUE;
37 case GI_TYPE_TAG_INT16:
38 arg_out->v_int16 = (gint16)c_long_in;
39 return TRUE;
40 case GI_TYPE_TAG_UINT16:
41 arg_out->v_uint16 = (guint16)c_long_in;
42 return TRUE;
43 case GI_TYPE_TAG_INT32:
44 arg_out->v_int32 = (gint32)c_long_in;
45 return TRUE;
46 case GI_TYPE_TAG_UINT32:
47 case GI_TYPE_TAG_UNICHAR:
48 arg_out->v_uint32 = (guint32)c_long_in;
49 return TRUE;
50 case GI_TYPE_TAG_INT64:
51 arg_out->v_int64 = (gint64)c_long_in;
52 return TRUE;
53 case GI_TYPE_TAG_UINT64:
54 arg_out->v_uint64 = (guint64)c_long_in;
55 return TRUE;
56 case GI_TYPE_TAG_VOID:
57 case GI_TYPE_TAG_BOOLEAN:
58 case GI_TYPE_TAG_FLOAT:
59 case GI_TYPE_TAG_DOUBLE:
60 case GI_TYPE_TAG_GTYPE:
61 case GI_TYPE_TAG_UTF8:
62 case GI_TYPE_TAG_FILENAME:
63 case GI_TYPE_TAG_ARRAY:
64 case GI_TYPE_TAG_INTERFACE:
65 case GI_TYPE_TAG_GLIST:
66 case GI_TYPE_TAG_GSLIST:
67 case GI_TYPE_TAG_GHASH:
68 case GI_TYPE_TAG_ERROR:
69 PyErr_Format (PyExc_TypeError, "Unable to marshal C long %ld to %s",
70 c_long_in, gi_type_tag_to_string (type_tag));
71 return FALSE;
72 default:
73 g_assert_not_reached ();
74 }
75 }
76
77 static gboolean
78 gi_argument_to_c_long (GIArgument *arg_in, long *c_long_out,
79 GITypeTag type_tag)
80 {
81 switch (type_tag) {
82 case GI_TYPE_TAG_INT8:
83 *c_long_out = arg_in->v_int8;
84 return TRUE;
85 case GI_TYPE_TAG_UINT8:
86 *c_long_out = arg_in->v_uint8;
87 return TRUE;
88 case GI_TYPE_TAG_INT16:
89 *c_long_out = arg_in->v_int16;
90 return TRUE;
91 case GI_TYPE_TAG_UINT16:
92 *c_long_out = arg_in->v_uint16;
93 return TRUE;
94 case GI_TYPE_TAG_INT32:
95 *c_long_out = arg_in->v_int32;
96 return TRUE;
97 case GI_TYPE_TAG_UINT32:
98 case GI_TYPE_TAG_UNICHAR:
99 *c_long_out = arg_in->v_uint32;
100 return TRUE;
101 case GI_TYPE_TAG_INT64:
102 if (arg_in->v_int64 > G_MAXLONG || arg_in->v_int64 < G_MINLONG) {
103 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to C long",
104 gi_type_tag_to_string (type_tag));
105 return FALSE;
106 }
107 *c_long_out = (glong)arg_in->v_int64;
108 return TRUE;
109 case GI_TYPE_TAG_UINT64:
110 if (arg_in->v_uint64 > G_MAXLONG) {
111 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to C long",
112 gi_type_tag_to_string (type_tag));
113 return FALSE;
114 }
115 *c_long_out = (glong)arg_in->v_uint64;
116 return TRUE;
117 case GI_TYPE_TAG_VOID:
118 case GI_TYPE_TAG_BOOLEAN:
119 case GI_TYPE_TAG_FLOAT:
120 case GI_TYPE_TAG_DOUBLE:
121 case GI_TYPE_TAG_GTYPE:
122 case GI_TYPE_TAG_UTF8:
123 case GI_TYPE_TAG_FILENAME:
124 case GI_TYPE_TAG_ARRAY:
125 case GI_TYPE_TAG_INTERFACE:
126 case GI_TYPE_TAG_GLIST:
127 case GI_TYPE_TAG_GSLIST:
128 case GI_TYPE_TAG_GHASH:
129 case GI_TYPE_TAG_ERROR:
130 PyErr_Format (PyExc_TypeError, "Unable to marshal %s to C long",
131 gi_type_tag_to_string (type_tag));
132 return FALSE;
133 default:
134 g_assert_not_reached ();
135 }
136 }
137
138 static gboolean
139 _pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
140 PyGICallableCache *callable_cache,
141 PyGIArgCache *arg_cache,
142 PyObject *py_arg, GIArgument *arg,
143 gpointer *cleanup_data)
144 {
145 gint c_int;
146 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
147 GIBaseInfo *interface = NULL;
148 GType enum_type =
149 gi_registered_type_info_get_g_type (iface_cache->interface_info);
150
151 if (pyg_enum_get_value (enum_type, py_arg, &c_int) != 0) {
152 return FALSE;
153 }
154
155 /* Write c_long into arg */
156 interface = gi_type_info_get_interface (arg_cache->type_info);
157 g_assert (GI_IS_ENUM_INFO (interface));
158 if (!gi_argument_from_c_long (
159 arg, c_int,
160 gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
161 gi_base_info_unref (interface);
162 return FALSE;
163 }
164
165
166 /* If this is not an instance of the Enum type that we want
167 * we need to check if the value is equivilant to one of the
168 * Enum's memebers */
169
170 if (!PyObject_IsInstance (py_arg, iface_cache->py_type)) {
171 unsigned int i;
172 gboolean is_found = FALSE;
173
174 for (i = 0; i < gi_enum_info_get_n_values (
175 GI_ENUM_INFO (iface_cache->interface_info));
176 i++) {
177 GIValueInfo *value_info = gi_enum_info_get_value (
178 GI_ENUM_INFO (iface_cache->interface_info), i);
179 gint64 enum_value = gi_value_info_get_value (value_info);
180 gi_base_info_unref ((GIBaseInfo *)value_info);
181 if (c_int == enum_value) {
182 is_found = TRUE;
183 break;
184 }
185 }
186
187 if (!is_found) {
188 PyErr_Format (PyExc_TypeError, "Expected a %s, but got %s",
189 iface_cache->type_name, Py_TYPE (py_arg)->tp_name);
190 gi_base_info_unref (interface);
191 return FALSE;
192 }
193 }
194
195 gi_base_info_unref (interface);
196 return TRUE;
197 }
198
199 static gboolean
200 _pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
201 PyGICallableCache *callable_cache,
202 PyGIArgCache *arg_cache,
203 PyObject *py_arg, GIArgument *arg,
204 gpointer *cleanup_data)
205 {
206 guint c_uint;
207 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
208 GIBaseInfo *interface;
209 GType flags_type =
210 gi_registered_type_info_get_g_type (iface_cache->interface_info);
211
212 if (pyg_flags_get_value (flags_type, py_arg, &c_uint) != 0) {
213 return FALSE;
214 }
215
216 /* Write c_long into arg */
217 interface = gi_type_info_get_interface (arg_cache->type_info);
218 g_assert (GI_IS_FLAGS_INFO (interface));
219 if (!gi_argument_from_c_long (
220 arg, c_uint,
221 gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
222 gi_base_info_unref (interface);
223 return FALSE;
224 }
225
226 gi_base_info_unref (interface);
227 return TRUE;
228 }
229
230 static PyObject *
231 _pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
232 PyGICallableCache *callable_cache,
233 PyGIArgCache *arg_cache, GIArgument *arg,
234 gpointer *cleanup_data)
235 {
236 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
237 GIBaseInfo *interface;
238 long c_long;
239
240 interface = gi_type_info_get_interface (arg_cache->type_info);
241 g_assert (GI_IS_ENUM_INFO (interface) && !GI_IS_FLAGS_INFO (interface));
242
243 if (!gi_argument_to_c_long (
244 arg, &c_long,
245 gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
246 return NULL;
247 }
248 gi_base_info_unref (interface);
249
250 if (c_long < INT_MIN || c_long > INT_MAX) {
251 PyErr_Format (PyExc_OverflowError, "Value %ld is not in integer range",
252 c_long);
253 return NULL;
254 }
255 return pyg_enum_val_new (iface_cache->py_type, (int)c_long);
256 }
257
258 static PyObject *
259 _pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
260 PyGICallableCache *callable_cache,
261 PyGIArgCache *arg_cache, GIArgument *arg,
262 gpointer *cleanup_data)
263 {
264 PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
265 GIBaseInfo *interface;
266 long c_long;
267
268 interface = gi_type_info_get_interface (arg_cache->type_info);
269 g_assert (GI_IS_FLAGS_INFO (interface));
270
271 if (!gi_argument_to_c_long (
272 arg, &c_long,
273 gi_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
274 gi_base_info_unref (interface);
275 return NULL;
276 }
277 gi_base_info_unref (interface);
278
279 return pyg_flags_val_new (iface_cache->py_type, (guint)c_long);
280 }
281
282 PyGIArgCache *
283 pygi_arg_enum_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
284 GITransfer transfer, PyGIDirection direction,
285 GIEnumInfo *iface_info)
286 {
287 PyGIArgCache *arg_cache = NULL;
288
289 arg_cache = pygi_arg_interface_new_from_info (
290 type_info, arg_info, transfer, direction,
291 GI_REGISTERED_TYPE_INFO (iface_info));
292 if (arg_cache == NULL) return NULL;
293
294 if (direction & PYGI_DIRECTION_FROM_PYTHON)
295 arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_enum;
296
297 if (direction & PYGI_DIRECTION_TO_PYTHON)
298 arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_enum;
299
300 return arg_cache;
301 }
302
303
304 PyGIArgCache *
305 pygi_arg_flags_new_from_info (GITypeInfo *type_info, GIArgInfo *arg_info,
306 GITransfer transfer, PyGIDirection direction,
307 GIFlagsInfo *iface_info)
308 {
309 PyGIArgCache *arg_cache = NULL;
310
311 arg_cache = pygi_arg_interface_new_from_info (
312 type_info, arg_info, transfer, direction,
313 GI_REGISTERED_TYPE_INFO (iface_info));
314 if (arg_cache == NULL) return NULL;
315
316 if (direction & PYGI_DIRECTION_FROM_PYTHON)
317 arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_flags;
318
319 if (direction & PYGI_DIRECTION_TO_PYTHON)
320 arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_flags;
321
322 return arg_cache;
323 }