Interface Properties

Starting from version 2.4 of GLib, GObject interfaces can also have properties. Declaration of the interface properties is similar to declaring the properties of ordinary GObject types as explained in the section called “Object properties”, except that g_object_interface_install_property is used to declare the properties instead of g_object_class_install_property.

To include a property named 'name' of type string in the maman_ibaz interface example code above, we only need to add one [15] line in the maman_ibaz_base_init [16] as shown below:

static void
maman_ibaz_base_init (gpointer g_iface)
{
  static gboolean is_initialized = FALSE;

  if (!is_initialized)
    {
      g_object_interface_install_property (g_iface,
                                           g_param_spec_string ("name",
                                                                "Name",
                                                                "Name of the MamanIbaz",
                                                                "maman",
                                                                G_PARAM_READWRITE));
      is_initialized = TRUE;
    }
}

One point worth noting is that the declared property wasn't assigned an integer ID. The reason being that integer IDs of properties are used only inside the get and set methods and since interfaces do not implement properties, there is no need to assign integer IDs to interface properties.

An implementation shall declare and define it's properties in the usual way as explained in the section called “Object properties”, except for one small change: it must declare the properties of the interface it implements using g_object_class_override_property instead of g_object_class_install_property. The following code snippet shows the modifications needed in the MamanBaz declaration and implementation above:


struct _MamanBaz
{
  GObject parent_instance;

  gint instance_member;
  gchar *name;
};

enum
{
  PROP_0,

  PROP_NAME
};

static void
maman_baz_set_property (GObject      *object,
                        guint         property_id,
                        const GValue *value,
                        GParamSpec   *pspec)
{
  MamanBaz *baz = MAMAN_BAZ (object);
  GObject *obj;

  switch (prop_id)
    {
    case ARG_NAME:
      g_free (baz->name);
      baz->name = g_value_dup_string (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
maman_baz_get_property (GObject    *object,
                        guint       prop_id,
                        GValue     *value,
                        GParamSpec *pspec)
{
  MamanBaz *baz = MAMAN_BAZ (object);

  switch (prop_id)
    {
    case ARG_NAME:
      g_value_set_string (value, baz->name);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
maman_baz_class_init (MamanBazClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = maman_baz_set_property;
  gobject_class->get_property = maman_baz_get_property;

  g_object_class_override_property (gobject_class, PROP_NAME, "name");
}



[15] That really is one line extended to six for the sake of clarity

[16] The g_object_interface_install_property can also be called from class_init but it must not be called after that point.