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