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