[notify] Remove GObjectNotifyContext indirection
[platform/upstream/glib.git] / gobject / gobject.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * MT safe with regards to reference counting.
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <signal.h>
28
29 #include "gobject.h"
30 #include "gtype-private.h"
31 #include "gvaluecollector.h"
32 #include "gsignal.h"
33 #include "gparamspecs.h"
34 #include "gvaluetypes.h"
35 #include "gobject_trace.h"
36
37 /**
38  * SECTION:objects
39  * @short_description: The base object type
40  * @see_also: #GParamSpecObject, g_param_spec_object()
41  * @title: The Base Object Type
42  *
43  * GObject is the fundamental type providing the common attributes and
44  * methods for all object types in GTK+, Pango and other libraries
45  * based on GObject.  The GObject class provides methods for object
46  * construction and destruction, property access methods, and signal
47  * support.  Signals are described in detail in <xref
48  * linkend="gobject-Signals"/>.
49  *
50  * <para id="floating-ref">
51  * GInitiallyUnowned is derived from GObject. The only difference between
52  * the two is that the initial reference of a GInitiallyUnowned is flagged
53  * as a <firstterm>floating</firstterm> reference.
54  * This means that it is not specifically claimed to be "owned" by
55  * any code portion. The main motivation for providing floating references is
56  * C convenience. In particular, it allows code to be written as:
57  * |[
58  * container = create_container ();
59  * container_add_child (container, create_child());
60  * ]|
61  * If <function>container_add_child()</function> will g_object_ref_sink() the
62  * passed in child, no reference of the newly created child is leaked.
63  * Without floating references, <function>container_add_child()</function>
64  * can only g_object_ref() the new child, so to implement this code without
65  * reference leaks, it would have to be written as:
66  * |[
67  * Child *child;
68  * container = create_container ();
69  * child = create_child ();
70  * container_add_child (container, child);
71  * g_object_unref (child);
72  * ]|
73  * The floating reference can be converted into
74  * an ordinary reference by calling g_object_ref_sink().
75  * For already sunken objects (objects that don't have a floating reference
76  * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
77  * a new reference.
78  * Since floating references are useful almost exclusively for C convenience,
79  * language bindings that provide automated reference and memory ownership
80  * maintenance (such as smart pointers or garbage collection) should not
81  * expose floating references in their API.
82  * </para>
83  *
84  * Some object implementations may need to save an objects floating state
85  * across certain code portions (an example is #GtkMenu), to achieve this,
86  * the following sequence can be used:
87  *
88  * |[
89  * /&ast; save floating state &ast;/
90  * gboolean was_floating = g_object_is_floating (object);
91  * g_object_ref_sink (object);
92  * /&ast; protected code portion &ast;/
93  * ...;
94  * /&ast; restore floating state &ast;/
95  * if (was_floating)
96  *   g_object_force_floating (object);
97  * g_object_unref (object); /&ast; release previously acquired reference &ast;/
98  * ]|
99  */
100
101
102 /* --- macros --- */
103 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
104 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
105
106 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
107 #define OBJECT_HAS_TOGGLE_REF(object) \
108     ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
109 #define OBJECT_FLOATING_FLAG 0x2
110
111 #define CLASS_HAS_PROPS_FLAG 0x1
112 #define CLASS_HAS_PROPS(class) \
113     ((class)->flags & CLASS_HAS_PROPS_FLAG)
114 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
115     ((class)->constructor != g_object_constructor)
116 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
117     ((class)->constructed != g_object_constructed)
118
119 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
120 #define CLASS_HAS_DERIVED_CLASS(class) \
121     ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
122
123 /* --- signals --- */
124 enum {
125   NOTIFY,
126   LAST_SIGNAL
127 };
128
129
130 /* --- properties --- */
131 enum {
132   PROP_NONE
133 };
134
135
136 /* --- prototypes --- */
137 static void     g_object_base_class_init                (GObjectClass   *class);
138 static void     g_object_base_class_finalize            (GObjectClass   *class);
139 static void     g_object_do_class_init                  (GObjectClass   *class);
140 static void     g_object_init                           (GObject        *object,
141                                                          GObjectClass   *class);
142 static GObject* g_object_constructor                    (GType                  type,
143                                                          guint                  n_construct_properties,
144                                                          GObjectConstructParam *construct_params);
145 static void     g_object_constructed                    (GObject        *object);
146 static void     g_object_real_dispose                   (GObject        *object);
147 static void     g_object_finalize                       (GObject        *object);
148 static void     g_object_do_set_property                (GObject        *object,
149                                                          guint           property_id,
150                                                          const GValue   *value,
151                                                          GParamSpec     *pspec);
152 static void     g_object_do_get_property                (GObject        *object,
153                                                          guint           property_id,
154                                                          GValue         *value,
155                                                          GParamSpec     *pspec);
156 static void     g_value_object_init                     (GValue         *value);
157 static void     g_value_object_free_value               (GValue         *value);
158 static void     g_value_object_copy_value               (const GValue   *src_value,
159                                                          GValue         *dest_value);
160 static void     g_value_object_transform_value          (const GValue   *src_value,
161                                                          GValue         *dest_value);
162 static gpointer g_value_object_peek_pointer             (const GValue   *value);
163 static gchar*   g_value_object_collect_value            (GValue         *value,
164                                                          guint           n_collect_values,
165                                                          GTypeCValue    *collect_values,
166                                                          guint           collect_flags);
167 static gchar*   g_value_object_lcopy_value              (const GValue   *value,
168                                                          guint           n_collect_values,
169                                                          GTypeCValue    *collect_values,
170                                                          guint           collect_flags);
171 static void     g_object_dispatch_properties_changed    (GObject        *object,
172                                                          guint           n_pspecs,
173                                                          GParamSpec    **pspecs);
174 static guint               object_floating_flag_handler (GObject        *object,
175                                                          gint            job);
176
177 static void object_interface_check_properties           (gpointer        func_data,
178                                                          gpointer        g_iface);
179
180 /* --- typedefs --- */
181 typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
182
183 struct _GObjectNotifyQueue
184 {
185   GSList  *pspecs;
186   guint16  n_pspecs;
187   guint16  freeze_count;
188 };
189
190 /* --- variables --- */
191 G_LOCK_DEFINE_STATIC (closure_array_mutex);
192 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
193 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
194 static GQuark               quark_closure_array = 0;
195 static GQuark               quark_weak_refs = 0;
196 static GQuark               quark_toggle_refs = 0;
197 static GQuark               quark_notify_queue;
198 static GParamSpecPool      *pspec_pool = NULL;
199 static gulong               gobject_signals[LAST_SIGNAL] = { 0, };
200 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
201 G_LOCK_DEFINE_STATIC (construction_mutex);
202 static GSList *construction_objects = NULL;
203
204 /* --- functions --- */
205 #ifdef  G_ENABLE_DEBUG
206 #define IF_DEBUG(debug_type)    if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
207 G_LOCK_DEFINE_STATIC     (debug_objects);
208 static volatile GObject *g_trap_object_ref = NULL;
209 static guint             debug_objects_count = 0;
210 static GHashTable       *debug_objects_ht = NULL;
211
212 G_LOCK_DEFINE_STATIC(notify_lock);
213
214 /* --- functions --- */
215 static void
216 g_object_notify_queue_free (gpointer data)
217 {
218   GObjectNotifyQueue *nqueue = data;
219
220   g_slist_free (nqueue->pspecs);
221   g_slice_free (GObjectNotifyQueue, nqueue);
222 }
223
224 static inline GObjectNotifyQueue*
225 g_object_notify_queue_freeze (GObject *object)
226 {
227   GObjectNotifyQueue *nqueue;
228
229   G_LOCK(notify_lock);
230   nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
231   if (!nqueue)
232     {
233       nqueue = g_slice_new0 (GObjectNotifyQueue);
234       g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
235                                    nqueue, g_object_notify_queue_free);
236     }
237
238   if (nqueue->freeze_count >= 65535)
239     g_critical("Free queue for %s (%p) is larger than 65535,"
240                " called g_object_freeze_notify() too often."
241                " Forgot to call g_object_thaw_notify() or infinite loop",
242                G_OBJECT_TYPE_NAME (object), object);
243   else
244     nqueue->freeze_count++;
245   G_UNLOCK(notify_lock);
246
247   return nqueue;
248 }
249
250 static inline void
251 g_object_notify_queue_thaw (GObject            *object,
252                             GObjectNotifyQueue *nqueue)
253 {
254   GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
255   GSList *slist;
256   guint n_pspecs = 0;
257
258   g_return_if_fail (nqueue->freeze_count > 0);
259   g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
260
261   G_LOCK(notify_lock);
262
263   /* Just make sure we never get into some nasty race condition */
264   if (G_UNLIKELY(nqueue->freeze_count == 0)) {
265     G_UNLOCK(notify_lock);
266     g_warning ("%s: property-changed notification for %s(%p) is not frozen",
267                G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
268     return;
269   }
270
271   nqueue->freeze_count--;
272   if (nqueue->freeze_count) {
273     G_UNLOCK(notify_lock);
274     return;
275   }
276
277   pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
278
279   for (slist = nqueue->pspecs; slist; slist = slist->next)
280     {
281       pspecs[n_pspecs++] = slist->data;
282     }
283   g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
284
285   G_UNLOCK(notify_lock);
286
287   if (n_pspecs)
288     G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
289   g_free (free_me);
290 }
291
292 static inline void
293 g_object_notify_queue_add (GObject            *object,
294                            GObjectNotifyQueue *nqueue,
295                            GParamSpec         *pspec)
296 {
297   G_LOCK(notify_lock);
298
299   g_return_if_fail (nqueue->n_pspecs < 65535);
300
301   if (g_slist_find (nqueue->pspecs, pspec) == NULL)
302     {
303       nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
304       nqueue->n_pspecs++;
305     }
306
307   G_UNLOCK(notify_lock);
308 }
309
310 static void
311 debug_objects_foreach (gpointer key,
312                        gpointer value,
313                        gpointer user_data)
314 {
315   GObject *object = value;
316
317   g_message ("[%p] stale %s\tref_count=%u",
318              object,
319              G_OBJECT_TYPE_NAME (object),
320              object->ref_count);
321 }
322
323 static void
324 debug_objects_atexit (void)
325 {
326   IF_DEBUG (OBJECTS)
327     {
328       G_LOCK (debug_objects);
329       g_message ("stale GObjects: %u", debug_objects_count);
330       g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
331       G_UNLOCK (debug_objects);
332     }
333 }
334 #endif  /* G_ENABLE_DEBUG */
335
336 void
337 _g_object_type_init (void)
338 {
339   static gboolean initialized = FALSE;
340   static const GTypeFundamentalInfo finfo = {
341     G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
342   };
343   static GTypeInfo info = {
344     sizeof (GObjectClass),
345     (GBaseInitFunc) g_object_base_class_init,
346     (GBaseFinalizeFunc) g_object_base_class_finalize,
347     (GClassInitFunc) g_object_do_class_init,
348     NULL        /* class_destroy */,
349     NULL        /* class_data */,
350     sizeof (GObject),
351     0           /* n_preallocs */,
352     (GInstanceInitFunc) g_object_init,
353     NULL,       /* value_table */
354   };
355   static const GTypeValueTable value_table = {
356     g_value_object_init,          /* value_init */
357     g_value_object_free_value,    /* value_free */
358     g_value_object_copy_value,    /* value_copy */
359     g_value_object_peek_pointer,  /* value_peek_pointer */
360     "p",                          /* collect_format */
361     g_value_object_collect_value, /* collect_value */
362     "p",                          /* lcopy_format */
363     g_value_object_lcopy_value,   /* lcopy_value */
364   };
365   GType type;
366   
367   g_return_if_fail (initialized == FALSE);
368   initialized = TRUE;
369   
370   /* G_TYPE_OBJECT
371    */
372   info.value_table = &value_table;
373   type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
374   g_assert (type == G_TYPE_OBJECT);
375   g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
376   
377 #ifdef  G_ENABLE_DEBUG
378   IF_DEBUG (OBJECTS)
379     {
380       debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
381       g_atexit (debug_objects_atexit);
382     }
383 #endif  /* G_ENABLE_DEBUG */
384 }
385
386 static void
387 g_object_base_class_init (GObjectClass *class)
388 {
389   GObjectClass *pclass = g_type_class_peek_parent (class);
390
391   /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
392   class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
393
394   if (pclass)
395     pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
396
397   /* reset instance specific fields and methods that don't get inherited */
398   class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
399   class->get_property = NULL;
400   class->set_property = NULL;
401 }
402
403 static void
404 g_object_base_class_finalize (GObjectClass *class)
405 {
406   GList *list, *node;
407   
408   _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
409
410   g_slist_free (class->construct_properties);
411   class->construct_properties = NULL;
412   list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
413   for (node = list; node; node = node->next)
414     {
415       GParamSpec *pspec = node->data;
416       
417       g_param_spec_pool_remove (pspec_pool, pspec);
418       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
419       g_param_spec_unref (pspec);
420     }
421   g_list_free (list);
422 }
423
424 static void
425 g_object_do_class_init (GObjectClass *class)
426 {
427   /* read the comment about typedef struct CArray; on why not to change this quark */
428   quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
429
430   quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
431   quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
432   quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
433   pspec_pool = g_param_spec_pool_new (TRUE);
434
435   class->constructor = g_object_constructor;
436   class->constructed = g_object_constructed;
437   class->set_property = g_object_do_set_property;
438   class->get_property = g_object_do_get_property;
439   class->dispose = g_object_real_dispose;
440   class->finalize = g_object_finalize;
441   class->dispatch_properties_changed = g_object_dispatch_properties_changed;
442   class->notify = NULL;
443
444   /**
445    * GObject::notify:
446    * @gobject: the object which received the signal.
447    * @pspec: the #GParamSpec of the property which changed.
448    *
449    * The notify signal is emitted on an object when one of its
450    * properties has been changed. Note that getting this signal
451    * doesn't guarantee that the value of the property has actually
452    * changed, it may also be emitted when the setter for the property
453    * is called to reinstate the previous value.
454    *
455    * This signal is typically used to obtain change notification for a
456    * single property, by specifying the property name as a detail in the
457    * g_signal_connect() call, like this:
458    * |[
459    * g_signal_connect (text_view->buffer, "notify::paste-target-list",
460    *                   G_CALLBACK (gtk_text_view_target_list_notify),
461    *                   text_view)
462    * ]|
463    * It is important to note that you must use
464    * <link linkend="canonical-parameter-name">canonical</link> parameter names as
465    * detail strings for the notify signal.
466    */
467   gobject_signals[NOTIFY] =
468     g_signal_new (g_intern_static_string ("notify"),
469                   G_TYPE_FROM_CLASS (class),
470                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
471                   G_STRUCT_OFFSET (GObjectClass, notify),
472                   NULL, NULL,
473                   g_cclosure_marshal_VOID__PARAM,
474                   G_TYPE_NONE,
475                   1, G_TYPE_PARAM);
476
477   /* Install a check function that we'll use to verify that classes that
478    * implement an interface implement all properties for that interface
479    */
480   g_type_add_interface_check (NULL, object_interface_check_properties);
481 }
482
483 static inline void
484 install_property_internal (GType       g_type,
485                            guint       property_id,
486                            GParamSpec *pspec)
487 {
488   if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
489     {
490       g_warning ("When installing property: type `%s' already has a property named `%s'",
491                  g_type_name (g_type),
492                  pspec->name);
493       return;
494     }
495
496   g_param_spec_ref_sink (pspec);
497   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
498   g_param_spec_pool_insert (pspec_pool, pspec, g_type);
499 }
500
501 /**
502  * g_object_class_install_property:
503  * @oclass: a #GObjectClass
504  * @property_id: the id for the new property
505  * @pspec: the #GParamSpec for the new property
506  *
507  * Installs a new property. This is usually done in the class initializer.
508  *
509  * Note that it is possible to redefine a property in a derived class,
510  * by installing a property with the same name. This can be useful at times,
511  * e.g. to change the range of allowed values or the default value.
512  */
513 void
514 g_object_class_install_property (GObjectClass *class,
515                                  guint         property_id,
516                                  GParamSpec   *pspec)
517 {
518   g_return_if_fail (G_IS_OBJECT_CLASS (class));
519   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
520
521   if (CLASS_HAS_DERIVED_CLASS (class))
522     g_error ("Attempt to add property %s::%s to class after it was derived",
523              G_OBJECT_CLASS_NAME (class), pspec->name);
524
525   class->flags |= CLASS_HAS_PROPS_FLAG;
526
527   if (pspec->flags & G_PARAM_WRITABLE)
528     g_return_if_fail (class->set_property != NULL);
529   if (pspec->flags & G_PARAM_READABLE)
530     g_return_if_fail (class->get_property != NULL);
531   g_return_if_fail (property_id > 0);
532   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
533   if (pspec->flags & G_PARAM_CONSTRUCT)
534     g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
535   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
536     g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
537
538   install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
539
540   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
541     class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
542
543   /* for property overrides of construct properties, we have to get rid
544    * of the overidden inherited construct property
545    */
546   pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
547   if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
548     class->construct_properties = g_slist_remove (class->construct_properties, pspec);
549 }
550
551 /**
552  * g_object_class_install_properties:
553  * @oclass: a #GObjectClass
554  * @n_pspecs: the length of the #GParamSpec<!-- -->s array
555  * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
556  *   defining the new properties
557  *
558  * Installs new properties from an array of #GParamSpec<!-- -->s. This is
559  * usually done in the class initializer.
560  *
561  * The property id of each property is the index of each #GParamSpec in
562  * the @pspecs array.
563  *
564  * The property id of 0 is treated specially by #GObject and it should not
565  * be used to store a #GParamSpec.
566  *
567  * This function should be used if you plan to use a static array of
568  * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
569  * class initialization:
570  *
571  * |[
572  * enum {
573  *   PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
574  * };
575  *
576  * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
577  *
578  * static void
579  * my_object_class_init (MyObjectClass *klass)
580  * {
581  *   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
582  *
583  *   obj_properties[PROP_FOO] =
584  *     g_param_spec_int ("foo", "Foo", "Foo",
585  *                       -1, G_MAXINT,
586  *                       0,
587  *                       G_PARAM_READWRITE);
588  *
589  *   obj_properties[PROP_BAR] =
590  *     g_param_spec_string ("bar", "Bar", "Bar",
591  *                          NULL,
592  *                          G_PARAM_READWRITE);
593  *
594  *   gobject_class->set_property = my_object_set_property;
595  *   gobject_class->get_property = my_object_get_property;
596  *   g_object_class_install_properties (gobject_class,
597  *                                      N_PROPERTIES,
598  *                                      obj_properties);
599  * }
600  * ]|
601  *
602  * allows calling g_object_notify_by_pspec() to notify of property changes:
603  *
604  * |[
605  * void
606  * my_object_set_foo (MyObject *self, gint foo)
607  * {
608  *   if (self->foo != foo)
609  *     {
610  *       self->foo = foo;
611  *       g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
612  *     }
613  *  }
614  * ]|
615  *
616  * Since: 2.26
617  */
618 void
619 g_object_class_install_properties (GObjectClass  *oclass,
620                                    guint          n_pspecs,
621                                    GParamSpec   **pspecs)
622 {
623   GType oclass_type, parent_type;
624   gint i;
625
626   g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
627   g_return_if_fail (n_pspecs > 1);
628   g_return_if_fail (pspecs[0] == NULL);
629
630   if (CLASS_HAS_DERIVED_CLASS (oclass))
631     g_error ("Attempt to add properties to %s after it was derived",
632              G_OBJECT_CLASS_NAME (oclass));
633
634   oclass_type = G_OBJECT_CLASS_TYPE (oclass);
635   parent_type = g_type_parent (oclass_type);
636
637   /* we skip the first element of the array as it would have a 0 prop_id */
638   for (i = 1; i < n_pspecs; i++)
639     {
640       GParamSpec *pspec = pspecs[i];
641
642       g_return_if_fail (pspec != NULL);
643
644       if (pspec->flags & G_PARAM_WRITABLE)
645         g_return_if_fail (oclass->set_property != NULL);
646       if (pspec->flags & G_PARAM_READABLE)
647         g_return_if_fail (oclass->get_property != NULL);
648       g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);      /* paranoid */
649       if (pspec->flags & G_PARAM_CONSTRUCT)
650         g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
651       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
652         g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
653
654       oclass->flags |= CLASS_HAS_PROPS_FLAG;
655       install_property_internal (oclass_type, i, pspec);
656
657       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
658         oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
659
660       /* for property overrides of construct properties, we have to get rid
661        * of the overidden inherited construct property
662        */
663       pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
664       if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
665         oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
666     }
667 }
668
669 /**
670  * g_object_interface_install_property:
671  * @g_iface: any interface vtable for the interface, or the default
672  *  vtable for the interface.
673  * @pspec: the #GParamSpec for the new property
674  *
675  * Add a property to an interface; this is only useful for interfaces
676  * that are added to GObject-derived types. Adding a property to an
677  * interface forces all objects classes with that interface to have a
678  * compatible property. The compatible property could be a newly
679  * created #GParamSpec, but normally
680  * g_object_class_override_property() will be used so that the object
681  * class only needs to provide an implementation and inherits the
682  * property description, default value, bounds, and so forth from the
683  * interface property.
684  *
685  * This function is meant to be called from the interface's default
686  * vtable initialization function (the @class_init member of
687  * #GTypeInfo.) It must not be called after after @class_init has
688  * been called for any object types implementing this interface.
689  *
690  * Since: 2.4
691  */
692 void
693 g_object_interface_install_property (gpointer      g_iface,
694                                      GParamSpec   *pspec)
695 {
696   GTypeInterface *iface_class = g_iface;
697         
698   g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
699   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
700   g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
701   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
702                     
703   install_property_internal (iface_class->g_type, 0, pspec);
704 }
705
706 /**
707  * g_object_class_find_property:
708  * @oclass: a #GObjectClass
709  * @property_name: the name of the property to look up
710  *
711  * Looks up the #GParamSpec for a property of a class.
712  *
713  * Returns: (transfer none): the #GParamSpec for the property, or
714  *          %NULL if the class doesn't have a property of that name
715  */
716 GParamSpec*
717 g_object_class_find_property (GObjectClass *class,
718                               const gchar  *property_name)
719 {
720   GParamSpec *pspec;
721   GParamSpec *redirect;
722         
723   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
724   g_return_val_if_fail (property_name != NULL, NULL);
725   
726   pspec = g_param_spec_pool_lookup (pspec_pool,
727                                     property_name,
728                                     G_OBJECT_CLASS_TYPE (class),
729                                     TRUE);
730   if (pspec)
731     {
732       redirect = g_param_spec_get_redirect_target (pspec);
733       if (redirect)
734         return redirect;
735       else
736         return pspec;
737     }
738   else
739     return NULL;
740 }
741
742 /**
743  * g_object_interface_find_property:
744  * @g_iface: any interface vtable for the interface, or the default
745  *  vtable for the interface
746  * @property_name: name of a property to lookup.
747  *
748  * Find the #GParamSpec with the given name for an
749  * interface. Generally, the interface vtable passed in as @g_iface
750  * will be the default vtable from g_type_default_interface_ref(), or,
751  * if you know the interface has already been loaded,
752  * g_type_default_interface_peek().
753  *
754  * Since: 2.4
755  *
756  * Returns: (transfer none): the #GParamSpec for the property of the
757  *          interface with the name @property_name, or %NULL if no
758  *          such property exists.
759  */
760 GParamSpec*
761 g_object_interface_find_property (gpointer      g_iface,
762                                   const gchar  *property_name)
763 {
764   GTypeInterface *iface_class = g_iface;
765         
766   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
767   g_return_val_if_fail (property_name != NULL, NULL);
768   
769   return g_param_spec_pool_lookup (pspec_pool,
770                                    property_name,
771                                    iface_class->g_type,
772                                    FALSE);
773 }
774
775 /**
776  * g_object_class_override_property:
777  * @oclass: a #GObjectClass
778  * @property_id: the new property ID
779  * @name: the name of a property registered in a parent class or
780  *  in an interface of this class.
781  *
782  * Registers @property_id as referring to a property with the
783  * name @name in a parent class or in an interface implemented
784  * by @oclass. This allows this class to <firstterm>override</firstterm>
785  * a property implementation in a parent class or to provide
786  * the implementation of a property from an interface.
787  *
788  * <note>
789  * Internally, overriding is implemented by creating a property of type
790  * #GParamSpecOverride; generally operations that query the properties of
791  * the object class, such as g_object_class_find_property() or
792  * g_object_class_list_properties() will return the overridden
793  * property. However, in one case, the @construct_properties argument of
794  * the @constructor virtual function, the #GParamSpecOverride is passed
795  * instead, so that the @param_id field of the #GParamSpec will be
796  * correct.  For virtually all uses, this makes no difference. If you
797  * need to get the overridden property, you can call
798  * g_param_spec_get_redirect_target().
799  * </note>
800  *
801  * Since: 2.4
802  */
803 void
804 g_object_class_override_property (GObjectClass *oclass,
805                                   guint         property_id,
806                                   const gchar  *name)
807 {
808   GParamSpec *overridden = NULL;
809   GParamSpec *new;
810   GType parent_type;
811   
812   g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
813   g_return_if_fail (property_id > 0);
814   g_return_if_fail (name != NULL);
815
816   /* Find the overridden property; first check parent types
817    */
818   parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
819   if (parent_type != G_TYPE_NONE)
820     overridden = g_param_spec_pool_lookup (pspec_pool,
821                                            name,
822                                            parent_type,
823                                            TRUE);
824   if (!overridden)
825     {
826       GType *ifaces;
827       guint n_ifaces;
828       
829       /* Now check interfaces
830        */
831       ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
832       while (n_ifaces-- && !overridden)
833         {
834           overridden = g_param_spec_pool_lookup (pspec_pool,
835                                                  name,
836                                                  ifaces[n_ifaces],
837                                                  FALSE);
838         }
839       
840       g_free (ifaces);
841     }
842
843   if (!overridden)
844     {
845       g_warning ("%s: Can't find property to override for '%s::%s'",
846                  G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
847       return;
848     }
849
850   new = g_param_spec_override (name, overridden);
851   g_object_class_install_property (oclass, property_id, new);
852 }
853
854 /**
855  * g_object_class_list_properties:
856  * @oclass: a #GObjectClass
857  * @n_properties: (out): return location for the length of the returned array
858  *
859  * Get an array of #GParamSpec* for all properties of a class.
860  *
861  * Returns: (array length=n_properties) (transfer container): an array of
862  *          #GParamSpec* which should be freed after use
863  */
864 GParamSpec** /* free result */
865 g_object_class_list_properties (GObjectClass *class,
866                                 guint        *n_properties_p)
867 {
868   GParamSpec **pspecs;
869   guint n;
870
871   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
872
873   pspecs = g_param_spec_pool_list (pspec_pool,
874                                    G_OBJECT_CLASS_TYPE (class),
875                                    &n);
876   if (n_properties_p)
877     *n_properties_p = n;
878
879   return pspecs;
880 }
881
882 /**
883  * g_object_interface_list_properties:
884  * @g_iface: any interface vtable for the interface, or the default
885  *  vtable for the interface
886  * @n_properties_p: (out): location to store number of properties returned.
887  *
888  * Lists the properties of an interface.Generally, the interface
889  * vtable passed in as @g_iface will be the default vtable from
890  * g_type_default_interface_ref(), or, if you know the interface has
891  * already been loaded, g_type_default_interface_peek().
892  *
893  * Since: 2.4
894  *
895  * Returns: (array length=n_properties_p) (transfer container): a
896  *          pointer to an array of pointers to #GParamSpec
897  *          structures. The paramspecs are owned by GLib, but the
898  *          array should be freed with g_free() when you are done with
899  *          it.
900  */
901 GParamSpec**
902 g_object_interface_list_properties (gpointer      g_iface,
903                                     guint        *n_properties_p)
904 {
905   GTypeInterface *iface_class = g_iface;
906   GParamSpec **pspecs;
907   guint n;
908
909   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
910
911   pspecs = g_param_spec_pool_list (pspec_pool,
912                                    iface_class->g_type,
913                                    &n);
914   if (n_properties_p)
915     *n_properties_p = n;
916
917   return pspecs;
918 }
919
920 static void
921 g_object_init (GObject          *object,
922                GObjectClass     *class)
923 {
924   object->ref_count = 1;
925   object->qdata = NULL;
926
927   if (CLASS_HAS_PROPS (class))
928     {
929       /* freeze object's notification queue, g_object_newv() preserves pairedness */
930       g_object_notify_queue_freeze (object);
931     }
932
933   if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
934     {
935       /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
936       G_LOCK (construction_mutex);
937       construction_objects = g_slist_prepend (construction_objects, object);
938       G_UNLOCK (construction_mutex);
939     }
940
941 #ifdef  G_ENABLE_DEBUG
942   IF_DEBUG (OBJECTS)
943     {
944       G_LOCK (debug_objects);
945       debug_objects_count++;
946       g_hash_table_insert (debug_objects_ht, object, object);
947       G_UNLOCK (debug_objects);
948     }
949 #endif  /* G_ENABLE_DEBUG */
950 }
951
952 static void
953 g_object_do_set_property (GObject      *object,
954                           guint         property_id,
955                           const GValue *value,
956                           GParamSpec   *pspec)
957 {
958   switch (property_id)
959     {
960     default:
961       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
962       break;
963     }
964 }
965
966 static void
967 g_object_do_get_property (GObject     *object,
968                           guint        property_id,
969                           GValue      *value,
970                           GParamSpec  *pspec)
971 {
972   switch (property_id)
973     {
974     default:
975       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
976       break;
977     }
978 }
979
980 static void
981 g_object_real_dispose (GObject *object)
982 {
983   g_signal_handlers_destroy (object);
984   g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
985   g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
986 }
987
988 static void
989 g_object_finalize (GObject *object)
990 {
991   g_datalist_clear (&object->qdata);
992   
993 #ifdef  G_ENABLE_DEBUG
994   IF_DEBUG (OBJECTS)
995     {
996       G_LOCK (debug_objects);
997       g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
998       g_hash_table_remove (debug_objects_ht, object);
999       debug_objects_count--;
1000       G_UNLOCK (debug_objects);
1001     }
1002 #endif  /* G_ENABLE_DEBUG */
1003 }
1004
1005
1006 static void
1007 g_object_dispatch_properties_changed (GObject     *object,
1008                                       guint        n_pspecs,
1009                                       GParamSpec **pspecs)
1010 {
1011   guint i;
1012
1013   for (i = 0; i < n_pspecs; i++)
1014     g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1015 }
1016
1017 /**
1018  * g_object_run_dispose:
1019  * @object: a #GObject
1020  *
1021  * Releases all references to other objects. This can be used to break
1022  * reference cycles.
1023  *
1024  * This functions should only be called from object system implementations.
1025  */
1026 void
1027 g_object_run_dispose (GObject *object)
1028 {
1029   g_return_if_fail (G_IS_OBJECT (object));
1030   g_return_if_fail (object->ref_count > 0);
1031
1032   g_object_ref (object);
1033   TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1034   G_OBJECT_GET_CLASS (object)->dispose (object);
1035   TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1036   g_object_unref (object);
1037 }
1038
1039 /**
1040  * g_object_freeze_notify:
1041  * @object: a #GObject
1042  *
1043  * Increases the freeze count on @object. If the freeze count is
1044  * non-zero, the emission of "notify" signals on @object is
1045  * stopped. The signals are queued until the freeze count is decreased
1046  * to zero.
1047  *
1048  * This is necessary for accessors that modify multiple properties to prevent
1049  * premature notification while the object is still being modified.
1050  */
1051 void
1052 g_object_freeze_notify (GObject *object)
1053 {
1054   g_return_if_fail (G_IS_OBJECT (object));
1055
1056   if (g_atomic_int_get (&object->ref_count) == 0)
1057     return;
1058
1059   g_object_ref (object);
1060   g_object_notify_queue_freeze (object);
1061   g_object_unref (object);
1062 }
1063
1064 static GParamSpec *
1065 get_notify_pspec (GParamSpec *pspec)
1066 {
1067   GParamSpec *redirected;
1068
1069   /* we don't notify on non-READABLE parameters */
1070   if (~pspec->flags & G_PARAM_READABLE)
1071     return NULL;
1072
1073   /* if the paramspec is redirected, notify on the target */
1074   redirected = g_param_spec_get_redirect_target (pspec);
1075   if (redirected != NULL)
1076     return redirected;
1077
1078   /* else, notify normally */
1079   return pspec;
1080 }
1081
1082 static inline void
1083 g_object_notify_by_spec_internal (GObject    *object,
1084                                   GParamSpec *pspec)
1085 {
1086   GObjectNotifyQueue *nqueue;
1087   GParamSpec *notify_pspec;
1088
1089   notify_pspec = get_notify_pspec (pspec);
1090
1091   if (notify_pspec != NULL)
1092     {
1093       nqueue = g_object_notify_queue_freeze (object);
1094       g_object_notify_queue_add (object, nqueue, notify_pspec);
1095       g_object_notify_queue_thaw (object, nqueue);
1096     }
1097 }
1098
1099 /**
1100  * g_object_notify:
1101  * @object: a #GObject
1102  * @property_name: the name of a property installed on the class of @object.
1103  *
1104  * Emits a "notify" signal for the property @property_name on @object.
1105  *
1106  * When possible, eg. when signaling a property change from within the class
1107  * that registered the property, you should use g_object_notify_by_pspec()
1108  * instead.
1109  */
1110 void
1111 g_object_notify (GObject     *object,
1112                  const gchar *property_name)
1113 {
1114   GParamSpec *pspec;
1115   
1116   g_return_if_fail (G_IS_OBJECT (object));
1117   g_return_if_fail (property_name != NULL);
1118   if (g_atomic_int_get (&object->ref_count) == 0)
1119     return;
1120   
1121   g_object_ref (object);
1122   /* We don't need to get the redirect target
1123    * (by, e.g. calling g_object_class_find_property())
1124    * because g_object_notify_queue_add() does that
1125    */
1126   pspec = g_param_spec_pool_lookup (pspec_pool,
1127                                     property_name,
1128                                     G_OBJECT_TYPE (object),
1129                                     TRUE);
1130
1131   if (!pspec)
1132     g_warning ("%s: object class `%s' has no property named `%s'",
1133                G_STRFUNC,
1134                G_OBJECT_TYPE_NAME (object),
1135                property_name);
1136   else
1137     g_object_notify_by_spec_internal (object, pspec);
1138   g_object_unref (object);
1139 }
1140
1141 /**
1142  * g_object_notify_by_pspec:
1143  * @object: a #GObject
1144  * @pspec: the #GParamSpec of a property installed on the class of @object.
1145  *
1146  * Emits a "notify" signal for the property specified by @pspec on @object.
1147  *
1148  * This function omits the property name lookup, hence it is faster than
1149  * g_object_notify().
1150  *
1151  * One way to avoid using g_object_notify() from within the
1152  * class that registered the properties, and using g_object_notify_by_pspec()
1153  * instead, is to store the GParamSpec used with
1154  * g_object_class_install_property() inside a static array, e.g.:
1155  *
1156  *|[
1157  *   enum
1158  *   {
1159  *     PROP_0,
1160  *     PROP_FOO,
1161  *     PROP_LAST
1162  *   };
1163  *
1164  *   static GParamSpec *properties[PROP_LAST];
1165  *
1166  *   static void
1167  *   my_object_class_init (MyObjectClass *klass)
1168  *   {
1169  *     properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1170  *                                              0, 100,
1171  *                                              50,
1172  *                                              G_PARAM_READWRITE);
1173  *     g_object_class_install_property (gobject_class,
1174  *                                      PROP_FOO,
1175  *                                      properties[PROP_FOO]);
1176  *   }
1177  * ]|
1178  *
1179  * and then notify a change on the "foo" property with:
1180  *
1181  * |[
1182  *   g_object_notify_by_pspec (self, properties[PROP_FOO]);
1183  * ]|
1184  *
1185  * Since: 2.26
1186  */
1187 void
1188 g_object_notify_by_pspec (GObject    *object,
1189                           GParamSpec *pspec)
1190 {
1191
1192   g_return_if_fail (G_IS_OBJECT (object));
1193   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1194
1195   g_object_ref (object);
1196   g_object_notify_by_spec_internal (object, pspec);
1197   g_object_unref (object);
1198 }
1199
1200 /**
1201  * g_object_thaw_notify:
1202  * @object: a #GObject
1203  *
1204  * Reverts the effect of a previous call to
1205  * g_object_freeze_notify(). The freeze count is decreased on @object
1206  * and when it reaches zero, all queued "notify" signals are emitted.
1207  *
1208  * It is an error to call this function when the freeze count is zero.
1209  */
1210 void
1211 g_object_thaw_notify (GObject *object)
1212 {
1213   GObjectNotifyQueue *nqueue;
1214   
1215   g_return_if_fail (G_IS_OBJECT (object));
1216   if (g_atomic_int_get (&object->ref_count) == 0)
1217     return;
1218   
1219   g_object_ref (object);
1220
1221   /* FIXME: Freezing is the only way to get at the notify queue.
1222    * So we freeze once and then thaw twice.
1223    */
1224   nqueue = g_object_notify_queue_freeze (object);
1225   g_object_notify_queue_thaw (object, nqueue);
1226   g_object_notify_queue_thaw (object, nqueue);
1227
1228   g_object_unref (object);
1229 }
1230
1231 static inline void
1232 object_get_property (GObject     *object,
1233                      GParamSpec  *pspec,
1234                      GValue      *value)
1235 {
1236   GObjectClass *class = g_type_class_peek (pspec->owner_type);
1237   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1238   GParamSpec *redirect;
1239
1240   if (class == NULL)
1241     {
1242       g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1243                  g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1244       return;
1245     }
1246
1247   redirect = g_param_spec_get_redirect_target (pspec);
1248   if (redirect)
1249     pspec = redirect;    
1250   
1251   class->get_property (object, param_id, value, pspec);
1252 }
1253
1254 static inline void
1255 object_set_property (GObject             *object,
1256                      GParamSpec          *pspec,
1257                      const GValue        *value,
1258                      GObjectNotifyQueue  *nqueue)
1259 {
1260   GValue tmp_value = G_VALUE_INIT;
1261   GObjectClass *class = g_type_class_peek (pspec->owner_type);
1262   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1263   GParamSpec *redirect;
1264   static const gchar * enable_diagnostic = NULL;
1265
1266   if (class == NULL)
1267     {
1268       g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1269                  g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1270       return;
1271     }
1272
1273   redirect = g_param_spec_get_redirect_target (pspec);
1274   if (redirect)
1275     pspec = redirect;
1276
1277   if (G_UNLIKELY (!enable_diagnostic))
1278     {
1279       enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1280       if (!enable_diagnostic)
1281         enable_diagnostic = "0";
1282     }
1283
1284   if (enable_diagnostic[0] == '1')
1285     {
1286       if (pspec->flags & G_PARAM_DEPRECATED)
1287         g_warning ("The property %s:%s is deprecated and shouldn't be used "
1288                    "anymore. It will be removed in a future version.",
1289                    G_OBJECT_TYPE_NAME (object), pspec->name);
1290     }
1291
1292   /* provide a copy to work from, convert (if necessary) and validate */
1293   g_value_init (&tmp_value, pspec->value_type);
1294   if (!g_value_transform (value, &tmp_value))
1295     g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1296                pspec->name,
1297                g_type_name (pspec->value_type),
1298                G_VALUE_TYPE_NAME (value));
1299   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1300     {
1301       gchar *contents = g_strdup_value_contents (value);
1302
1303       g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1304                  contents,
1305                  G_VALUE_TYPE_NAME (value),
1306                  pspec->name,
1307                  g_type_name (pspec->value_type));
1308       g_free (contents);
1309     }
1310   else
1311     {
1312       GParamSpec *notify_pspec;
1313
1314       class->set_property (object, param_id, &tmp_value, pspec);
1315
1316       notify_pspec = get_notify_pspec (pspec);
1317
1318       if (notify_pspec != NULL)
1319         g_object_notify_queue_add (object, nqueue, notify_pspec);
1320     }
1321   g_value_unset (&tmp_value);
1322 }
1323
1324 static void
1325 object_interface_check_properties (gpointer func_data,
1326                                    gpointer g_iface)
1327 {
1328   GTypeInterface *iface_class = g_iface;
1329   GObjectClass *class;
1330   GType iface_type = iface_class->g_type;
1331   GParamSpec **pspecs;
1332   guint n;
1333
1334   class = g_type_class_ref (iface_class->g_instance_type);
1335
1336   if (!G_IS_OBJECT_CLASS (class))
1337     return;
1338
1339   pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1340
1341   while (n--)
1342     {
1343       GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1344                                                           pspecs[n]->name,
1345                                                           G_OBJECT_CLASS_TYPE (class),
1346                                                           TRUE);
1347
1348       if (!class_pspec)
1349         {
1350           g_critical ("Object class %s doesn't implement property "
1351                       "'%s' from interface '%s'",
1352                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1353                       pspecs[n]->name,
1354                       g_type_name (iface_type));
1355
1356           continue;
1357         }
1358
1359       /* The implementation paramspec must have a less restrictive
1360        * type than the interface parameter spec for set() and a
1361        * more restrictive type for get(). We just require equality,
1362        * rather than doing something more complicated checking
1363        * the READABLE and WRITABLE flags. We also simplify here
1364        * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1365        */
1366       if (class_pspec &&
1367           !g_type_is_a (pspecs[n]->value_type,
1368                         class_pspec->value_type))
1369         {
1370           g_critical ("Property '%s' on class '%s' has type '%s' "
1371                       "which is different from the type '%s', "
1372                       "of the property on interface '%s'\n",
1373                       pspecs[n]->name,
1374                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1375                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1376                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1377                       g_type_name (iface_type));
1378         }
1379
1380 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1381
1382       /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1383        * READABLE and WRITABLE remove restrictions. The implementation
1384        * paramspec must have less restrictive flags.
1385        */
1386       if (class_pspec &&
1387           (!SUBSET (class_pspec->flags,
1388                     pspecs[n]->flags,
1389                     G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1390            !SUBSET (pspecs[n]->flags,
1391                     class_pspec->flags,
1392                     G_PARAM_READABLE | G_PARAM_WRITABLE)))
1393         {
1394           g_critical ("Flags for property '%s' on class '%s' "
1395                       "are not compatible with the property on"
1396                       "interface '%s'\n",
1397                       pspecs[n]->name,
1398                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
1399                       g_type_name (iface_type));
1400         }
1401 #undef SUBSET
1402     }
1403
1404   g_free (pspecs);
1405
1406   g_type_class_unref (class);
1407 }
1408
1409 GType
1410 g_object_get_type (void)
1411 {
1412     return G_TYPE_OBJECT;
1413 }
1414
1415 /**
1416  * g_object_new: (skip)
1417  * @object_type: the type id of the #GObject subtype to instantiate
1418  * @first_property_name: the name of the first property
1419  * @...: the value of the first property, followed optionally by more
1420  *  name/value pairs, followed by %NULL
1421  *
1422  * Creates a new instance of a #GObject subtype and sets its properties.
1423  *
1424  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1425  * which are not explicitly specified are set to their default values.
1426  *
1427  * Returns: (transfer full): a new instance of @object_type
1428  */
1429 gpointer
1430 g_object_new (GType        object_type,
1431               const gchar *first_property_name,
1432               ...)
1433 {
1434   GObject *object;
1435   va_list var_args;
1436   
1437   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1438   
1439   /* short circuit for calls supplying no properties */
1440   if (!first_property_name)
1441     return g_object_newv (object_type, 0, NULL);
1442
1443   va_start (var_args, first_property_name);
1444   object = g_object_new_valist (object_type, first_property_name, var_args);
1445   va_end (var_args);
1446   
1447   return object;
1448 }
1449
1450 static gboolean
1451 slist_maybe_remove (GSList       **slist,
1452                     gconstpointer  data)
1453 {
1454   GSList *last = NULL, *node = *slist;
1455   while (node)
1456     {
1457       if (node->data == data)
1458         {
1459           if (last)
1460             last->next = node->next;
1461           else
1462             *slist = node->next;
1463           g_slist_free_1 (node);
1464           return TRUE;
1465         }
1466       last = node;
1467       node = last->next;
1468     }
1469   return FALSE;
1470 }
1471
1472 static inline gboolean
1473 object_in_construction_list (GObject *object)
1474 {
1475   gboolean in_construction;
1476   G_LOCK (construction_mutex);
1477   in_construction = g_slist_find (construction_objects, object) != NULL;
1478   G_UNLOCK (construction_mutex);
1479   return in_construction;
1480 }
1481
1482 /**
1483  * g_object_newv:
1484  * @object_type: the type id of the #GObject subtype to instantiate
1485  * @n_parameters: the length of the @parameters array
1486  * @parameters: (array length=n_parameters): an array of #GParameter
1487  *
1488  * Creates a new instance of a #GObject subtype and sets its properties.
1489  *
1490  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1491  * which are not explicitly specified are set to their default values.
1492  *
1493  * Rename to: g_object_new
1494  * Returns: (type GObject.Object) (transfer full): a new instance of
1495  * @object_type
1496  */
1497 gpointer
1498 g_object_newv (GType       object_type,
1499                guint       n_parameters,
1500                GParameter *parameters)
1501 {
1502   GObjectConstructParam *cparams = NULL, *oparams;
1503   GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1504   GObject *object;
1505   GObjectClass *class, *unref_class = NULL;
1506   GSList *slist;
1507   guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1508   GValue *cvalues;
1509   GList *clist = NULL;
1510   gboolean newly_constructed;
1511   guint i;
1512
1513   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1514
1515   class = g_type_class_peek_static (object_type);
1516   if (!class)
1517     class = unref_class = g_type_class_ref (object_type);
1518   for (slist = class->construct_properties; slist; slist = slist->next)
1519     {
1520       clist = g_list_prepend (clist, slist->data);
1521       n_total_cparams += 1;
1522     }
1523
1524   if (n_parameters == 0 && n_total_cparams == 0)
1525     {
1526       /* This is a simple object with no construct properties, and
1527        * no properties are being set, so short circuit the parameter
1528        * handling. This speeds up simple object construction.
1529        */
1530       oparams = NULL;
1531       object = class->constructor (object_type, 0, NULL);
1532       goto did_construction;
1533     }
1534
1535   /* collect parameters, sort into construction and normal ones */
1536   oparams = g_new (GObjectConstructParam, n_parameters);
1537   cparams = g_new (GObjectConstructParam, n_total_cparams);
1538   for (i = 0; i < n_parameters; i++)
1539     {
1540       GValue *value = &parameters[i].value;
1541       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1542                                                     parameters[i].name,
1543                                                     object_type,
1544                                                     TRUE);
1545       if (!pspec)
1546         {
1547           g_warning ("%s: object class `%s' has no property named `%s'",
1548                      G_STRFUNC,
1549                      g_type_name (object_type),
1550                      parameters[i].name);
1551           continue;
1552         }
1553       if (!(pspec->flags & G_PARAM_WRITABLE))
1554         {
1555           g_warning ("%s: property `%s' of object class `%s' is not writable",
1556                      G_STRFUNC,
1557                      pspec->name,
1558                      g_type_name (object_type));
1559           continue;
1560         }
1561       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1562         {
1563           GList *list = g_list_find (clist, pspec);
1564
1565           if (!list)
1566             {
1567               g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1568                          G_STRFUNC, pspec->name, g_type_name (object_type));
1569               continue;
1570             }
1571           cparams[n_cparams].pspec = pspec;
1572           cparams[n_cparams].value = value;
1573           n_cparams++;
1574           if (!list->prev)
1575             clist = list->next;
1576           else
1577             list->prev->next = list->next;
1578           if (list->next)
1579             list->next->prev = list->prev;
1580           g_list_free_1 (list);
1581         }
1582       else
1583         {
1584           oparams[n_oparams].pspec = pspec;
1585           oparams[n_oparams].value = value;
1586           n_oparams++;
1587         }
1588     }
1589
1590   /* set remaining construction properties to default values */
1591   n_cvalues = n_total_cparams - n_cparams;
1592   cvalues = g_new (GValue, n_cvalues);
1593   while (clist)
1594     {
1595       GList *tmp = clist->next;
1596       GParamSpec *pspec = clist->data;
1597       GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1598
1599       value->g_type = 0;
1600       g_value_init (value, pspec->value_type);
1601       g_param_value_set_default (pspec, value);
1602
1603       cparams[n_cparams].pspec = pspec;
1604       cparams[n_cparams].value = value;
1605       n_cparams++;
1606
1607       g_list_free_1 (clist);
1608       clist = tmp;
1609     }
1610
1611   /* construct object from construction parameters */
1612   object = class->constructor (object_type, n_total_cparams, cparams);
1613   /* free construction values */
1614   g_free (cparams);
1615   while (n_cvalues--)
1616     g_value_unset (cvalues + n_cvalues);
1617   g_free (cvalues);
1618
1619  did_construction:
1620   if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1621     {
1622       /* adjust freeze_count according to g_object_init() and remaining properties */
1623       G_LOCK (construction_mutex);
1624       newly_constructed = slist_maybe_remove (&construction_objects, object);
1625       G_UNLOCK (construction_mutex);
1626     }
1627   else
1628     newly_constructed = TRUE;
1629
1630   if (CLASS_HAS_PROPS (class))
1631     {
1632       if (newly_constructed || n_oparams)
1633         nqueue = g_object_notify_queue_freeze (object);
1634       if (newly_constructed)
1635         g_object_notify_queue_thaw (object, nqueue);
1636     }
1637
1638   /* run 'constructed' handler if there is a custom one */
1639   if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1640     class->constructed (object);
1641
1642   /* set remaining properties */
1643   for (i = 0; i < n_oparams; i++)
1644     object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1645   g_free (oparams);
1646
1647   if (CLASS_HAS_PROPS (class))
1648     {
1649       /* release our own freeze count and handle notifications */
1650       if (newly_constructed || n_oparams)
1651         g_object_notify_queue_thaw (object, nqueue);
1652     }
1653
1654   if (unref_class)
1655     g_type_class_unref (unref_class);
1656
1657   return object;
1658 }
1659
1660 /**
1661  * g_object_new_valist: (skip)
1662  * @object_type: the type id of the #GObject subtype to instantiate
1663  * @first_property_name: the name of the first property
1664  * @var_args: the value of the first property, followed optionally by more
1665  *  name/value pairs, followed by %NULL
1666  *
1667  * Creates a new instance of a #GObject subtype and sets its properties.
1668  *
1669  * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1670  * which are not explicitly specified are set to their default values.
1671  *
1672  * Returns: a new instance of @object_type
1673  */
1674 GObject*
1675 g_object_new_valist (GType        object_type,
1676                      const gchar *first_property_name,
1677                      va_list      var_args)
1678 {
1679   GObjectClass *class;
1680   GParameter *params;
1681   const gchar *name;
1682   GObject *object;
1683   guint n_params = 0, n_alloced_params = 16;
1684   
1685   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1686
1687   if (!first_property_name)
1688     return g_object_newv (object_type, 0, NULL);
1689
1690   class = g_type_class_ref (object_type);
1691
1692   params = g_new0 (GParameter, n_alloced_params);
1693   name = first_property_name;
1694   while (name)
1695     {
1696       gchar *error = NULL;
1697       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1698                                                     name,
1699                                                     object_type,
1700                                                     TRUE);
1701       if (!pspec)
1702         {
1703           g_warning ("%s: object class `%s' has no property named `%s'",
1704                      G_STRFUNC,
1705                      g_type_name (object_type),
1706                      name);
1707           break;
1708         }
1709       if (n_params >= n_alloced_params)
1710         {
1711           n_alloced_params += 16;
1712           params = g_renew (GParameter, params, n_alloced_params);
1713           memset (params + n_params, 0, 16 * (sizeof *params));
1714         }
1715       params[n_params].name = name;
1716       G_VALUE_COLLECT_INIT (&params[n_params].value, pspec->value_type,
1717                             var_args, 0, &error);
1718       if (error)
1719         {
1720           g_warning ("%s: %s", G_STRFUNC, error);
1721           g_free (error);
1722           g_value_unset (&params[n_params].value);
1723           break;
1724         }
1725       n_params++;
1726       name = va_arg (var_args, gchar*);
1727     }
1728
1729   object = g_object_newv (object_type, n_params, params);
1730
1731   while (n_params--)
1732     g_value_unset (&params[n_params].value);
1733   g_free (params);
1734
1735   g_type_class_unref (class);
1736
1737   return object;
1738 }
1739
1740 static GObject*
1741 g_object_constructor (GType                  type,
1742                       guint                  n_construct_properties,
1743                       GObjectConstructParam *construct_params)
1744 {
1745   GObject *object;
1746
1747   /* create object */
1748   object = (GObject*) g_type_create_instance (type);
1749   
1750   /* set construction parameters */
1751   if (n_construct_properties)
1752     {
1753       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object);
1754       
1755       /* set construct properties */
1756       while (n_construct_properties--)
1757         {
1758           GValue *value = construct_params->value;
1759           GParamSpec *pspec = construct_params->pspec;
1760
1761           construct_params++;
1762           object_set_property (object, pspec, value, nqueue);
1763         }
1764       g_object_notify_queue_thaw (object, nqueue);
1765       /* the notification queue is still frozen from g_object_init(), so
1766        * we don't need to handle it here, g_object_newv() takes
1767        * care of that
1768        */
1769     }
1770
1771   return object;
1772 }
1773
1774 static void
1775 g_object_constructed (GObject *object)
1776 {
1777   /* empty default impl to allow unconditional upchaining */
1778 }
1779
1780 /**
1781  * g_object_set_valist: (skip)
1782  * @object: a #GObject
1783  * @first_property_name: name of the first property to set
1784  * @var_args: value for the first property, followed optionally by more
1785  *  name/value pairs, followed by %NULL
1786  *
1787  * Sets properties on an object.
1788  */
1789 void
1790 g_object_set_valist (GObject     *object,
1791                      const gchar *first_property_name,
1792                      va_list      var_args)
1793 {
1794   GObjectNotifyQueue *nqueue;
1795   const gchar *name;
1796   
1797   g_return_if_fail (G_IS_OBJECT (object));
1798   
1799   g_object_ref (object);
1800   nqueue = g_object_notify_queue_freeze (object);
1801   
1802   name = first_property_name;
1803   while (name)
1804     {
1805       GValue value = G_VALUE_INIT;
1806       GParamSpec *pspec;
1807       gchar *error = NULL;
1808       
1809       pspec = g_param_spec_pool_lookup (pspec_pool,
1810                                         name,
1811                                         G_OBJECT_TYPE (object),
1812                                         TRUE);
1813       if (!pspec)
1814         {
1815           g_warning ("%s: object class `%s' has no property named `%s'",
1816                      G_STRFUNC,
1817                      G_OBJECT_TYPE_NAME (object),
1818                      name);
1819           break;
1820         }
1821       if (!(pspec->flags & G_PARAM_WRITABLE))
1822         {
1823           g_warning ("%s: property `%s' of object class `%s' is not writable",
1824                      G_STRFUNC,
1825                      pspec->name,
1826                      G_OBJECT_TYPE_NAME (object));
1827           break;
1828         }
1829       if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1830         {
1831           g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1832                      G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1833           break;
1834         }
1835
1836       G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1837                             0, &error);
1838       if (error)
1839         {
1840           g_warning ("%s: %s", G_STRFUNC, error);
1841           g_free (error);
1842           g_value_unset (&value);
1843           break;
1844         }
1845       
1846       object_set_property (object, pspec, &value, nqueue);
1847       g_value_unset (&value);
1848       
1849       name = va_arg (var_args, gchar*);
1850     }
1851
1852   g_object_notify_queue_thaw (object, nqueue);
1853   g_object_unref (object);
1854 }
1855
1856 /**
1857  * g_object_get_valist: (skip)
1858  * @object: a #GObject
1859  * @first_property_name: name of the first property to get
1860  * @var_args: return location for the first property, followed optionally by more
1861  *  name/return location pairs, followed by %NULL
1862  *
1863  * Gets properties of an object.
1864  *
1865  * In general, a copy is made of the property contents and the caller
1866  * is responsible for freeing the memory in the appropriate manner for
1867  * the type, for instance by calling g_free() or g_object_unref().
1868  *
1869  * See g_object_get().
1870  */
1871 void
1872 g_object_get_valist (GObject     *object,
1873                      const gchar *first_property_name,
1874                      va_list      var_args)
1875 {
1876   const gchar *name;
1877   
1878   g_return_if_fail (G_IS_OBJECT (object));
1879   
1880   g_object_ref (object);
1881   
1882   name = first_property_name;
1883   
1884   while (name)
1885     {
1886       GValue value = G_VALUE_INIT;
1887       GParamSpec *pspec;
1888       gchar *error;
1889       
1890       pspec = g_param_spec_pool_lookup (pspec_pool,
1891                                         name,
1892                                         G_OBJECT_TYPE (object),
1893                                         TRUE);
1894       if (!pspec)
1895         {
1896           g_warning ("%s: object class `%s' has no property named `%s'",
1897                      G_STRFUNC,
1898                      G_OBJECT_TYPE_NAME (object),
1899                      name);
1900           break;
1901         }
1902       if (!(pspec->flags & G_PARAM_READABLE))
1903         {
1904           g_warning ("%s: property `%s' of object class `%s' is not readable",
1905                      G_STRFUNC,
1906                      pspec->name,
1907                      G_OBJECT_TYPE_NAME (object));
1908           break;
1909         }
1910       
1911       g_value_init (&value, pspec->value_type);
1912       
1913       object_get_property (object, pspec, &value);
1914       
1915       G_VALUE_LCOPY (&value, var_args, 0, &error);
1916       if (error)
1917         {
1918           g_warning ("%s: %s", G_STRFUNC, error);
1919           g_free (error);
1920           g_value_unset (&value);
1921           break;
1922         }
1923       
1924       g_value_unset (&value);
1925       
1926       name = va_arg (var_args, gchar*);
1927     }
1928   
1929   g_object_unref (object);
1930 }
1931
1932 /**
1933  * g_object_set: (skip)
1934  * @object: a #GObject
1935  * @first_property_name: name of the first property to set
1936  * @...: value for the first property, followed optionally by more
1937  *  name/value pairs, followed by %NULL
1938  *
1939  * Sets properties on an object.
1940  */
1941 void
1942 g_object_set (gpointer     _object,
1943               const gchar *first_property_name,
1944               ...)
1945 {
1946   GObject *object = _object;
1947   va_list var_args;
1948   
1949   g_return_if_fail (G_IS_OBJECT (object));
1950   
1951   va_start (var_args, first_property_name);
1952   g_object_set_valist (object, first_property_name, var_args);
1953   va_end (var_args);
1954 }
1955
1956 /**
1957  * g_object_get: (skip)
1958  * @object: a #GObject
1959  * @first_property_name: name of the first property to get
1960  * @...: return location for the first property, followed optionally by more
1961  *  name/return location pairs, followed by %NULL
1962  *
1963  * Gets properties of an object.
1964  *
1965  * In general, a copy is made of the property contents and the caller
1966  * is responsible for freeing the memory in the appropriate manner for
1967  * the type, for instance by calling g_free() or g_object_unref().
1968  *
1969  * <example>
1970  * <title>Using g_object_get(<!-- -->)</title>
1971  * An example of using g_object_get() to get the contents
1972  * of three properties - one of type #G_TYPE_INT,
1973  * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1974  * <programlisting>
1975  *  gint intval;
1976  *  gchar *strval;
1977  *  GObject *objval;
1978  *
1979  *  g_object_get (my_object,
1980  *                "int-property", &intval,
1981  *                "str-property", &strval,
1982  *                "obj-property", &objval,
1983  *                NULL);
1984  *
1985  *  // Do something with intval, strval, objval
1986  *
1987  *  g_free (strval);
1988  *  g_object_unref (objval);
1989  * </programlisting>
1990  * </example>
1991  */
1992 void
1993 g_object_get (gpointer     _object,
1994               const gchar *first_property_name,
1995               ...)
1996 {
1997   GObject *object = _object;
1998   va_list var_args;
1999   
2000   g_return_if_fail (G_IS_OBJECT (object));
2001   
2002   va_start (var_args, first_property_name);
2003   g_object_get_valist (object, first_property_name, var_args);
2004   va_end (var_args);
2005 }
2006
2007 /**
2008  * g_object_set_property:
2009  * @object: a #GObject
2010  * @property_name: the name of the property to set
2011  * @value: the value
2012  *
2013  * Sets a property on an object.
2014  */
2015 void
2016 g_object_set_property (GObject      *object,
2017                        const gchar  *property_name,
2018                        const GValue *value)
2019 {
2020   GObjectNotifyQueue *nqueue;
2021   GParamSpec *pspec;
2022   
2023   g_return_if_fail (G_IS_OBJECT (object));
2024   g_return_if_fail (property_name != NULL);
2025   g_return_if_fail (G_IS_VALUE (value));
2026   
2027   g_object_ref (object);
2028   nqueue = g_object_notify_queue_freeze (object);
2029   
2030   pspec = g_param_spec_pool_lookup (pspec_pool,
2031                                     property_name,
2032                                     G_OBJECT_TYPE (object),
2033                                     TRUE);
2034   if (!pspec)
2035     g_warning ("%s: object class `%s' has no property named `%s'",
2036                G_STRFUNC,
2037                G_OBJECT_TYPE_NAME (object),
2038                property_name);
2039   else if (!(pspec->flags & G_PARAM_WRITABLE))
2040     g_warning ("%s: property `%s' of object class `%s' is not writable",
2041                G_STRFUNC,
2042                pspec->name,
2043                G_OBJECT_TYPE_NAME (object));
2044   else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2045     g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2046                G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2047   else
2048     object_set_property (object, pspec, value, nqueue);
2049   
2050   g_object_notify_queue_thaw (object, nqueue);
2051   g_object_unref (object);
2052 }
2053
2054 /**
2055  * g_object_get_property:
2056  * @object: a #GObject
2057  * @property_name: the name of the property to get
2058  * @value: return location for the property value
2059  *
2060  * Gets a property of an object. @value must have been initialized to the
2061  * expected type of the property (or a type to which the expected type can be
2062  * transformed) using g_value_init().
2063  *
2064  * In general, a copy is made of the property contents and the caller is
2065  * responsible for freeing the memory by calling g_value_unset().
2066  *
2067  * Note that g_object_get_property() is really intended for language
2068  * bindings, g_object_get() is much more convenient for C programming.
2069  */
2070 void
2071 g_object_get_property (GObject     *object,
2072                        const gchar *property_name,
2073                        GValue      *value)
2074 {
2075   GParamSpec *pspec;
2076   
2077   g_return_if_fail (G_IS_OBJECT (object));
2078   g_return_if_fail (property_name != NULL);
2079   g_return_if_fail (G_IS_VALUE (value));
2080   
2081   g_object_ref (object);
2082   
2083   pspec = g_param_spec_pool_lookup (pspec_pool,
2084                                     property_name,
2085                                     G_OBJECT_TYPE (object),
2086                                     TRUE);
2087   if (!pspec)
2088     g_warning ("%s: object class `%s' has no property named `%s'",
2089                G_STRFUNC,
2090                G_OBJECT_TYPE_NAME (object),
2091                property_name);
2092   else if (!(pspec->flags & G_PARAM_READABLE))
2093     g_warning ("%s: property `%s' of object class `%s' is not readable",
2094                G_STRFUNC,
2095                pspec->name,
2096                G_OBJECT_TYPE_NAME (object));
2097   else
2098     {
2099       GValue *prop_value, tmp_value = G_VALUE_INIT;
2100       
2101       /* auto-conversion of the callers value type
2102        */
2103       if (G_VALUE_TYPE (value) == pspec->value_type)
2104         {
2105           g_value_reset (value);
2106           prop_value = value;
2107         }
2108       else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2109         {
2110           g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2111                      G_STRFUNC, pspec->name,
2112                      g_type_name (pspec->value_type),
2113                      G_VALUE_TYPE_NAME (value));
2114           g_object_unref (object);
2115           return;
2116         }
2117       else
2118         {
2119           g_value_init (&tmp_value, pspec->value_type);
2120           prop_value = &tmp_value;
2121         }
2122       object_get_property (object, pspec, prop_value);
2123       if (prop_value != value)
2124         {
2125           g_value_transform (prop_value, value);
2126           g_value_unset (&tmp_value);
2127         }
2128     }
2129   
2130   g_object_unref (object);
2131 }
2132
2133 /**
2134  * g_object_connect: (skip)
2135  * @object: a #GObject
2136  * @signal_spec: the spec for the first signal
2137  * @...: #GCallback for the first signal, followed by data for the
2138  *       first signal, followed optionally by more signal
2139  *       spec/callback/data triples, followed by %NULL
2140  *
2141  * A convenience function to connect multiple signals at once.
2142  *
2143  * The signal specs expected by this function have the form
2144  * "modifier::signal_name", where modifier can be one of the following:
2145  * <variablelist>
2146  * <varlistentry>
2147  * <term>signal</term>
2148  * <listitem><para>
2149  * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2150  * </para></listitem>
2151  * </varlistentry>
2152  * <varlistentry>
2153  * <term>object_signal</term>
2154  * <term>object-signal</term>
2155  * <listitem><para>
2156  * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2157  * </para></listitem>
2158  * </varlistentry>
2159  * <varlistentry>
2160  * <term>swapped_signal</term>
2161  * <term>swapped-signal</term>
2162  * <listitem><para>
2163  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2164  * </para></listitem>
2165  * </varlistentry>
2166  * <varlistentry>
2167  * <term>swapped_object_signal</term>
2168  * <term>swapped-object-signal</term>
2169  * <listitem><para>
2170  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2171  * </para></listitem>
2172  * </varlistentry>
2173  * <varlistentry>
2174  * <term>signal_after</term>
2175  * <term>signal-after</term>
2176  * <listitem><para>
2177  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2178  * </para></listitem>
2179  * </varlistentry>
2180  * <varlistentry>
2181  * <term>object_signal_after</term>
2182  * <term>object-signal-after</term>
2183  * <listitem><para>
2184  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2185  * </para></listitem>
2186  * </varlistentry>
2187  * <varlistentry>
2188  * <term>swapped_signal_after</term>
2189  * <term>swapped-signal-after</term>
2190  * <listitem><para>
2191  * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2192  * </para></listitem>
2193  * </varlistentry>
2194  * <varlistentry>
2195  * <term>swapped_object_signal_after</term>
2196  * <term>swapped-object-signal-after</term>
2197  * <listitem><para>
2198  * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2199  * </para></listitem>
2200  * </varlistentry>
2201  * </variablelist>
2202  *
2203  * |[
2204  *   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2205  *                                                 "type", GTK_WINDOW_POPUP,
2206  *                                                 "child", menu,
2207  *                                                 NULL),
2208  *                                   "signal::event", gtk_menu_window_event, menu,
2209  *                                   "signal::size_request", gtk_menu_window_size_request, menu,
2210  *                                   "signal::destroy", gtk_widget_destroyed, &amp;menu-&gt;toplevel,
2211  *                                   NULL);
2212  * ]|
2213  *
2214  * Returns: (transfer none): @object
2215  */
2216 gpointer
2217 g_object_connect (gpointer     _object,
2218                   const gchar *signal_spec,
2219                   ...)
2220 {
2221   GObject *object = _object;
2222   va_list var_args;
2223
2224   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2225   g_return_val_if_fail (object->ref_count > 0, object);
2226
2227   va_start (var_args, signal_spec);
2228   while (signal_spec)
2229     {
2230       GCallback callback = va_arg (var_args, GCallback);
2231       gpointer data = va_arg (var_args, gpointer);
2232
2233       if (strncmp (signal_spec, "signal::", 8) == 0)
2234         g_signal_connect_data (object, signal_spec + 8,
2235                                callback, data, NULL,
2236                                0);
2237       else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2238                strncmp (signal_spec, "object-signal::", 15) == 0)
2239         g_signal_connect_object (object, signal_spec + 15,
2240                                  callback, data,
2241                                  0);
2242       else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2243                strncmp (signal_spec, "swapped-signal::", 16) == 0)
2244         g_signal_connect_data (object, signal_spec + 16,
2245                                callback, data, NULL,
2246                                G_CONNECT_SWAPPED);
2247       else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2248                strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2249         g_signal_connect_object (object, signal_spec + 23,
2250                                  callback, data,
2251                                  G_CONNECT_SWAPPED);
2252       else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2253                strncmp (signal_spec, "signal-after::", 14) == 0)
2254         g_signal_connect_data (object, signal_spec + 14,
2255                                callback, data, NULL,
2256                                G_CONNECT_AFTER);
2257       else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2258                strncmp (signal_spec, "object-signal-after::", 21) == 0)
2259         g_signal_connect_object (object, signal_spec + 21,
2260                                  callback, data,
2261                                  G_CONNECT_AFTER);
2262       else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2263                strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2264         g_signal_connect_data (object, signal_spec + 22,
2265                                callback, data, NULL,
2266                                G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2267       else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2268                strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2269         g_signal_connect_object (object, signal_spec + 29,
2270                                  callback, data,
2271                                  G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2272       else
2273         {
2274           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2275           break;
2276         }
2277       signal_spec = va_arg (var_args, gchar*);
2278     }
2279   va_end (var_args);
2280
2281   return object;
2282 }
2283
2284 /**
2285  * g_object_disconnect: (skip)
2286  * @object: a #GObject
2287  * @signal_spec: the spec for the first signal
2288  * @...: #GCallback for the first signal, followed by data for the first signal,
2289  *  followed optionally by more signal spec/callback/data triples,
2290  *  followed by %NULL
2291  *
2292  * A convenience function to disconnect multiple signals at once.
2293  *
2294  * The signal specs expected by this function have the form
2295  * "any_signal", which means to disconnect any signal with matching
2296  * callback and data, or "any_signal::signal_name", which only
2297  * disconnects the signal named "signal_name".
2298  */
2299 void
2300 g_object_disconnect (gpointer     _object,
2301                      const gchar *signal_spec,
2302                      ...)
2303 {
2304   GObject *object = _object;
2305   va_list var_args;
2306
2307   g_return_if_fail (G_IS_OBJECT (object));
2308   g_return_if_fail (object->ref_count > 0);
2309
2310   va_start (var_args, signal_spec);
2311   while (signal_spec)
2312     {
2313       GCallback callback = va_arg (var_args, GCallback);
2314       gpointer data = va_arg (var_args, gpointer);
2315       guint sid = 0, detail = 0, mask = 0;
2316
2317       if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2318           strncmp (signal_spec, "any-signal::", 12) == 0)
2319         {
2320           signal_spec += 12;
2321           mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2322         }
2323       else if (strcmp (signal_spec, "any_signal") == 0 ||
2324                strcmp (signal_spec, "any-signal") == 0)
2325         {
2326           signal_spec += 10;
2327           mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2328         }
2329       else
2330         {
2331           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2332           break;
2333         }
2334
2335       if ((mask & G_SIGNAL_MATCH_ID) &&
2336           !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2337         g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2338       else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2339                                                       sid, detail,
2340                                                       NULL, (gpointer)callback, data))
2341         g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2342       signal_spec = va_arg (var_args, gchar*);
2343     }
2344   va_end (var_args);
2345 }
2346
2347 typedef struct {
2348   GObject *object;
2349   guint n_weak_refs;
2350   struct {
2351     GWeakNotify notify;
2352     gpointer    data;
2353   } weak_refs[1];  /* flexible array */
2354 } WeakRefStack;
2355
2356 static void
2357 weak_refs_notify (gpointer data)
2358 {
2359   WeakRefStack *wstack = data;
2360   guint i;
2361
2362   for (i = 0; i < wstack->n_weak_refs; i++)
2363     wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2364   g_free (wstack);
2365 }
2366
2367 /**
2368  * g_object_weak_ref: (skip)
2369  * @object: #GObject to reference weakly
2370  * @notify: callback to invoke before the object is freed
2371  * @data: extra data to pass to notify
2372  *
2373  * Adds a weak reference callback to an object. Weak references are
2374  * used for notification when an object is finalized. They are called
2375  * "weak references" because they allow you to safely hold a pointer
2376  * to an object without calling g_object_ref() (g_object_ref() adds a
2377  * strong reference, that is, forces the object to stay alive).
2378  */
2379 void
2380 g_object_weak_ref (GObject    *object,
2381                    GWeakNotify notify,
2382                    gpointer    data)
2383 {
2384   WeakRefStack *wstack;
2385   guint i;
2386   
2387   g_return_if_fail (G_IS_OBJECT (object));
2388   g_return_if_fail (notify != NULL);
2389   g_return_if_fail (object->ref_count >= 1);
2390
2391   G_LOCK (weak_refs_mutex);
2392   wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2393   if (wstack)
2394     {
2395       i = wstack->n_weak_refs++;
2396       wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2397     }
2398   else
2399     {
2400       wstack = g_renew (WeakRefStack, NULL, 1);
2401       wstack->object = object;
2402       wstack->n_weak_refs = 1;
2403       i = 0;
2404     }
2405   wstack->weak_refs[i].notify = notify;
2406   wstack->weak_refs[i].data = data;
2407   g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2408   G_UNLOCK (weak_refs_mutex);
2409 }
2410
2411 /**
2412  * g_object_weak_unref: (skip)
2413  * @object: #GObject to remove a weak reference from
2414  * @notify: callback to search for
2415  * @data: data to search for
2416  *
2417  * Removes a weak reference callback to an object.
2418  */
2419 void
2420 g_object_weak_unref (GObject    *object,
2421                      GWeakNotify notify,
2422                      gpointer    data)
2423 {
2424   WeakRefStack *wstack;
2425   gboolean found_one = FALSE;
2426
2427   g_return_if_fail (G_IS_OBJECT (object));
2428   g_return_if_fail (notify != NULL);
2429
2430   G_LOCK (weak_refs_mutex);
2431   wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2432   if (wstack)
2433     {
2434       guint i;
2435
2436       for (i = 0; i < wstack->n_weak_refs; i++)
2437         if (wstack->weak_refs[i].notify == notify &&
2438             wstack->weak_refs[i].data == data)
2439           {
2440             found_one = TRUE;
2441             wstack->n_weak_refs -= 1;
2442             if (i != wstack->n_weak_refs)
2443               wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2444
2445             break;
2446           }
2447     }
2448   G_UNLOCK (weak_refs_mutex);
2449   if (!found_one)
2450     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2451 }
2452
2453 /**
2454  * g_object_add_weak_pointer: (skip)
2455  * @object: The object that should be weak referenced.
2456  * @weak_pointer_location: (inout): The memory address of a pointer.
2457  *
2458  * Adds a weak reference from weak_pointer to @object to indicate that
2459  * the pointer located at @weak_pointer_location is only valid during
2460  * the lifetime of @object. When the @object is finalized,
2461  * @weak_pointer will be set to %NULL.
2462  */
2463 void
2464 g_object_add_weak_pointer (GObject  *object, 
2465                            gpointer *weak_pointer_location)
2466 {
2467   g_return_if_fail (G_IS_OBJECT (object));
2468   g_return_if_fail (weak_pointer_location != NULL);
2469
2470   g_object_weak_ref (object, 
2471                      (GWeakNotify) g_nullify_pointer, 
2472                      weak_pointer_location);
2473 }
2474
2475 /**
2476  * g_object_remove_weak_pointer: (skip)
2477  * @object: The object that is weak referenced.
2478  * @weak_pointer_location: (inout): The memory address of a pointer.
2479  *
2480  * Removes a weak reference from @object that was previously added
2481  * using g_object_add_weak_pointer(). The @weak_pointer_location has
2482  * to match the one used with g_object_add_weak_pointer().
2483  */
2484 void
2485 g_object_remove_weak_pointer (GObject  *object, 
2486                               gpointer *weak_pointer_location)
2487 {
2488   g_return_if_fail (G_IS_OBJECT (object));
2489   g_return_if_fail (weak_pointer_location != NULL);
2490
2491   g_object_weak_unref (object, 
2492                        (GWeakNotify) g_nullify_pointer, 
2493                        weak_pointer_location);
2494 }
2495
2496 static guint
2497 object_floating_flag_handler (GObject        *object,
2498                               gint            job)
2499 {
2500   switch (job)
2501     {
2502       gpointer oldvalue;
2503     case +1:    /* force floating if possible */
2504       do
2505         oldvalue = g_atomic_pointer_get (&object->qdata);
2506       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2507                                                      (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2508       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2509     case -1:    /* sink if possible */
2510       do
2511         oldvalue = g_atomic_pointer_get (&object->qdata);
2512       while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2513                                                      (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2514       return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2515     default:    /* check floating */
2516       return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2517     }
2518 }
2519
2520 /**
2521  * g_object_is_floating:
2522  * @object: (type GObject.Object): a #GObject
2523  *
2524  * Checks whether @object has a <link linkend="floating-ref">floating</link>
2525  * reference.
2526  *
2527  * Since: 2.10
2528  *
2529  * Returns: %TRUE if @object has a floating reference
2530  */
2531 gboolean
2532 g_object_is_floating (gpointer _object)
2533 {
2534   GObject *object = _object;
2535   g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2536   return floating_flag_handler (object, 0);
2537 }
2538
2539 /**
2540  * g_object_ref_sink:
2541  * @object: (type GObject.Object): a #GObject
2542  *
2543  * Increase the reference count of @object, and possibly remove the
2544  * <link linkend="floating-ref">floating</link> reference, if @object
2545  * has a floating reference.
2546  *
2547  * In other words, if the object is floating, then this call "assumes
2548  * ownership" of the floating reference, converting it to a normal
2549  * reference by clearing the floating flag while leaving the reference
2550  * count unchanged.  If the object is not floating, then this call
2551  * adds a new normal reference increasing the reference count by one.
2552  *
2553  * Since: 2.10
2554  *
2555  * Returns: (type GObject.Object) (transfer none): @object
2556  */
2557 gpointer
2558 g_object_ref_sink (gpointer _object)
2559 {
2560   GObject *object = _object;
2561   gboolean was_floating;
2562   g_return_val_if_fail (G_IS_OBJECT (object), object);
2563   g_return_val_if_fail (object->ref_count >= 1, object);
2564   g_object_ref (object);
2565   was_floating = floating_flag_handler (object, -1);
2566   if (was_floating)
2567     g_object_unref (object);
2568   return object;
2569 }
2570
2571 /**
2572  * g_object_force_floating:
2573  * @object: a #GObject
2574  *
2575  * This function is intended for #GObject implementations to re-enforce a
2576  * <link linkend="floating-ref">floating</link> object reference.
2577  * Doing this is seldom required: all
2578  * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2579  * usually just needs to be sunken by calling g_object_ref_sink().
2580  *
2581  * Since: 2.10
2582  */
2583 void
2584 g_object_force_floating (GObject *object)
2585 {
2586   g_return_if_fail (G_IS_OBJECT (object));
2587   g_return_if_fail (object->ref_count >= 1);
2588
2589   floating_flag_handler (object, +1);
2590 }
2591
2592 typedef struct {
2593   GObject *object;
2594   guint n_toggle_refs;
2595   struct {
2596     GToggleNotify notify;
2597     gpointer    data;
2598   } toggle_refs[1];  /* flexible array */
2599 } ToggleRefStack;
2600
2601 static void
2602 toggle_refs_notify (GObject *object,
2603                     gboolean is_last_ref)
2604 {
2605   ToggleRefStack tstack, *tstackptr;
2606
2607   G_LOCK (toggle_refs_mutex);
2608   tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2609   tstack = *tstackptr;
2610   G_UNLOCK (toggle_refs_mutex);
2611
2612   /* Reentrancy here is not as tricky as it seems, because a toggle reference
2613    * will only be notified when there is exactly one of them.
2614    */
2615   g_assert (tstack.n_toggle_refs == 1);
2616   tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2617 }
2618
2619 /**
2620  * g_object_add_toggle_ref: (skip)
2621  * @object: a #GObject
2622  * @notify: a function to call when this reference is the
2623  *  last reference to the object, or is no longer
2624  *  the last reference.
2625  * @data: data to pass to @notify
2626  *
2627  * Increases the reference count of the object by one and sets a
2628  * callback to be called when all other references to the object are
2629  * dropped, or when this is already the last reference to the object
2630  * and another reference is established.
2631  *
2632  * This functionality is intended for binding @object to a proxy
2633  * object managed by another memory manager. This is done with two
2634  * paired references: the strong reference added by
2635  * g_object_add_toggle_ref() and a reverse reference to the proxy
2636  * object which is either a strong reference or weak reference.
2637  *
2638  * The setup is that when there are no other references to @object,
2639  * only a weak reference is held in the reverse direction from @object
2640  * to the proxy object, but when there are other references held to
2641  * @object, a strong reference is held. The @notify callback is called
2642  * when the reference from @object to the proxy object should be
2643  * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2644  * true) or weak to strong (@is_last_ref false).
2645  *
2646  * Since a (normal) reference must be held to the object before
2647  * calling g_object_add_toggle_ref(), the initial state of the reverse
2648  * link is always strong.
2649  *
2650  * Multiple toggle references may be added to the same gobject,
2651  * however if there are multiple toggle references to an object, none
2652  * of them will ever be notified until all but one are removed.  For
2653  * this reason, you should only ever use a toggle reference if there
2654  * is important state in the proxy object.
2655  *
2656  * Since: 2.8
2657  */
2658 void
2659 g_object_add_toggle_ref (GObject       *object,
2660                          GToggleNotify  notify,
2661                          gpointer       data)
2662 {
2663   ToggleRefStack *tstack;
2664   guint i;
2665   
2666   g_return_if_fail (G_IS_OBJECT (object));
2667   g_return_if_fail (notify != NULL);
2668   g_return_if_fail (object->ref_count >= 1);
2669
2670   g_object_ref (object);
2671
2672   G_LOCK (toggle_refs_mutex);
2673   tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2674   if (tstack)
2675     {
2676       i = tstack->n_toggle_refs++;
2677       /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2678        * in tstate->toggle_refs */
2679       tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2680     }
2681   else
2682     {
2683       tstack = g_renew (ToggleRefStack, NULL, 1);
2684       tstack->object = object;
2685       tstack->n_toggle_refs = 1;
2686       i = 0;
2687     }
2688
2689   /* Set a flag for fast lookup after adding the first toggle reference */
2690   if (tstack->n_toggle_refs == 1)
2691     g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2692   
2693   tstack->toggle_refs[i].notify = notify;
2694   tstack->toggle_refs[i].data = data;
2695   g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2696                                (GDestroyNotify)g_free);
2697   G_UNLOCK (toggle_refs_mutex);
2698 }
2699
2700 /**
2701  * g_object_remove_toggle_ref: (skip)
2702  * @object: a #GObject
2703  * @notify: a function to call when this reference is the
2704  *  last reference to the object, or is no longer
2705  *  the last reference.
2706  * @data: data to pass to @notify
2707  *
2708  * Removes a reference added with g_object_add_toggle_ref(). The
2709  * reference count of the object is decreased by one.
2710  *
2711  * Since: 2.8
2712  */
2713 void
2714 g_object_remove_toggle_ref (GObject       *object,
2715                             GToggleNotify  notify,
2716                             gpointer       data)
2717 {
2718   ToggleRefStack *tstack;
2719   gboolean found_one = FALSE;
2720
2721   g_return_if_fail (G_IS_OBJECT (object));
2722   g_return_if_fail (notify != NULL);
2723
2724   G_LOCK (toggle_refs_mutex);
2725   tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2726   if (tstack)
2727     {
2728       guint i;
2729
2730       for (i = 0; i < tstack->n_toggle_refs; i++)
2731         if (tstack->toggle_refs[i].notify == notify &&
2732             tstack->toggle_refs[i].data == data)
2733           {
2734             found_one = TRUE;
2735             tstack->n_toggle_refs -= 1;
2736             if (i != tstack->n_toggle_refs)
2737               tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2738
2739             if (tstack->n_toggle_refs == 0)
2740               g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2741
2742             break;
2743           }
2744     }
2745   G_UNLOCK (toggle_refs_mutex);
2746
2747   if (found_one)
2748     g_object_unref (object);
2749   else
2750     g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2751 }
2752
2753 /**
2754  * g_object_ref:
2755  * @object: (type GObject.Object): a #GObject
2756  *
2757  * Increases the reference count of @object.
2758  *
2759  * Returns: (type GObject.Object) (transfer none): the same @object
2760  */
2761 gpointer
2762 g_object_ref (gpointer _object)
2763 {
2764   GObject *object = _object;
2765   gint old_val;
2766
2767   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2768   g_return_val_if_fail (object->ref_count > 0, NULL);
2769   
2770 #ifdef  G_ENABLE_DEBUG
2771   if (g_trap_object_ref == object)
2772     G_BREAKPOINT ();
2773 #endif  /* G_ENABLE_DEBUG */
2774
2775
2776   old_val = g_atomic_int_add (&object->ref_count, 1);
2777
2778   if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2779     toggle_refs_notify (object, FALSE);
2780
2781   TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2782
2783   return object;
2784 }
2785
2786 /**
2787  * g_object_unref:
2788  * @object: (type GObject.Object): a #GObject
2789  *
2790  * Decreases the reference count of @object. When its reference count
2791  * drops to 0, the object is finalized (i.e. its memory is freed).
2792  */
2793 void
2794 g_object_unref (gpointer _object)
2795 {
2796   GObject *object = _object;
2797   gint old_ref;
2798   
2799   g_return_if_fail (G_IS_OBJECT (object));
2800   g_return_if_fail (object->ref_count > 0);
2801   
2802 #ifdef  G_ENABLE_DEBUG
2803   if (g_trap_object_ref == object)
2804     G_BREAKPOINT ();
2805 #endif  /* G_ENABLE_DEBUG */
2806
2807   /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2808  retry_atomic_decrement1:
2809   old_ref = g_atomic_int_get (&object->ref_count);
2810   if (old_ref > 1)
2811     {
2812       /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2813       gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2814
2815       if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2816         goto retry_atomic_decrement1;
2817
2818       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2819
2820       /* if we went from 2->1 we need to notify toggle refs if any */
2821       if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2822         toggle_refs_notify (object, TRUE);
2823     }
2824   else
2825     {
2826       /* we are about tp remove the last reference */
2827       TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2828       G_OBJECT_GET_CLASS (object)->dispose (object);
2829       TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2830
2831       /* may have been re-referenced meanwhile */
2832     retry_atomic_decrement2:
2833       old_ref = g_atomic_int_get ((int *)&object->ref_count);
2834       if (old_ref > 1)
2835         {
2836           /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2837           gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2838
2839           if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2840             goto retry_atomic_decrement2;
2841
2842           TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2843
2844           /* if we went from 2->1 we need to notify toggle refs if any */
2845           if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2846             toggle_refs_notify (object, TRUE);
2847
2848           return;
2849         }
2850
2851       /* we are still in the process of taking away the last ref */
2852       g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2853       g_signal_handlers_destroy (object);
2854       g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2855       
2856       /* decrement the last reference */
2857       old_ref = g_atomic_int_add (&object->ref_count, -1);
2858
2859       TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2860
2861       /* may have been re-referenced meanwhile */
2862       if (G_LIKELY (old_ref == 1))
2863         {
2864           TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2865           G_OBJECT_GET_CLASS (object)->finalize (object);
2866
2867           TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2868
2869 #ifdef  G_ENABLE_DEBUG
2870           IF_DEBUG (OBJECTS)
2871             {
2872               /* catch objects not chaining finalize handlers */
2873               G_LOCK (debug_objects);
2874               g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2875               G_UNLOCK (debug_objects);
2876             }
2877 #endif  /* G_ENABLE_DEBUG */
2878           g_type_free_instance ((GTypeInstance*) object);
2879         }
2880     }
2881 }
2882
2883 /**
2884  * g_clear_object: (skip)
2885  * @object_ptr: a pointer to a #GObject reference
2886  *
2887  * Clears a reference to a #GObject.
2888  *
2889  * @object_ptr must not be %NULL.
2890  *
2891  * If the reference is %NULL then this function does nothing.
2892  * Otherwise, the reference count of the object is decreased and the
2893  * pointer is set to %NULL.
2894  *
2895  * This function is threadsafe and modifies the pointer atomically,
2896  * using memory barriers where needed.
2897  *
2898  * A macro is also included that allows this function to be used without
2899  * pointer casts.
2900  *
2901  * Since: 2.28
2902  **/
2903 #undef g_clear_object
2904 void
2905 g_clear_object (volatile GObject **object_ptr)
2906 {
2907   gpointer *ptr = (gpointer) object_ptr;
2908   gpointer old;
2909
2910   /* This is a little frustrating.
2911    * Would be nice to have an atomic exchange (with no compare).
2912    */
2913   do
2914     old = g_atomic_pointer_get (ptr);
2915   while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2916
2917   if (old)
2918     g_object_unref (old);
2919 }
2920
2921 /**
2922  * g_object_get_qdata:
2923  * @object: The GObject to get a stored user data pointer from
2924  * @quark: A #GQuark, naming the user data pointer
2925  * 
2926  * This function gets back user data pointers stored via
2927  * g_object_set_qdata().
2928  * 
2929  * Returns: (transfer none): The user data pointer set, or %NULL
2930  */
2931 gpointer
2932 g_object_get_qdata (GObject *object,
2933                     GQuark   quark)
2934 {
2935   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2936   
2937   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2938 }
2939
2940 /**
2941  * g_object_set_qdata: (skip)
2942  * @object: The GObject to set store a user data pointer
2943  * @quark: A #GQuark, naming the user data pointer
2944  * @data: An opaque user data pointer
2945  *
2946  * This sets an opaque, named pointer on an object.
2947  * The name is specified through a #GQuark (retrived e.g. via
2948  * g_quark_from_static_string()), and the pointer
2949  * can be gotten back from the @object with g_object_get_qdata()
2950  * until the @object is finalized.
2951  * Setting a previously set user data pointer, overrides (frees)
2952  * the old pointer set, using #NULL as pointer essentially
2953  * removes the data stored.
2954  */
2955 void
2956 g_object_set_qdata (GObject *object,
2957                     GQuark   quark,
2958                     gpointer data)
2959 {
2960   g_return_if_fail (G_IS_OBJECT (object));
2961   g_return_if_fail (quark > 0);
2962   
2963   g_datalist_id_set_data (&object->qdata, quark, data);
2964 }
2965
2966 /**
2967  * g_object_set_qdata_full: (skip)
2968  * @object: The GObject to set store a user data pointer
2969  * @quark: A #GQuark, naming the user data pointer
2970  * @data: An opaque user data pointer
2971  * @destroy: Function to invoke with @data as argument, when @data
2972  *           needs to be freed
2973  *
2974  * This function works like g_object_set_qdata(), but in addition,
2975  * a void (*destroy) (gpointer) function may be specified which is
2976  * called with @data as argument when the @object is finalized, or
2977  * the data is being overwritten by a call to g_object_set_qdata()
2978  * with the same @quark.
2979  */
2980 void
2981 g_object_set_qdata_full (GObject       *object,
2982                          GQuark         quark,
2983                          gpointer       data,
2984                          GDestroyNotify destroy)
2985 {
2986   g_return_if_fail (G_IS_OBJECT (object));
2987   g_return_if_fail (quark > 0);
2988   
2989   g_datalist_id_set_data_full (&object->qdata, quark, data,
2990                                data ? destroy : (GDestroyNotify) NULL);
2991 }
2992
2993 /**
2994  * g_object_steal_qdata:
2995  * @object: The GObject to get a stored user data pointer from
2996  * @quark: A #GQuark, naming the user data pointer
2997  *
2998  * This function gets back user data pointers stored via
2999  * g_object_set_qdata() and removes the @data from object
3000  * without invoking its destroy() function (if any was
3001  * set).
3002  * Usually, calling this function is only required to update
3003  * user data pointers with a destroy notifier, for example:
3004  * |[
3005  * void
3006  * object_add_to_user_list (GObject     *object,
3007  *                          const gchar *new_string)
3008  * {
3009  *   // the quark, naming the object data
3010  *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3011  *   // retrive the old string list
3012  *   GList *list = g_object_steal_qdata (object, quark_string_list);
3013  *
3014  *   // prepend new string
3015  *   list = g_list_prepend (list, g_strdup (new_string));
3016  *   // this changed 'list', so we need to set it again
3017  *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3018  * }
3019  * static void
3020  * free_string_list (gpointer data)
3021  * {
3022  *   GList *node, *list = data;
3023  *
3024  *   for (node = list; node; node = node->next)
3025  *     g_free (node->data);
3026  *   g_list_free (list);
3027  * }
3028  * ]|
3029  * Using g_object_get_qdata() in the above example, instead of
3030  * g_object_steal_qdata() would have left the destroy function set,
3031  * and thus the partial string list would have been freed upon
3032  * g_object_set_qdata_full().
3033  *
3034  * Returns: (transfer full): The user data pointer set, or %NULL
3035  */
3036 gpointer
3037 g_object_steal_qdata (GObject *object,
3038                       GQuark   quark)
3039 {
3040   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3041   g_return_val_if_fail (quark > 0, NULL);
3042   
3043   return g_datalist_id_remove_no_notify (&object->qdata, quark);
3044 }
3045
3046 /**
3047  * g_object_get_data:
3048  * @object: #GObject containing the associations
3049  * @key: name of the key for that association
3050  * 
3051  * Gets a named field from the objects table of associations (see g_object_set_data()).
3052  * 
3053  * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3054  */
3055 gpointer
3056 g_object_get_data (GObject     *object,
3057                    const gchar *key)
3058 {
3059   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3060   g_return_val_if_fail (key != NULL, NULL);
3061
3062   return g_datalist_get_data (&object->qdata, key);
3063 }
3064
3065 /**
3066  * g_object_set_data:
3067  * @object: #GObject containing the associations.
3068  * @key: name of the key
3069  * @data: data to associate with that key
3070  *
3071  * Each object carries around a table of associations from
3072  * strings to pointers.  This function lets you set an association.
3073  *
3074  * If the object already had an association with that name,
3075  * the old association will be destroyed.
3076  */
3077 void
3078 g_object_set_data (GObject     *object,
3079                    const gchar *key,
3080                    gpointer     data)
3081 {
3082   g_return_if_fail (G_IS_OBJECT (object));
3083   g_return_if_fail (key != NULL);
3084
3085   g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3086 }
3087
3088 /**
3089  * g_object_set_data_full: (skip)
3090  * @object: #GObject containing the associations
3091  * @key: name of the key
3092  * @data: data to associate with that key
3093  * @destroy: function to call when the association is destroyed
3094  *
3095  * Like g_object_set_data() except it adds notification
3096  * for when the association is destroyed, either by setting it
3097  * to a different value or when the object is destroyed.
3098  *
3099  * Note that the @destroy callback is not called if @data is %NULL.
3100  */
3101 void
3102 g_object_set_data_full (GObject       *object,
3103                         const gchar   *key,
3104                         gpointer       data,
3105                         GDestroyNotify destroy)
3106 {
3107   g_return_if_fail (G_IS_OBJECT (object));
3108   g_return_if_fail (key != NULL);
3109
3110   g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3111                                data ? destroy : (GDestroyNotify) NULL);
3112 }
3113
3114 /**
3115  * g_object_steal_data:
3116  * @object: #GObject containing the associations
3117  * @key: name of the key
3118  *
3119  * Remove a specified datum from the object's data associations,
3120  * without invoking the association's destroy handler.
3121  *
3122  * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3123  */
3124 gpointer
3125 g_object_steal_data (GObject     *object,
3126                      const gchar *key)
3127 {
3128   GQuark quark;
3129
3130   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3131   g_return_val_if_fail (key != NULL, NULL);
3132
3133   quark = g_quark_try_string (key);
3134
3135   return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3136 }
3137
3138 static void
3139 g_value_object_init (GValue *value)
3140 {
3141   value->data[0].v_pointer = NULL;
3142 }
3143
3144 static void
3145 g_value_object_free_value (GValue *value)
3146 {
3147   if (value->data[0].v_pointer)
3148     g_object_unref (value->data[0].v_pointer);
3149 }
3150
3151 static void
3152 g_value_object_copy_value (const GValue *src_value,
3153                            GValue       *dest_value)
3154 {
3155   if (src_value->data[0].v_pointer)
3156     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3157   else
3158     dest_value->data[0].v_pointer = NULL;
3159 }
3160
3161 static void
3162 g_value_object_transform_value (const GValue *src_value,
3163                                 GValue       *dest_value)
3164 {
3165   if (src_value->data[0].v_pointer && g_type_is_a (G_OBJECT_TYPE (src_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
3166     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3167   else
3168     dest_value->data[0].v_pointer = NULL;
3169 }
3170
3171 static gpointer
3172 g_value_object_peek_pointer (const GValue *value)
3173 {
3174   return value->data[0].v_pointer;
3175 }
3176
3177 static gchar*
3178 g_value_object_collect_value (GValue      *value,
3179                               guint        n_collect_values,
3180                               GTypeCValue *collect_values,
3181                               guint        collect_flags)
3182 {
3183   if (collect_values[0].v_pointer)
3184     {
3185       GObject *object = collect_values[0].v_pointer;
3186       
3187       if (object->g_type_instance.g_class == NULL)
3188         return g_strconcat ("invalid unclassed object pointer for value type `",
3189                             G_VALUE_TYPE_NAME (value),
3190                             "'",
3191                             NULL);
3192       else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3193         return g_strconcat ("invalid object type `",
3194                             G_OBJECT_TYPE_NAME (object),
3195                             "' for value type `",
3196                             G_VALUE_TYPE_NAME (value),
3197                             "'",
3198                             NULL);
3199       /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3200       value->data[0].v_pointer = g_object_ref (object);
3201     }
3202   else
3203     value->data[0].v_pointer = NULL;
3204   
3205   return NULL;
3206 }
3207
3208 static gchar*
3209 g_value_object_lcopy_value (const GValue *value,
3210                             guint        n_collect_values,
3211                             GTypeCValue *collect_values,
3212                             guint        collect_flags)
3213 {
3214   GObject **object_p = collect_values[0].v_pointer;
3215   
3216   if (!object_p)
3217     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3218
3219   if (!value->data[0].v_pointer)
3220     *object_p = NULL;
3221   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3222     *object_p = value->data[0].v_pointer;
3223   else
3224     *object_p = g_object_ref (value->data[0].v_pointer);
3225   
3226   return NULL;
3227 }
3228
3229 /**
3230  * g_value_set_object:
3231  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3232  * @v_object: (type GObject.Object) (allow-none): object value to be set
3233  *
3234  * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3235  *
3236  * g_value_set_object() increases the reference count of @v_object
3237  * (the #GValue holds a reference to @v_object).  If you do not wish
3238  * to increase the reference count of the object (i.e. you wish to
3239  * pass your current reference to the #GValue because you no longer
3240  * need it), use g_value_take_object() instead.
3241  *
3242  * It is important that your #GValue holds a reference to @v_object (either its
3243  * own, or one it has taken) to ensure that the object won't be destroyed while
3244  * the #GValue still exists).
3245  */
3246 void
3247 g_value_set_object (GValue   *value,
3248                     gpointer  v_object)
3249 {
3250   GObject *old;
3251         
3252   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3253
3254   old = value->data[0].v_pointer;
3255   
3256   if (v_object)
3257     {
3258       g_return_if_fail (G_IS_OBJECT (v_object));
3259       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3260
3261       value->data[0].v_pointer = v_object;
3262       g_object_ref (value->data[0].v_pointer);
3263     }
3264   else
3265     value->data[0].v_pointer = NULL;
3266   
3267   if (old)
3268     g_object_unref (old);
3269 }
3270
3271 /**
3272  * g_value_set_object_take_ownership: (skip)
3273  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3274  * @v_object: (allow-none): object value to be set
3275  *
3276  * This is an internal function introduced mainly for C marshallers.
3277  *
3278  * Deprecated: 2.4: Use g_value_take_object() instead.
3279  */
3280 void
3281 g_value_set_object_take_ownership (GValue  *value,
3282                                    gpointer v_object)
3283 {
3284   g_value_take_object (value, v_object);
3285 }
3286
3287 /**
3288  * g_value_take_object: (skip)
3289  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3290  * @v_object: (allow-none): object value to be set
3291  *
3292  * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3293  * and takes over the ownership of the callers reference to @v_object;
3294  * the caller doesn't have to unref it any more (i.e. the reference
3295  * count of the object is not increased).
3296  *
3297  * If you want the #GValue to hold its own reference to @v_object, use
3298  * g_value_set_object() instead.
3299  *
3300  * Since: 2.4
3301  */
3302 void
3303 g_value_take_object (GValue  *value,
3304                      gpointer v_object)
3305 {
3306   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3307
3308   if (value->data[0].v_pointer)
3309     {
3310       g_object_unref (value->data[0].v_pointer);
3311       value->data[0].v_pointer = NULL;
3312     }
3313
3314   if (v_object)
3315     {
3316       g_return_if_fail (G_IS_OBJECT (v_object));
3317       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3318
3319       value->data[0].v_pointer = v_object; /* we take over the reference count */
3320     }
3321 }
3322
3323 /**
3324  * g_value_get_object:
3325  * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3326  * 
3327  * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3328  * 
3329  * Returns: (type GObject.Object) (transfer none): object contents of @value
3330  */
3331 gpointer
3332 g_value_get_object (const GValue *value)
3333 {
3334   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3335   
3336   return value->data[0].v_pointer;
3337 }
3338
3339 /**
3340  * g_value_dup_object:
3341  * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3342  *
3343  * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3344  * its reference count. If the contents of the #GValue are %NULL, then
3345  * %NULL will be returned.
3346  *
3347  * Returns: (type GObject.Object) (transfer full): object content of @value,
3348  *          should be unreferenced when no longer needed.
3349  */
3350 gpointer
3351 g_value_dup_object (const GValue *value)
3352 {
3353   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3354   
3355   return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3356 }
3357
3358 /**
3359  * g_signal_connect_object: (skip)
3360  * @instance: the instance to connect to.
3361  * @detailed_signal: a string of the form "signal-name::detail".
3362  * @c_handler: the #GCallback to connect.
3363  * @gobject: the object to pass as data to @c_handler.
3364  * @connect_flags: a combination of #GConnectFlags.
3365  *
3366  * This is similar to g_signal_connect_data(), but uses a closure which
3367  * ensures that the @gobject stays alive during the call to @c_handler
3368  * by temporarily adding a reference count to @gobject.
3369  *
3370  * Note that there is a bug in GObject that makes this function
3371  * much less useful than it might seem otherwise. Once @gobject is
3372  * disposed, the callback will no longer be called, but, the signal
3373  * handler is <emphasis>not</emphasis> currently disconnected. If the
3374  * @instance is itself being freed at the same time than this doesn't
3375  * matter, since the signal will automatically be removed, but
3376  * if @instance persists, then the signal handler will leak. You
3377  * should not remove the signal yourself because in a future versions of
3378  * GObject, the handler <emphasis>will</emphasis> automatically
3379  * be disconnected.
3380  *
3381  * It's possible to work around this problem in a way that will
3382  * continue to work with future versions of GObject by checking
3383  * that the signal handler is still connected before disconnected it:
3384  * <informalexample><programlisting>
3385  *  if (g_signal_handler_is_connected (instance, id))
3386  *    g_signal_handler_disconnect (instance, id);
3387  * </programlisting></informalexample>
3388  *
3389  * Returns: the handler id.
3390  */
3391 gulong
3392 g_signal_connect_object (gpointer      instance,
3393                          const gchar  *detailed_signal,
3394                          GCallback     c_handler,
3395                          gpointer      gobject,
3396                          GConnectFlags connect_flags)
3397 {
3398   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3399   g_return_val_if_fail (detailed_signal != NULL, 0);
3400   g_return_val_if_fail (c_handler != NULL, 0);
3401
3402   if (gobject)
3403     {
3404       GClosure *closure;
3405
3406       g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3407
3408       closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3409
3410       return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3411     }
3412   else
3413     return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3414 }
3415
3416 typedef struct {
3417   GObject  *object;
3418   guint     n_closures;
3419   GClosure *closures[1]; /* flexible array */
3420 } CArray;
3421 /* don't change this structure without supplying an accessor for
3422  * watched closures, e.g.:
3423  * GSList* g_object_list_watched_closures (GObject *object)
3424  * {
3425  *   CArray *carray;
3426  *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3427  *   carray = g_object_get_data (object, "GObject-closure-array");
3428  *   if (carray)
3429  *     {
3430  *       GSList *slist = NULL;
3431  *       guint i;
3432  *       for (i = 0; i < carray->n_closures; i++)
3433  *         slist = g_slist_prepend (slist, carray->closures[i]);
3434  *       return slist;
3435  *     }
3436  *   return NULL;
3437  * }
3438  */
3439
3440 static void
3441 object_remove_closure (gpointer  data,
3442                        GClosure *closure)
3443 {
3444   GObject *object = data;
3445   CArray *carray;
3446   guint i;
3447   
3448   G_LOCK (closure_array_mutex);
3449   carray = g_object_get_qdata (object, quark_closure_array);
3450   for (i = 0; i < carray->n_closures; i++)
3451     if (carray->closures[i] == closure)
3452       {
3453         carray->n_closures--;
3454         if (i < carray->n_closures)
3455           carray->closures[i] = carray->closures[carray->n_closures];
3456         G_UNLOCK (closure_array_mutex);
3457         return;
3458       }
3459   G_UNLOCK (closure_array_mutex);
3460   g_assert_not_reached ();
3461 }
3462
3463 static void
3464 destroy_closure_array (gpointer data)
3465 {
3466   CArray *carray = data;
3467   GObject *object = carray->object;
3468   guint i, n = carray->n_closures;
3469   
3470   for (i = 0; i < n; i++)
3471     {
3472       GClosure *closure = carray->closures[i];
3473       
3474       /* removing object_remove_closure() upfront is probably faster than
3475        * letting it fiddle with quark_closure_array which is empty anyways
3476        */
3477       g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3478       g_closure_invalidate (closure);
3479     }
3480   g_free (carray);
3481 }
3482
3483 /**
3484  * g_object_watch_closure:
3485  * @object: GObject restricting lifetime of @closure
3486  * @closure: GClosure to watch
3487  *
3488  * This function essentially limits the life time of the @closure to
3489  * the life time of the object. That is, when the object is finalized,
3490  * the @closure is invalidated by calling g_closure_invalidate() on
3491  * it, in order to prevent invocations of the closure with a finalized
3492  * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3493  * added as marshal guards to the @closure, to ensure that an extra
3494  * reference count is held on @object during invocation of the
3495  * @closure.  Usually, this function will be called on closures that
3496  * use this @object as closure data.
3497  */
3498 void
3499 g_object_watch_closure (GObject  *object,
3500                         GClosure *closure)
3501 {
3502   CArray *carray;
3503   guint i;
3504   
3505   g_return_if_fail (G_IS_OBJECT (object));
3506   g_return_if_fail (closure != NULL);
3507   g_return_if_fail (closure->is_invalid == FALSE);
3508   g_return_if_fail (closure->in_marshal == FALSE);
3509   g_return_if_fail (object->ref_count > 0);     /* this doesn't work on finalizing objects */
3510   
3511   g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3512   g_closure_add_marshal_guards (closure,
3513                                 object, (GClosureNotify) g_object_ref,
3514                                 object, (GClosureNotify) g_object_unref);
3515   G_LOCK (closure_array_mutex);
3516   carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3517   if (!carray)
3518     {
3519       carray = g_renew (CArray, NULL, 1);
3520       carray->object = object;
3521       carray->n_closures = 1;
3522       i = 0;
3523     }
3524   else
3525     {
3526       i = carray->n_closures++;
3527       carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3528     }
3529   carray->closures[i] = closure;
3530   g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3531   G_UNLOCK (closure_array_mutex);
3532 }
3533
3534 /**
3535  * g_closure_new_object:
3536  * @sizeof_closure: the size of the structure to allocate, must be at least
3537  *  <literal>sizeof (GClosure)</literal>
3538  * @object: a #GObject pointer to store in the @data field of the newly
3539  *  allocated #GClosure
3540  *
3541  * A variant of g_closure_new_simple() which stores @object in the
3542  * @data field of the closure and calls g_object_watch_closure() on
3543  * @object and the created closure. This function is mainly useful
3544  * when implementing new types of closures.
3545  *
3546  * Returns: (transfer full): a newly allocated #GClosure
3547  */
3548 GClosure*
3549 g_closure_new_object (guint    sizeof_closure,
3550                       GObject *object)
3551 {
3552   GClosure *closure;
3553
3554   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3555   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3556
3557   closure = g_closure_new_simple (sizeof_closure, object);
3558   g_object_watch_closure (object, closure);
3559
3560   return closure;
3561 }
3562
3563 /**
3564  * g_cclosure_new_object: (skip)
3565  * @callback_func: the function to invoke
3566  * @object: a #GObject pointer to pass to @callback_func
3567  *
3568  * A variant of g_cclosure_new() which uses @object as @user_data and
3569  * calls g_object_watch_closure() on @object and the created
3570  * closure. This function is useful when you have a callback closely
3571  * associated with a #GObject, and want the callback to no longer run
3572  * after the object is is freed.
3573  *
3574  * Returns: a new #GCClosure
3575  */
3576 GClosure*
3577 g_cclosure_new_object (GCallback callback_func,
3578                        GObject  *object)
3579 {
3580   GClosure *closure;
3581
3582   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3583   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3584   g_return_val_if_fail (callback_func != NULL, NULL);
3585
3586   closure = g_cclosure_new (callback_func, object, NULL);
3587   g_object_watch_closure (object, closure);
3588
3589   return closure;
3590 }
3591
3592 /**
3593  * g_cclosure_new_object_swap: (skip)
3594  * @callback_func: the function to invoke
3595  * @object: a #GObject pointer to pass to @callback_func
3596  *
3597  * A variant of g_cclosure_new_swap() which uses @object as @user_data
3598  * and calls g_object_watch_closure() on @object and the created
3599  * closure. This function is useful when you have a callback closely
3600  * associated with a #GObject, and want the callback to no longer run
3601  * after the object is is freed.
3602  *
3603  * Returns: a new #GCClosure
3604  */
3605 GClosure*
3606 g_cclosure_new_object_swap (GCallback callback_func,
3607                             GObject  *object)
3608 {
3609   GClosure *closure;
3610
3611   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3612   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
3613   g_return_val_if_fail (callback_func != NULL, NULL);
3614
3615   closure = g_cclosure_new_swap (callback_func, object, NULL);
3616   g_object_watch_closure (object, closure);
3617
3618   return closure;
3619 }
3620
3621 gsize
3622 g_object_compat_control (gsize           what,
3623                          gpointer        data)
3624 {
3625   switch (what)
3626     {
3627       gpointer *pp;
3628     case 1:     /* floating base type */
3629       return G_TYPE_INITIALLY_UNOWNED;
3630     case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3631       floating_flag_handler = (guint(*)(GObject*,gint)) data;
3632       return 1;
3633     case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
3634       pp = data;
3635       *pp = floating_flag_handler;
3636       return 1;
3637     default:
3638       return 0;
3639     }
3640 }
3641
3642 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3643
3644 static void
3645 g_initially_unowned_init (GInitiallyUnowned *object)
3646 {
3647   g_object_force_floating (object);
3648 }
3649
3650 static void
3651 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3652 {
3653 }