As a language binding author, mostly you will be querying the GType system for information, rather than registering new types. One exception would be creating new GObjects subclasses from the language the binding is for. Discussion of GObject subclassing will be covered in a later section.
Another is if you wish to create an equivalent of the GTK+ 1.2 GTK_TYPE_FOREIGN type. GTK_TYPE_FOREIGN was removed in GTK 2.0, in favour of bindings registering their own type. You will probably want to use a boxed type for this purpose, as seen in the following example:
static gpointer
pyobject_copy(gpointer boxed)
{
PyObject *obj = (PyObject *)boxed;
/* we use referencing as our copying semantics */
Py_INCREF(obj);
return obj;
}
static void
pyobject_free(gpointer boxed)
{
PyObject *obj = (PyObject *)boxed;
Py_DECREF(obj);
}
...
/* in binding initialisation function */
g_boxed_type_register_static("PyObject", pyobject_copy,
pyobject_free); |
A type such as this may be used in the definitions of new signals created for new GObjects created by your binding.
For a language binding, you will need some way of converting between GValues and the native types in the language you are writing the binding for. You will probably need functions for converting to and from GValues. In pygtk, these are pyg_value_from_pyobject and pyg_value_as_pyobject:
int
pyg_value_from_pyobject(GValue *value, PyObject *obj);
PyObject *
pyg_value_as_pyobject(const GValue *value);
The first function pyg_value_from_pyobject takes a GValue that has had its type set and a PyObject which is the python object we wish to marshal into the value. If the PyObject has the wrong type, then an error is is returned. The second function pyg_value_as_pyobject takes a GValue, and returns the equivalent python object.
In both of these functions, there is a big if statement calling the various G_VALUE_HOLDS_* macros to check the type of the value.