| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2011 John (J5) Palmieri <[email protected]>, 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 |
#include "pygi-util.h" |
| 24 |
|
| 25 |
static PyObject * |
| 26 |
_ccallback_vectorcall (PyGICCallback *self, PyObject *const *args, |
| 27 |
size_t nargsf, PyObject *kwnames) |
| 28 |
{ |
| 29 |
PyObject *result; |
| 30 |
|
| 31 |
if (self->cache == NULL) { |
| 32 |
self->cache = (PyGICCallbackCache *)pygi_ccallback_cache_new ( |
| 33 |
GI_CALLABLE_INFO (self->info), self->callback); |
| 34 |
if (self->cache == NULL) return NULL; |
| 35 |
} |
| 36 |
|
| 37 |
result = pygi_ccallback_cache_invoke (self->cache, args, nargsf, kwnames, |
| 38 |
self->user_data); |
| 39 |
return result; |
| 40 |
} |
| 41 |
|
| 42 |
PYGI_DEFINE_TYPE ("gi.CCallback", PyGICCallback_Type, PyGICCallback); |
| 43 |
|
| 44 |
PyObject * |
| 45 |
_pygi_ccallback_new (GCallback callback, gpointer user_data, GIScopeType scope, |
| 46 |
GICallableInfo *info, GDestroyNotify destroy_notify) |
| 47 |
{ |
| 48 |
PyGICCallback *self; |
| 49 |
|
| 50 |
if (!callback) { |
| 51 |
Py_RETURN_NONE; |
| 52 |
} |
| 53 |
|
| 54 |
self = |
| 55 |
(PyGICCallback *)PyGICCallback_Type.tp_alloc (&PyGICCallback_Type, 0); |
| 56 |
if (self == NULL) { |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
self->callback = (GCallback)callback; |
| 61 |
self->user_data = user_data; |
| 62 |
self->scope = scope; |
| 63 |
self->destroy_notify_func = destroy_notify; |
| 64 |
self->info = GI_CALLABLE_INFO (gi_base_info_ref (info)); |
| 65 |
self->vectorcall = (vectorcallfunc)_ccallback_vectorcall; |
| 66 |
|
| 67 |
return (PyObject *)self; |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
_ccallback_dealloc (PyGICCallback *self) |
| 72 |
{ |
| 73 |
gi_base_info_unref ((GIBaseInfo *)self->info); |
| 74 |
|
| 75 |
if (self->cache != NULL) { |
| 76 |
pygi_callable_cache_free ((PyGICallableCache *)self->cache); |
| 77 |
} |
| 78 |
|
| 79 |
Py_TYPE (self)->tp_free ((PyObject *)self); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns 0 on success, or -1 and sets an exception. |
| 84 |
*/ |
| 85 |
int |
| 86 |
pygi_ccallback_register_types (PyObject *m) |
| 87 |
{ |
| 88 |
Py_SET_TYPE (&PyGICCallback_Type, &PyType_Type); |
| 89 |
PyGICCallback_Type.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 90 |
| Py_TPFLAGS_HAVE_VECTORCALL); |
| 91 |
PyGICCallback_Type.tp_dealloc = (destructor)_ccallback_dealloc; |
| 92 |
PyGICCallback_Type.tp_call = PyVectorcall_Call; |
| 93 |
PyGICCallback_Type.tp_vectorcall_offset = |
| 94 |
offsetof (PyGICCallback, vectorcall); |
| 95 |
|
| 96 |
|
| 97 |
if (PyType_Ready (&PyGICCallback_Type) < 0) return -1; |
| 98 |
Py_INCREF ((PyObject *)&PyGICCallback_Type); |
| 99 |
if (PyModule_AddObject (m, "CCallback", (PyObject *)&PyGICCallback_Type) |
| 100 |
< 0) { |
| 101 |
Py_DECREF ((PyObject *)&PyGICCallback_Type); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |