Non-instantiable classed types: Interfaces.

GType's Interfaces are very similar to Java's interfaces. To declare one of these
you have to register a non-instantiable classed type which derives from
GTypeInterface. The following piece of code declares such an interface.

maman_ibaz_get_type registers a type named MamanIBaz
which inherits from G_TYPE_INTERFACE. All interfaces must be children of G_TYPE_INTERFACE in the
inheritance tree.

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: maman_ibaz_do_action
is one of these.

Once an interface type is registered, you must register implementations for these
interfaces. The function named maman_baz_get_type registers
a new GType named MamanBaz which inherits from GObject and which
implements the interface MamanIBaz.

g_type_add_interface_static records in the type system that
a given type implements also FooInterface
(foo_interface_get_type returns the type of
FooInterface).
The GInterfaceInfo structure holds
information about the implementation of the interface:

Interface Initialization

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 type_class_init_Wm (implemented in
gtype.c) initializes the interface implementations associated with
that type by calling type_iface_vtable_init_Wm for each
interface.

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 base_init function and then
the implementation's interface_init
function are invoked. It is important to understand that if there are multiple
implementations of an interface the base_init and
interface_init functions will be
invoked once for each implementation initialized.

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:

Initialize interface' implementation. That is, initialize the interface
method pointers in the interface structure to the function's implementation.

It is highly unlikely (ie: I do not know of anyone who actually
used it) you will ever need other more fancy things such as the ones described in the
following section (the section called “Interface Destruction”).

Interface Destruction

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
type_iface_vtable_finalize_Wm (in gtype.c).

type_iface_vtable_finalize_Wm invokes first the implementation's
interface_finalize function and then the interface's most-derived
base_finalize function.

Again, it is important to understand, as in
the section called “Interface Initialization”,
that both interface_finalize and base_finalize
are invoked exactly once for the destruction of each implementation of an interface. Thus,
if you were to use one of these functions, you would need to use a static integer variable
which would hold the number of instances of implementations of an interface such that
the interface's class is destroyed only once (when the integer variable reaches zero).