+2006-02-22 Stefan Kost <ensonic@users.sf.net>
+
+ * gobject/tmpl/gtype.sgml:
+ add @since: for _add_private, _GET_PRIVATE
+ * gobject/tut_gobject.xml:
+ fix example to use ->priv and not ->private
+ * gobject/tut_howto.xml:
+ fix g_type_class_add_private example
+
2006-02-14 Tor Lillqvist <tml@novell.com>
* glib/tmpl/iochannels.sgml: Document some Windows-specific issues.
used specifically for managing interface types.
</para>
-@interface_init: Location of the function that initializes the interface.
-@interface_finalize: Location of the function that finalizes the interface.
-@interface_data: Location of user data passed to the @interface_init and
- @interface_finalize functions (optional).
<!-- ##### STRUCT GTypeValueTable ##### -->
<para>
@instance: the instance of a type deriving from @private_type.
@g_type: the type identifying which private data to retrieve.
@c_type: The C type for the private structure.
+@Since: 2.4
<!-- ##### MACRO G_TYPE_CHECK_INSTANCE ##### -->
@g_class: class structure for an instantiatable type
@private_size: size of private structure.
+@Since: 2.4
<!-- ##### FUNCTION g_type_interface_peek ##### -->
switch (property_id) {
case MAMAN_BAR_CONSTRUCT_NAME: {
- g_free (self->private->name);
- self->private->name = g_value_dup_string (value);
- g_print ("maman: %s\n",self->private->name);
+ g_free (self->priv->name);
+ self->priv->name = g_value_dup_string (value);
+ g_print ("maman: %s\n",self->priv->name);
}
break;
case MAMAN_BAR_PAPA_NUMBER: {
- self->private->papa_number = g_value_get_uchar (value);
- g_print ("papa: %u\n",self->private->papa_number);
+ self->priv->papa_number = g_value_get_uchar (value);
+ g_print ("papa: %u\n",self->priv->papa_number);
}
break;
default:
switch (property_id) {
case MAMAN_BAR_CONSTRUCT_NAME: {
- g_value_set_string (value, self->private->name);
+ g_value_set_string (value, self->priv->name);
}
break;
case MAMAN_BAR_PAPA_NUMBER: {
- g_value_set_uchar (value, self->private->papa_number);
+ g_value_set_uchar (value, self->priv->papa_number);
}
break;
default:
The private structure is then defined in the .c file, instantiated in the object's
<function>init</function> function and destroyed in the object's <function>finalize</function> function.
<programlisting>
-static void maman_bar_finalize(GObject *object) {
+static void
+maman_bar_finalize (GObject *object) {
MamanBar *self = MAMAN_BAR (object);
/* do stuff */
g_free (self->priv);
}
-static void maman_bar_init(GTypeInstance *instance, gpointer g_class) {
+static void
+maman_bar_init (GTypeInstance *instance, gpointer g_class) {
MamanBar *self = MAMAN_BAR (instance);
- self->priv = g_new0(MamanBarPrivate,1);
+ self->priv = g_new0 (MamanBarPrivate,1);
/* do stuff */
}
</programlisting>
<listitem><para>
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 <function>class_init</function> using
- <function><link linkend="g-type-class-add-private">g_type_class_add_private</link></function> and declare a macro to allow convenient access to this structure.
+ declare it as a private structure in <function>maman_bar_class_init</function> using
+ <function><link linkend="g-type-class-add-private">g_type_class_add_private</link></function>.
+ Instead of allocating memory in <function>maman_bar_init</function> a pointer to the private memory area is
+ stored in the instance 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.
+ Another advantage of this to the previous version is that is lessens memory fragmentation,
+ as the public and private parts of the instance memory are allocated at once.
<programlisting>
typedef struct _MamanBarPrivate 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)
{
...
}
-static int
-maman_bar_get_private_field (MamanBar *self)
-{
- MamanBarPrivate *priv = MAMAN_BAR_GET_PRIVATE (self);
-
- return priv->private_field;
+static void
+maman_bar_init (GTypeInstance *instance, gpointer g_class) {
+ MamanBar *self = MAMAN_BAR (instance);
+ self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MAMAN_BAR_TYPE, MamanBarPrivate);
+ /* do stuff */
}
</programlisting>
</para></listitem>