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