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