Function Macro

GObjectADD_PRIVATE

Declaration [src]

#define G_ADD_PRIVATE (
  TypeName
)

Description [src]

A convenience macro to ease adding private data to instances of a new type in the _C_ section of G_DEFINE_TYPE_WITH_CODE() or G_DEFINE_ABSTRACT_TYPE_WITH_CODE().

For instance:

  typedef struct _MyObject MyObject;
  typedef struct _MyObjectClass MyObjectClass;

  typedef struct {
    gint foo;
    gint bar;
  } MyObjectPrivate;

  G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
                           G_ADD_PRIVATE (MyObject))

Will add MyObjectPrivate as the private data to any instance of the MyObject type.

G_DEFINE_TYPE_* macros will automatically create a private function based on the arguments to this macro, which can be used to safely retrieve the private data from an instance of the type; for instance:

  gint
  my_object_get_foo (MyObject *obj)
  {
    MyObjectPrivate *priv = my_object_get_instance_private (obj);

    g_return_val_if_fail (MY_IS_OBJECT (obj), 0);

    return priv->foo;
  }

  void
  my_object_set_bar (MyObject *obj,
                     gint      bar)
  {
    MyObjectPrivate *priv = my_object_get_instance_private (obj);

    g_return_if_fail (MY_IS_OBJECT (obj));

    if (priv->bar != bar)
      priv->bar = bar;
  }

Note that this macro can only be used together with the G_DEFINE_TYPE_* macros, since it depends on variable names from those macros.

Also note that private structs added with these macros must have a struct name of the form TypeNamePrivate.

It is safe to call the _get_instance_private function on NULL or invalid objects since it’s only adding an offset to the instance pointer. In that case the returned pointer must not be dereferenced.

Available since:2.38

Parameters

TypeName -
 

The name of the type in CamelCase.