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