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__
25 #include <girepository.h>
28 #include "pygi-invoke-state-struct.h"
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,
48 gpointer *cleanup_data);
50 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState *state,
51 PyGICallableCache *callable_cache,
52 PyGIArgCache *arg_cache,
55 typedef void (*PyGIMarshalCleanupFunc) (PyGIInvokeState *state,
56 PyGIArgCache *arg_cache,
57 PyObject *py_arg, /* always NULL for to_py cleanup */
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
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
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,
81 * Argument direction types denotes how we marshal,
82 * e.g. to Python or from Python or both.
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
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.
96 PYGI_CALLING_CONTEXT_IS_FROM_C,
97 PYGI_CALLING_CONTEXT_IS_FROM_PY
103 const gchar *arg_name;
105 PyGIMetaArgType meta_type;
107 gboolean is_caller_allocates;
110 gboolean has_default;
112 PyGIDirection direction;
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;
128 /* Set when has_default is true. */
129 GIArgument default_value;
132 typedef struct _PyGISequenceCache
134 PyGIArgCache arg_cache;
135 PyGIArgCache *item_cache;
138 typedef struct _PyGIArgGArray
140 PyGISequenceCache seq_cache;
142 gssize len_arg_index;
143 gboolean is_zero_terminated;
145 GIArrayType array_type;
148 typedef struct _PyGIInterfaceCache
150 PyGIArgCache arg_cache;
154 GIInterfaceInfo *interface_info;
156 } PyGIInterfaceCache;
158 struct _PyGICallableCache
161 const gchar *container_name;
162 const gchar *namespace;
164 PyGICallingContext calling_context;
166 PyGIArgCache *return_cache;
167 GPtrArray *args_cache;
169 GSList *arg_name_list; /* for keyword arg matching */
170 GHashTable *arg_name_hash;
173 /* Index of user_data arg that can eat variable args passed to a callable. */
174 gssize user_data_varargs_index;
176 /* Number of args already added */
179 /* Number of out args passed to g_function_info_invoke.
180 * This is used for the length of PyGIInvokeState.out_values */
183 /* Number of out args for g_function_info_invoke that will be skipped
184 * when marshaling to Python due to them being implicitly available
185 * (list/array length).
187 gssize n_to_py_child_args;
189 /* Number of Python arguments expected for invoking the gi function. */
192 /* Minimum number of args required to call the callable from Python.
193 * This count does not include args with defaults. */
194 gssize n_py_required_args;
196 void (*deinit) (PyGICallableCache *callable_cache);
198 gboolean (*generate_args_cache) (PyGICallableCache *callable_cache,
199 GICallableInfo *callable_info);
202 struct _PyGIFunctionCache {
203 PyGICallableCache callable_cache;
205 /* An invoker with ffi_cif already setup */
206 GIFunctionInvoker invoker;
208 PyObject *(*invoke) (PyGIFunctionCache *function_cache,
209 PyGIInvokeState *state,
211 PyObject *py_kwargs);
214 struct _PyGIVFuncCache {
215 PyGIFunctionWithInstanceCache fwi_cache;
222 pygi_arg_base_setup (PyGIArgCache *arg_cache,
223 GITypeInfo *type_info,
224 GIArgInfo *arg_info, /* may be NULL for return arguments */
226 PyGIDirection direction);
229 pygi_arg_interface_setup (PyGIInterfaceCache *iface_cache,
230 GITypeInfo *type_info,
231 GIArgInfo *arg_info, /* may be NULL for return arguments */
233 PyGIDirection direction,
234 GIInterfaceInfo *iface_info);
237 pygi_arg_sequence_setup (PyGISequenceCache *sc,
238 GITypeInfo *type_info,
239 GIArgInfo *arg_info, /* may be NULL for return arguments */
241 PyGIDirection direction,
242 PyGICallableCache *callable_cache);
245 pygi_arg_interface_new_from_info (GITypeInfo *type_info,
246 GIArgInfo *arg_info, /* may be NULL for return arguments */
248 PyGIDirection direction,
249 GIInterfaceInfo *iface_info);
252 pygi_arg_cache_alloc (void);
255 pygi_arg_cache_new (GITypeInfo *type_info,
258 PyGIDirection direction,
259 PyGICallableCache *callable_cache,
260 /* will be removed */
262 gssize py_arg_index);
265 pygi_arg_cache_free (PyGIArgCache *cache);
268 pygi_callable_cache_free (PyGICallableCache *cache);
271 pygi_callable_cache_get_full_name (PyGICallableCache *cache);
274 pygi_function_cache_new (GICallableInfo *info);
277 pygi_function_cache_invoke (PyGIFunctionCache *function_cache,
279 PyObject *py_kwargs);
282 pygi_ccallback_cache_new (GICallableInfo *info,
283 GCallback function_ptr);
286 pygi_ccallback_cache_invoke (PyGIFunctionCache *function_cache,
292 pygi_constructor_cache_new (GICallableInfo *info);
295 pygi_method_cache_new (GICallableInfo *info);
298 pygi_vfunc_cache_new (GICallableInfo *info);
301 pygi_closure_cache_new (GICallableInfo *info);
303 #define _pygi_callable_cache_args_len(cache) ((cache)->args_cache)->len
305 inline static PyGIArgCache *
306 _pygi_callable_cache_get_arg (PyGICallableCache *cache, guint index) {
307 return (PyGIArgCache *) g_ptr_array_index (cache->args_cache, index);
311 _pygi_callable_cache_set_arg (PyGICallableCache *cache, guint index, PyGIArgCache *arg_cache) {
312 cache->args_cache->pdata[index] = arg_cache;
317 #endif /* __PYGI_CACHE_H__ */