3fe5366a3e856dcf04720f86e0437ed3d49258e6
[platform/upstream/pygobject2.git] / gi / pygi-ccallback.c
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>, Red Hat, Inc.
5  *
6  *   pygi-boxed-closure.c: wrapper to handle GClosure box types with C callbacks.
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "pygi-ccallback.h"
23
24 #include <girepository.h>
25 #include <pyglib-python-compat.h>
26
27
28 static PyObject *
29 _ccallback_call(PyGICCallback *self, PyObject *args, PyObject *kwargs)
30 {
31     PyObject *result;
32
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;
38     }
39
40     result = pygi_ccallback_cache_invoke (self->cache,
41                                           args,
42                                           kwargs,
43                                           self->user_data);
44     return result;
45 }
46
47 PYGLIB_DEFINE_TYPE("gi.CCallback", PyGICCallback_Type, PyGICCallback);
48
49 PyObject *
50 _pygi_ccallback_new (GCallback callback,
51                      gpointer user_data,
52                      GIScopeType scope,
53                      GIFunctionInfo *info,
54                      GDestroyNotify destroy_notify)
55 {
56     PyGICCallback *self;
57
58     if (!callback) {
59         Py_RETURN_NONE;
60     }
61
62     self = (PyGICCallback *) PyGICCallback_Type.tp_alloc (&PyGICCallback_Type, 0);
63     if (self == NULL) {
64         return NULL;
65     }
66
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);
72
73     return (PyObject *) self;
74 }
75
76 static void
77 _ccallback_dealloc (PyGICCallback *self)
78 {
79     g_base_info_unref ( (GIBaseInfo *)self->info);
80
81     if (self->cache != NULL) {
82         pygi_callable_cache_free ( (PyGICallableCache *)self->cache);
83     }
84
85     Py_TYPE (self)->tp_free ((PyObject *)self);
86 }
87
88 void
89 _pygi_ccallback_register_types (PyObject *m)
90 {
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;
95
96
97     if (PyType_Ready (&PyGICCallback_Type))
98         return;
99     if (PyModule_AddObject (m, "CCallback", (PyObject *) &PyGICCallback_Type))
100         return;
101 }