Initial revision
[platform/upstream/glib.git] / docs / reference / gobject / tut_gobject.xml
1 <?xml version='1.0' encoding="ISO-8859-1"?>
2
3   <chapter id="chapter-gobject">
4     <title>GObject: what brings everything together.</title>
5
6     <para>
7       The two previous chapters discussed the details of Glib's Dynamic Type System
8       and its signal control system. The GObject library also contains an implementation
9       for a base fundamental type named <type>GObject</type>.
10     </para>
11
12     <para>
13       <type>GObject</type> is a fundamental classed instantiable type. It implements:
14       <itemizedlist>
15         <listitem><para>Memory management with reference counting</para></listitem>
16         <listitem><para>Construction/Destruction of instances</para></listitem>
17         <listitem><para>Generic per-object properties with set/get function pairs</para></listitem>
18         <listitem><para>Easy use of signals</para></listitem>
19       </itemizedlist>
20       All the GTK objects and all of the objects in Gnome libraries which use the glib type
21       system inherit from <type>GObject</type> which is why it is important to understand
22       the details of how it works.
23     </para>
24
25     <sect1 id="gobject-instanciation">
26       <title>Object instanciation</title>
27
28       <para>
29         The <function>g_object_new</function> family of functions can be used to instantiate any
30         GType which inherits from the GObject base type. All these functions make sure the class
31         has been correctly initialized by glib's type system and then invoke at one
32         point or another the constructor class method which is used to:
33         <itemizedlist>
34           <listitem><para>
35               Allocate memory through <function>g_type_create_instance</function>,
36             </para></listitem>
37           <listitem><para>
38               Initialize the object' instance with the construction properties.
39             </para></listitem>
40         </itemizedlist>
41       </para>
42
43       <para>
44         Objects which inherit from GObject are allowed to override this constructor class method:
45         they should however chain to their parent constructor method before doing so:
46 <programlisting>
47   GObject*   (*constructor)     (GType                  type,
48                                  guint                  n_construct_properties,
49                                  GObjectConstructParam *construct_properties);
50 </programlisting>
51       </para>
52
53       <para>
54         The example below shows how <type>MamanBar</type> overrides the parent's constructor:
55 <programlisting>
56 #define MAMAN_BAR_TYPE            (maman_bar_get_type ())
57 #define MAMAN_BAR(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_BAR_TYPE, MamanBar))
58 #define MAMAN_BAR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_BAR_TYPE, MamanBarClass))
59 #define MAMAN_IS_BAR(obj)         (G_TYPE_CHECK_TYPE ((obj), MAMAN_BAR_TYPE))
60 #define MAMAN_IS_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_BAR_TYPE))
61 #define MAMAN_BAR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_BAR_TYPE, MamanBarClass))
62
63 typedef struct _MamanBar MamanBar;
64 typedef struct _MamanBarClass MamanBarClass;
65
66 struct _MamanBar {
67   GObject parent;
68   /* instance members */
69 };
70
71 struct _MamanBarClass {
72   GObjectClass parent;
73
74   /* class members */
75 };
76
77 /* used by MAMAN_BAR_TYPE */
78 GType maman_bar_get_type (void);
79
80 static GObject *
81 maman_bar_constructor (GType                  type,
82                        guint                  n_construct_properties,
83                        GObjectConstructParam *construct_properties)
84 {
85   GObject *obj;
86
87   {
88     /* Invoke parent constructor. */
89     MamanBarClass *klass;
90     GObjectClass *parent_class;  
91     klass = MAMAN_BAR_CLASS (g_type_class_peek (MAMAN_BAR_TYPE));
92     parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
93     obj = parent_class->constructor (type,
94                                      n_construct_properties,
95                                      construct_properties);
96   }
97   
98   /* do stuff. */
99
100   return obj;
101 }
102
103 static void
104 maman_bar_instance_init (GTypeInstance   *instance,
105                          gpointer         g_class)
106 {
107   MamanBar *self = (MamanBar *)instance;
108   /* do stuff */
109 }
110
111 static void
112 maman_bar_class_init (gpointer g_class,
113                       gpointer g_class_data)
114 {
115   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
116   MamanBarClass *klass = MAMAN_BAR_CLASS (g_class);
117
118   gobject_class->constructor = maman_bar_constructor;
119 }
120
121 GType maman_bar_get_type (void)
122 {
123   static GType type = 0;
124   if (type == 0) {
125     static const GTypeInfo info = {
126       sizeof (MamanBarClass),
127       NULL,   /* base_init */
128       NULL,   /* base_finalize */
129       maman_bar_class_init,   /* class_init */
130       NULL,   /* class_finalize */
131       NULL,   /* class_data */
132       sizeof (MamanBar),
133       0,      /* n_preallocs */
134       maman_bar_instance_init    /* instance_init */
135     };
136     type = g_type_register_static (G_TYPE_OBJECT,
137                                    "MamanBarType",
138                                    &amp;info, 0);
139   }
140   return type;
141 }
142 </programlisting>
143         If the user instantiates an object <type>MamanBar</type> with:
144 <programlisting>
145 MamanBar *bar = g_object_new (MAMAN_BAR_TYPE, NULL);
146 </programlisting>       
147         If this is the first instantiation of such an object, the <function>maman_b_class_init</function>
148         function will be invoked after any <function>maman_b_base_class_init</function> function. 
149         This will make sure the class structure of this new object is correctly initialized. Here, 
150         <function>maman_bar_class_init</function> is expected to override the object's class methods
151         and setup the class' own methods. In the example above, the constructor method is the only 
152         overridden method: it is set to <function>maman_bar_constructor</function>.
153       </para>
154
155       <para>
156         Once <function>g_object_new</function> has obtained a reference to an initialized
157         class structure, it invokes its constructor method to create an instance of the new 
158         object. Since it has just been overridden by <function>maman_bar_class_init</function> 
159         to <function>maman_bar_constructor</function>, the latter is called and, because it
160         was implemented correctly, it chains up to its parent's constructor. The problem here
161         is how we can find the parent constructor. An approach (used in GTK+ source code) would be
162         to save the original constructor in a static variable from <function>maman_bar_class_init</function>
163         and then to re-use it from <function>maman_bar_constructor</function>. This is clearly possible
164         and very simple but I was told it was not nice and the prefered way is to use the 
165         <function>g_type_class_peek</function> and <function>g_type_class_peek_parent</function> functions.
166       </para>
167
168       <para>
169         Finally, at one point or another, <function>g_object_constructor</function> is invoked
170         by the last constructor in the chain. This function allocates the object's instance' buffer 
171         through <function>g_type_create_instance</function>
172         which means that the instance_init function is invoked at this point if one
173         was registered. After instance_init returns, the object is fully initialized and should be 
174         ready to answer any user-request. When <function>g_type_create_instance</function>
175         returns, <function>g_object_constructor</function> sets the construction properties
176         (ie: the properties which were given to <function>g_object_new</function>) and returns
177         to the user's constructor which is then allowed to do useful instance initialization...
178       </para>
179
180       <para>
181         The process described above might seem a bit complicated (it <emphasis>is</emphasis> actually
182         overly complicated in my opinion..) but it can be summarized easily by the table below which
183         lists the functions invoked by <function>g_object_new</function> and their order of
184         invocation.
185       </para>
186
187       <para>
188         The array below lists the functions invoked by <function>g_object_new</function> and 
189         their order of invocation:
190
191         <table id="gobject-construction-table">
192           <title><function>g_object_new</function></title>
193           <tgroup cols="3">
194             <colspec colwidth="*" colnum="1" align="left"/>
195             <colspec colwidth="*" colnum="2" align="left"/>
196             <colspec colwidth="8*" colnum="3" align="left"/>
197
198             <thead>
199               <row>
200                 <entry>Invocation time</entry>
201                 <entry>Function Invoked</entry>
202                 <entry>Function's parameters</entry>
203                 <entry>Remark</entry>
204               </row>
205             </thead>
206             <tbody>
207               <row>
208                 <entry>First call to <function>g_object_new</function> for target type</entry>
209                 <entry>target type's base_init function</entry>
210                 <entry>On the inheritance tree of classes from fundamental type to target type. 
211                   base_init is invoked once for each class structure.</entry>
212                 <entry>
213                   I have no real idea on how this can be used. If you have a good real-life
214                   example of how a class' base_init can be used, please, let me know.
215                 </entry>
216               </row>
217               <row>
218                 <entry>First call to <function>g_object_new</function> for target type</entry>
219                 <entry>target type's class_init function</entry>
220                 <entry>On target type's class structure</entry>
221                 <entry>
222                   Here, you should make sure to initialize or override class methods (that is,
223                   assign to each class' method its function pointer) and create the signals and
224                   the properties associated to your object.
225                 </entry>
226               </row>
227               <row>
228                 <entry>First call to <function>g_object_new</function> for target type</entry>
229                 <entry>interface' base_init function</entry>
230                 <entry>On interface' vtable</entry>
231                 <entry></entry>
232               </row>
233               <row>
234                 <entry>First call to <function>g_object_new</function> for target type</entry>
235                 <entry>interface' interface_init function</entry>
236                 <entry>On interface' vtable</entry>
237                 <entry></entry>
238               </row>
239               <row>
240                 <entry>Each call to <function>g_object_new</function> for target type</entry>
241                 <entry>target type's class constructor method: GObjectClass->constructor</entry>
242                 <entry>On object's instance</entry>
243                 <entry>
244                   If you need to complete the object initialization after all the construction properties
245                   are set, override the constructor method and make sure to chain up to the object's
246                   parent class before doing your own initialization.
247                   In doubt, do not override the constructor method.
248                 </entry>
249               </row>
250               <row>
251                 <entry>Each call to <function>g_object_new</function> for target type</entry>
252                 <entry>type's instance_init function</entry>
253                 <entry>On the inheritance tree of classes from fundamental type to target type. 
254                 the instance_init provided for each type is invoked once for each instance 
255                 structure.</entry>
256                 <entry>
257                   Provide an instance_init function to initialize your object before its construction
258                   properties are set. This is the preferred way to initialize a GObject instance.
259                   This function is equivalent to C++ constructors.
260                 </entry>
261               </row>
262             </tbody>
263           </tgroup>
264         </table>
265       </para>
266
267       <para>
268         Readers should feel concerned about one little twist in the order in which functions 
269         are invoked: while, technically, the class' constructor method is called
270         <emphasis>before</emphasis> the GType's instance_init function (since 
271         <function>g_type_create_instance</function> which calls instance_init is called by
272         <function>g_object_constructor</function> which is the top-level class 
273         constructor method and to which users are expected to chain to), the user's code
274         which runs in a user-provided constructor will always run <emphasis>after</emphasis>
275         GType's instance_init function since the user-provided constructor 
276         <emphasis>must</emphasis> (you've been warned) chain up <emphasis>before</emphasis>
277         doing anything useful.
278       </para>
279     </sect1>
280
281     <sect1 id="gobject-memory">
282       <title>Object memory management</title>
283
284       <para>
285         The memory-management API for GObjects is a bit complicated but the idea behind it
286         is pretty simple: the goal is to provide a flexible model based on reference counting
287         which can be integrated in applications which use or require different memory management
288         models (such as garbage collection, aso...)
289 <programlisting>
290 /*
291   Refcounting
292 */
293 gpointer    g_object_ref                      (gpointer        object);
294 void        g_object_unref                    (gpointer        object);
295
296 /*
297   Weak References
298 */
299 typedef void (*GWeakNotify)             (gpointer      data,
300                                          GObject      *where_the_object_was);
301 void        g_object_weak_ref                 (GObject        *object,
302                                                GWeakNotify     notify,
303                                                gpointer        data);
304 void        g_object_weak_unref               (GObject        *object,
305                                                GWeakNotify     notify,
306                                                gpointer        data);
307 void        g_object_add_weak_pointer         (GObject        *object, 
308                                                gpointer       *weak_pointer_location);
309 void        g_object_remove_weak_pointer      (GObject        *object, 
310                                                gpointer       *weak_pointer_location);
311 /*
312   Cycle handling
313 */
314 void        g_object_run_dispose              (GObject        *object);
315 </programlisting>
316       </para>
317
318       <sect2 id="gobject-memory-refcount">
319         <title>Reference count</title>
320
321         <para>
322           <function>g_object_ref</function>/<function>g_object_unref</function> respectively 
323           increase and decrease the reference count. None of these function is thread-safe.
324           The reference count is, unsurprisingly, initialized to one by 
325           <function>g_object_new</function>. When the reference count reaches zero, that is, 
326           when <function>g_object_unref</function> is called by the last client holding
327           a reference to the object, the <emphasis>dispose</emphasis> and the 
328           <emphasis>finalize</emphasis> class methods are invoked.
329         </para>
330         <para>
331           Finally, after <emphasis>finalize</emphasis> is invoked, 
332           <function>g_type_free_instance</function> is called to free the object instance.
333           Depending on the memory allocation policy decided when the type was registered (through
334           one of the <function>g_type_register_*</function> functions), the object's instance 
335           memory will be freed or returned to the object pool for this type.
336           Once the object has been freed, if it was the last instance of the type, the type's class
337           will be destroyed as described in <xref linkend="gtype-instantiable-classed"></xref> and 
338             <xref linkend="gtype-non-instantiable-classed"></xref>.
339         </para>
340
341         <para>
342           The table below summarizes the destruction process of a GObject:
343           <table id="gobject-destruction-table">
344             <title><function>g_object_unref</function></title>
345             <tgroup cols="3">
346               <colspec colwidth="*" colnum="1" align="left"/>
347               <colspec colwidth="*" colnum="2" align="left"/>
348               <colspec colwidth="8*" colnum="3" align="left"/>
349
350               <thead>
351                 <row>
352                   <entry>Invocation time</entry>
353                   <entry>Function Invoked</entry>
354                   <entry>Function's parameters</entry>
355                   <entry>Remark</entry>
356                 </row>
357             </thead>
358               <tbody>
359                 <row>
360                   <entry>Last call to <function>g_object_unref</function> for an instance
361                     of target type</entry>
362                   <entry>target type's dispose class function</entry>
363                   <entry>GObject instance</entry>
364                   <entry>
365                     When dispose ends, the object should not hold any reference to any other
366                     member object. The object is also expected to be able to answer client
367                     method invocations (with possibly an error code but no memory violation)
368                     until finalize is executed. dispose can be executed more than once.
369                   dispose should chain up to its parent implementation just before returning
370                   to the caller.
371                   </entry>
372                 </row>
373                 <row>
374                   <entry>Last call to <function>g_object_unref</function> for an instance
375                     of target type
376                   </entry>
377                   <entry>target type's finalize class function</entry>
378                   <entry>GObject instance</entry>
379                   <entry>
380                     Finalize is expected to complete the destruction process initiated by
381                     dispose. It should complete the object's destruction. finalize will be
382                     executed only once.
383                   finalize should chain up to its parent implementation just before returning
384                   to the caller.
385                     The reason why the destruction process is split is two different phases is
386                     explained in <xref linkend="gobject-memory-cycles"></xref>.
387                   </entry>
388                 </row>
389                 <row>
390                   <entry>Last call to <function>g_object_unref</function> for the last
391                     instance of target type</entry>
392                   <entry>interface' interface_finalize function</entry>
393                   <entry>On interface' vtable</entry>
394                   <entry>Never used in practice. Unlikely you will need it.</entry>
395                 </row>
396                 <row>
397                   <entry>Last call to <function>g_object_unref</function>for the last
398                     instance of target type</entry>
399                   <entry>interface' base_finalize function</entry>
400                   <entry>On interface' vtable</entry>
401                   <entry>Never used in practice. Unlikely you will need it.</entry>
402                 </row>
403                 <row>
404                   <entry>Last call to <function>g_object_unref</function> for the last
405                     instance of target type</entry>
406                   <entry>target type's class_finalize function</entry>
407                   <entry>On target type's class structure</entry>
408                   <entry>Never used in practice. Unlikely you will need it.</entry>
409                 </row>
410                 <row>
411                   <entry>Last call to <function>g_object_unref</function> for the last
412                     instance of target type</entry>
413                   <entry>type's base_finalize function</entry>
414                   <entry>On the inheritance tree of classes from fundamental type to target type. 
415                     base_init is invoked once for each class structure.</entry>
416                   <entry>Never used in practice. Unlikely you will need it.</entry>
417                 </row>
418               </tbody>
419             </tgroup>
420           </table>              
421         </para>
422
423       </sect2>
424
425       <sect2 id="gobject-memory-weakref">
426         <title>Weak References</title>
427       
428         <para>
429         Weak References are used to monitor object finalization: 
430         <function>g_object_weak_ref</function> adds a monitoring callback which does
431         not hold a reference to the object but which is invoked when the object runs 
432         its dispose method. As such, each weak ref can be invoked more than once upon
433         object finalization (since dispose can run more than once during object 
434         finalization).
435         </para>
436
437       <para>
438         <function>g_object_weak_unref</function> can be used to remove a monitoring
439         callback from the object. 
440       </para>
441
442       <para>
443         Weak References are also used to implement <function>g_object_add_weak_pointer</function>
444         and <function>g_object_remove_weak_pointer</function>. These functions add a weak reference
445         to the object they are applied to which makes sure to nullify the pointer given by the user
446         when object is finalized.
447       </para>
448
449     </sect2>
450
451     <sect2 id="gobject-memory-cycles">
452       <title>Reference counts and cycles</title>
453       
454       <para>
455         Note: the following section was inspired by James Henstridge. I guess this means that 
456         all praise and all curses will be directly forwarded to him.
457       </para>
458
459       <para>
460         GObject's memory management model was designed to be easily integrated in existing code
461         using garbage collection. This is why the destruction process is split in two phases:
462         the first phase, executed in the dispose handler is supposed to release all references
463         to other member objects. The second phase, executed by the finalize handler is supposed
464         to complete the object's destruction process. Object methods should be able to run
465         without program error (that is, without segfault :) in-between the two phases.
466       </para>
467
468       <para>
469         This two-step destruction process is very useful to break reference counting cycles.
470         While the detection of the cycles is up to the external code, once the cycles have been
471         detected, the external code can invoke <function>g_object_dispose</function> which 
472         will indeed break any existing cycles since it will run the dispose handler associated
473         to the object and thus release all references to other objects.
474       </para>
475
476       <para>
477         Attentive readers might now have understood one of the rules about the dispose handler
478         we stated a bit sooner: the dispose handler can be invoked multiple times. Let's say we
479         have a reference count cycle: object A references B which itself references object A.
480         Let's say we have detected the cycle and we want to destroy the two objects. One way to 
481         do this would be to invoke <function>g_object_dispose</function> on one of the 
482         objects.
483       </para>
484
485       <para>
486         If object A releases all its references to all objects, this means it releases its
487         reference to object B. If object B was not owned by anyone else, this is its last
488         reference count which means this last unref runs B's dispose handler which, in turn,
489         releases B's reference on object A. If this is A's last reference count, this last 
490         unref runs A's dispose handler which is running for the second time before
491         A's finalize handler is invoked !
492       </para>
493
494       <para>
495         The above example, which might seem a bit contrived can really happen if your
496         GObject's are being by language bindings. I would thus suggest the rules stated above
497         for object destruction are closely followed. Otherwise, <emphasis>Bad Bad Things</emphasis> 
498         will happen.
499       </para>
500     </sect2>
501     </sect1>
502
503   <sect1 id="gobject-properties">
504     <title>Object properties</title>
505
506     <para>
507       One of GObject's nice features is its generic get/set mechanism. When an object
508       is instanciated, the object's class_init handler should be used to register
509       the object's properties with <function>g_object_class_install_property</function>
510       (implemented in <filename>gobject.c</filename>).
511     </para>
512
513     <para>
514       The best way to understand how object properties work is by looking at a real example
515       on how it is used:
516 <programlisting>
517 /************************************************/
518 /* Implementation                               */
519 /************************************************/
520
521 enum {
522   MAMAN_BAR_CONSTRUCT_NAME = 1,
523   MAMAN_BAR_PAPA_NUMBER = 2,
524 };
525
526 static void
527 maman_bar_instance_init (GTypeInstance   *instance,
528                          gpointer         g_class)
529 {
530   MamanBar *self = (MamanBar *)instance;
531 }
532
533
534 static void
535 maman_bar_set_property (GObject      *object,
536                         guint         property_id,
537                         const GValue *value,
538                         GParamSpec   *pspec)
539 {
540   MamanBar *self = (MamanBar *) object;
541
542   switch (property_id) {
543   case MAMAN_BAR_CONSTRUCT_NAME: {
544     g_free (self->private->name);
545     self->private->name = g_value_dup_string (value);
546     g_print ("maman: %s\n",self->private->name);
547   }
548     break;
549   case MAMAN_BAR_PAPA_NUMBER: {
550     self->private->papa_number = g_value_get_uchar (value);
551     g_print ("papa: %u\n",self->private->papa_number);
552   }
553     break;
554   default:
555     /* We don't have any other property... */
556     g_assert (FALSE);
557     break;
558   }
559 }
560
561 static void
562 maman_bar_get_property (GObject      *object,
563                         guint         property_id,
564                         GValue       *value,
565                         GParamSpec   *pspec)
566 {
567   MamanBar *self = (MamanBar *) object;
568
569   switch (property_id) {
570   case MAMAN_BAR_CONSTRUCT_NAME: {
571     g_value_set_string (value, self->private->name);
572   }
573     break;
574   case MAMAN_BAR_PAPA_NUMBER: {
575     g_value_set_uchar (value, self->private->papa_number);
576   }
577     break;
578   default:
579     /* We don't have any other property... */
580     g_assert (FALSE);
581     break;
582   }
583 }
584
585 static void
586 maman_bar_class_init (gpointer g_class,
587                       gpointer g_class_data)
588 {
589   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
590   MamanBarClass *klass = MAMAN_BAR_CLASS (g_class);
591   GParamSpec *pspec;
592
593   gobject_class->set_property = maman_bar_set_property;
594   gobject_class->get_property = maman_bar_get_property;
595
596   pspec = g_param_spec_string ("maman-name",
597                                "Maman construct prop",
598                                "Set maman's name",
599                                "no-name-set" /* default value */,
600                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
601   g_object_class_install_property (gobject_class,
602                                    MAMAN_BAR_CONSTRUCT_NAME,
603                                    pspec);
604
605   pspec = g_param_spec_uchar ("papa-number",
606                               "Number of current Papa",
607                               "Set/Get papa's number",
608                               0  /* minimum value */,
609                               10 /* maximum value */,
610                               2  /* default value */,
611                               G_PARAM_READWRITE);
612   g_object_class_install_property (gobject_class,
613                                    MAMAN_BAR_PAPA_NUMBER,
614                                    pspec);
615 }
616
617 /************************************************/
618 /* Use                                          */
619 /************************************************/
620
621 GObject *bar;
622 GValue val = {0,};
623 bar = g_object_new (MAMAN_SUBBAR_TYPE, NULL);
624 g_value_init (&amp;val, G_TYPE_CHAR);
625 g_value_set_char (&amp;val, 11);
626 g_object_set_property (G_OBJECT (bar), "papa-number", &amp;val);
627 </programlisting>
628       The client code just above looks simple but a lot of things happen under the hood:
629     </para>
630
631     <para>
632       <function>g_object_set_property</function> first ensures a property
633       with this name was registered in bar's class_init handler. If so, it calls
634       <function>object_set_property</function> which first walks the class hierarchy,
635       from bottom, most derived type, to top, fundamental type to find the class
636       which registered that property. It then tries to convert the user-provided GValue
637       into a GValue whose type if that of the associated property.
638     </para>
639
640     <para>
641       If the user provides a signed char GValue, as is shown
642       here, and if the object's property was registered as an unsigned int, 
643       <function>g_value_transform</function> will try to transform the input signed char into
644       an unsigned int. Of course, the success of the transformation depends on the availability
645       of the required transform function. In practice, there will almost always be a transformation
646         <footnote>
647           <para>Its behaviour might not be what you expect but it is up to you to actually avoid
648       relying on these transformations.
649           </para>
650         </footnote>
651       which matches and conversion will be caried out if needed.
652     </para>
653
654     <para>
655       After transformation, the <type>GValue</type> is validated by 
656       <function>g_param_value_validate</function> which makes sure the user's
657       data stored in the <type>GValue</type> matches the characteristics specified by
658       the property's <type>GParamSpec</type>.  Here, the <type>GParamSpec</type> we 
659       provided in class_init has a validation function which makes sure that the GValue
660       contains a value which respects the minimum and maximum bounds of the 
661       <type>GParamSpec</type>. In the example above, the client's GValue does not
662       respect these constraints (it is set to 11, while the maximum is 10). As such, the
663       <function>g_object_set_property</function> function will return with an error.
664     </para>
665
666     <para>
667       If the user's GValue had been set to a valid value, <function>object_set_property</function>
668       would have proceeded with calling the object's set_property class method. Here, since our
669       implementation of Foo did override this method, the code path would jump to
670       <function>foo_set_property</function> after having retrieved from the 
671       <type>GParamSpec</type> the <emphasis>param_id</emphasis>
672       <footnote>
673         <para>
674           It should be noted that the param_id used here need only to uniquely identify each 
675           <type>GParamSpec</type> within the <type>FooClass</type> such that the switch
676           used in the set and get methods actually works. Of course, this locally-unique 
677           integer is purely an optimization: it would have been possible to use a set of 
678           <emphasis>if (strcmp (a, b) == 0) {} else if (strcmp (a, b) == 0) {}</emphasis> statements.
679         </para>
680       </footnote>
681       which had been stored by
682       <function>g_object_class_install_property</function>.
683     </para>
684
685     <para>
686       Once the property has been set by the object's set_property class method, the code path
687       returns to <function>g_object_set_property</function> which calls 
688         <function>g_object_notify_queue_thaw</function>. This function makes sure that
689         the "notify" signal is emitted on the object's instance with the changed property as
690         parameter unless notifications were frozen by <function>g_object_freeze_notify</function>.
691     </para>
692
693     <para>
694       <function>g_object_thaw_notify</function> can be used to re-enable notification of 
695       property modifications through the "notify" signal. It is important to remember that
696       even if properties are changed while property change notification is frozen, the "notify"
697       signal will be emitted once for each of these changed properties as soon as the property
698       change notification is thawn: no property change is lost for the "notify" signal. Signal
699       can only be delayed by the notification freezing mechanism.
700     </para>
701
702     <para>
703       It is interesting to note that the <function>g_object_set</function> and 
704       <function>g_object_set_valist</function> (vararg version) functions can be used to set
705       multiple properties at once. The client code shown above can then be re-written as:
706 <programlisting>
707 MamanBar *foo;
708 foo = /* */;
709 g_object_set (G_OBJECT (foo),
710               "papa-number", 2, 
711               "maman-name", "test", 
712               NULL);
713 </programlisting>
714       The code above will trigger one notify signal emission for each property modified.
715     </para>
716
717     <para>
718       Of course, the _get versions are also available: <function>g_object_get</function>
719       and <function>g_object_get_valist</function> (vararg version) can be used to get numerous
720       properties at once.
721     </para>
722
723     <para>
724       Really attentive readers now understand how <function>g_object_new</function>,
725       <function>g_object_newv</function> and <function>g_object_new_valist</function> 
726       work: they parse the user-provided variable number of parameters and invoke
727         <function>g_object_set</function> on each pair of parameters only after the object has been successfully constructed.
728       Of course, the "notify" signal will be emitted for each property set.
729     </para>
730
731   </sect1>
732
733   </chapter>
734
735
736
737