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
,
54 gpointer
*cleanup_data
);
56 typedef void (*PyGIMarshalCleanupFunc
) (PyGIInvokeState
*state
,
57 PyGIArgCache
*arg_cache
,
60 gboolean was_processed
);
62 typedef void (*PyGIMarshalToPyCleanupFunc
) (PyGIInvokeState
*state
,
63 PyGIArgCache
*arg_cache
,
64 gpointer cleanup_data
,
66 gboolean was_processed
);
68 /* Argument meta types denote how we process the argument:
69 * - PYGI_META_ARG_TYPE_PARENT - parents may or may not have children
70 * but are always processed via the normal marshaller for their
71 * actual GI type. If they have children the marshaller will
72 * also handle marshalling the children.
73 * - PYGI_META_ARG_TYPE_CHILD - Children without python argument are
74 * ignored by the marshallers and handled directly by their parents
76 * - Children with pyargs (PYGI_META_ARG_TYPE_CHILD_WITH_PYARG) are processed
77 * the same as other child args but also have an index into the
78 * python parameters passed to the invoker
81 PYGI_META_ARG_TYPE_PARENT
,
82 PYGI_META_ARG_TYPE_CHILD
,
83 PYGI_META_ARG_TYPE_CHILD_WITH_PYARG
,
84 PYGI_META_ARG_TYPE_CLOSURE
,
88 * Argument direction types denotes how we marshal,
89 * e.g. to Python or from Python or both.
92 PYGI_DIRECTION_TO_PYTHON
= 1 << 0,
93 PYGI_DIRECTION_FROM_PYTHON
= 1 << 1,
94 PYGI_DIRECTION_BIDIRECTIONAL
= PYGI_DIRECTION_TO_PYTHON
| PYGI_DIRECTION_FROM_PYTHON
98 * In PyGI IN and OUT arguments mean different things depending on the context
99 * of the callable, e.g. is it a callback that is being called from C or a
100 * function that is being called from Python.
103 PYGI_CALLING_CONTEXT_IS_FROM_C
,
104 PYGI_CALLING_CONTEXT_IS_FROM_PY
105 } PyGICallingContext
;
110 const gchar
*arg_name
;
112 PyGIMetaArgType meta_type
;
114 gboolean is_caller_allocates
;
117 gboolean has_default
;
119 PyGIDirection direction
;
122 GITypeInfo
*type_info
;
124 PyGIMarshalFromPyFunc from_py_marshaller
;
125 PyGIMarshalToPyFunc to_py_marshaller
;
127 PyGIMarshalCleanupFunc from_py_cleanup
;
128 PyGIMarshalToPyCleanupFunc to_py_cleanup
;
130 GDestroyNotify destroy_notify
;
135 /* Set when has_default is true. */
136 GIArgument default_value
;
139 typedef struct _PyGISequenceCache
141 PyGIArgCache arg_cache
;
142 PyGIArgCache
*item_cache
;
145 typedef struct _PyGIArgGArray
147 PyGISequenceCache seq_cache
;
149 gssize len_arg_index
;
150 gboolean is_zero_terminated
;
152 GIArrayType array_type
;
155 typedef struct _PyGIInterfaceCache
157 PyGIArgCache arg_cache
;
161 GIInterfaceInfo
*interface_info
;
163 } PyGIInterfaceCache
;
165 struct _PyGICallableCache
168 const gchar
*container_name
;
169 const gchar
*namespace;
171 PyGICallingContext calling_context
;
173 PyGIArgCache
*return_cache
;
174 GPtrArray
*args_cache
;
176 GSList
*arg_name_list
; /* for keyword arg matching */
177 GHashTable
*arg_name_hash
;
180 /* Index of user_data arg passed to a callable. */
181 gssize user_data_index
;
183 /* Index of user_data arg that can eat variable args passed to a callable. */
184 gssize user_data_varargs_index
;
186 /* Number of args already added */
189 /* Number of out args passed to g_function_info_invoke.
190 * This is used for the length of PyGIInvokeState.out_values */
193 /* If the callable return value gets used */
196 /* The type used for returning multiple values or NULL */
197 PyTypeObject
* resulttuple_type
;
199 /* Number of out args for g_function_info_invoke that will be skipped
200 * when marshaling to Python due to them being implicitly available
201 * (list/array length).
203 gssize n_to_py_child_args
;
205 /* Number of Python arguments expected for invoking the gi function. */
208 /* Minimum number of args required to call the callable from Python.
209 * This count does not include args with defaults. */
210 gssize n_py_required_args
;
212 void (*deinit
) (PyGICallableCache
*callable_cache
);
214 gboolean (*generate_args_cache
) (PyGICallableCache
*callable_cache
,
215 GICallableInfo
*callable_info
);
218 struct _PyGIFunctionCache
{
219 PyGICallableCache callable_cache
;
221 /* An invoker with ffi_cif already setup */
222 GIFunctionInvoker invoker
;
224 PyObject
*(*invoke
) (PyGIFunctionCache
*function_cache
,
225 PyGIInvokeState
*state
,
227 PyObject
*py_kwargs
);
230 struct _PyGIVFuncCache
{
231 PyGIFunctionWithInstanceCache fwi_cache
;
238 pygi_arg_base_setup (PyGIArgCache
*arg_cache
,
239 GITypeInfo
*type_info
,
240 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
242 PyGIDirection direction
);
245 pygi_arg_interface_setup (PyGIInterfaceCache
*iface_cache
,
246 GITypeInfo
*type_info
,
247 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
249 PyGIDirection direction
,
250 GIInterfaceInfo
*iface_info
);
253 pygi_arg_sequence_setup (PyGISequenceCache
*sc
,
254 GITypeInfo
*type_info
,
255 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
257 PyGIDirection direction
,
258 PyGICallableCache
*callable_cache
);
261 pygi_arg_interface_new_from_info (GITypeInfo
*type_info
,
262 GIArgInfo
*arg_info
, /* may be NULL for return arguments */
264 PyGIDirection direction
,
265 GIInterfaceInfo
*iface_info
);
268 pygi_arg_cache_alloc (void);
271 pygi_arg_cache_new (GITypeInfo
*type_info
,
274 PyGIDirection direction
,
275 PyGICallableCache
*callable_cache
,
276 /* will be removed */
278 gssize py_arg_index
);
281 pygi_arg_cache_free (PyGIArgCache
*cache
);
284 pygi_callable_cache_free (PyGICallableCache
*cache
);
287 pygi_callable_cache_get_full_name (PyGICallableCache
*cache
);
290 pygi_function_cache_new (GICallableInfo
*info
);
293 pygi_function_cache_invoke (PyGIFunctionCache
*function_cache
,
295 PyObject
*py_kwargs
);
298 pygi_ccallback_cache_new (GICallableInfo
*info
,
299 GCallback function_ptr
);
302 pygi_ccallback_cache_invoke (PyGIFunctionCache
*function_cache
,
308 pygi_constructor_cache_new (GICallableInfo
*info
);
311 pygi_method_cache_new (GICallableInfo
*info
);
314 pygi_vfunc_cache_new (GICallableInfo
*info
);
317 pygi_closure_cache_new (GICallableInfo
*info
);
320 _pygi_callable_cache_args_len (PyGICallableCache
*cache
) {
321 return ((cache
)->args_cache
)->len
;
324 inline static PyGIArgCache
*
325 _pygi_callable_cache_get_arg (PyGICallableCache
*cache
, guint index
) {
326 return (PyGIArgCache
*) g_ptr_array_index (cache
->args_cache
, index
);
330 _pygi_callable_cache_set_arg (PyGICallableCache
*cache
, guint index
, PyGIArgCache
*arg_cache
) {
331 cache
->args_cache
->pdata
[index
] = arg_cache
;
336 #endif /* __PYGI_CACHE_H__ */