Imported Upstream version 3.3.1
[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  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  */
21
22 #ifndef __PYGI_CACHE_H__
23 #define __PYGI_CACHE_H__
24
25 #include <Python.h>
26 #include <girepository.h>
27
28 #include "pygi-invoke-state-struct.h"
29
30 G_BEGIN_DECLS
31
32 typedef struct _PyGICallableCache PyGICallableCache;
33 typedef struct _PyGIArgCache PyGIArgCache;
34
35 typedef gboolean (*PyGIMarshalFromPyFunc) (PyGIInvokeState   *state,
36                                            PyGICallableCache *callable_cache,
37                                            PyGIArgCache      *arg_cache,
38                                            PyObject          *py_arg,
39                                            GIArgument        *arg);
40
41 typedef PyObject *(*PyGIMarshalToPyFunc) (PyGIInvokeState   *state,
42                                           PyGICallableCache *callable_cache,
43                                           PyGIArgCache      *arg_cache,
44                                           GIArgument        *arg);
45
46 typedef void (*PyGIMarshalCleanupFunc) (PyGIInvokeState *state,
47                                         PyGIArgCache    *arg_cache,
48                                         gpointer         data,
49                                         gboolean         was_processed);
50
51 /* Argument meta types denote how we process the argument:
52  *  - PYGI_META_ARG_TYPE_PARENT - parents may or may not have children
53  *    but are always processed via the normal marshaller for their
54  *    actual GI type.  If they have children the marshaller will
55  *    also handle marshalling the children.
56  *  - PYGI_META_ARG_TYPE_CHILD - Children without python argument are
57  *    ignored by the marshallers and handled directly by their parents
58  *    marshaller.
59  *  - PYGI_META_ARG_TYPE_CHILD_NEEDS_UPDATE -Sometimes children arguments
60  *    come before the parent.  In these cases they need to be flagged
61  *    so that the argument list counts must be updated for the cache to
62  *    be valid
63  *  - Children with pyargs (PYGI_META_ARG_TYPE_CHILD_WITH_PYARG) are processed
64  *    the same as other child args but also have an index into the 
65  *    python parameters passed to the invoker
66  */
67 typedef enum {
68     PYGI_META_ARG_TYPE_PARENT,
69     PYGI_META_ARG_TYPE_CHILD,
70     PYGI_META_ARG_TYPE_CHILD_NEEDS_UPDATE,
71     PYGI_META_ARG_TYPE_CHILD_WITH_PYARG,
72     PYGI_META_ARG_TYPE_CLOSURE,
73 } PyGIMetaArgType;
74
75 /*
76  * GI determines function types via a combination of flags and info classes.
77  * Since for branching purposes they are mutually exclusive, the 
78  * PyGIFunctionType enum consolidates them into one enumeration for ease of 
79  * branching and debugging.
80  */
81 typedef enum {
82     PYGI_FUNCTION_TYPE_FUNCTION,
83     PYGI_FUNCTION_TYPE_METHOD,
84     PYGI_FUNCTION_TYPE_CONSTRUCTOR,
85     PYGI_FUNCTION_TYPE_VFUNC,
86     PYGI_FUNCTION_TYPE_CALLBACK,
87     PYGI_FUNCTION_TYPE_CCALLBACK,
88  } PyGIFunctionType;
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).  We don't as much care if the
94  * parameter is an IN or OUT C parameter, than we do if the parameter is being
95  * marshalled into Python or from Python.
96  */
97 typedef enum {
98     PYGI_DIRECTION_TO_PYTHON,
99     PYGI_DIRECTION_FROM_PYTHON,
100     PYGI_DIRECTION_BIDIRECTIONAL
101  } PyGIDirection;
102
103
104 struct _PyGIArgCache
105 {
106     const gchar *arg_name;
107
108     PyGIMetaArgType meta_type;
109     gboolean is_pointer;
110     gboolean is_caller_allocates;
111     gboolean is_skipped;
112     gboolean allow_none;
113
114     PyGIDirection direction;
115     GITransfer transfer;
116     GITypeTag type_tag;
117     GITypeInfo *type_info;
118
119     PyGIMarshalFromPyFunc from_py_marshaller;
120     PyGIMarshalToPyFunc to_py_marshaller;
121
122     PyGIMarshalCleanupFunc from_py_cleanup;
123     PyGIMarshalCleanupFunc to_py_cleanup;
124
125     GDestroyNotify destroy_notify;
126
127     gssize c_arg_index;
128     gssize py_arg_index;
129 };
130
131 typedef struct _PyGISequenceCache
132 {
133     PyGIArgCache arg_cache;
134     gssize fixed_size;
135     gssize len_arg_index;
136     gboolean is_zero_terminated;
137     gsize item_size;
138     GIArrayType array_type;
139     PyGIArgCache *item_cache;
140 } PyGISequenceCache;
141
142 typedef struct _PyGIInterfaceCache
143 {
144     PyGIArgCache arg_cache;
145     gboolean is_foreign;
146     GType g_type;
147     PyObject *py_type;
148     GIInterfaceInfo *interface_info;
149     gchar *type_name;
150 } PyGIInterfaceCache;
151
152 typedef struct _PyGIHashCache
153 {
154     PyGIArgCache arg_cache;
155     PyGIArgCache *key_cache;
156     PyGIArgCache *value_cache;
157 } PyGIHashCache;
158
159 typedef struct _PyGICallbackCache
160 {
161     PyGIArgCache arg_cache;
162     gssize user_data_index;
163     gssize destroy_notify_index;
164     GIScopeType scope;
165     GIInterfaceInfo *interface_info;
166 } PyGICallbackCache;
167
168 struct _PyGICallableCache
169 {
170     const gchar *name;
171
172     PyGIFunctionType function_type;
173
174     PyGIArgCache *return_cache;
175     PyGIArgCache **args_cache;
176     GSList *to_py_args;
177     GSList *arg_name_list; /* for keyword arg matching */
178     GHashTable *arg_name_hash;
179
180     /* counts */
181     gssize n_from_py_args;
182     gssize n_to_py_args;
183     gssize n_to_py_child_args;
184
185     gssize n_args;
186     gssize n_py_args;
187 };
188
189 void _pygi_arg_cache_clear      (PyGIArgCache *cache);
190 void _pygi_callable_cache_free  (PyGICallableCache *cache);
191
192 PyGICallableCache *_pygi_callable_cache_new (GICallableInfo *callable_info,
193                                              gboolean is_ccallback);
194
195 G_END_DECLS
196
197 #endif /* __PYGI_CACHE_H__ */