docs/gobject/tutorial: Use g_object_class_install_properties

This commit is contained in:
Javier Jardón 2011-12-06 23:15:58 +00:00
parent c3d6595f5a
commit 9e732ab0ea
2 changed files with 48 additions and 34 deletions

View File

@ -495,14 +495,14 @@ void g_object_run_dispose (GObject *object);
One of GObject's nice features is its generic get/set mechanism for object
properties. When an object
is instantiated, the object's class_init handler should be used to register
the object's properties with <function><link linkend="g-object-class-install-property">g_object_class_install_property</link></function>
the object's properties with <function><link linkend="g-object-class-install-properties">g_object_class_install_properties</link></function>
(implemented in <filename>gobject.c</filename>).
</para>
<para>
The best way to understand how object properties work is by looking at a real example
on how it is used:
<programlisting>
<informalexample><programlisting>
/************************************************/
/* Implementation */
/************************************************/
@ -512,9 +512,13 @@ enum
PROP_0,
PROP_MAMAN_NAME,
PROP_PAPA_NUMBER
PROP_PAPA_NUMBER,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void
maman_bar_set_property (GObject *object,
guint property_id,
@ -572,30 +576,29 @@ static void
maman_bar_class_init (MamanBarClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GParamSpec *pspec;
gobject_class-&gt;set_property = maman_bar_set_property;
gobject_class-&gt;get_property = maman_bar_get_property;
pspec = g_param_spec_string ("maman-name",
"Maman construct prop",
"Set maman's name",
"no-name-set" /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_property (gobject_class,
PROP_MAMAN_NAME,
pspec);
obj_properties[PROP_NAME] =
g_param_spec_string ("maman-name",
"Maman construct prop",
"Set maman's name",
"no-name-set" /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
pspec = g_param_spec_uchar ("papa-number",
"Number of current Papa",
"Set/Get papa's number",
0 /* minimum value */,
10 /* maximum value */,
2 /* default value */,
G_PARAM_READWRITE);
g_object_class_install_property (gobject_class,
PROP_PAPA_NUMBER,
pspec);
obj_properties[PROP_NUMBER] =
g_param_spec_uchar ("papa-number",
"Number of current Papa",
"Set/Get papa's number",
0 /* minimum value */,
10 /* maximum value */,
2 /* default value */,
G_PARAM_READWRITE);
g_object_class_install_properties (gobject_class,
N_PROPERTIES,
obj_properties);
}
/************************************************/
@ -613,7 +616,7 @@ g_value_set_char (&amp;val, 11);
g_object_set_property (G_OBJECT (bar), "papa-number", &amp;val);
g_value_unset (&amp;val);
</programlisting>
</programlisting></informalexample>
The client code just above looks simple but a lot of things happen under the hood:
</para>

View File

@ -341,26 +341,37 @@ maman_bar_init (MamanBar *self)
<xref linkend="gobject-properties"/>. Make sure that these properties use a construct only
<link linkend="GParamSpec"><type>GParamSpec</type></link> by setting the param spec's flag field to G_PARAM_CONSTRUCT_ONLY: this helps
GType ensure that these properties are not set again later by malicious user code.
<programlisting>
<informalexample><programlisting>
enum {
PROP_0,
PROP_MAMAN,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void
bar_class_init (MamanBarClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GParamSpec *pspec;
gobject_class->set_property = bar_set_property;
gobject_class->get_property = bar_get_property;
pspec = g_param_spec_string ("maman",
"Maman construct prop",
"Set maman's name",
"no-name-set" /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_property (gobject_class,
PROP_MAMAN,
pspec);
obj_properties[PROP_MAMAN] =
g_param_spec_string ("maman",
"Maman construct prop",
"Set maman's name",
"no-name-set" /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties (gobject_class,
N_PROPERTIES,
obj_properties);
}
</programlisting>
</programlisting></informalexample>
If you need this, make sure you can build and run code similar to the code shown above. Make sure
your construct properties can set correctly during construction, make sure you cannot set them
afterwards and make sure that if your users do not call <function><link linkend="g-object-new">g_object_new</link></function>