Make work again.
[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 #include        "gobject.h"
20 #include        "gobjectalias.h"
21
22 /*
23  * MT safe
24  */
25
26 #include        "gvaluecollector.h"
27 #include        "gsignal.h"
28 #include        "gparamspecs.h"
29 #include        "gvaluetypes.h"
30 #include        "gobjectnotifyqueue.c"
31 #include        <string.h>
32 #include        <signal.h>
33
34
35 #define PREALLOC_CPARAMS        (8)
36
37
38 /* --- macros --- */
39 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
40 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
41
42
43 /* --- signals --- */
44 enum {
45   NOTIFY,
46   LAST_SIGNAL
47 };
48
49
50 /* --- properties --- */
51 enum {
52   PROP_NONE
53 };
54
55
56 /* --- prototypes --- */
57 static void     g_object_base_class_init                (GObjectClass   *class);
58 static void     g_object_base_class_finalize            (GObjectClass   *class);
59 static void     g_object_do_class_init                  (GObjectClass   *class);
60 static void     g_object_init                           (GObject        *object);
61 static GObject* g_object_constructor                    (GType                  type,
62                                                          guint                  n_construct_properties,
63                                                          GObjectConstructParam *construct_params);
64 static void     g_object_last_unref                     (GObject        *object);
65 static void     g_object_real_dispose                   (GObject        *object);
66 static void     g_object_finalize                       (GObject        *object);
67 static void     g_object_do_set_property                (GObject        *object,
68                                                          guint           property_id,
69                                                          const GValue   *value,
70                                                          GParamSpec     *pspec);
71 static void     g_object_do_get_property                (GObject        *object,
72                                                          guint           property_id,
73                                                          GValue         *value,
74                                                          GParamSpec     *pspec);
75 static void     g_value_object_init                     (GValue         *value);
76 static void     g_value_object_free_value               (GValue         *value);
77 static void     g_value_object_copy_value               (const GValue   *src_value,
78                                                          GValue         *dest_value);
79 static void     g_value_object_transform_value          (const GValue   *src_value,
80                                                          GValue         *dest_value);
81 static gpointer g_value_object_peek_pointer             (const GValue   *value);
82 static gchar*   g_value_object_collect_value            (GValue         *value,
83                                                          guint           n_collect_values,
84                                                          GTypeCValue    *collect_values,
85                                                          guint           collect_flags);
86 static gchar*   g_value_object_lcopy_value              (const GValue   *value,
87                                                          guint           n_collect_values,
88                                                          GTypeCValue    *collect_values,
89                                                          guint           collect_flags);
90 static void     g_object_dispatch_properties_changed    (GObject        *object,
91                                                          guint           n_pspecs,
92                                                          GParamSpec    **pspecs);
93 static inline void         object_get_property          (GObject        *object,
94                                                          GParamSpec     *pspec,
95                                                          GValue         *value);
96 static inline void         object_set_property          (GObject        *object,
97                                                          GParamSpec     *pspec,
98                                                          const GValue   *value,
99                                                          GObjectNotifyQueue *nqueue);
100
101 static void object_interface_check_properties           (gpointer        func_data,
102                                                          gpointer        g_iface);
103
104
105 /* --- variables --- */
106 static GQuark               quark_closure_array = 0;
107 static GQuark               quark_weak_refs = 0;
108 static GParamSpecPool      *pspec_pool = NULL;
109 static GObjectNotifyContext property_notify_context = { 0, };
110 static gulong               gobject_signals[LAST_SIGNAL] = { 0, };
111 G_LOCK_DEFINE_STATIC (construct_objects_lock);
112 static GSList *construct_objects = NULL;
113
114
115 /* --- functions --- */
116 #ifdef  G_ENABLE_DEBUG
117 #define IF_DEBUG(debug_type)    if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
118 G_LOCK_DEFINE_STATIC     (debug_objects);
119 static volatile GObject *g_trap_object_ref = NULL;
120 static guint             debug_objects_count = 0;
121 static GHashTable       *debug_objects_ht = NULL;
122 static void
123 debug_objects_foreach (gpointer key,
124                        gpointer value,
125                        gpointer user_data)
126 {
127   GObject *object = value;
128
129   g_message ("[%p] stale %s\tref_count=%u",
130              object,
131              G_OBJECT_TYPE_NAME (object),
132              object->ref_count);
133 }
134 static void
135 debug_objects_atexit (void)
136 {
137   IF_DEBUG (OBJECTS)
138     {
139       G_LOCK (debug_objects);
140       g_message ("stale GObjects: %u", debug_objects_count);
141       g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
142       G_UNLOCK (debug_objects);
143     }
144 }
145 #endif  /* G_ENABLE_DEBUG */
146
147 void
148 g_object_type_init (void)
149 {
150   static gboolean initialized = FALSE;
151   static const GTypeFundamentalInfo finfo = {
152     G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
153   };
154   static GTypeInfo info = {
155     sizeof (GObjectClass),
156     (GBaseInitFunc) g_object_base_class_init,
157     (GBaseFinalizeFunc) g_object_base_class_finalize,
158     (GClassInitFunc) g_object_do_class_init,
159     NULL        /* class_destroy */,
160     NULL        /* class_data */,
161     sizeof (GObject),
162     0           /* n_preallocs */,
163     (GInstanceInitFunc) g_object_init,
164     NULL,       /* value_table */
165   };
166   static const GTypeValueTable value_table = {
167     g_value_object_init,          /* value_init */
168     g_value_object_free_value,    /* value_free */
169     g_value_object_copy_value,    /* value_copy */
170     g_value_object_peek_pointer,  /* value_peek_pointer */
171     "p",                          /* collect_format */
172     g_value_object_collect_value, /* collect_value */
173     "p",                          /* lcopy_format */
174     g_value_object_lcopy_value,   /* lcopy_value */
175   };
176   GType type;
177   
178   g_return_if_fail (initialized == FALSE);
179   initialized = TRUE;
180   
181   /* G_TYPE_OBJECT
182    */
183   info.value_table = &value_table;
184   type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
185   g_assert (type == G_TYPE_OBJECT);
186   g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
187   
188 #ifdef  G_ENABLE_DEBUG
189   IF_DEBUG (OBJECTS)
190     {
191       debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
192       g_atexit (debug_objects_atexit);
193     }
194 #endif  /* G_ENABLE_DEBUG */
195 }
196
197 static void
198 g_object_base_class_init (GObjectClass *class)
199 {
200   GObjectClass *pclass = g_type_class_peek_parent (class);
201
202   /* reset instance specific fields and methods that don't get inherited */
203   class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
204   class->get_property = NULL;
205   class->set_property = NULL;
206 }
207
208 static void
209 g_object_base_class_finalize (GObjectClass *class)
210 {
211   GList *list, *node;
212   
213   _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
214
215   g_slist_free (class->construct_properties);
216   class->construct_properties = NULL;
217   list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
218   for (node = list; node; node = node->next)
219     {
220       GParamSpec *pspec = node->data;
221       
222       g_param_spec_pool_remove (pspec_pool, pspec);
223       PARAM_SPEC_SET_PARAM_ID (pspec, 0);
224       g_param_spec_unref (pspec);
225     }
226   g_list_free (list);
227 }
228
229 static void
230 g_object_notify_dispatcher (GObject     *object,
231                             guint        n_pspecs,
232                             GParamSpec **pspecs)
233 {
234   G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
235 }
236
237 static void
238 g_object_do_class_init (GObjectClass *class)
239 {
240   /* read the comment about typedef struct CArray; on why not to change this quark */
241   quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
242
243   quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
244   pspec_pool = g_param_spec_pool_new (TRUE);
245   property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
246   property_notify_context.dispatcher = g_object_notify_dispatcher;
247   
248   class->constructor = g_object_constructor;
249   class->set_property = g_object_do_set_property;
250   class->get_property = g_object_do_get_property;
251   class->dispose = g_object_real_dispose;
252   class->finalize = g_object_finalize;
253   class->dispatch_properties_changed = g_object_dispatch_properties_changed;
254   class->notify = NULL;
255
256   gobject_signals[NOTIFY] =
257     g_signal_new ("notify",
258                   G_TYPE_FROM_CLASS (class),
259                   G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
260                   G_STRUCT_OFFSET (GObjectClass, notify),
261                   NULL, NULL,
262                   g_cclosure_marshal_VOID__PARAM,
263                   G_TYPE_NONE,
264                   1, G_TYPE_PARAM);
265
266   /* Install a check function that we'll use to verify that classes that
267    * implement an interface implement all properties for that interface
268    */
269   g_type_add_interface_check (NULL, object_interface_check_properties);
270 }
271
272 static void
273 install_property_internal (GType       g_type,
274                            guint       property_id,
275                            GParamSpec *pspec)
276 {
277   if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
278     {
279       g_warning ("When installing property: type `%s' already has a property named `%s'",
280                  g_type_name (g_type),
281                  pspec->name);
282       return;
283     }
284
285   g_param_spec_ref (pspec);
286   g_param_spec_sink (pspec);
287   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
288   g_param_spec_pool_insert (pspec_pool, pspec, g_type);
289 }
290
291 void
292 g_object_class_install_property (GObjectClass *class,
293                                  guint         property_id,
294                                  GParamSpec   *pspec)
295 {
296   g_return_if_fail (G_IS_OBJECT_CLASS (class));
297   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
298   if (pspec->flags & G_PARAM_WRITABLE)
299     g_return_if_fail (class->set_property != NULL);
300   if (pspec->flags & G_PARAM_READABLE)
301     g_return_if_fail (class->get_property != NULL);
302   g_return_if_fail (property_id > 0);
303   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
304   if (pspec->flags & G_PARAM_CONSTRUCT)
305     g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
306   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
307     g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
308
309   install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
310
311   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
312     class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
313
314   /* for property overrides of construct poperties, we have to get rid
315    * of the overidden inherited construct property
316    */
317   pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
318   if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
319     class->construct_properties = g_slist_remove (class->construct_properties, pspec);
320 }
321
322 void
323 g_object_interface_install_property (gpointer      g_iface,
324                                      GParamSpec   *pspec)
325 {
326   GTypeInterface *iface_class = g_iface;
327         
328   g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
329   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
330   g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
331   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
332                     
333   install_property_internal (iface_class->g_type, 0, pspec);
334 }
335
336 GParamSpec*
337 g_object_class_find_property (GObjectClass *class,
338                               const gchar  *property_name)
339 {
340   GParamSpec *pspec;
341   GParamSpec *redirect;
342         
343   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
344   g_return_val_if_fail (property_name != NULL, NULL);
345   
346   pspec = g_param_spec_pool_lookup (pspec_pool,
347                                     property_name,
348                                     G_OBJECT_CLASS_TYPE (class),
349                                     TRUE);
350   if (pspec)
351     {
352       redirect = g_param_spec_get_redirect_target (pspec);
353       if (redirect)
354         return redirect;
355       else
356         return pspec;
357     }
358   else
359     return NULL;
360 }
361
362 GParamSpec*
363 g_object_interface_find_property (gpointer      g_iface,
364                                   const gchar  *property_name)
365 {
366   GTypeInterface *iface_class = g_iface;
367         
368   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
369   g_return_val_if_fail (property_name != NULL, NULL);
370   
371   return g_param_spec_pool_lookup (pspec_pool,
372                                    property_name,
373                                    iface_class->g_type,
374                                    FALSE);
375 }
376
377 void
378 g_object_class_override_property (GObjectClass *oclass,
379                                   guint         property_id,
380                                   const gchar  *name)
381 {
382   GParamSpec *overridden = NULL;
383   GParamSpec *new;
384   GType parent_type;
385   
386   g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
387   g_return_if_fail (property_id > 0);
388   g_return_if_fail (name != NULL);
389
390   /* Find the overridden property; first check parent types
391    */
392   parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
393   if (parent_type != G_TYPE_NONE)
394     overridden = g_param_spec_pool_lookup (pspec_pool,
395                                            name,
396                                            parent_type,
397                                            TRUE);
398   if (!overridden)
399     {
400       GType *ifaces;
401       guint n_ifaces;
402       
403       /* Now check interfaces
404        */
405       ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
406       while (n_ifaces-- && !overridden)
407         {
408           overridden = g_param_spec_pool_lookup (pspec_pool,
409                                                  name,
410                                                  ifaces[n_ifaces],
411                                                  FALSE);
412         }
413       
414       g_free (ifaces);
415     }
416
417   if (!overridden)
418     {
419       g_warning ("%s: Can't find property to override for '%s::%s'",
420                  G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
421       return;
422     }
423
424   new = g_param_spec_override (name, overridden);
425   g_object_class_install_property (oclass, property_id, new);
426 }
427
428 GParamSpec** /* free result */
429 g_object_class_list_properties (GObjectClass *class,
430                                 guint        *n_properties_p)
431 {
432   GParamSpec **pspecs;
433   guint n;
434
435   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
436
437   pspecs = g_param_spec_pool_list (pspec_pool,
438                                    G_OBJECT_CLASS_TYPE (class),
439                                    &n);
440   if (n_properties_p)
441     *n_properties_p = n;
442
443   return pspecs;
444 }
445
446 GParamSpec** /* free result */
447 g_object_interface_list_properties (gpointer      g_iface,
448                                     guint        *n_properties_p)
449 {
450   GTypeInterface *iface_class = g_iface;
451   GParamSpec **pspecs;
452   guint n;
453
454   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
455
456   pspecs = g_param_spec_pool_list (pspec_pool,
457                                    iface_class->g_type,
458                                    &n);
459   if (n_properties_p)
460     *n_properties_p = n;
461
462   return pspecs;
463 }
464
465 static void
466 g_object_init (GObject *object)
467 {
468   object->ref_count = 1;
469   g_datalist_init (&object->qdata);
470   
471   /* freeze object's notification queue, g_object_newv() preserves pairedness */
472   g_object_notify_queue_freeze (object, &property_notify_context);
473
474   /* allow construct-only properties to be set */
475   G_LOCK (construct_objects_lock);
476   construct_objects = g_slist_prepend (construct_objects, object);
477   G_UNLOCK (construct_objects_lock);
478   
479 #ifdef  G_ENABLE_DEBUG
480   IF_DEBUG (OBJECTS)
481     {
482       G_LOCK (debug_objects);
483       debug_objects_count++;
484       g_hash_table_insert (debug_objects_ht, object, object);
485       G_UNLOCK (debug_objects);
486     }
487 #endif  /* G_ENABLE_DEBUG */
488 }
489
490 static void
491 g_object_do_set_property (GObject      *object,
492                           guint         property_id,
493                           const GValue *value,
494                           GParamSpec   *pspec)
495 {
496   switch (property_id)
497     {
498     default:
499       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
500       break;
501     }
502 }
503
504 static void
505 g_object_do_get_property (GObject     *object,
506                           guint        property_id,
507                           GValue      *value,
508                           GParamSpec  *pspec)
509 {
510   switch (property_id)
511     {
512     default:
513       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
514       break;
515     }
516 }
517
518 static void
519 g_object_real_dispose (GObject *object)
520 {
521   guint ref_count;
522
523   g_signal_handlers_destroy (object);
524   g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
525
526   /* yes, temporarily altering the ref_count is hackish, but that
527    * enforces people not jerking around with weak_ref notifiers
528    */
529   ref_count = object->ref_count;
530   object->ref_count = 0;
531   g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
532   object->ref_count = ref_count;
533 }
534
535 static void
536 g_object_finalize (GObject *object)
537 {
538   g_datalist_clear (&object->qdata);
539   
540 #ifdef  G_ENABLE_DEBUG
541   IF_DEBUG (OBJECTS)
542     {
543       G_LOCK (debug_objects);
544       g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
545       g_hash_table_remove (debug_objects_ht, object);
546       debug_objects_count--;
547       G_UNLOCK (debug_objects);
548     }
549 #endif  /* G_ENABLE_DEBUG */
550 }
551
552 static void
553 g_object_last_unref (GObject *object)
554 {
555   g_return_if_fail (object->ref_count > 0);
556   
557   if (object->ref_count == 1)   /* may have been re-referenced meanwhile */
558     G_OBJECT_GET_CLASS (object)->dispose (object);
559   
560 #ifdef  G_ENABLE_DEBUG
561   if (g_trap_object_ref == object)
562     G_BREAKPOINT ();
563 #endif  /* G_ENABLE_DEBUG */
564
565   object->ref_count -= 1;
566   
567   if (object->ref_count == 0)   /* may have been re-referenced meanwhile */
568     {
569       g_signal_handlers_destroy (object);
570       g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
571       G_OBJECT_GET_CLASS (object)->finalize (object);
572 #ifdef  G_ENABLE_DEBUG
573       IF_DEBUG (OBJECTS)
574         {
575           /* catch objects not chaining finalize handlers */
576           G_LOCK (debug_objects);
577           g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
578           G_UNLOCK (debug_objects);
579         }
580 #endif  /* G_ENABLE_DEBUG */
581       g_type_free_instance ((GTypeInstance*) object);
582     }
583 }
584
585 static void
586 g_object_dispatch_properties_changed (GObject     *object,
587                                       guint        n_pspecs,
588                                       GParamSpec **pspecs)
589 {
590   guint i;
591
592   for (i = 0; i < n_pspecs; i++)
593     g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
594 }
595
596 void
597 g_object_run_dispose (GObject *object)
598 {
599   g_return_if_fail (G_IS_OBJECT (object));
600   g_return_if_fail (object->ref_count > 0);
601
602   g_object_ref (object);
603   G_OBJECT_GET_CLASS (object)->dispose (object);
604   g_object_unref (object);
605 }
606
607 void
608 g_object_freeze_notify (GObject *object)
609 {
610   g_return_if_fail (G_IS_OBJECT (object));
611   if (!object->ref_count)
612     return;
613
614   g_object_ref (object);
615   g_object_notify_queue_freeze (object, &property_notify_context);
616   g_object_unref (object);
617 }
618
619 void
620 g_object_notify (GObject     *object,
621                  const gchar *property_name)
622 {
623   GParamSpec *pspec;
624   
625   g_return_if_fail (G_IS_OBJECT (object));
626   g_return_if_fail (property_name != NULL);
627   if (!object->ref_count)
628     return;
629   
630   g_object_ref (object);
631   /* We don't need to get the redirect target
632    * (by, e.g. calling g_object_class_find_property())
633    * because g_object_notify_queue_add() does that
634    */
635   pspec = g_param_spec_pool_lookup (pspec_pool,
636                                     property_name,
637                                     G_OBJECT_TYPE (object),
638                                     TRUE);
639
640   if (!pspec)
641     g_warning ("%s: object class `%s' has no property named `%s'",
642                G_STRFUNC,
643                G_OBJECT_TYPE_NAME (object),
644                property_name);
645   else
646     {
647       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
648
649       g_object_notify_queue_add (object, nqueue, pspec);
650       g_object_notify_queue_thaw (object, nqueue);
651     }
652   g_object_unref (object);
653 }
654
655 void
656 g_object_thaw_notify (GObject *object)
657 {
658   GObjectNotifyQueue *nqueue;
659   
660   g_return_if_fail (G_IS_OBJECT (object));
661   if (!object->ref_count)
662     return;
663   
664   g_object_ref (object);
665   nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
666   if (!nqueue || !nqueue->freeze_count)
667     g_warning ("%s: property-changed notification for %s(%p) is not frozen",
668                G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
669   else
670     g_object_notify_queue_thaw (object, nqueue);
671   g_object_unref (object);
672 }
673
674 static inline void
675 object_get_property (GObject     *object,
676                      GParamSpec  *pspec,
677                      GValue      *value)
678 {
679   GObjectClass *class = g_type_class_peek (pspec->owner_type);
680   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
681   GParamSpec *redirect;
682
683   redirect = g_param_spec_get_redirect_target (pspec);
684   if (redirect)
685     pspec = redirect;    
686   
687   class->get_property (object, param_id, value, pspec);
688 }
689
690 static inline void
691 object_set_property (GObject             *object,
692                      GParamSpec          *pspec,
693                      const GValue        *value,
694                      GObjectNotifyQueue  *nqueue)
695 {
696   GValue tmp_value = { 0, };
697   GObjectClass *class = g_type_class_peek (pspec->owner_type);
698   guint param_id = PARAM_SPEC_PARAM_ID (pspec);
699   GParamSpec *redirect;
700
701   redirect = g_param_spec_get_redirect_target (pspec);
702   if (redirect)
703     pspec = redirect;
704
705   /* provide a copy to work from, convert (if necessary) and validate */
706   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
707   if (!g_value_transform (value, &tmp_value))
708     g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
709                pspec->name,
710                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
711                G_VALUE_TYPE_NAME (value));
712   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
713     {
714       gchar *contents = g_strdup_value_contents (value);
715
716       g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
717                  contents,
718                  G_VALUE_TYPE_NAME (value),
719                  pspec->name,
720                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
721       g_free (contents);
722     }
723   else
724     {
725       class->set_property (object, param_id, &tmp_value, pspec);
726       g_object_notify_queue_add (object, nqueue, pspec);
727     }
728   g_value_unset (&tmp_value);
729 }
730
731 static void
732 object_interface_check_properties (gpointer func_data,
733                                    gpointer g_iface)
734 {
735   GTypeInterface *iface_class = g_iface;
736   GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
737   GType iface_type = iface_class->g_type;
738   GParamSpec **pspecs;
739   guint n;
740
741   if (!G_IS_OBJECT_CLASS (class))
742     return;
743
744   pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
745
746   while (n--)
747     {
748       GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
749                                                           pspecs[n]->name,
750                                                           G_OBJECT_CLASS_TYPE (class),
751                                                           TRUE);
752       
753       if (!class_pspec)
754         {
755           g_critical ("Object class %s doesn't implement property "
756                       "'%s' from interface '%s'",
757                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
758                       pspecs[n]->name,
759                       g_type_name (iface_type));
760
761           continue;
762         }
763
764       /* The implementation paramspec must have a less restrictive
765        * type than the interface parameter spec for set() and a
766        * more restrictive type for get(). We just require equality,
767        * rather than doing something more complicated checking
768        * the READABLE and WRITABLE flags. We also simplify here
769        * by only checking the value type, not the G_PARAM_SPEC_TYPE.
770        */
771       if (class_pspec &&
772           !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
773                         G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
774         {
775           g_critical ("Property '%s' on class '%s' has type '%s' "
776                       "which is different from the type '%s', "
777                       "of the property on interface '%s'\n",
778                       pspecs[n]->name,
779                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
780                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
781                       g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
782                       g_type_name (iface_type));
783         }
784       
785 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
786       
787       /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
788        * READABLE and WRITABLE remove restrictions. The implementation
789        * paramspec must have less restrictive flags.
790        */
791       if (class_pspec &&
792           (!SUBSET (class_pspec->flags,
793                     pspecs[n]->flags,
794                     G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
795            !SUBSET (pspecs[n]->flags,
796                     class_pspec->flags,
797                     G_PARAM_READABLE | G_PARAM_WRITABLE)))
798         {
799           g_critical ("Flags for property '%s' on class '%s' "
800                       "are not compatible with the property on"
801                       "interface '%s'\n",
802                       pspecs[n]->name,
803                       g_type_name (G_OBJECT_CLASS_TYPE (class)),
804                       g_type_name (iface_type));
805         }
806 #undef SUBSET     
807     }
808   
809   g_free (pspecs);
810 }
811
812 gpointer
813 g_object_new (GType        object_type,
814               const gchar *first_property_name,
815               ...)
816 {
817   GObject *object;
818   va_list var_args;
819   
820   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
821   
822   va_start (var_args, first_property_name);
823   object = g_object_new_valist (object_type, first_property_name, var_args);
824   va_end (var_args);
825   
826   return object;
827 }
828
829 static gboolean
830 object_in_construction (GObject *object)
831 {
832   gboolean in_construction;
833   G_LOCK (construct_objects_lock);
834   in_construction = g_slist_find (construct_objects, object) != NULL;
835   G_UNLOCK (construct_objects_lock);
836   return in_construction;
837 }
838
839 gpointer
840 g_object_newv (GType       object_type,
841                guint       n_parameters,
842                GParameter *parameters)
843 {
844   GObjectConstructParam *cparams, *oparams;
845   GObjectNotifyQueue *nqueue;
846   GObject *object;
847   GObjectClass *class, *unref_class = NULL;
848   GSList *slist;
849   guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
850   GValue *cvalues;
851   GList *clist = NULL;
852   guint i;
853
854   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
855
856   class = g_type_class_peek_static (object_type);
857   if (!class)
858     class = unref_class = g_type_class_ref (object_type);
859   for (slist = class->construct_properties; slist; slist = slist->next)
860     {
861       clist = g_list_prepend (clist, slist->data);
862       n_total_cparams += 1;
863     }
864
865   /* collect parameters, sort into construction and normal ones */
866   oparams = g_new (GObjectConstructParam, n_parameters);
867   cparams = g_new (GObjectConstructParam, n_total_cparams);
868   for (i = 0; i < n_parameters; i++)
869     {
870       GValue *value = &parameters[i].value;
871       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
872                                                     parameters[i].name,
873                                                     object_type,
874                                                     TRUE);
875       if (!pspec)
876         {
877           g_warning ("%s: object class `%s' has no property named `%s'",
878                      G_STRFUNC,
879                      g_type_name (object_type),
880                      parameters[i].name);
881           continue;
882         }
883       if (!(pspec->flags & G_PARAM_WRITABLE))
884         {
885           g_warning ("%s: property `%s' of object class `%s' is not writable",
886                      G_STRFUNC,
887                      pspec->name,
888                      g_type_name (object_type));
889           continue;
890         }
891       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
892         {
893           GList *list = g_list_find (clist, pspec);
894
895           if (!list)
896             {
897               g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
898                          G_STRFUNC, pspec->name, g_type_name (object_type));
899               continue;
900             }
901           cparams[n_cparams].pspec = pspec;
902           cparams[n_cparams].value = value;
903           n_cparams++;
904           if (!list->prev)
905             clist = list->next;
906           else
907             list->prev->next = list->next;
908           if (list->next)
909             list->next->prev = list->prev;
910           g_list_free_1 (list);
911         }
912       else
913         {
914           oparams[n_oparams].pspec = pspec;
915           oparams[n_oparams].value = value;
916           n_oparams++;
917         }
918     }
919
920   /* set remaining construction properties to default values */
921   n_cvalues = n_total_cparams - n_cparams;
922   cvalues = g_new (GValue, n_cvalues);
923   while (clist)
924     {
925       GList *tmp = clist->next;
926       GParamSpec *pspec = clist->data;
927       GValue *value = cvalues + n_total_cparams - n_cparams - 1;
928
929       value->g_type = 0;
930       g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
931       g_param_value_set_default (pspec, value);
932
933       cparams[n_cparams].pspec = pspec;
934       cparams[n_cparams].value = value;
935       n_cparams++;
936
937       g_list_free_1 (clist);
938       clist = tmp;
939     }
940
941   /* construct object from construction parameters */
942   object = class->constructor (object_type, n_total_cparams, cparams);
943   G_LOCK (construct_objects_lock);
944   construct_objects = g_slist_remove (construct_objects, object);
945   G_UNLOCK (construct_objects_lock);
946
947   /* free construction values */
948   g_free (cparams);
949   while (n_cvalues--)
950     g_value_unset (cvalues + n_cvalues);
951   g_free (cvalues);
952   
953   /* release g_object_init() notification queue freeze_count */
954   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
955   g_object_notify_queue_thaw (object, nqueue);
956   
957   /* set remaining properties */
958   for (i = 0; i < n_oparams; i++)
959     object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
960   g_free (oparams);
961
962   if (unref_class)
963     g_type_class_unref (unref_class);
964
965   /* release our own freeze count and handle notifications */
966   g_object_notify_queue_thaw (object, nqueue);
967   
968   return object;
969 }
970
971 GObject*
972 g_object_new_valist (GType        object_type,
973                      const gchar *first_property_name,
974                      va_list      var_args)
975 {
976   GObjectClass *class;
977   GParameter *params;
978   const gchar *name;
979   GObject *object;
980   guint n_params = 0, n_alloced_params = 16;
981   
982   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
983
984   if (!first_property_name)
985     return g_object_newv (object_type, 0, NULL);
986
987   class = g_type_class_ref (object_type);
988
989   params = g_new (GParameter, n_alloced_params);
990   name = first_property_name;
991   while (name)
992     {
993       gchar *error = NULL;
994       GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
995                                                     name,
996                                                     object_type,
997                                                     TRUE);
998       if (!pspec)
999         {
1000           g_warning ("%s: object class `%s' has no property named `%s'",
1001                      G_STRFUNC,
1002                      g_type_name (object_type),
1003                      name);
1004           break;
1005         }
1006       if (n_params >= n_alloced_params)
1007         {
1008           n_alloced_params += 16;
1009           params = g_renew (GParameter, params, n_alloced_params);
1010         }
1011       params[n_params].name = name;
1012       params[n_params].value.g_type = 0;
1013       g_value_init (&params[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1014       G_VALUE_COLLECT (&params[n_params].value, var_args, 0, &error);
1015       if (error)
1016         {
1017           g_warning ("%s: %s", G_STRFUNC, error);
1018           g_free (error);
1019           g_value_unset (&params[n_params].value);
1020           break;
1021         }
1022       n_params++;
1023       name = va_arg (var_args, gchar*);
1024     }
1025
1026   object = g_object_newv (object_type, n_params, params);
1027
1028   while (n_params--)
1029     g_value_unset (&params[n_params].value);
1030   g_free (params);
1031
1032   g_type_class_unref (class);
1033
1034   return object;
1035 }
1036
1037 static GObject*
1038 g_object_constructor (GType                  type,
1039                       guint                  n_construct_properties,
1040                       GObjectConstructParam *construct_params)
1041 {
1042   GObject *object;
1043
1044   /* create object */
1045   object = (GObject*) g_type_create_instance (type);
1046   
1047   /* set construction parameters */
1048   if (n_construct_properties)
1049     {
1050       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1051       
1052       /* set construct properties */
1053       while (n_construct_properties--)
1054         {
1055           GValue *value = construct_params->value;
1056           GParamSpec *pspec = construct_params->pspec;
1057
1058           construct_params++;
1059           object_set_property (object, pspec, value, nqueue);
1060         }
1061       g_object_notify_queue_thaw (object, nqueue);
1062       /* the notification queue is still frozen from g_object_init(), so
1063        * we don't need to handle it here, g_object_newv() takes
1064        * care of that
1065        */
1066     }
1067
1068   return object;
1069 }
1070
1071 void
1072 g_object_set_valist (GObject     *object,
1073                      const gchar *first_property_name,
1074                      va_list      var_args)
1075 {
1076   GObjectNotifyQueue *nqueue;
1077   const gchar *name;
1078   
1079   g_return_if_fail (G_IS_OBJECT (object));
1080   
1081   g_object_ref (object);
1082   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1083   
1084   name = first_property_name;
1085   while (name)
1086     {
1087       GValue value = { 0, };
1088       GParamSpec *pspec;
1089       gchar *error = NULL;
1090       
1091       pspec = g_param_spec_pool_lookup (pspec_pool,
1092                                         name,
1093                                         G_OBJECT_TYPE (object),
1094                                         TRUE);
1095       if (!pspec)
1096         {
1097           g_warning ("%s: object class `%s' has no property named `%s'",
1098                      G_STRFUNC,
1099                      G_OBJECT_TYPE_NAME (object),
1100                      name);
1101           break;
1102         }
1103       if (!(pspec->flags & G_PARAM_WRITABLE))
1104         {
1105           g_warning ("%s: property `%s' of object class `%s' is not writable",
1106                      G_STRFUNC,
1107                      pspec->name,
1108                      G_OBJECT_TYPE_NAME (object));
1109           break;
1110         }
1111       if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1112         {
1113           g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1114                      G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1115           break;
1116         }
1117
1118       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1119       
1120       G_VALUE_COLLECT (&value, var_args, 0, &error);
1121       if (error)
1122         {
1123           g_warning ("%s: %s", G_STRFUNC, error);
1124           g_free (error);
1125           g_value_unset (&value);
1126           break;
1127         }
1128       
1129       object_set_property (object, pspec, &value, nqueue);
1130       g_value_unset (&value);
1131       
1132       name = va_arg (var_args, gchar*);
1133     }
1134
1135   g_object_notify_queue_thaw (object, nqueue);
1136   g_object_unref (object);
1137 }
1138
1139 void
1140 g_object_get_valist (GObject     *object,
1141                      const gchar *first_property_name,
1142                      va_list      var_args)
1143 {
1144   const gchar *name;
1145   
1146   g_return_if_fail (G_IS_OBJECT (object));
1147   
1148   g_object_ref (object);
1149   
1150   name = first_property_name;
1151   
1152   while (name)
1153     {
1154       GValue value = { 0, };
1155       GParamSpec *pspec;
1156       gchar *error;
1157       
1158       pspec = g_param_spec_pool_lookup (pspec_pool,
1159                                         name,
1160                                         G_OBJECT_TYPE (object),
1161                                         TRUE);
1162       if (!pspec)
1163         {
1164           g_warning ("%s: object class `%s' has no property named `%s'",
1165                      G_STRFUNC,
1166                      G_OBJECT_TYPE_NAME (object),
1167                      name);
1168           break;
1169         }
1170       if (!(pspec->flags & G_PARAM_READABLE))
1171         {
1172           g_warning ("%s: property `%s' of object class `%s' is not readable",
1173                      G_STRFUNC,
1174                      pspec->name,
1175                      G_OBJECT_TYPE_NAME (object));
1176           break;
1177         }
1178       
1179       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1180       
1181       object_get_property (object, pspec, &value);
1182       
1183       G_VALUE_LCOPY (&value, var_args, 0, &error);
1184       if (error)
1185         {
1186           g_warning ("%s: %s", G_STRFUNC, error);
1187           g_free (error);
1188           g_value_unset (&value);
1189           break;
1190         }
1191       
1192       g_value_unset (&value);
1193       
1194       name = va_arg (var_args, gchar*);
1195     }
1196   
1197   g_object_unref (object);
1198 }
1199
1200 void
1201 g_object_set (gpointer     _object,
1202               const gchar *first_property_name,
1203               ...)
1204 {
1205   GObject *object = _object;
1206   va_list var_args;
1207   
1208   g_return_if_fail (G_IS_OBJECT (object));
1209   
1210   va_start (var_args, first_property_name);
1211   g_object_set_valist (object, first_property_name, var_args);
1212   va_end (var_args);
1213 }
1214
1215 void
1216 g_object_get (gpointer     _object,
1217               const gchar *first_property_name,
1218               ...)
1219 {
1220   GObject *object = _object;
1221   va_list var_args;
1222   
1223   g_return_if_fail (G_IS_OBJECT (object));
1224   
1225   va_start (var_args, first_property_name);
1226   g_object_get_valist (object, first_property_name, var_args);
1227   va_end (var_args);
1228 }
1229
1230 void
1231 g_object_set_property (GObject      *object,
1232                        const gchar  *property_name,
1233                        const GValue *value)
1234 {
1235   GObjectNotifyQueue *nqueue;
1236   GParamSpec *pspec;
1237   
1238   g_return_if_fail (G_IS_OBJECT (object));
1239   g_return_if_fail (property_name != NULL);
1240   g_return_if_fail (G_IS_VALUE (value));
1241   
1242   g_object_ref (object);
1243   nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1244   
1245   pspec = g_param_spec_pool_lookup (pspec_pool,
1246                                     property_name,
1247                                     G_OBJECT_TYPE (object),
1248                                     TRUE);
1249   if (!pspec)
1250     g_warning ("%s: object class `%s' has no property named `%s'",
1251                G_STRFUNC,
1252                G_OBJECT_TYPE_NAME (object),
1253                property_name);
1254   else if (!(pspec->flags & G_PARAM_WRITABLE))
1255     g_warning ("%s: property `%s' of object class `%s' is not writable",
1256                G_STRFUNC,
1257                pspec->name,
1258                G_OBJECT_TYPE_NAME (object));
1259   else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1260     g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1261                G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1262   else
1263     object_set_property (object, pspec, value, nqueue);
1264   
1265   g_object_notify_queue_thaw (object, nqueue);
1266   g_object_unref (object);
1267 }
1268
1269 void
1270 g_object_get_property (GObject     *object,
1271                        const gchar *property_name,
1272                        GValue      *value)
1273 {
1274   GParamSpec *pspec;
1275   
1276   g_return_if_fail (G_IS_OBJECT (object));
1277   g_return_if_fail (property_name != NULL);
1278   g_return_if_fail (G_IS_VALUE (value));
1279   
1280   g_object_ref (object);
1281   
1282   pspec = g_param_spec_pool_lookup (pspec_pool,
1283                                     property_name,
1284                                     G_OBJECT_TYPE (object),
1285                                     TRUE);
1286   if (!pspec)
1287     g_warning ("%s: object class `%s' has no property named `%s'",
1288                G_STRFUNC,
1289                G_OBJECT_TYPE_NAME (object),
1290                property_name);
1291   else if (!(pspec->flags & G_PARAM_READABLE))
1292     g_warning ("%s: property `%s' of object class `%s' is not readable",
1293                G_STRFUNC,
1294                pspec->name,
1295                G_OBJECT_TYPE_NAME (object));
1296   else
1297     {
1298       GValue *prop_value, tmp_value = { 0, };
1299       
1300       /* auto-conversion of the callers value type
1301        */
1302       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1303         {
1304           g_value_reset (value);
1305           prop_value = value;
1306         }
1307       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1308         {
1309           g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1310                      G_STRFUNC, pspec->name,
1311                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1312                      G_VALUE_TYPE_NAME (value));
1313           g_object_unref (object);
1314           return;
1315         }
1316       else
1317         {
1318           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1319           prop_value = &tmp_value;
1320         }
1321       object_get_property (object, pspec, prop_value);
1322       if (prop_value != value)
1323         {
1324           g_value_transform (prop_value, value);
1325           g_value_unset (&tmp_value);
1326         }
1327     }
1328   
1329   g_object_unref (object);
1330 }
1331
1332 gpointer
1333 g_object_connect (gpointer     _object,
1334                   const gchar *signal_spec,
1335                   ...)
1336 {
1337   GObject *object = _object;
1338   va_list var_args;
1339
1340   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1341   g_return_val_if_fail (object->ref_count > 0, object);
1342
1343   va_start (var_args, signal_spec);
1344   while (signal_spec)
1345     {
1346       GCallback callback = va_arg (var_args, GCallback);
1347       gpointer data = va_arg (var_args, gpointer);
1348       gulong sid;
1349
1350       if (strncmp (signal_spec, "signal::", 8) == 0)
1351         sid = g_signal_connect_data (object, signal_spec + 8,
1352                                      callback, data, NULL,
1353                                      0);
1354       else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1355                strncmp (signal_spec, "object-signal::", 15) == 0)
1356         sid = g_signal_connect_object (object, signal_spec + 15,
1357                                        callback, data,
1358                                        0);
1359       else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1360                strncmp (signal_spec, "swapped-signal::", 16) == 0)
1361         sid = g_signal_connect_data (object, signal_spec + 16,
1362                                      callback, data, NULL,
1363                                      G_CONNECT_SWAPPED);
1364       else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1365                strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1366         sid = g_signal_connect_object (object, signal_spec + 23,
1367                                        callback, data,
1368                                        G_CONNECT_SWAPPED);
1369       else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1370                strncmp (signal_spec, "signal-after::", 14) == 0)
1371         sid = g_signal_connect_data (object, signal_spec + 14,
1372                                      callback, data, NULL,
1373                                      G_CONNECT_AFTER);
1374       else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1375                strncmp (signal_spec, "object-signal-after::", 21) == 0)
1376         sid = g_signal_connect_object (object, signal_spec + 21,
1377                                        callback, data,
1378                                        G_CONNECT_AFTER);
1379       else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1380                strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1381         sid = g_signal_connect_data (object, signal_spec + 22,
1382                                      callback, data, NULL,
1383                                      G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1384       else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1385                strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1386         sid = g_signal_connect_object (object, signal_spec + 29,
1387                                        callback, data,
1388                                        G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1389       else
1390         {
1391           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1392           break;
1393         }
1394       signal_spec = va_arg (var_args, gchar*);
1395     }
1396   va_end (var_args);
1397
1398   return object;
1399 }
1400
1401 void
1402 g_object_disconnect (gpointer     _object,
1403                      const gchar *signal_spec,
1404                      ...)
1405 {
1406   GObject *object = _object;
1407   va_list var_args;
1408
1409   g_return_if_fail (G_IS_OBJECT (object));
1410   g_return_if_fail (object->ref_count > 0);
1411
1412   va_start (var_args, signal_spec);
1413   while (signal_spec)
1414     {
1415       GCallback callback = va_arg (var_args, GCallback);
1416       gpointer data = va_arg (var_args, gpointer);
1417       guint sid = 0, detail = 0, mask = 0;
1418
1419       if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1420           strncmp (signal_spec, "any-signal::", 12) == 0)
1421         {
1422           signal_spec += 12;
1423           mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1424         }
1425       else if (strcmp (signal_spec, "any_signal") == 0 ||
1426                strcmp (signal_spec, "any-signal") == 0)
1427         {
1428           signal_spec += 10;
1429           mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1430         }
1431       else
1432         {
1433           g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1434           break;
1435         }
1436
1437       if ((mask & G_SIGNAL_MATCH_ID) &&
1438           !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1439         g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1440       else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1441                                                       sid, detail,
1442                                                       NULL, (gpointer)callback, data))
1443         g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1444       signal_spec = va_arg (var_args, gchar*);
1445     }
1446   va_end (var_args);
1447 }
1448
1449 typedef struct {
1450   GObject *object;
1451   guint n_weak_refs;
1452   struct {
1453     GWeakNotify notify;
1454     gpointer    data;
1455   } weak_refs[1];  /* flexible array */
1456 } WeakRefStack;
1457
1458 static void
1459 weak_refs_notify (gpointer data)
1460 {
1461   WeakRefStack *wstack = data;
1462   guint i;
1463
1464   for (i = 0; i < wstack->n_weak_refs; i++)
1465     wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1466   g_free (wstack);
1467 }
1468
1469 void
1470 g_object_weak_ref (GObject    *object,
1471                    GWeakNotify notify,
1472                    gpointer    data)
1473 {
1474   WeakRefStack *wstack;
1475   guint i;
1476   
1477   g_return_if_fail (G_IS_OBJECT (object));
1478   g_return_if_fail (notify != NULL);
1479   g_return_if_fail (object->ref_count >= 1);
1480
1481   wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1482   if (wstack)
1483     {
1484       i = wstack->n_weak_refs++;
1485       wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1486     }
1487   else
1488     {
1489       wstack = g_renew (WeakRefStack, NULL, 1);
1490       wstack->object = object;
1491       wstack->n_weak_refs = 1;
1492       i = 0;
1493     }
1494   wstack->weak_refs[i].notify = notify;
1495   wstack->weak_refs[i].data = data;
1496   g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1497 }
1498
1499 void
1500 g_object_weak_unref (GObject    *object,
1501                      GWeakNotify notify,
1502                      gpointer    data)
1503 {
1504   WeakRefStack *wstack;
1505   gboolean found_one = FALSE;
1506
1507   g_return_if_fail (G_IS_OBJECT (object));
1508   g_return_if_fail (notify != NULL);
1509
1510   wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1511   if (wstack)
1512     {
1513       guint i;
1514
1515       for (i = 0; i < wstack->n_weak_refs; i++)
1516         if (wstack->weak_refs[i].notify == notify &&
1517             wstack->weak_refs[i].data == data)
1518           {
1519             found_one = TRUE;
1520             wstack->n_weak_refs -= 1;
1521             if (i != wstack->n_weak_refs)
1522               {
1523                 wstack->weak_refs[i].notify = wstack->weak_refs[wstack->n_weak_refs].notify;
1524                 wstack->weak_refs[i].data = wstack->weak_refs[wstack->n_weak_refs].data;
1525               }
1526             break;
1527           }
1528     }
1529   if (!found_one)
1530     g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
1531 }
1532
1533 void
1534 g_object_add_weak_pointer (GObject  *object, 
1535                            gpointer *weak_pointer_location)
1536 {
1537   g_return_if_fail (G_IS_OBJECT (object));
1538   g_return_if_fail (weak_pointer_location != NULL);
1539
1540   g_object_weak_ref (object, 
1541                      (GWeakNotify) g_nullify_pointer, 
1542                      weak_pointer_location);
1543 }
1544
1545 void
1546 g_object_remove_weak_pointer (GObject  *object, 
1547                               gpointer *weak_pointer_location)
1548 {
1549   g_return_if_fail (G_IS_OBJECT (object));
1550   g_return_if_fail (weak_pointer_location != NULL);
1551
1552   g_object_weak_unref (object, 
1553                        (GWeakNotify) g_nullify_pointer, 
1554                        weak_pointer_location);
1555 }
1556
1557 gpointer
1558 g_object_ref (gpointer _object)
1559 {
1560   GObject *object = _object;
1561
1562   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1563   g_return_val_if_fail (object->ref_count > 0, NULL);
1564   
1565 #ifdef  G_ENABLE_DEBUG
1566   if (g_trap_object_ref == object)
1567     G_BREAKPOINT ();
1568 #endif  /* G_ENABLE_DEBUG */
1569
1570   object->ref_count += 1;
1571   
1572   return object;
1573 }
1574
1575 void
1576 g_object_unref (gpointer _object)
1577 {
1578   GObject *object = _object;
1579
1580   g_return_if_fail (G_IS_OBJECT (object));
1581   g_return_if_fail (object->ref_count > 0);
1582   
1583 #ifdef  G_ENABLE_DEBUG
1584   if (g_trap_object_ref == object)
1585     G_BREAKPOINT ();
1586 #endif  /* G_ENABLE_DEBUG */
1587
1588   if (object->ref_count > 1)
1589     object->ref_count -= 1;
1590   else
1591     g_object_last_unref (object);
1592 }
1593
1594 gpointer
1595 g_object_get_qdata (GObject *object,
1596                     GQuark   quark)
1597 {
1598   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1599   
1600   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1601 }
1602
1603 void
1604 g_object_set_qdata (GObject *object,
1605                     GQuark   quark,
1606                     gpointer data)
1607 {
1608   g_return_if_fail (G_IS_OBJECT (object));
1609   g_return_if_fail (quark > 0);
1610   
1611   g_datalist_id_set_data (&object->qdata, quark, data);
1612 }
1613
1614 void
1615 g_object_set_qdata_full (GObject       *object,
1616                          GQuark         quark,
1617                          gpointer       data,
1618                          GDestroyNotify destroy)
1619 {
1620   g_return_if_fail (G_IS_OBJECT (object));
1621   g_return_if_fail (quark > 0);
1622   
1623   g_datalist_id_set_data_full (&object->qdata, quark, data,
1624                                data ? destroy : (GDestroyNotify) NULL);
1625 }
1626
1627 gpointer
1628 g_object_steal_qdata (GObject *object,
1629                       GQuark   quark)
1630 {
1631   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1632   g_return_val_if_fail (quark > 0, NULL);
1633   
1634   return g_datalist_id_remove_no_notify (&object->qdata, quark);
1635 }
1636
1637 gpointer
1638 g_object_get_data (GObject     *object,
1639                    const gchar *key)
1640 {
1641   GQuark quark;
1642
1643   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1644   g_return_val_if_fail (key != NULL, NULL);
1645
1646   quark = g_quark_try_string (key);
1647
1648   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1649 }
1650
1651 void
1652 g_object_set_data (GObject     *object,
1653                    const gchar *key,
1654                    gpointer     data)
1655 {
1656   g_return_if_fail (G_IS_OBJECT (object));
1657   g_return_if_fail (key != NULL);
1658
1659   g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1660 }
1661
1662 void
1663 g_object_set_data_full (GObject       *object,
1664                         const gchar   *key,
1665                         gpointer       data,
1666                         GDestroyNotify destroy)
1667 {
1668   g_return_if_fail (G_IS_OBJECT (object));
1669   g_return_if_fail (key != NULL);
1670
1671   g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1672                                data ? destroy : (GDestroyNotify) NULL);
1673 }
1674
1675 gpointer
1676 g_object_steal_data (GObject     *object,
1677                      const gchar *key)
1678 {
1679   GQuark quark;
1680
1681   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1682   g_return_val_if_fail (key != NULL, NULL);
1683
1684   quark = g_quark_try_string (key);
1685
1686   return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1687 }
1688
1689 static void
1690 g_value_object_init (GValue *value)
1691 {
1692   value->data[0].v_pointer = NULL;
1693 }
1694
1695 static void
1696 g_value_object_free_value (GValue *value)
1697 {
1698   if (value->data[0].v_pointer)
1699     g_object_unref (value->data[0].v_pointer);
1700 }
1701
1702 static void
1703 g_value_object_copy_value (const GValue *src_value,
1704                            GValue       *dest_value)
1705 {
1706   if (src_value->data[0].v_pointer)
1707     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1708   else
1709     dest_value->data[0].v_pointer = NULL;
1710 }
1711
1712 static void
1713 g_value_object_transform_value (const GValue *src_value,
1714                                 GValue       *dest_value)
1715 {
1716   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)))
1717     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1718   else
1719     dest_value->data[0].v_pointer = NULL;
1720 }
1721
1722 static gpointer
1723 g_value_object_peek_pointer (const GValue *value)
1724 {
1725   return value->data[0].v_pointer;
1726 }
1727
1728 static gchar*
1729 g_value_object_collect_value (GValue      *value,
1730                               guint        n_collect_values,
1731                               GTypeCValue *collect_values,
1732                               guint        collect_flags)
1733 {
1734   if (collect_values[0].v_pointer)
1735     {
1736       GObject *object = collect_values[0].v_pointer;
1737       
1738       if (object->g_type_instance.g_class == NULL)
1739         return g_strconcat ("invalid unclassed object pointer for value type `",
1740                             G_VALUE_TYPE_NAME (value),
1741                             "'",
1742                             NULL);
1743       else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1744         return g_strconcat ("invalid object type `",
1745                             G_OBJECT_TYPE_NAME (object),
1746                             "' for value type `",
1747                             G_VALUE_TYPE_NAME (value),
1748                             "'",
1749                             NULL);
1750       /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1751       value->data[0].v_pointer = g_object_ref (object);
1752     }
1753   else
1754     value->data[0].v_pointer = NULL;
1755   
1756   return NULL;
1757 }
1758
1759 static gchar*
1760 g_value_object_lcopy_value (const GValue *value,
1761                             guint        n_collect_values,
1762                             GTypeCValue *collect_values,
1763                             guint        collect_flags)
1764 {
1765   GObject **object_p = collect_values[0].v_pointer;
1766   
1767   if (!object_p)
1768     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1769
1770   if (!value->data[0].v_pointer)
1771     *object_p = NULL;
1772   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1773     *object_p = value->data[0].v_pointer;
1774   else
1775     *object_p = g_object_ref (value->data[0].v_pointer);
1776   
1777   return NULL;
1778 }
1779
1780 void
1781 g_value_set_object (GValue   *value,
1782                     gpointer  v_object)
1783 {
1784   GObject *old;
1785         
1786   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1787
1788   old = value->data[0].v_pointer;
1789   
1790   if (v_object)
1791     {
1792       g_return_if_fail (G_IS_OBJECT (v_object));
1793       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1794
1795       value->data[0].v_pointer = v_object;
1796       g_object_ref (value->data[0].v_pointer);
1797     }
1798   else
1799     value->data[0].v_pointer = NULL;
1800   
1801   if (old)
1802     g_object_unref (old);
1803 }
1804
1805 void
1806 g_value_set_object_take_ownership (GValue  *value,
1807                                    gpointer v_object)
1808 {
1809   g_value_take_object (value, v_object);
1810 }
1811
1812 void
1813 g_value_take_object (GValue  *value,
1814                      gpointer v_object)
1815 {
1816   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1817
1818   if (value->data[0].v_pointer)
1819     {
1820       g_object_unref (value->data[0].v_pointer);
1821       value->data[0].v_pointer = NULL;
1822     }
1823
1824   if (v_object)
1825     {
1826       g_return_if_fail (G_IS_OBJECT (v_object));
1827       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1828
1829       value->data[0].v_pointer = v_object; /* we take over the reference count */
1830     }
1831 }
1832
1833 gpointer
1834 g_value_get_object (const GValue *value)
1835 {
1836   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1837   
1838   return value->data[0].v_pointer;
1839 }
1840
1841 GObject*
1842 g_value_dup_object (const GValue *value)
1843 {
1844   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1845   
1846   return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1847 }
1848
1849 gulong
1850 g_signal_connect_object (gpointer      instance,
1851                          const gchar  *detailed_signal,
1852                          GCallback     c_handler,
1853                          gpointer      gobject,
1854                          GConnectFlags connect_flags)
1855 {
1856   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1857   g_return_val_if_fail (detailed_signal != NULL, 0);
1858   g_return_val_if_fail (c_handler != NULL, 0);
1859
1860   if (gobject)
1861     {
1862       GClosure *closure;
1863
1864       g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1865
1866       closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1867
1868       return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1869     }
1870   else
1871     return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1872 }
1873
1874 typedef struct {
1875   GObject  *object;
1876   guint     n_closures;
1877   GClosure *closures[1]; /* flexible array */
1878 } CArray;
1879 /* don't change this structure without supplying an accessor for
1880  * watched closures, e.g.:
1881  * GSList* g_object_list_watched_closures (GObject *object)
1882  * {
1883  *   CArray *carray;
1884  *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1885  *   carray = g_object_get_data (object, "GObject-closure-array");
1886  *   if (carray)
1887  *     {
1888  *       GSList *slist = NULL;
1889  *       guint i;
1890  *       for (i = 0; i < carray->n_closures; i++)
1891  *         slist = g_slist_prepend (slist, carray->closures[i]);
1892  *       return slist;
1893  *     }
1894  *   return NULL;
1895  * }
1896  */
1897
1898 static void
1899 object_remove_closure (gpointer  data,
1900                        GClosure *closure)
1901 {
1902   GObject *object = data;
1903   CArray *carray = g_object_get_qdata (object, quark_closure_array);
1904   guint i;
1905   
1906   for (i = 0; i < carray->n_closures; i++)
1907     if (carray->closures[i] == closure)
1908       {
1909         carray->n_closures--;
1910         if (i < carray->n_closures)
1911           carray->closures[i] = carray->closures[carray->n_closures];
1912         return;
1913       }
1914   g_assert_not_reached ();
1915 }
1916
1917 static void
1918 destroy_closure_array (gpointer data)
1919 {
1920   CArray *carray = data;
1921   GObject *object = carray->object;
1922   guint i, n = carray->n_closures;
1923   
1924   for (i = 0; i < n; i++)
1925     {
1926       GClosure *closure = carray->closures[i];
1927       
1928       /* removing object_remove_closure() upfront is probably faster than
1929        * letting it fiddle with quark_closure_array which is empty anyways
1930        */
1931       g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1932       g_closure_invalidate (closure);
1933     }
1934   g_free (carray);
1935 }
1936
1937 void
1938 g_object_watch_closure (GObject  *object,
1939                         GClosure *closure)
1940 {
1941   CArray *carray;
1942   guint i;
1943   
1944   g_return_if_fail (G_IS_OBJECT (object));
1945   g_return_if_fail (closure != NULL);
1946   g_return_if_fail (closure->is_invalid == FALSE);
1947   g_return_if_fail (closure->in_marshal == FALSE);
1948   g_return_if_fail (object->ref_count > 0);     /* this doesn't work on finalizing objects */
1949   
1950   g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1951   g_closure_add_marshal_guards (closure,
1952                                 object, (GClosureNotify) g_object_ref,
1953                                 object, (GClosureNotify) g_object_unref);
1954   carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
1955   if (!carray)
1956     {
1957       carray = g_renew (CArray, NULL, 1);
1958       carray->object = object;
1959       carray->n_closures = 1;
1960       i = 0;
1961     }
1962   else
1963     {
1964       i = carray->n_closures++;
1965       carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1966     }
1967   carray->closures[i] = closure;
1968   g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
1969 }
1970
1971 GClosure*
1972 g_closure_new_object (guint    sizeof_closure,
1973                       GObject *object)
1974 {
1975   GClosure *closure;
1976
1977   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1978   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
1979
1980   closure = g_closure_new_simple (sizeof_closure, object);
1981   g_object_watch_closure (object, closure);
1982
1983   return closure;
1984 }
1985
1986 GClosure*
1987 g_cclosure_new_object (GCallback callback_func,
1988                        GObject  *object)
1989 {
1990   GClosure *closure;
1991
1992   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1993   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
1994   g_return_val_if_fail (callback_func != NULL, NULL);
1995
1996   closure = g_cclosure_new (callback_func, object, NULL);
1997   g_object_watch_closure (object, closure);
1998
1999   return closure;
2000 }
2001
2002 GClosure*
2003 g_cclosure_new_object_swap (GCallback callback_func,
2004                             GObject  *object)
2005 {
2006   GClosure *closure;
2007
2008   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2009   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
2010   g_return_val_if_fail (callback_func != NULL, NULL);
2011
2012   closure = g_cclosure_new_swap (callback_func, object, NULL);
2013   g_object_watch_closure (object, closure);
2014
2015   return closure;
2016 }
2017
2018 #define __G_OBJECT_C__
2019 #include "gobjectaliasdef.c"