Remove pygi.h and pygi-private.h
[pygobject.git] / gi / pygpointer.c
blobd160fff1ae8ab96771bcff4ce9ee69206ec9f63c
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/>.
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <pyglib.h>
26 #include "pygobject-private.h"
27 #include "pygpointer.h"
29 #include "pygi-type.h"
32 GQuark pygpointer_class_key;
34 PYGLIB_DEFINE_TYPE("gobject.GPointer", PyGPointer_Type, PyGPointer);
36 static void
37 pyg_pointer_dealloc(PyGPointer *self)
39 Py_TYPE(self)->tp_free((PyObject *)self);
42 static PyObject*
43 pyg_pointer_richcompare(PyObject *self, PyObject *other, int op)
45 if (Py_TYPE(self) == Py_TYPE(other))
46 return _pyglib_generic_ptr_richcompare (pyg_pointer_get_ptr (self),
47 pyg_pointer_get_ptr (other),
48 op);
49 else {
50 Py_INCREF(Py_NotImplemented);
51 return Py_NotImplemented;
55 static long
56 pyg_pointer_hash(PyGPointer *self)
58 return (long)pyg_pointer_get_ptr (self);
61 static PyObject *
62 pyg_pointer_repr(PyGPointer *self)
64 gchar buf[128];
66 g_snprintf(buf, sizeof(buf), "<%s at 0x%lx>", g_type_name(self->gtype),
67 (long)pyg_pointer_get_ptr (self));
68 return PYGLIB_PyUnicode_FromString(buf);
71 static int
72 pyg_pointer_init(PyGPointer *self, PyObject *args, PyObject *kwargs)
74 gchar buf[512];
76 if (!PyArg_ParseTuple(args, ":GPointer.__init__"))
77 return -1;
79 pyg_pointer_set_ptr (self, NULL);
80 self->gtype = 0;
82 g_snprintf(buf, sizeof(buf), "%s can not be constructed",
83 Py_TYPE(self)->tp_name);
84 PyErr_SetString(PyExc_NotImplementedError, buf);
85 return -1;
88 static void
89 pyg_pointer_free(PyObject *op)
91 PyObject_FREE(op);
94 /**
95 * pyg_register_pointer:
96 * @dict: the module dictionary to store the wrapper class.
97 * @class_name: the Python name for the wrapper class.
98 * @pointer_type: the GType of the pointer type being wrapped.
99 * @type: the wrapper class.
101 * Registers a wrapper for a pointer type. The wrapper class will be
102 * a subclass of gobject.GPointer, and a reference to the wrapper
103 * class will be stored in the provided module dictionary.
105 void
106 pyg_register_pointer(PyObject *dict, const gchar *class_name,
107 GType pointer_type, PyTypeObject *type)
109 PyObject *o;
111 g_return_if_fail(dict != NULL);
112 g_return_if_fail(class_name != NULL);
113 g_return_if_fail(pointer_type != 0);
115 if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_pointer_dealloc;
117 Py_TYPE(type) = &PyType_Type;
118 type->tp_base = &PyGPointer_Type;
120 if (PyType_Ready(type) < 0) {
121 g_warning("could not get type `%s' ready", type->tp_name);
122 return;
125 PyDict_SetItemString(type->tp_dict, "__gtype__",
126 o=pyg_type_wrapper_new(pointer_type));
127 Py_DECREF(o);
129 g_type_set_qdata(pointer_type, pygpointer_class_key, type);
131 PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
135 * pyg_pointer_new:
136 * @pointer_type: the GType of the pointer value.
137 * @pointer: the pointer value.
139 * Creates a wrapper for a pointer value. Since G_TYPE_POINTER types
140 * don't register any information about how to copy/free them, there
141 * is no guarantee that the pointer will remain valid, and there is
142 * nothing registered to release the pointer when the pointer goes out
143 * of scope. This is why we don't recommend people use these types.
145 * Returns: the boxed wrapper.
147 PyObject *
148 pyg_pointer_new(GType pointer_type, gpointer pointer)
150 PyGILState_STATE state;
151 PyGPointer *self;
152 PyTypeObject *tp;
153 g_return_val_if_fail(pointer_type != 0, NULL);
155 state = pyglib_gil_state_ensure();
157 if (!pointer) {
158 Py_INCREF(Py_None);
159 pyglib_gil_state_release(state);
160 return Py_None;
163 tp = g_type_get_qdata(pointer_type, pygpointer_class_key);
165 if (!tp)
166 tp = (PyTypeObject *)pygi_type_import_by_g_type(pointer_type);
168 if (!tp)
169 tp = (PyTypeObject *)&PyGPointer_Type; /* fallback */
170 self = PyObject_NEW(PyGPointer, tp);
172 pyglib_gil_state_release(state);
174 if (self == NULL)
175 return NULL;
177 pyg_pointer_set_ptr (self, pointer);
178 self->gtype = pointer_type;
180 return (PyObject *)self;
183 void
184 pygobject_pointer_register_types(PyObject *d)
186 pygpointer_class_key = g_quark_from_static_string("PyGPointer::class");
188 PyGPointer_Type.tp_dealloc = (destructor)pyg_pointer_dealloc;
189 PyGPointer_Type.tp_richcompare = pyg_pointer_richcompare;
190 PyGPointer_Type.tp_repr = (reprfunc)pyg_pointer_repr;
191 PyGPointer_Type.tp_hash = (hashfunc)pyg_pointer_hash;
192 PyGPointer_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
193 PyGPointer_Type.tp_init = (initproc)pyg_pointer_init;
194 PyGPointer_Type.tp_free = (freefunc)pyg_pointer_free;
195 PYGOBJECT_REGISTER_GTYPE(d, PyGPointer_Type, "GPointer", G_TYPE_POINTER);