a00386bc694ff671efc7d541b5c4fdbc4ce8eebc
[platform/upstream/pygobject2.git] / gobject / pygboxed.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  *   pygboxed.c: wrapper for GBoxed
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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <pyglib.h>
28 #include "pygobject-private.h"
29 #include "pygboxed.h"
30
31 #include "pygi.h"
32
33 GQuark pygboxed_type_key;
34 GQuark pygboxed_marshal_key;
35
36 PYGLIB_DEFINE_TYPE("gobject.GBoxed", PyGBoxed_Type, PyGBoxed);
37
38 static void
39 pyg_boxed_dealloc(PyGBoxed *self)
40 {
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);
45     }
46
47     Py_TYPE(self)->tp_free((PyObject *)self);
48 }
49
50 static PyObject*
51 pyg_boxed_richcompare(PyObject *self, PyObject *other, int op)
52 {
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,
56                                                op);
57     else {
58         Py_INCREF(Py_NotImplemented);
59         return Py_NotImplemented;
60     }
61 }
62
63
64 static long
65 pyg_boxed_hash(PyGBoxed *self)
66 {
67     return (long)self->boxed;
68 }
69
70 static PyObject *
71 pyg_boxed_repr(PyGBoxed *self)
72 {
73     gchar buf[128];
74
75     g_snprintf(buf, sizeof(buf), "<%s at 0x%lx>", g_type_name(self->gtype),
76                (long)self->boxed);
77     return PYGLIB_PyUnicode_FromString(buf);
78 }
79
80 static int
81 pyg_boxed_init(PyGBoxed *self, PyObject *args, PyObject *kwargs)
82 {
83     gchar buf[512];
84
85     if (!PyArg_ParseTuple(args, ":GBoxed.__init__"))
86         return -1;
87
88     self->boxed = NULL;
89     self->gtype = 0;
90     self->free_on_dealloc = FALSE;
91
92     g_snprintf(buf, sizeof(buf), "%s can not be constructed",
93                Py_TYPE(self)->tp_name);
94     PyErr_SetString(PyExc_NotImplementedError, buf);
95     return -1;
96 }
97
98 static void
99 pyg_boxed_free(PyObject *op)
100 {
101   PyObject_FREE(op);
102 }
103
104 static PyObject *
105 pyg_boxed_copy(PyGBoxed *self)
106 {
107     return pyg_boxed_new (self->gtype, self->boxed, TRUE, TRUE);
108 }
109
110
111
112 static PyMethodDef pygboxed_methods[] = {
113     { "copy", (PyCFunction) pyg_boxed_copy, METH_NOARGS },
114     { NULL, NULL, 0 }
115 };
116
117
118 /**
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.
124  *
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.
128  */
129 void
130 pyg_register_boxed(PyObject *dict, const gchar *class_name,
131                    GType boxed_type, PyTypeObject *type)
132 {
133     PyObject *o;
134
135     g_return_if_fail(dict != NULL);
136     g_return_if_fail(class_name != NULL);
137     g_return_if_fail(boxed_type != 0);
138
139     if (!type->tp_dealloc)  type->tp_dealloc  = (destructor)pyg_boxed_dealloc;
140
141     Py_TYPE(type) = &PyType_Type;
142     type->tp_base = &PyGBoxed_Type;
143
144     if (PyType_Ready(type) < 0) {
145         g_warning("could not get type `%s' ready", type->tp_name);
146         return;
147     }
148
149     PyDict_SetItemString(type->tp_dict, "__gtype__",
150                          o=pyg_type_wrapper_new(boxed_type));
151     Py_DECREF(o);
152
153     g_type_set_qdata(boxed_type, pygboxed_type_key, type);
154
155     PyDict_SetItemString(dict, (char *)class_name, (PyObject *)type);
156 }
157
158 /**
159  * pyg_boxed_new:
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.
164  *
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.
170  *
171  * Returns: the boxed wrapper.
172  */
173 PyObject *
174 pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
175               gboolean own_ref)
176 {
177     PyGILState_STATE state;
178     PyGBoxed *self;
179     PyTypeObject *tp;
180
181     g_return_val_if_fail(boxed_type != 0, NULL);
182     g_return_val_if_fail(!copy_boxed || (copy_boxed && own_ref), NULL);
183
184     state = pyglib_gil_state_ensure();
185
186     if (!boxed) {
187         Py_INCREF(Py_None);
188         pyglib_gil_state_release(state);
189         return Py_None;
190     }
191
192     tp = g_type_get_qdata(boxed_type, pygboxed_type_key);
193
194     if (!tp)
195         tp = (PyTypeObject *)pygi_type_import_by_g_type(boxed_type);
196
197     if (!tp)
198         tp = (PyTypeObject *)&PyGBoxed_Type; /* fallback */
199
200     self = (PyGBoxed *)tp->tp_alloc(tp, 0);
201
202     if (self == NULL) {
203         pyglib_gil_state_release(state);
204         return NULL;
205     }
206
207     if (copy_boxed)
208         boxed = g_boxed_copy(boxed_type, boxed);
209     self->boxed = boxed;
210     self->gtype = boxed_type;
211     self->free_on_dealloc = own_ref;
212
213     pyglib_gil_state_release(state);
214     
215     return (PyObject *)self;
216 }
217
218 void
219 pygobject_boxed_register_types(PyObject *d)
220 {
221     pygboxed_type_key        = g_quark_from_static_string("PyGBoxed::class");
222     pygboxed_marshal_key     = g_quark_from_static_string("PyGBoxed::marshal");
223
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;
232     
233     PYGOBJECT_REGISTER_GTYPE(d, PyGBoxed_Type, "GBoxed", G_TYPE_BOXED);
234 }