[glib] docs/gobject/tutorial: Use g_object_class_install_properties



commit 9e732ab0ea742d57a46f72be475ac1d2f026eae2
Author: Javier JardÃn <jjardon gnome org>
Date:   Tue Dec 6 23:15:58 2011 +0000

    docs/gobject/tutorial: Use g_object_class_install_properties

 docs/reference/gobject/tut_gobject.xml |   51 +++++++++++++++++---------------
 docs/reference/gobject/tut_howto.xml   |   33 ++++++++++++++-------
 2 files changed, 49 insertions(+), 35 deletions(-)
---
diff --git a/docs/reference/gobject/tut_gobject.xml b/docs/reference/gobject/tut_gobject.xml
index b34441b..221b35b 100644
--- a/docs/reference/gobject/tut_gobject.xml
+++ b/docs/reference/gobject/tut_gobject.xml
@@ -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);
-
-  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_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);
+
+  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>
   
diff --git a/docs/reference/gobject/tut_howto.xml b/docs/reference/gobject/tut_howto.xml
index 9d592b8..6564d8b 100644
--- a/docs/reference/gobject/tut_howto.xml
+++ b/docs/reference/gobject/tut_howto.xml
@@ -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>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]