GType's interfaces are very similar to Java's interfaces. They allow to describe a common API that several classes will adhere to. Imagine the play, pause and stop buttons on hi-fi equipment - those can be seen as a playback interface. Once you know what they do, you can control your CD player, MP3 player or anything that uses these symbols. To declare an interface you have to register a non-instantiable classed type which derives from GTypeInterface. The following piece of code declares such an interface. #define MAMAN_IBAZ_TYPE (maman_ibaz_get_type ()) #define MAMAN_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_IBAZ_TYPE, MamanIbaz)) #define MAMAN_IS_IBAZ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_IBAZ_TYPE)) #define MAMAN_IBAZ_GET_INTERFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), MAMAN_IBAZ_TYPE, MamanIbazInterface)) typedef struct _MamanIbaz MamanIbaz; /* dummy object */ typedef struct _MamanIbazInterface MamanIbazInterface; struct _MamanIbazInterface { GTypeInterface parent; void (*do_action) (MamanIbaz *self); }; GType maman_ibaz_get_type (void); void maman_ibaz_do_action (MamanIbaz *self);
The interface function, void maman_ibaz_do_action (MamanIbaz *self) { MAMAN_IBAZ_GET_INTERFACE (self)->do_action (self); }
An interface is defined by only one structure which must contain as first member
a GTypeInterface structure. The interface structure is expected to
contain the function pointers of the interface methods. It is good style to
define helper functions for each of the interface methods which simply call
the interface' method directly:
Once an interface type is registered, you must register implementations for these
interfaces. The function named static void maman_baz_do_action (MamanIbaz *self) { g_print ("Baz implementation of IBaz interface Action.\n"); } static void baz_interface_init (gpointer g_iface, gpointer iface_data) { MamanIbazInterface *iface = (MamanIbazInterface *)g_iface; iface->do_action = maman_baz_do_action; } GType maman_baz_get_type (void) { static GType type = 0; if (type == 0) { static const GTypeInfo info = { sizeof (MamanBazInterface), NULL, /* base_init */ NULL, /* base_finalize */ NULL, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (MamanBaz), 0, /* n_preallocs */ NULL /* instance_init */ }; static const GInterfaceInfo ibaz_info = { (GInterfaceInitFunc) baz_interface_init, /* interface_init */ NULL, /* interface_finalize */ NULL /* interface_data */ }; type = g_type_register_static (G_TYPE_OBJECT, "MamanBazType", &info, 0); g_type_add_interface_static (type, MAMAN_IBAZ_TYPE, &ibaz_info); } return type; }
struct _GInterfaceInfo { GInterfaceInitFunc interface_init; GInterfaceFinalizeFunc interface_finalize; gpointer interface_data; };
When an instantiable classed type which registered an interface implementation
is created for the first time, its class structure is initialized following the process
described in the section called “Instantiable classed types: objects”. Once the class structure is
initialized, the function First a memory buffer is allocated to hold the interface structure. The parent's interface structure is then copied over to the new interface structure (the parent interface is already initialized at that point). If there is no parent interface, the interface structure is initialized with zeros. The g_type and the g_instance_type fields are then initialized: g_type is set to the type of the most-derived interface and g_instance_type is set to the type of the most derived type which implements this interface.
Finally, the interface' most-derived It is thus common for base_init functions to hold a local static boolean variable which makes sure that the interface type is initialized only once even if there are multiple implementations of the interface: static void maman_ibaz_base_init (gpointer g_iface) { static gboolean initialized = FALSE; if (!initialized) { /* create interface signals here. */ initialized = TRUE; } }
If you have found the stuff about interface hairy, you are right: it is hairy but there is not much I can do about it. What I can do is summarize what you need to know about interfaces: The above process can be summarized as follows: Table 2. Interface Initialization
When the last instance of an instantiable type which registered an interface implementation
is destroyed, the interface's implementations associated to the type are destroyed by
Again, it is important to understand, as in
the section called “Interface Initialization”,
that both The above process can be summarized as follows: Table 3. Interface Finalization
Now that you have read this section, you can forget about it. Please, forget it as soon as possible. |