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