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