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