Imported Upstream version 3.21.91
[platform/upstream/python-gobject.git] / gi / pygpointer.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 1998-2003  James Henstridge
4  *
5  *   pygpointer.c: wrapper for GPointer
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <pyglib.h>
26 #include <glib-object.h>
27 #include "pygpointer.h"
28 #include "pygtype.h"
29
30 #include "pygi-type.h"
31
32
33 GQuark pygpointer_class_key;
34
35 PYGLIB_DEFINE_TYPE("gobject.GPointer", PyGPointer_Type, PyGPointer);
36
37 static void
38 pyg_pointer_dealloc(PyGPointer *self)
39 {
40     Py_TYPE(self)->tp_free((PyObject *)self);
41 }
42
43 static PyObject*
44 pyg_pointer_richcompare(PyObject *self, PyObject *other, int op)
45 {
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),
49                                                 op);
50     else {
51         Py_INCREF(Py_NotImplemented);
52         return Py_NotImplemented;
53     }
54 }
55
56 static long
57 pyg_pointer_hash(PyGPointer *self)
58 {
59     return (long)pyg_pointer_get_ptr (self);
60 }
61
62 static PyObject *
63 pyg_pointer_repr(PyGPointer *self)
64 {
65     gchar buf[128];
66
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);
70 }
71
72 static int
73 pyg_pointer_init(PyGPointer *self, PyObject *args, PyObject *kwargs)
74 {
75     gchar buf[512];
76
77     if (!PyArg_ParseTuple(args, ":GPointer.__init__"))
78         return -1;
79
80     pyg_pointer_set_ptr (self, NULL);
81     self->gtype = 0;
82
83     g_snprintf(buf, sizeof(buf), "%s can not be constructed",
84                Py_TYPE(self)->tp_name);
85     PyErr_SetString(PyExc_NotImplementedError, buf);
86     return -1;
87 }
88
89 static void
90 pyg_pointer_free(PyObject *op)
91 {
92   PyObject_FREE(op);
93 }
94
95 /**
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.
101  *
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.
105  */
106 void
107 pyg_register_pointer(PyObject *dict, const gchar *class_name,
108                      GType pointer_type, PyTypeObject *type)
109 {
110     PyObject *o;
111
112     g_return_if_fail(dict != NULL);
113     g_return_if_fail(class_name != NULL);
114     g_return_if_fail(pointer_type != 0);
115
116     if (!type->tp_dealloc) type->tp_dealloc = (destructor)pyg_pointer_dealloc;
117
118     Py_TYPE(type) = &PyType_Type;
119     type->tp_base = &PyGPointer_Type;
120
121     if (PyType_Ready(type) < 0) {
122         g_warning("could not get type `%s' ready", type->tp_name);
123         return;
124     }
125
126     PyDict_SetItemString(type->tp_dict, "__gtype__",
127                          o=pyg_type_wrapper_new(pointer_type));
128     Py_DECREF(o);
129
130     g_type_set_qdata(pointer_type, pygpointer_class_key, type);
131
132     PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
133 }
134
135 /**
136  * pyg_pointer_new:
137  * @pointer_type: the GType of the pointer value.
138  * @pointer: the pointer value.
139  *
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.
145  *
146  * Returns: the boxed wrapper.
147  */
148 PyObject *
149 pyg_pointer_new(GType pointer_type, gpointer pointer)
150 {
151     PyGILState_STATE state;
152     PyGPointer *self;
153     PyTypeObject *tp;
154     g_return_val_if_fail(pointer_type != 0, NULL);
155
156     state = pyglib_gil_state_ensure();
157
158     if (!pointer) {
159         Py_INCREF(Py_None);
160         pyglib_gil_state_release(state);
161         return Py_None;
162     }
163
164     tp = g_type_get_qdata(pointer_type, pygpointer_class_key);
165
166     if (!tp)
167         tp = (PyTypeObject *)pygi_type_import_by_g_type(pointer_type);
168
169     if (!tp)
170         tp = (PyTypeObject *)&PyGPointer_Type; /* fallback */
171     self = PyObject_NEW(PyGPointer, tp);
172
173     pyglib_gil_state_release(state);
174
175     if (self == NULL)
176         return NULL;
177
178     pyg_pointer_set_ptr (self, pointer);
179     self->gtype = pointer_type;
180
181     return (PyObject *)self;
182 }
183
184 void
185 pygobject_pointer_register_types(PyObject *d)
186 {
187     pygpointer_class_key     = g_quark_from_static_string("PyGPointer::class");
188
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); 
197 }