How to define and implement interfaces

How to define interfaces
How To define implement an Interface?
Interface definition prerequisites
Interface Properties

How to define interfaces

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 _GET_CLASS macro is called _GET_INTERFACE and not implemented with G_TYPE_INSTANCE_GET_CLASS but with G_TYPE_INSTANCE_GET_INTERFACE.

  • The instance type, MamanIbaz is not fully defined: it is used merely as an abstract type which represents an instance of whatever object which implements the interface.

  • The parent of the MamanIbazInterface is not GObjectClass but GTypeInterface.

The implementation of the MamanIbaz type itself is trivial:

  • maman_ibaz_get_type registers the type in the type system.

  • maman_ibaz_base_init is expected to register the interface's signals if there are any (we will see a bit (later how to use them). Make sure to use a static local boolean variable to make sure not to run the initialization code twice (as described in the section called “Interface Initialization”, base_init is run once for each interface implementation instantiation)

  • maman_ibaz_do_action dereferences the class structure to access its associated class function and calls it.

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 iface_type;
}

void
maman_ibaz_do_action (MamanIbaz *self)
{
  g_return_if_fail (MAMAN_IS_IBAZ (self));

  MAMAN_IBAZ_GET_INTERFACE (self)->do_action (self);
}