From be092b4ac436c58e180782aad08efa3a07d42c8d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 5 Nov 2004 07:33:28 +0000 Subject: [PATCH] integrate patches by Sylvain Foret --- docs/reference/gobject/tut_howto.xml | 52 ++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/docs/reference/gobject/tut_howto.xml b/docs/reference/gobject/tut_howto.xml index be5c35e..73e2b4d 100644 --- a/docs/reference/gobject/tut_howto.xml +++ b/docs/reference/gobject/tut_howto.xml @@ -182,6 +182,40 @@ static void maman_bar_init(GTypeInstance *instance, gpointer g_class) { } + + + A similar alternative, available since Glib version 2.4, is to define a private structure in the .c file, + declare it as a private structure in class_init using + g_type_class_add_private and declare a macro to allow convenient access to this structure. + A private structure will then be attached to each newly created object by the GObject system. + You dont need to free or allocate the private structure, only the objects or pointers that it may contain. + +typedef struct _MamanBarPrivate MamanBarPrivate; + +struct _MamanBarPrivate { + int private_field; +}; + +#define MAMAN_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MAMAN_BAR_TYPE, MamanBarPrivate)) + +static void +maman_bar_class_init (MamanBarClass *klass) +{ + ... + g_type_class_add_private (klass, sizeof (MamanBarPrivate)); + ... +} + +static int +maman_bar_get_private_field (MamanBar *self) +{ + MamanBarPrivate *priv = MAMAN_BAR_GET_PRIVATE (self); + + return priv->private_field; +} + + + @@ -377,9 +411,13 @@ struct _MamanBarPrivate { gboolean dispose_has_run; }; +static GObjectClass parent_class = NULL; + static void -bar_dispose (MamanBar *self) +bar_dispose (GObject *obj) { + MamanBar *self = (MamanBar *)obj; + if (self->private->dispose_has_run) { /* If dispose did already run, return. */ return; @@ -393,16 +431,24 @@ bar_dispose (MamanBar *self) * the most simple solution is to unref all members on which you own a * reference. */ + + /* Chain up to the parent class */ + G_OBJECT_CLASS (parent_class)->dispose (obj); } static void -bar_finalize (MamanBar *self) +bar_finalize (GObject *obj) { + MamanBar *self = (MamanBar *)obj; + /* * Here, complete object destruction. * You might not need to do much... */ g_free (self->private); + + /* Chain up to the parent class */ + G_OBJECT_CLASS (parent_class)->finalize (obj); } static void @@ -421,6 +467,8 @@ maman_bar_init (GTypeInstance *instance, MamanBar *self = (MamanBar *)instance; self->private = g_new0 (MamanBarPrivate, 1); self->private->dispose_has_run = FALSE; + + parent_class = g_type_class_peek_parent (klass); } -- 2.7.4