The bulk of interface definition has already been shown in the section called “Non-instantiable classed types: interfaces” but I feel it is needed to show exactly how to create an interface. As above, the first step is to get the header right: #ifndef __MAMAN_IBAZ_H__ #define __MAMAN_IBAZ_H__ #include <glib-object.h> #define MAMAN_TYPE_IBAZ (maman_ibaz_get_type ()) #define MAMAN_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_IBAZ, MamanIbaz)) #define MAMAN_IS_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_IBAZ)) #define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_TYPE_IBAZ, MamanIbazInterface)) typedef struct _MamanIbaz MamanIbaz; /* dummy object */ typedef struct _MamanIbazInterface MamanIbazInterface; struct _MamanIbazInterface { GTypeInterface parent_iface; void (*do_action) (MamanIbaz *self); }; GType maman_ibaz_get_type (void); void maman_ibaz_do_action (MamanIbaz *self); #endif /* __MAMAN_IBAZ_H__ */ This code is the same as the code for a normal GType which derives from a GObject except for a few details:
The implementation of the MamanIbaz type itself is trivial:
static void maman_ibaz_base_init (gpointer g_class) { static gboolean is_initialized = FALSE; if (!is_initialized) { /* add properties and signals to the interface here */ is_initialized = TRUE; } } GType maman_ibaz_get_type (void) { static GType iface_type = 0; if (iface_type == 0) { static const GTypeInfo info = { sizeof (MamanIbazInterface), maman_ibaz_base_init, /* base_init */ NULL, /* base_finalize */ }; iface_type = g_type_register_static (G_TYPE_INTERFACE, "MamanIbaz", &info, 0); } return type; } void maman_ibaz_do_action (MamanIbaz *self) { g_return_if_fail (MAMAN_IS_IBAZ (self)); MAMAN_IBAZ_GET_INTERFACE (self)->do_action (self); }
|