release 3.27.0
[pygobject.git] / gi / pygi-ccallback.c
blob3fe5366a3e856dcf04720f86e0437ed3d49258e6
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
6 * pygi-boxed-closure.c: wrapper to handle GClosure box types with C callbacks.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "pygi-ccallback.h"
24 #include <girepository.h>
25 #include <pyglib-python-compat.h>
28 static PyObject *
29 _ccallback_call(PyGICCallback *self, PyObject *args, PyObject *kwargs)
31 PyObject *result;
33 if (self->cache == NULL) {
34 self->cache = (PyGICCallbackCache *)pygi_ccallback_cache_new (self->info,
35 self->callback);
36 if (self->cache == NULL)
37 return NULL;
40 result = pygi_ccallback_cache_invoke (self->cache,
41 args,
42 kwargs,
43 self->user_data);
44 return result;
47 PYGLIB_DEFINE_TYPE("gi.CCallback", PyGICCallback_Type, PyGICCallback);
49 PyObject *
50 _pygi_ccallback_new (GCallback callback,
51 gpointer user_data,
52 GIScopeType scope,
53 GIFunctionInfo *info,
54 GDestroyNotify destroy_notify)
56 PyGICCallback *self;
58 if (!callback) {
59 Py_RETURN_NONE;
62 self = (PyGICCallback *) PyGICCallback_Type.tp_alloc (&PyGICCallback_Type, 0);
63 if (self == NULL) {
64 return NULL;
67 self->callback = (GCallback) callback;
68 self->user_data = user_data;
69 self->scope = scope;
70 self->destroy_notify_func = destroy_notify;
71 self->info = g_base_info_ref( (GIBaseInfo *) info);
73 return (PyObject *) self;
76 static void
77 _ccallback_dealloc (PyGICCallback *self)
79 g_base_info_unref ( (GIBaseInfo *)self->info);
81 if (self->cache != NULL) {
82 pygi_callable_cache_free ( (PyGICallableCache *)self->cache);
85 Py_TYPE (self)->tp_free ((PyObject *)self);
88 void
89 _pygi_ccallback_register_types (PyObject *m)
91 Py_TYPE(&PyGICCallback_Type) = &PyType_Type;
92 PyGICCallback_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE);
93 PyGICCallback_Type.tp_dealloc = (destructor) _ccallback_dealloc;
94 PyGICCallback_Type.tp_call = (ternaryfunc) _ccallback_call;
97 if (PyType_Ready (&PyGICCallback_Type))
98 return;
99 if (PyModule_AddObject (m, "CCallback", (PyObject *) &PyGICCallback_Type))
100 return;