Imported Upstream version 3.15.91
[platform/upstream/pygobject2.git] / gi / pygi-cache.h
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
5  * Copyright (C) 2013 Simon Feltman <sfeltman@gnome.org>
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 #ifndef __PYGI_CACHE_H__
22 #define __PYGI_CACHE_H__
23
24 #include <Python.h>
25 #include <girepository.h>
26 #include <girffi.h>
27
28 #include "pygi-invoke-state-struct.h"
29
30 G_BEGIN_DECLS
31
32 typedef struct _PyGIArgCache PyGIArgCache;
33 typedef struct _PyGICallableCache PyGICallableCache;
34 typedef struct _PyGIFunctionCache PyGIFunctionCache;
35 typedef struct _PyGIVFuncCache PyGIVFuncCache;
36
37 typedef PyGIFunctionCache PyGICCallbackCache;
38 typedef PyGIFunctionCache PyGIConstructorCache;
39 typedef PyGIFunctionCache PyGIFunctionWithInstanceCache;
40 typedef PyGIFunctionCache PyGIMethodCache;
41 typedef PyGICallableCache PyGIClosureCache;
42
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);
49
50 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState   *state,
51                                           PyGICallableCache *callable_cache,
52                                           PyGIArgCache      *arg_cache,
53                                           GIArgument        *arg);
54
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);
60
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
72  */
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;
79
80 /*
81  * Argument direction types denotes how we marshal,
82  * e.g. to Python or from Python or both.
83  */
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;
89
90 /*
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.
94  */
95 typedef enum {
96     PYGI_CALLING_CONTEXT_IS_FROM_C,
97     PYGI_CALLING_CONTEXT_IS_FROM_PY
98 } PyGICallingContext;
99
100
101 struct _PyGIArgCache
102 {
103     const gchar *arg_name;
104
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;
111
112     PyGIDirection direction;
113     GITransfer transfer;
114     GITypeTag type_tag;
115     GITypeInfo *type_info;
116
117     PyGIMarshalFromPyFunc from_py_marshaller;
118     PyGIMarshalToPyFunc to_py_marshaller;
119
120     PyGIMarshalCleanupFunc from_py_cleanup;
121     PyGIMarshalCleanupFunc to_py_cleanup;
122
123     GDestroyNotify destroy_notify;
124
125     gssize c_arg_index;
126     gssize py_arg_index;
127
128     /* Set when has_default is true. */
129     GIArgument default_value;
130 };
131
132 typedef struct _PyGISequenceCache
133 {
134     PyGIArgCache arg_cache;
135     PyGIArgCache *item_cache;
136 } PyGISequenceCache;
137
138 typedef struct _PyGIArgGArray
139 {
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;
147
148 typedef struct _PyGIInterfaceCache
149 {
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;
157
158 struct _PyGICallableCache
159 {
160     const gchar *name;
161     const gchar *container_name;
162     const gchar *namespace;
163
164     PyGICallingContext calling_context;
165
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;
172
173     /* Index of user_data arg that can eat variable args passed to a callable. */
174     gssize user_data_varargs_index;
175
176     /* Number of args already added */
177     gssize args_offset;
178
179     /* Number of out args passed to g_function_info_invoke.
180      * This is used for the length of PyGIInvokeState.out_values */
181     gssize n_to_py_args;
182
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).
186      */
187     gssize n_to_py_child_args;
188
189     /* Number of Python arguments expected for invoking the gi function. */
190     gssize n_py_args;
191
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;
195
196     void     (*deinit)              (PyGICallableCache *callable_cache);
197
198     gboolean (*generate_args_cache) (PyGICallableCache *callable_cache,
199                                      GICallableInfo *callable_info);
200 };
201
202 struct _PyGIFunctionCache {
203     PyGICallableCache callable_cache;
204
205     /* An invoker with ffi_cif already setup */
206     GIFunctionInvoker invoker;
207
208     PyObject *(*invoke) (PyGIFunctionCache *function_cache,
209                          PyGIInvokeState *state,
210                          PyObject *py_args,
211                          PyObject *py_kwargs);
212 } ;
213
214 struct _PyGIVFuncCache {
215     PyGIFunctionWithInstanceCache fwi_cache;
216
217     GIBaseInfo *info;
218 };
219
220
221 gboolean
222 pygi_arg_base_setup      (PyGIArgCache *arg_cache,
223                           GITypeInfo   *type_info,
224                           GIArgInfo    *arg_info,  /* may be NULL for return arguments */
225                           GITransfer    transfer,
226                           PyGIDirection direction);
227
228 gboolean
229 pygi_arg_interface_setup (PyGIInterfaceCache *iface_cache,
230                           GITypeInfo         *type_info,
231                           GIArgInfo          *arg_info,  /* may be NULL for return arguments */
232                           GITransfer          transfer,
233                           PyGIDirection       direction,
234                           GIInterfaceInfo    *iface_info);
235
236 gboolean
237 pygi_arg_sequence_setup  (PyGISequenceCache  *sc,
238                           GITypeInfo         *type_info,
239                           GIArgInfo          *arg_info,    /* may be NULL for return arguments */
240                           GITransfer          transfer,
241                           PyGIDirection       direction,
242                           PyGICallableCache  *callable_cache);
243
244 PyGIArgCache *
245 pygi_arg_interface_new_from_info (GITypeInfo         *type_info,
246                                   GIArgInfo          *arg_info,     /* may be NULL for return arguments */
247                                   GITransfer          transfer,
248                                   PyGIDirection       direction,
249                                   GIInterfaceInfo    *iface_info);
250
251 PyGIArgCache *
252 pygi_arg_cache_alloc     (void);
253
254 PyGIArgCache *
255 pygi_arg_cache_new       (GITypeInfo *type_info,
256                           GIArgInfo *arg_info,
257                           GITransfer transfer,
258                           PyGIDirection direction,
259                           PyGICallableCache *callable_cache,
260                           /* will be removed */
261                           gssize c_arg_index,
262                           gssize py_arg_index);
263
264 void
265 pygi_arg_cache_free      (PyGIArgCache *cache);
266
267 void
268 pygi_callable_cache_free    (PyGICallableCache *cache);
269
270 gchar *
271 pygi_callable_cache_get_full_name (PyGICallableCache *cache);
272
273 PyGIFunctionCache *
274 pygi_function_cache_new     (GICallableInfo *info);
275
276 PyObject *
277 pygi_function_cache_invoke  (PyGIFunctionCache *function_cache,
278                              PyObject *py_args,
279                              PyObject *py_kwargs);
280
281 PyGIFunctionCache *
282 pygi_ccallback_cache_new    (GICallableInfo *info,
283                              GCallback function_ptr);
284
285 PyObject *
286 pygi_ccallback_cache_invoke (PyGIFunctionCache *function_cache,
287                              PyObject *py_args,
288                              PyObject *py_kwargs,
289                              gpointer user_data);
290
291 PyGIFunctionCache *
292 pygi_constructor_cache_new  (GICallableInfo *info);
293
294 PyGIFunctionCache *
295 pygi_method_cache_new       (GICallableInfo *info);
296
297 PyGIFunctionCache *
298 pygi_vfunc_cache_new        (GICallableInfo *info);
299
300 PyGIClosureCache *
301 pygi_closure_cache_new      (GICallableInfo *info);
302
303 #define _pygi_callable_cache_args_len(cache) ((cache)->args_cache)->len
304
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);
308 }
309
310 inline static void
311 _pygi_callable_cache_set_arg (PyGICallableCache *cache, guint index, PyGIArgCache *arg_cache) {
312     cache->args_cache->pdata[index] = arg_cache;
313 }
314
315 G_END_DECLS
316
317 #endif /* __PYGI_CACHE_H__ */