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