Add gitlab CI tests
[pygobject.git] / gi / pygi-cache.h
blob4dfabd863f36d19cc6f85c8313743262e1447da1
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5 * Copyright (C) 2013 Simon Feltman <sfeltman@gnome.org>
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.
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.
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/>.
21 #ifndef __PYGI_CACHE_H__
22 #define __PYGI_CACHE_H__
24 #include <Python.h>
25 #include <girepository.h>
26 #include <girffi.h>
28 #include "pygi-invoke-state-struct.h"
30 G_BEGIN_DECLS
32 typedef struct _PyGIArgCache PyGIArgCache;
33 typedef struct _PyGICallableCache PyGICallableCache;
34 typedef struct _PyGIFunctionCache PyGIFunctionCache;
35 typedef struct _PyGIVFuncCache PyGIVFuncCache;
37 typedef PyGIFunctionCache PyGICCallbackCache;
38 typedef PyGIFunctionCache PyGIConstructorCache;
39 typedef PyGIFunctionCache PyGIFunctionWithInstanceCache;
40 typedef PyGIFunctionCache PyGIMethodCache;
41 typedef PyGICallableCache PyGIClosureCache;
43 typedef gboolean (*PyGIMarshalFromPyFunc) (PyGIInvokeState *state,
44 PyGICallableCache *callable_cache,
45 PyGIArgCache *arg_cache,
46 PyObject *py_arg,
47 GIArgument *arg,
48 gpointer *cleanup_data);
50 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState *state,
51 PyGICallableCache *callable_cache,
52 PyGIArgCache *arg_cache,
53 GIArgument *arg);
55 typedef void (*PyGIMarshalCleanupFunc) (PyGIInvokeState *state,
56 PyGIArgCache *arg_cache,
57 PyObject *py_arg, /* always NULL for to_py cleanup */
58 gpointer data,
59 gboolean was_processed);
61 /* Argument meta types denote how we process the argument:
62 * - PYGI_META_ARG_TYPE_PARENT - parents may or may not have children
63 * but are always processed via the normal marshaller for their
64 * actual GI type. If they have children the marshaller will
65 * also handle marshalling the children.
66 * - PYGI_META_ARG_TYPE_CHILD - Children without python argument are
67 * ignored by the marshallers and handled directly by their parents
68 * marshaller.
69 * - Children with pyargs (PYGI_META_ARG_TYPE_CHILD_WITH_PYARG) are processed
70 * the same as other child args but also have an index into the
71 * python parameters passed to the invoker
73 typedef enum {
74 PYGI_META_ARG_TYPE_PARENT,
75 PYGI_META_ARG_TYPE_CHILD,
76 PYGI_META_ARG_TYPE_CHILD_WITH_PYARG,
77 PYGI_META_ARG_TYPE_CLOSURE,
78 } PyGIMetaArgType;
81 * Argument direction types denotes how we marshal,
82 * e.g. to Python or from Python or both.
84 typedef enum {
85 PYGI_DIRECTION_TO_PYTHON = 1 << 0,
86 PYGI_DIRECTION_FROM_PYTHON = 1 << 1,
87 PYGI_DIRECTION_BIDIRECTIONAL = PYGI_DIRECTION_TO_PYTHON | PYGI_DIRECTION_FROM_PYTHON
88 } PyGIDirection;
91 * In PyGI IN and OUT arguments mean different things depending on the context
92 * of the callable, e.g. is it a callback that is being called from C or a
93 * function that is being called from Python.
95 typedef enum {
96 PYGI_CALLING_CONTEXT_IS_FROM_C,
97 PYGI_CALLING_CONTEXT_IS_FROM_PY
98 } PyGICallingContext;
101 struct _PyGIArgCache
103 const gchar *arg_name;
105 PyGIMetaArgType meta_type;
106 gboolean is_pointer;
107 gboolean is_caller_allocates;
108 gboolean is_skipped;
109 gboolean allow_none;
110 gboolean has_default;
112 PyGIDirection direction;
113 GITransfer transfer;
114 GITypeTag type_tag;
115 GITypeInfo *type_info;
117 PyGIMarshalFromPyFunc from_py_marshaller;
118 PyGIMarshalToPyFunc to_py_marshaller;
120 PyGIMarshalCleanupFunc from_py_cleanup;
121 PyGIMarshalCleanupFunc to_py_cleanup;
123 GDestroyNotify destroy_notify;
125 gssize c_arg_index;
126 gssize py_arg_index;
128 /* Set when has_default is true. */
129 GIArgument default_value;
132 typedef struct _PyGISequenceCache
134 PyGIArgCache arg_cache;
135 PyGIArgCache *item_cache;
136 } PyGISequenceCache;
138 typedef struct _PyGIArgGArray
140 PyGISequenceCache seq_cache;
141 gssize fixed_size;
142 gssize len_arg_index;
143 gboolean is_zero_terminated;
144 gsize item_size;
145 GIArrayType array_type;
146 } PyGIArgGArray;
148 typedef struct _PyGIInterfaceCache
150 PyGIArgCache arg_cache;
151 gboolean is_foreign;
152 GType g_type;
153 PyObject *py_type;
154 GIInterfaceInfo *interface_info;
155 gchar *type_name;
156 } PyGIInterfaceCache;
158 struct _PyGICallableCache
160 const gchar *name;
161 const gchar *container_name;
162 const gchar *namespace;
164 PyGICallingContext calling_context;
166 PyGIArgCache *return_cache;
167 GPtrArray *args_cache;
168 GSList *to_py_args;
169 GSList *arg_name_list; /* for keyword arg matching */
170 GHashTable *arg_name_hash;
171 gboolean throws;
173 /* Index of user_data arg passed to a callable. */
174 gssize user_data_index;
176 /* Index of user_data arg that can eat variable args passed to a callable. */
177 gssize user_data_varargs_index;
179 /* Number of args already added */
180 gssize args_offset;
182 /* Number of out args passed to g_function_info_invoke.
183 * This is used for the length of PyGIInvokeState.out_values */
184 gssize n_to_py_args;
186 /* If the callable return value gets used */
187 gboolean has_return;
189 /* The type used for returning multiple values or NULL */
190 PyTypeObject* resulttuple_type;
192 /* Number of out args for g_function_info_invoke that will be skipped
193 * when marshaling to Python due to them being implicitly available
194 * (list/array length).
196 gssize n_to_py_child_args;
198 /* Number of Python arguments expected for invoking the gi function. */
199 gssize n_py_args;
201 /* Minimum number of args required to call the callable from Python.
202 * This count does not include args with defaults. */
203 gssize n_py_required_args;
205 void (*deinit) (PyGICallableCache *callable_cache);
207 gboolean (*generate_args_cache) (PyGICallableCache *callable_cache,
208 GICallableInfo *callable_info);
211 struct _PyGIFunctionCache {
212 PyGICallableCache callable_cache;
214 /* An invoker with ffi_cif already setup */
215 GIFunctionInvoker invoker;
217 PyObject *(*invoke) (PyGIFunctionCache *function_cache,
218 PyGIInvokeState *state,
219 PyObject *py_args,
220 PyObject *py_kwargs);
223 struct _PyGIVFuncCache {
224 PyGIFunctionWithInstanceCache fwi_cache;
226 GIBaseInfo *info;
230 gboolean
231 pygi_arg_base_setup (PyGIArgCache *arg_cache,
232 GITypeInfo *type_info,
233 GIArgInfo *arg_info, /* may be NULL for return arguments */
234 GITransfer transfer,
235 PyGIDirection direction);
237 gboolean
238 pygi_arg_interface_setup (PyGIInterfaceCache *iface_cache,
239 GITypeInfo *type_info,
240 GIArgInfo *arg_info, /* may be NULL for return arguments */
241 GITransfer transfer,
242 PyGIDirection direction,
243 GIInterfaceInfo *iface_info);
245 gboolean
246 pygi_arg_sequence_setup (PyGISequenceCache *sc,
247 GITypeInfo *type_info,
248 GIArgInfo *arg_info, /* may be NULL for return arguments */
249 GITransfer transfer,
250 PyGIDirection direction,
251 PyGICallableCache *callable_cache);
253 PyGIArgCache *
254 pygi_arg_interface_new_from_info (GITypeInfo *type_info,
255 GIArgInfo *arg_info, /* may be NULL for return arguments */
256 GITransfer transfer,
257 PyGIDirection direction,
258 GIInterfaceInfo *iface_info);
260 PyGIArgCache *
261 pygi_arg_cache_alloc (void);
263 PyGIArgCache *
264 pygi_arg_cache_new (GITypeInfo *type_info,
265 GIArgInfo *arg_info,
266 GITransfer transfer,
267 PyGIDirection direction,
268 PyGICallableCache *callable_cache,
269 /* will be removed */
270 gssize c_arg_index,
271 gssize py_arg_index);
273 void
274 pygi_arg_cache_free (PyGIArgCache *cache);
276 void
277 pygi_callable_cache_free (PyGICallableCache *cache);
279 gchar *
280 pygi_callable_cache_get_full_name (PyGICallableCache *cache);
282 PyGIFunctionCache *
283 pygi_function_cache_new (GICallableInfo *info);
285 PyObject *
286 pygi_function_cache_invoke (PyGIFunctionCache *function_cache,
287 PyObject *py_args,
288 PyObject *py_kwargs);
290 PyGIFunctionCache *
291 pygi_ccallback_cache_new (GICallableInfo *info,
292 GCallback function_ptr);
294 PyObject *
295 pygi_ccallback_cache_invoke (PyGIFunctionCache *function_cache,
296 PyObject *py_args,
297 PyObject *py_kwargs,
298 gpointer user_data);
300 PyGIFunctionCache *
301 pygi_constructor_cache_new (GICallableInfo *info);
303 PyGIFunctionCache *
304 pygi_method_cache_new (GICallableInfo *info);
306 PyGIFunctionCache *
307 pygi_vfunc_cache_new (GICallableInfo *info);
309 PyGIClosureCache *
310 pygi_closure_cache_new (GICallableInfo *info);
312 #define _pygi_callable_cache_args_len(cache) ((cache)->args_cache)->len
314 inline static PyGIArgCache *
315 _pygi_callable_cache_get_arg (PyGICallableCache *cache, guint index) {
316 return (PyGIArgCache *) g_ptr_array_index (cache->args_cache, index);
319 inline static void
320 _pygi_callable_cache_set_arg (PyGICallableCache *cache, guint index, PyGIArgCache *arg_cache) {
321 cache->args_cache->pdata[index] = arg_cache;
324 G_END_DECLS
326 #endif /* __PYGI_CACHE_H__ */