1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
5 * pygpointer.c: wrapper for GPointer
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, see <http://www.gnu.org/licenses/>.
26 #include <glib-object.h>
27 #include "pygpointer.h"
30 #include "pygi-type.h"
33 GQuark pygpointer_class_key;
35 PYGLIB_DEFINE_TYPE("gobject.GPointer", PyGPointer_Type, PyGPointer);
38 pyg_pointer_dealloc(PyGPointer *self)
40 Py_TYPE(self)->tp_free((PyObject *)self);
44 pyg_pointer_richcompare(PyObject *self, PyObject *other, int op)
46 if (Py_TYPE(self) == Py_TYPE(other))
47 return _pyglib_generic_ptr_richcompare (pyg_pointer_get_ptr (self),
48 pyg_pointer_get_ptr (other),
51 Py_INCREF(Py_NotImplemented);
52 return Py_NotImplemented;
57 pyg_pointer_hash(PyGPointer *self)
59 return (long)pyg_pointer_get_ptr (self);
63 pyg_pointer_repr(PyGPointer *self)
67 g_snprintf(buf, sizeof(buf), "<%s at 0x%lx>", g_type_name(self->gtype),
68 (long)pyg_pointer_get_ptr (self));
69 return PYGLIB_PyUnicode_FromString(buf);
73 pyg_pointer_init(PyGPointer *self, PyObject *args, PyObject *kwargs)
77 if (!PyArg_ParseTuple(args, ":GPointer.__init__"))
80 pyg_pointer_set_ptr (self, NULL);
83 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
84 Py_TYPE(self)->tp_name);
85 PyErr_SetString(PyExc_NotImplementedError, buf);
90 pyg_pointer_free(PyObject *op)
96 * pyg_register_pointer:
97 * @dict: the module dictionary to store the wrapper class.
98 * @class_name: the Python name for the wrapper class.
99 * @pointer_type: the GType of the pointer type being wrapped.
100 * @type: the wrapper class.
102 * Registers a wrapper for a pointer type. The wrapper class will be
103 * a subclass of gobject.GPointer, and a reference to the wrapper
104 * class will be stored in the provided module dictionary.
107 pyg_register_pointer(PyObject *dict, const gchar *class_name,
108 GType pointer_type, PyTypeObject *type)
112 g_return_if_fail(dict != NULL);
113 g_return_if_fail(class_name != NULL);
114 g_return_if_fail(pointer_type != 0);
116 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_pointer_dealloc;
118 Py_TYPE(type) = &PyType_Type;
119 type->tp_base = &PyGPointer_Type;
121 if (PyType_Ready(type) < 0) {
122 g_warning("could not get type `%s' ready", type->tp_name);
126 PyDict_SetItemString(type->tp_dict, "__gtype__",
127 o=pyg_type_wrapper_new(pointer_type));
130 g_type_set_qdata(pointer_type, pygpointer_class_key, type);
132 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
137 * @pointer_type: the GType of the pointer value.
138 * @pointer: the pointer value.
140 * Creates a wrapper for a pointer value. Since G_TYPE_POINTER types
141 * don't register any information about how to copy/free them, there
142 * is no guarantee that the pointer will remain valid, and there is
143 * nothing registered to release the pointer when the pointer goes out
144 * of scope. This is why we don't recommend people use these types.
146 * Returns: the boxed wrapper.
149 pyg_pointer_new(GType pointer_type, gpointer pointer)
151 PyGILState_STATE state;
154 g_return_val_if_fail(pointer_type != 0, NULL);
156 state = pyglib_gil_state_ensure();
160 pyglib_gil_state_release(state);
164 tp = g_type_get_qdata(pointer_type, pygpointer_class_key);
167 tp = (PyTypeObject *)pygi_type_import_by_g_type(pointer_type);
170 tp = (PyTypeObject *)&PyGPointer_Type; /* fallback */
171 self = PyObject_NEW(PyGPointer, tp);
173 pyglib_gil_state_release(state);
178 pyg_pointer_set_ptr (self, pointer);
179 self->gtype = pointer_type;
181 return (PyObject *)self;
185 pygobject_pointer_register_types(PyObject *d)
187 pygpointer_class_key = g_quark_from_static_string("PyGPointer::class");
189 PyGPointer_Type.tp_dealloc = (destructor)pyg_pointer_dealloc;
190 PyGPointer_Type.tp_richcompare = pyg_pointer_richcompare;
191 PyGPointer_Type.tp_repr = (reprfunc)pyg_pointer_repr;
192 PyGPointer_Type.tp_hash = (hashfunc)pyg_pointer_hash;
193 PyGPointer_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
194 PyGPointer_Type.tp_init = (initproc)pyg_pointer_init;
195 PyGPointer_Type.tp_free = (freefunc)pyg_pointer_free;
196 PYGOBJECT_REGISTER_GTYPE(d, PyGPointer_Type, "GPointer", G_TYPE_POINTER);