1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
5 * pygboxed.c: wrapper for GBoxed
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
28 #include "pygobject-private.h"
33 GQuark pygboxed_type_key;
34 GQuark pygboxed_marshal_key;
36 PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
39 pyg_boxed_dealloc(PyGBoxed *self)
41 if (self->free_on_dealloc && self->boxed) {
42 PyGILState_STATE state = pyglib_gil_state_ensure();
43 g_boxed_free(self->gtype, self->boxed);
44 pyglib_gil_state_release(state);
47 Py_TYPE(self)->tp_free((PyObject *)self);
51 pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
53 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGBoxed_Type)
54 return _pyglib_generic_ptr_richcompare(((PyGBoxed*)self)->boxed,
55 ((PyGBoxed*)other)->boxed,
58 Py_INCREF(Py_NotImplemented);
59 return Py_NotImplemented;
65 pyg_boxed_hash(PyGBoxed *self)
67 return (long)self->boxed;
71 pyg_boxed_repr(PyGBoxed *self)
75 g_snprintf(buf, sizeof(buf), "<%s at 0x%lx>", g_type_name(self->gtype),
77 return PYGLIB_PyUnicode_FromString(buf);
81 pyg_boxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
85 if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
90 self->free_on_dealloc = FALSE;
92 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
93 Py_TYPE(self)->tp_name);
94 PyErr_SetString(PyExc_NotImplementedError, buf);
99 pyg_boxed_free(PyObject *op)
105 pyg_boxed_copy(PyGBoxed *self)
107 return pyg_boxed_new (self->gtype, self->boxed, TRUE, TRUE);
112 static PyMethodDef pygboxed_methods[] = {
113 { "copy", (PyCFunction) pyg_boxed_copy, METH_NOARGS },
119 * pyg_register_boxed:
120 * @dict: the module dictionary to store the wrapper class.
121 * @class_name: the Python name for the wrapper class.
122 * @boxed_type: the GType of the boxed type being wrapped.
123 * @type: the wrapper class.
125 * Registers a wrapper for a boxed type. The wrapper class will be a
126 * subclass of gobject.GBoxed, and a reference to the wrapper class
127 * will be stored in the provided module dictionary.
130 pyg_register_boxed(PyObject *dict, const gchar *class_name,
131 GType boxed_type, PyTypeObject *type)
135 g_return_if_fail(dict != NULL);
136 g_return_if_fail(class_name != NULL);
137 g_return_if_fail(boxed_type != 0);
139 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_boxed_dealloc;
141 Py_TYPE(type) = &PyType_Type;
142 type->tp_base = &PyGBoxed_Type;
144 if (PyType_Ready(type) < 0) {
145 g_warning("could not get type `%s' ready", type->tp_name);
149 PyDict_SetItemString(type->tp_dict, "__gtype__",
150 o=pyg_type_wrapper_new(boxed_type));
153 g_type_set_qdata(boxed_type, pygboxed_type_key, type);
155 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
160 * @boxed_type: the GType of the boxed value.
161 * @boxed: the boxed value.
162 * @copy_boxed: whether the new boxed wrapper should hold a copy of the value.
163 * @own_ref: whether the boxed wrapper should own the boxed value.
165 * Creates a wrapper for a boxed value. If @copy_boxed is set to
166 * True, the wrapper will hold a copy of the value, instead of the
167 * value itself. If @own_ref is True, then the value held by the
168 * wrapper will be freed when the wrapper is deallocated. If
169 * @copy_boxed is True, then @own_ref must also be True.
171 * Returns: the boxed wrapper.
174 pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
177 PyGILState_STATE state;
181 g_return_val_if_fail(boxed_type != 0, NULL);
182 g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);
184 state = pyglib_gil_state_ensure();
188 pyglib_gil_state_release(state);
192 tp = g_type_get_qdata(boxed_type, pygboxed_type_key);
195 tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);
198 tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
200 self = (PyGBoxed *)tp->tp_alloc(tp, 0);
203 pyglib_gil_state_release(state);
208 boxed = g_boxed_copy(boxed_type, boxed);
210 self->gtype = boxed_type;
211 self->free_on_dealloc = own_ref;
213 pyglib_gil_state_release(state);
215 return (PyObject *)self;
219 pygobject_boxed_register_types(PyObject *d)
221 pygboxed_type_key = g_quark_from_static_string("PyGBoxed::class");
222 pygboxed_marshal_key = g_quark_from_static_string("PyGBoxed::marshal");
224 PyGBoxed_Type.tp_dealloc = (destructor)pyg_boxed_dealloc;
225 PyGBoxed_Type.tp_richcompare = pyg_boxed_richcompare;
226 PyGBoxed_Type.tp_repr = (reprfunc)pyg_boxed_repr;
227 PyGBoxed_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
228 PyGBoxed_Type.tp_methods = pygboxed_methods;
229 PyGBoxed_Type.tp_init = (initproc)pyg_boxed_init;
230 PyGBoxed_Type.tp_free = (freefunc)pyg_boxed_free;
231 PyGBoxed_Type.tp_hash = (hashfunc)pyg_boxed_hash;
233 PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);