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 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 */
182 /* Number of out args passed to g_function_info_invoke.
183 * This is used for the length of PyGIInvokeState.out_values */
186 /* If the callable return value gets used */
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. */
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
,
220 PyObject
*py_kwargs
);
223 struct _PyGIVFuncCache
{
224 PyGIFunctionWithInstanceCache fwi_cache
;
231 pygi_arg_base_setup (PyGIArgCache
*arg_cache
,
232 GITypeInfo
*type_info
,
233 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
235 PyGIDirection direction
);
238 pygi_arg_interface_setup (PyGIInterfaceCache
*iface_cache
,
239 GITypeInfo
*type_info
,
240 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
242 PyGIDirection direction
,
243 GIInterfaceInfo
*iface_info
);
246 pygi_arg_sequence_setup (PyGISequenceCache
*sc
,
247 GITypeInfo
*type_info
,
248 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
250 PyGIDirection direction
,
251 PyGICallableCache
*callable_cache
);
254 pygi_arg_interface_new_from_info (GITypeInfo
*type_info
,
255 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
257 PyGIDirection direction
,
258 GIInterfaceInfo
*iface_info
);
261 pygi_arg_cache_alloc (void);
264 pygi_arg_cache_new (GITypeInfo
*type_info
,
267 PyGIDirection direction
,
268 PyGICallableCache
*callable_cache
,
269 /* will be removed */
271 gssize py_arg_index
);
274 pygi_arg_cache_free (PyGIArgCache
*cache
);
277 pygi_callable_cache_free (PyGICallableCache
*cache
);
280 pygi_callable_cache_get_full_name (PyGICallableCache
*cache
);
283 pygi_function_cache_new (GICallableInfo
*info
);
286 pygi_function_cache_invoke (PyGIFunctionCache
*function_cache
,
288 PyObject
*py_kwargs
);
291 pygi_ccallback_cache_new (GICallableInfo
*info
,
292 GCallback function_ptr
);
295 pygi_ccallback_cache_invoke (PyGIFunctionCache
*function_cache
,
301 pygi_constructor_cache_new (GICallableInfo
*info
);
304 pygi_method_cache_new (GICallableInfo
*info
);
307 pygi_vfunc_cache_new (GICallableInfo
*info
);
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
);
320 _pygi_callable_cache_set_arg (PyGICallableCache
*cache
, guint index
, PyGIArgCache
*arg_cache
) {
321 cache
->args_cache
->pdata
[index
] = arg_cache
;
326 #endif /* __PYGI_CACHE_H__ */