gpointer, not gpointer *.
[platform/upstream/glib.git] / gobject / gobject.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * MT safe
22  */
23
24 #include        "gobject.h"
25
26
27 #include        "gvaluecollector.h"
28 #include        "gsignal.h"
29 #include        "gparamspecs.h"
30 #include        "gvaluetypes.h"
31 #include        <string.h>
32
33
34 #define PREALLOC_CPARAMS        (8)
35
36
37 /* --- macros --- */
38 #define PARAM_SPEC_PARAM_ID(pspec)      (GPOINTER_TO_UINT (g_param_spec_get_qdata ((pspec), quark_property_id)))
39
40
41 /* --- signals --- */
42 enum {
43   PROPERTIES_CHANGED,
44   NOTIFY,
45   LAST_SIGNAL
46 };
47
48
49 /* --- properties --- */
50 enum {
51   PROP_NONE
52 };
53
54
55 /* --- typedefs --- */
56 typedef struct _NotifyQueue NotifyQueue;
57
58
59 /* --- prototypes --- */
60 static void     g_object_base_class_init                (GObjectClass   *class);
61 static void     g_object_base_class_finalize            (GObjectClass   *class);
62 static void     g_object_do_class_init                  (GObjectClass   *class);
63 static void     g_object_init                           (GObject        *object);
64 static GObject* g_object_constructor                    (GType                  type,
65                                                          guint                  n_construct_properties,
66                                                          GObjectConstructParam *construct_params);
67 static void     g_object_last_unref                     (GObject        *object);
68 static void     g_object_shutdown                       (GObject        *object);
69 static void     g_object_finalize                       (GObject        *object);
70 static void     g_object_do_set_property                (GObject        *object,
71                                                          guint           property_id,
72                                                          const GValue   *value,
73                                                          GParamSpec     *pspec);
74 static void     g_object_do_get_property                (GObject        *object,
75                                                          guint           property_id,
76                                                          GValue         *value,
77                                                          GParamSpec     *pspec);
78 static void     g_value_object_init                     (GValue         *value);
79 static void     g_value_object_free_value               (GValue         *value);
80 static void     g_value_object_copy_value               (const GValue   *src_value,
81                                                          GValue         *dest_value);
82 static void     g_value_object_transform_value          (const GValue   *src_value,
83                                                          GValue         *dest_value);
84 static gpointer g_value_object_peek_pointer             (const GValue   *value);
85 static gchar*   g_value_object_collect_value            (GValue         *value,
86                                                          guint           n_collect_values,
87                                                          GTypeCValue    *collect_values,
88                                                          guint           collect_flags);
89 static gchar*   g_value_object_lcopy_value              (const GValue   *value,
90                                                          guint           n_collect_values,
91                                                          GTypeCValue    *collect_values,
92                                                          guint           collect_flags);
93 static void     g_object_dispatch_properties_changed    (GObject        *object,
94                                                          guint           n_pspecs,
95                                                          GParamSpec    **pspecs);
96 static void     g_object_properties_changed             (GObject        *object,
97                                                          guint           n_pspecs,
98                                                          GParamSpec    **pspecs);
99 static void     g_object_notify_property_changed        (GObject        *object,
100                                                          GParamSpec     *pspec);
101 static inline NotifyQueue* object_freeze_notifies       (GObject        *object);
102 static inline void         object_queue_property        (GObject        *object,
103                                                          GParamSpec     *pspec,
104                                                          NotifyQueue    *nqueue);
105 static inline void         object_thaw_notifies         (GObject        *object,
106                                                          NotifyQueue    *nqueue);
107 static inline void         object_get_property          (GObject        *object,
108                                                          GParamSpec     *pspec,
109                                                          GValue         *value);
110 static inline void         object_set_property          (GObject        *object,
111                                                          GParamSpec     *pspec,
112                                                          const GValue   *value,
113                                                          NotifyQueue    *nqueue);
114
115
116 /* --- structures --- */
117 struct _NotifyQueue
118 {
119   GSList *pspecs;
120   guint   n_pspecs;
121   guint   freeze_count;
122 };
123
124
125 /* --- variables --- */
126 static GQuark            quark_notify_queue = 0;
127 static GQuark            quark_property_id = 0;
128 static GQuark            quark_closure_array = 0;
129 static GParamSpecPool   *pspec_pool = NULL;
130 static gulong            gobject_signals[LAST_SIGNAL] = { 0, };
131
132
133 /* --- functions --- */
134 #ifdef  G_ENABLE_DEBUG
135 #define IF_DEBUG(debug_type)    if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
136 G_LOCK_DEFINE_STATIC     (debug_objects);
137 static volatile GObject *g_trap_object_ref = NULL;
138 static guint             debug_objects_count = 0;
139 static GHashTable       *debug_objects_ht = NULL;
140 static void
141 debug_objects_foreach (gpointer key,
142                        gpointer value,
143                        gpointer user_data)
144 {
145   GObject *object = value;
146
147   g_message ("[%p] stale %s\tref_count=%u",
148              object,
149              G_OBJECT_TYPE_NAME (object),
150              object->ref_count);
151 }
152 static void
153 debug_objects_atexit (void)
154 {
155   IF_DEBUG (OBJECTS)
156     {
157       G_LOCK (debug_objects);
158       if (debug_objects_ht)
159         {
160           g_message ("stale GObjects: %u", debug_objects_count);
161           g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
162         }
163       G_UNLOCK (debug_objects);
164     }
165 }
166 #endif  /* G_ENABLE_DEBUG */
167
168 void
169 g_object_type_init (void)       /* sync with gtype.c */
170 {
171   static gboolean initialized = FALSE;
172   static const GTypeFundamentalInfo finfo = {
173     G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
174   };
175   static GTypeInfo info = {
176     sizeof (GObjectClass),
177     (GBaseInitFunc) g_object_base_class_init,
178     (GBaseFinalizeFunc) g_object_base_class_finalize,
179     (GClassInitFunc) g_object_do_class_init,
180     NULL        /* class_destroy */,
181     NULL        /* class_data */,
182     sizeof (GObject),
183     0           /* n_preallocs */,
184     (GInstanceInitFunc) g_object_init,
185     NULL,       /* value_table */
186   };
187   static const GTypeValueTable value_table = {
188     g_value_object_init,          /* value_init */
189     g_value_object_free_value,    /* value_free */
190     g_value_object_copy_value,    /* value_copy */
191     g_value_object_peek_pointer,  /* value_peek_pointer */
192     "p",                          /* collect_format */
193     g_value_object_collect_value, /* collect_value */
194     "p",                          /* lcopy_format */
195     g_value_object_lcopy_value,   /* lcopy_value */
196   };
197   GType type;
198   
199   g_return_if_fail (initialized == FALSE);
200   initialized = TRUE;
201   
202   /* G_TYPE_OBJECT
203    */
204   info.value_table = &value_table;
205   type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
206   g_assert (type == G_TYPE_OBJECT);
207   g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
208   
209 #ifdef  G_ENABLE_DEBUG
210   IF_DEBUG (OBJECTS)
211     g_atexit (debug_objects_atexit);
212 #endif  /* G_ENABLE_DEBUG */
213 }
214
215 static void
216 g_object_base_class_init (GObjectClass *class)
217 {
218   GObjectClass *pclass = g_type_class_peek_parent (class);
219
220   /* reset instance specific fields and methods that don't get inherited */
221   class->n_property_specs = 0;
222   class->property_specs = NULL;
223   class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
224   class->get_property = NULL;
225   class->set_property = NULL;
226 }
227
228 static void
229 g_object_base_class_finalize (GObjectClass *class)
230 {
231   guint i;
232   
233   g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
234
235   _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
236
237   g_slist_free (class->construct_properties);
238   class->construct_properties = NULL;
239   for (i = 0; i < class->n_property_specs; i++)
240     {
241       GParamSpec *pspec = class->property_specs[i];
242       
243       g_param_spec_pool_remove (pspec_pool, pspec);
244       g_param_spec_set_qdata (pspec, quark_property_id, NULL);
245       g_param_spec_unref (pspec);
246     }
247   class->n_property_specs = 0;
248   g_free (class->property_specs);
249   class->property_specs = NULL;
250 }
251
252 static void
253 g_object_do_class_init (GObjectClass *class)
254 {
255   quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
256   quark_property_id = g_quark_from_static_string ("GObject-property-id");
257   quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
258   pspec_pool = g_param_spec_pool_new (TRUE);
259   
260   class->constructor = g_object_constructor;
261   class->set_property = g_object_do_set_property;
262   class->get_property = g_object_do_get_property;
263   class->shutdown = g_object_shutdown;
264   class->finalize = g_object_finalize;
265   class->dispatch_properties_changed = g_object_dispatch_properties_changed;
266   class->properties_changed = g_object_properties_changed;
267   class->notify = g_object_notify_property_changed;
268
269   gobject_signals[PROPERTIES_CHANGED] =
270     g_signal_newc ("properties_changed",
271                    G_TYPE_FROM_CLASS (class),
272                    G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
273                    G_STRUCT_OFFSET (GObjectClass, properties_changed),
274                    NULL, NULL,
275                    g_cclosure_marshal_VOID__UINT_POINTER,
276                    G_TYPE_NONE,
277                    2, G_TYPE_UINT, G_TYPE_POINTER);
278   gobject_signals[NOTIFY] =
279     g_signal_newc ("notify",
280                    G_TYPE_FROM_CLASS (class),
281                    G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
282                    G_STRUCT_OFFSET (GObjectClass, notify),
283                    NULL, NULL,
284                    g_cclosure_marshal_VOID__PARAM,
285                    G_TYPE_NONE,
286                    1, G_TYPE_PARAM);
287 }
288
289 void
290 g_object_class_install_property (GObjectClass *class,
291                                  guint         property_id,
292                                  GParamSpec   *pspec)
293 {
294   guint i;
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   /* expensive paranoia checks ;( */
310   for (i = 0; i < class->n_property_specs; i++)
311     if (PARAM_SPEC_PARAM_ID (class->property_specs[i]) == property_id)
312       {
313         g_warning (G_STRLOC ": class `%s' already contains a property `%s' with id %u, "
314                    "cannot install property `%s'",
315                    G_OBJECT_CLASS_NAME (class),
316                    class->property_specs[i]->name,
317                    property_id,
318                    pspec->name);
319         return;
320       }
321   if (g_param_spec_pool_lookup (pspec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
322     {
323       g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
324                  G_OBJECT_CLASS_NAME (class),
325                  pspec->name);
326       return;
327     }
328
329   g_param_spec_ref (pspec);
330   g_param_spec_sink (pspec);
331   g_param_spec_set_qdata (pspec, quark_property_id, GUINT_TO_POINTER (property_id));
332   g_param_spec_pool_insert (pspec_pool, pspec, G_OBJECT_CLASS_TYPE (class));
333   i = class->n_property_specs++;
334   class->property_specs = g_renew (GParamSpec*, class->property_specs, class->n_property_specs);
335   class->property_specs[i] = pspec;
336   if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
337     class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
338
339   /* for property overrides of construct poperties, we have to get rid
340    * of the overidden inherited construct property
341    */
342   pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
343   if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
344     class->construct_properties = g_slist_remove (class->construct_properties, pspec);
345 }
346
347 GParamSpec*
348 g_object_class_find_property (GObjectClass *class,
349                               const gchar  *property_name)
350 {
351   g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
352   g_return_val_if_fail (property_name != NULL, NULL);
353   
354   return g_param_spec_pool_lookup (pspec_pool,
355                                    property_name,
356                                    G_OBJECT_CLASS_TYPE (class),
357                                    TRUE);
358 }
359
360 static void
361 free_notify_queue (gpointer data)
362 {
363   NotifyQueue *nqueue = data;
364   
365   g_slist_free (nqueue->pspecs);
366   g_free (nqueue);
367 }
368
369 static inline NotifyQueue*
370 object_freeze_notifies (GObject *object)
371 {
372   NotifyQueue *nqueue;
373
374   nqueue = g_object_get_qdata (object, quark_notify_queue);
375   if (!nqueue)
376     {
377       nqueue = g_new0 (NotifyQueue, 1);
378       g_object_set_qdata_full (object, quark_notify_queue, nqueue, free_notify_queue);
379     }
380   nqueue->freeze_count++;
381
382   return nqueue;
383 }
384
385 static inline void
386 object_queue_property (GObject     *object,
387                        GParamSpec  *pspec,
388                        NotifyQueue *nqueue)
389 {
390   if (pspec->flags & G_PARAM_READABLE)
391     {
392       /* we will dedup later */
393       nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
394       nqueue->n_pspecs++;
395     }
396 }
397
398 static void
399 g_object_init (GObject *object)
400 {
401   object->ref_count = 1;
402   g_datalist_init (&object->qdata);
403   
404   /* freeze object's notification queue, g_object_new_valist() takes care of that */
405   object_freeze_notifies (object);
406   
407 #ifdef  G_ENABLE_DEBUG
408   IF_DEBUG (OBJECTS)
409     {
410       G_LOCK (debug_objects);
411       if (!debug_objects_ht)
412         debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
413       debug_objects_count++;
414       g_hash_table_insert (debug_objects_ht, object, object);
415       G_UNLOCK (debug_objects);
416     }
417 #endif  /* G_ENABLE_DEBUG */
418 }
419
420 static void
421 g_object_do_set_property (GObject      *object,
422                           guint         property_id,
423                           const GValue *value,
424                           GParamSpec   *pspec)
425 {
426   switch (property_id)
427     {
428     default:
429       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
430       break;
431     }
432 }
433
434 static void
435 g_object_do_get_property (GObject     *object,
436                           guint        property_id,
437                           GValue      *value,
438                           GParamSpec  *pspec)
439 {
440   switch (property_id)
441     {
442     default:
443       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
444       break;
445     }
446 }
447
448 static void
449 g_object_last_unref (GObject *object)
450 {
451   g_return_if_fail (object->ref_count > 0);
452   
453   if (object->ref_count == 1)   /* may have been re-referenced meanwhile */
454     G_OBJECT_GET_CLASS (object)->shutdown (object);
455   
456 #ifdef  G_ENABLE_DEBUG
457   if (g_trap_object_ref == object)
458     G_BREAKPOINT ();
459 #endif  /* G_ENABLE_DEBUG */
460
461   object->ref_count -= 1;
462   
463   if (object->ref_count == 0)   /* may have been re-referenced meanwhile */
464     {
465       g_signal_handlers_destroy (object);
466       g_object_set_qdata (object, quark_closure_array, NULL);
467       G_OBJECT_GET_CLASS (object)->finalize (object);
468 #ifdef  G_ENABLE_DEBUG
469       IF_DEBUG (OBJECTS)
470         {
471           G_LOCK (debug_objects);
472           if (debug_objects_ht)
473             g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
474           G_UNLOCK (debug_objects);
475         }
476 #endif  /* G_ENABLE_DEBUG */
477       g_type_free_instance ((GTypeInstance*) object);
478     }
479 }
480
481 static void
482 g_object_shutdown (GObject *object)
483 {
484   /* this function needs to be always present for unconditional
485    * chaining, we also might add some code here later.
486    * beware though, subclasses may invoke shutdown() arbitrarily.
487    */
488 }
489
490 static void
491 g_object_finalize (GObject *object)
492 {
493   g_signal_handlers_destroy (object);
494   g_datalist_clear (&object->qdata);
495   
496 #ifdef  G_ENABLE_DEBUG
497   IF_DEBUG (OBJECTS)
498     {
499       G_LOCK (debug_objects);
500       g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
501       g_hash_table_remove (debug_objects_ht, object);
502       debug_objects_count--;
503       G_UNLOCK (debug_objects);
504     }
505 #endif  /* G_ENABLE_DEBUG */
506 }
507
508 static inline void
509 object_thaw_notifies (GObject     *object,
510                       NotifyQueue *nqueue)
511 {
512   GParamSpec **pspecs;
513   GSList *slist;
514   guint n_pspecs = 0;
515   
516   nqueue->freeze_count--;
517   if (nqueue->freeze_count)
518     return;
519   g_return_if_fail (object->ref_count > 0);
520   
521   pspecs = g_new (GParamSpec*, nqueue->n_pspecs);
522   for (slist = nqueue->pspecs; slist; slist = slist->next)
523     {
524       GParamSpec *pspec = slist->data;
525       gint i = 0;
526       
527       /* dedup, make pspecs in the list unique */
528     redo_dedup_check:
529       if (pspecs[i] == pspec)
530         continue;
531       if (++i < n_pspecs)
532         goto redo_dedup_check;
533       
534       pspecs[n_pspecs++] = pspec;
535     }
536   g_object_set_qdata (object, quark_notify_queue, NULL);
537   
538   if (n_pspecs)
539     G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
540   
541   g_free (pspecs);
542 }
543
544 static void
545 g_object_dispatch_properties_changed (GObject     *object,
546                                       guint        n_pspecs,
547                                       GParamSpec **pspecs)
548 {
549   g_signal_emit (object, gobject_signals[PROPERTIES_CHANGED], 0, n_pspecs, pspecs);
550 }
551
552 static void
553 g_object_properties_changed (GObject     *object,
554                              guint        n_pspecs,
555                              GParamSpec **pspecs)
556 {
557   guint i;
558
559   for (i = 0; i < n_pspecs; i++)
560     g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
561 }
562
563 static void
564 g_object_notify_property_changed (GObject    *object,
565                                   GParamSpec *pspec)
566 {
567   if (0) /* FIXME */
568     g_message ("NOTIFICATION: property `%s' changed on object `%s'",
569                pspec->name,
570                G_OBJECT_TYPE_NAME (object));
571 }
572
573 void
574 g_object_freeze_notify (GObject *object)
575 {
576   g_return_if_fail (G_IS_OBJECT (object));
577   if (!object->ref_count)
578     return;
579
580   g_object_ref (object);
581   object_freeze_notifies (object);
582   g_object_unref (object);
583 }
584
585 void
586 g_object_notify (GObject     *object,
587                  const gchar *property_name)
588 {
589   GParamSpec *pspec;
590   
591   g_return_if_fail (G_IS_OBJECT (object));
592   g_return_if_fail (property_name != NULL);
593   if (!object->ref_count)
594     return;
595   
596   g_object_ref (object);
597   pspec = g_param_spec_pool_lookup (pspec_pool,
598                                     property_name,
599                                     G_OBJECT_TYPE (object),
600                                     TRUE);
601   if (!pspec)
602     g_warning ("%s: object class `%s' has no property named `%s'",
603                G_STRLOC,
604                G_OBJECT_TYPE_NAME (object),
605                property_name);
606   else
607     {
608       NotifyQueue *nqueue = object_freeze_notifies (object);
609
610       object_queue_property (object, pspec, nqueue);
611       object_thaw_notifies (object, nqueue);
612     }
613   g_object_unref (object);
614 }
615
616 void
617 g_object_thaw_notify (GObject *object)
618 {
619   NotifyQueue *nqueue;
620   
621   g_return_if_fail (G_IS_OBJECT (object));
622   if (!object->ref_count)
623     return;
624   
625   g_object_ref (object);
626   nqueue = g_object_get_qdata (object, quark_notify_queue);
627   if (!nqueue || !nqueue->freeze_count)
628     g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
629                G_OBJECT_TYPE_NAME (object), object);
630   else
631     object_thaw_notifies (object, nqueue);
632   g_object_unref (object);
633 }
634
635 static inline void
636 object_get_property (GObject     *object,
637                      GParamSpec  *pspec,
638                      GValue      *value)
639 {
640   GObjectClass *class;
641   
642   class = g_type_class_peek (pspec->owner_type);
643   
644   class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
645 }
646
647 static inline void
648 object_set_property (GObject      *object,
649                      GParamSpec   *pspec,
650                      const GValue *value,
651                      NotifyQueue  *nqueue)
652 {
653   GValue tmp_value = { 0, };
654   GObjectClass *class = g_type_class_peek (pspec->owner_type);
655
656   /* provide a copy to work from, convert (if necessary) and validate */
657   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
658   if (!g_value_transform (value, &tmp_value))
659     g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
660                pspec->name,
661                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
662                G_VALUE_TYPE_NAME (value));
663   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
664     {
665       gchar *contents = g_strdup_value_contents (value);
666
667       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
668                  contents,
669                  G_VALUE_TYPE_NAME (value),
670                  pspec->name,
671                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
672       g_free (contents);
673     }
674   else
675     {
676       class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
677       object_queue_property (object, pspec, nqueue);
678     }
679   g_value_unset (&tmp_value);
680 }
681
682 gpointer
683 g_object_new (GType        object_type,
684               const gchar *first_property_name,
685               ...)
686 {
687   GObject *object;
688   va_list var_args;
689   
690   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
691   
692   va_start (var_args, first_property_name);
693   object = g_object_new_valist (object_type, first_property_name, var_args);
694   va_end (var_args);
695   
696   return object;
697 }
698
699 gpointer
700 g_object_new_valist (GType        object_type,
701                      const gchar *first_property_name,
702                      va_list      var_args)
703 {
704   NotifyQueue *nqueue;
705   GObject *object;
706   GObjectClass *class;
707   const gchar *name;
708   GObjectConstructParam *cparams = NULL, *nparams = NULL;
709   guint n_cparams = 0, n_nparams = 0;
710   GSList *clist;
711   
712   g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
713
714   class = g_type_class_ref (object_type);
715   clist = g_slist_copy (class->construct_properties);
716
717   /* collect parameters, sort into construction and normal ones */
718   name = first_property_name;
719   while (name)
720     {
721       GValue *value;
722       GParamSpec *pspec;
723       gchar *error = NULL;
724       
725       pspec = g_param_spec_pool_lookup (pspec_pool,
726                                         name,
727                                         object_type,
728                                         TRUE);
729       if (!pspec)
730         {
731           g_warning ("%s: object class `%s' has no property named `%s'",
732                      G_STRLOC,
733                      g_type_name (object_type),
734                      name);
735           break;
736         }
737       if (!(pspec->flags & G_PARAM_WRITABLE))
738         {
739           g_warning ("%s: property `%s' of object class `%s' is not writable",
740                      G_STRLOC,
741                      pspec->name,
742                      g_type_name (object_type));
743           break;
744         }
745
746       value = g_new (GValue, 1);
747       value->g_type = 0;
748       g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
749       G_VALUE_COLLECT (value, var_args, 0, &error);
750       if (error)
751         {
752           g_warning ("%s: %s", G_STRLOC, error);
753           g_free (error);
754
755           /* we purposely leak the value here, it might not be
756            * in a sane state if an error condition occoured
757            */
758           break;
759         }
760       if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
761         {
762           guint i;
763
764           if (!n_cparams || n_cparams >= PREALLOC_CPARAMS)
765             cparams = g_renew (GObjectConstructParam, cparams, MAX (n_cparams + 1, PREALLOC_CPARAMS));
766           cparams[n_cparams].pspec = pspec;
767           cparams[n_cparams].value = value;
768           for (i = 0; i < n_cparams; i++)       /* picky, aren't we? ;) */
769             if (cparams[i].pspec == pspec)
770               g_warning (G_STRLOC ": construct property \"%s\" for object `%s' is being set twice",
771                          pspec->name, g_type_name (object_type));
772           n_cparams++;
773           clist = g_slist_remove (clist, pspec);   /* FIXME: unique */
774         }
775       else
776         {
777           if (!n_nparams || n_nparams >= PREALLOC_CPARAMS)
778             nparams = g_renew (GObjectConstructParam, nparams, MAX (n_nparams + 1, PREALLOC_CPARAMS));
779           nparams[n_nparams].pspec = pspec;
780           nparams[n_nparams].value = value;
781           n_nparams++;
782         }
783
784       name = va_arg (var_args, gchar*);
785     }
786
787   /* construct object from construction parameters */
788   while (clist)
789     {
790       GSList *tmp = clist->next;
791       GParamSpec *pspec = clist->data;
792       GValue *value = g_new (GValue, 1);
793
794       value->g_type = 0;
795       g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
796       g_param_value_set_default (pspec, value);
797
798       if (!n_cparams || n_cparams >= PREALLOC_CPARAMS)
799         cparams = g_renew (GObjectConstructParam, cparams, MAX (n_cparams + 1, PREALLOC_CPARAMS));
800       cparams[n_cparams].pspec = pspec;
801       cparams[n_cparams].value = value;
802       n_cparams++;
803
804       g_slist_free_1 (clist);
805       clist = tmp;
806     }
807   object = class->constructor (object_type, n_cparams, cparams);
808
809   /* free construction values */
810   while (n_cparams--)
811     {
812       g_value_unset (cparams[n_cparams].value);
813       g_free (cparams[n_cparams].value);
814     }
815   g_free (cparams);
816   
817   /* release g_object_init() notification queue freeze_count */
818   nqueue = object_freeze_notifies (object);
819   nqueue->freeze_count--;
820   
821   /* set remaining properties */
822   cparams = nparams;
823   while (n_nparams--)
824     {
825       GValue *value = nparams->value;
826       GParamSpec *pspec = nparams->pspec;
827
828       nparams++;
829       object_set_property (object, pspec, value, nqueue);
830       g_value_unset (value);
831       g_free (value);
832     }
833   g_free (cparams);
834
835   g_type_class_unref (class);
836
837   /* release our own freeze count and handle notifications */
838   object_thaw_notifies (object, nqueue);
839   
840   return object;
841 }
842
843 static GObject*
844 g_object_constructor (GType                  type,
845                       guint                  n_construct_properties,
846                       GObjectConstructParam *construct_params)
847 {
848   GObject *object;
849
850   /* create object */
851   object = (GObject*) g_type_create_instance (type);
852
853   /* set construction parameters */
854   if (n_construct_properties)
855     {
856       NotifyQueue *nqueue = object_freeze_notifies (object);
857       
858       /* set construct properties */
859       while (n_construct_properties--)
860         {
861           GValue *value = construct_params->value;
862           GParamSpec *pspec = construct_params->pspec;
863
864           construct_params++;
865           object_set_property (object, pspec, value, nqueue);
866         }
867       nqueue->freeze_count--;
868       /* the notification queue is still frozen from g_object_init(), so
869        * we don't need to handle it here, g_object_new_valist() takes
870        * care of that
871        */
872     }
873
874   return object;
875 }
876
877 void
878 g_object_set_valist (GObject     *object,
879                      const gchar *first_property_name,
880                      va_list      var_args)
881 {
882   NotifyQueue *nqueue;
883   const gchar *name;
884   
885   g_return_if_fail (G_IS_OBJECT (object));
886   
887   g_object_ref (object);
888   nqueue = object_freeze_notifies (object);
889   
890   name = first_property_name;
891   while (name)
892     {
893       GValue value = { 0, };
894       GParamSpec *pspec;
895       gchar *error = NULL;
896       
897       pspec = g_param_spec_pool_lookup (pspec_pool,
898                                         name,
899                                         G_OBJECT_TYPE (object),
900                                         TRUE);
901       if (!pspec)
902         {
903           g_warning ("%s: object class `%s' has no property named `%s'",
904                      G_STRLOC,
905                      G_OBJECT_TYPE_NAME (object),
906                      name);
907           break;
908         }
909       if (!(pspec->flags & G_PARAM_WRITABLE))
910         {
911           g_warning ("%s: property `%s' of object class `%s' is not writable",
912                      G_STRLOC,
913                      pspec->name,
914                      G_OBJECT_TYPE_NAME (object));
915           break;
916         }
917       
918       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
919       
920       G_VALUE_COLLECT (&value, var_args, 0, &error);
921       if (error)
922         {
923           g_warning ("%s: %s", G_STRLOC, error);
924           g_free (error);
925           
926           /* we purposely leak the value here, it might not be
927            * in a sane state if an error condition occoured
928            */
929           break;
930         }
931       
932       object_set_property (object, pspec, &value, nqueue);
933       g_value_unset (&value);
934       
935       name = va_arg (var_args, gchar*);
936     }
937
938   object_thaw_notifies (object, nqueue);
939   g_object_unref (object);
940 }
941
942 void
943 g_object_get_valist (GObject     *object,
944                      const gchar *first_property_name,
945                      va_list      var_args)
946 {
947   const gchar *name;
948   
949   g_return_if_fail (G_IS_OBJECT (object));
950   
951   g_object_ref (object);
952   
953   name = first_property_name;
954   
955   while (name)
956     {
957       GValue value = { 0, };
958       GParamSpec *pspec;
959       gchar *error;
960       
961       pspec = g_param_spec_pool_lookup (pspec_pool,
962                                         name,
963                                         G_OBJECT_TYPE (object),
964                                         TRUE);
965       if (!pspec)
966         {
967           g_warning ("%s: object class `%s' has no property named `%s'",
968                      G_STRLOC,
969                      G_OBJECT_TYPE_NAME (object),
970                      name);
971           break;
972         }
973       if (!(pspec->flags & G_PARAM_READABLE))
974         {
975           g_warning ("%s: property `%s' of object class `%s' is not readable",
976                      G_STRLOC,
977                      pspec->name,
978                      G_OBJECT_TYPE_NAME (object));
979           break;
980         }
981       
982       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
983       
984       object_get_property (object, pspec, &value);
985       
986       G_VALUE_LCOPY (&value, var_args, 0, &error);
987       if (error)
988         {
989           g_warning ("%s: %s", G_STRLOC, error);
990           g_free (error);
991           g_value_unset (&value);
992           break;
993         }
994       
995       g_value_unset (&value);
996       
997       name = va_arg (var_args, gchar*);
998     }
999   
1000   g_object_unref (object);
1001 }
1002
1003 gpointer
1004 g_object_set (gpointer     _object,
1005               const gchar *first_property_name,
1006               ...)
1007 {
1008   GObject *object = _object;
1009   va_list var_args;
1010   
1011   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1012   
1013   va_start (var_args, first_property_name);
1014   g_object_set_valist (object, first_property_name, var_args);
1015   va_end (var_args);
1016
1017   return object;
1018 }
1019
1020 void
1021 g_object_get (gpointer     _object,
1022               const gchar *first_property_name,
1023               ...)
1024 {
1025   GObject *object = _object;
1026   va_list var_args;
1027   
1028   g_return_if_fail (G_IS_OBJECT (object));
1029   
1030   va_start (var_args, first_property_name);
1031   g_object_get_valist (object, first_property_name, var_args);
1032   va_end (var_args);
1033 }
1034
1035 void
1036 g_object_set_property (GObject      *object,
1037                        const gchar  *property_name,
1038                        const GValue *value)
1039 {
1040   NotifyQueue *nqueue;
1041   GParamSpec *pspec;
1042   
1043   g_return_if_fail (G_IS_OBJECT (object));
1044   g_return_if_fail (property_name != NULL);
1045   g_return_if_fail (G_IS_VALUE (value));
1046   
1047   g_object_ref (object);
1048   nqueue = object_freeze_notifies (object);
1049   
1050   pspec = g_param_spec_pool_lookup (pspec_pool,
1051                                     property_name,
1052                                     G_OBJECT_TYPE (object),
1053                                     TRUE);
1054   if (!pspec)
1055     g_warning ("%s: object class `%s' has no property named `%s'",
1056                G_STRLOC,
1057                G_OBJECT_TYPE_NAME (object),
1058                property_name);
1059   else
1060     object_set_property (object, pspec, value, nqueue);
1061   
1062   object_thaw_notifies (object, nqueue);
1063   g_object_unref (object);
1064 }
1065
1066 void
1067 g_object_get_property (GObject     *object,
1068                        const gchar *property_name,
1069                        GValue      *value)
1070 {
1071   GParamSpec *pspec;
1072   
1073   g_return_if_fail (G_IS_OBJECT (object));
1074   g_return_if_fail (property_name != NULL);
1075   g_return_if_fail (G_IS_VALUE (value));
1076   
1077   g_object_ref (object);
1078   
1079   pspec = g_param_spec_pool_lookup (pspec_pool,
1080                                     property_name,
1081                                     G_OBJECT_TYPE (object),
1082                                     TRUE);
1083   if (!pspec)
1084     g_warning ("%s: object class `%s' has no property named `%s'",
1085                G_STRLOC,
1086                G_OBJECT_TYPE_NAME (object),
1087                property_name);
1088   else
1089     {
1090       GValue *prop_value, tmp_value = { 0, };
1091       
1092       /* auto-conversion of the callers value type
1093        */
1094       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1095         {
1096           g_value_reset (value);
1097           prop_value = value;
1098         }
1099       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1100         {
1101           g_warning ("can't retrive property `%s' of type `%s' as value of type `%s'",
1102                      pspec->name,
1103                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1104                      G_VALUE_TYPE_NAME (value));
1105           g_object_unref (object);
1106           return;
1107         }
1108       else
1109         {
1110           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1111           prop_value = &tmp_value;
1112         }
1113       object_get_property (object, pspec, prop_value);
1114       if (prop_value != value)
1115         {
1116           g_value_transform (prop_value, value);
1117           g_value_unset (&tmp_value);
1118         }
1119     }
1120   
1121   g_object_unref (object);
1122 }
1123
1124 gpointer
1125 g_object_connect (gpointer     _object,
1126                   const gchar *signal_spec,
1127                   ...)
1128 {
1129   GObject *object = _object;
1130   va_list var_args;
1131
1132   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1133   g_return_val_if_fail (object->ref_count > 0, object);
1134
1135   va_start (var_args, signal_spec);
1136   while (signal_spec)
1137     {
1138       gpointer callback = va_arg (var_args, gpointer);
1139       gpointer data = va_arg (var_args, gpointer);
1140       guint sid;
1141
1142       if (strncmp (signal_spec, "signal::", 8) == 0)
1143         sid = g_signal_connect_data (object, signal_spec + 8,
1144                                      callback, data, NULL,
1145                                      FALSE, FALSE);
1146       else if (strncmp (signal_spec, "swapped_signal::", 16) == 0)
1147         sid = g_signal_connect_data (object, signal_spec + 16,
1148                                      callback, data, NULL,
1149                                      TRUE, FALSE);
1150       else if (strncmp (signal_spec, "signal_after::", 14) == 0)
1151         sid = g_signal_connect_data (object, signal_spec + 14,
1152                                      callback, data, NULL,
1153                                      FALSE, TRUE);
1154       else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0)
1155         sid = g_signal_connect_data (object, signal_spec + 22,
1156                                      callback, data, NULL,
1157                                      TRUE, TRUE);
1158       else
1159         {
1160           g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1161           break;
1162         }
1163       signal_spec = va_arg (var_args, gchar*);
1164     }
1165   va_end (var_args);
1166
1167   return object;
1168 }
1169
1170 gpointer
1171 g_object_disconnect (gpointer     _object,
1172                      const gchar *signal_spec,
1173                      ...)
1174 {
1175   GObject *object = _object;
1176   va_list var_args;
1177
1178   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1179   g_return_val_if_fail (object->ref_count > 0, object);
1180
1181   va_start (var_args, signal_spec);
1182   while (signal_spec)
1183     {
1184       gpointer callback = va_arg (var_args, gpointer);
1185       gpointer data = va_arg (var_args, gpointer);
1186       guint sid = 0, detail = 0, mask = 0;
1187
1188       if (strncmp (signal_spec, "any_signal::", 12) == 0)
1189         {
1190           signal_spec += 12;
1191           mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1192         }
1193       else if (strcmp (signal_spec, "any_signal") == 0)
1194         {
1195           signal_spec += 10;
1196           mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1197         }
1198       else
1199         {
1200           g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1201           break;
1202         }
1203
1204       if ((mask & G_SIGNAL_MATCH_ID) &&
1205           !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1206         g_warning ("%s: invalid signal name \"%s\"", G_STRLOC, signal_spec);
1207       else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1208                                                       sid, detail,
1209                                                       NULL, callback, data))
1210         g_warning (G_STRLOC ": signal handler %p(%p) is not connected", callback, data);
1211       signal_spec = va_arg (var_args, gchar*);
1212     }
1213   va_end (var_args);
1214
1215   return object;
1216 }
1217
1218 gpointer
1219 g_object_ref (gpointer _object)
1220 {
1221   GObject *object = _object;
1222
1223   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1224   g_return_val_if_fail (object->ref_count > 0, NULL);
1225   
1226 #ifdef  G_ENABLE_DEBUG
1227   if (g_trap_object_ref == object)
1228     G_BREAKPOINT ();
1229 #endif  /* G_ENABLE_DEBUG */
1230
1231   object->ref_count += 1;
1232   
1233   return object;
1234 }
1235
1236 void
1237 g_object_unref (gpointer _object)
1238 {
1239   GObject *object = _object;
1240
1241   g_return_if_fail (G_IS_OBJECT (object));
1242   g_return_if_fail (object->ref_count > 0);
1243   
1244 #ifdef  G_ENABLE_DEBUG
1245   if (g_trap_object_ref == object)
1246     G_BREAKPOINT ();
1247 #endif  /* G_ENABLE_DEBUG */
1248
1249   if (object->ref_count > 1)
1250     object->ref_count -= 1;
1251   else
1252     g_object_last_unref (object);
1253 }
1254
1255 gpointer
1256 g_object_get_qdata (GObject *object,
1257                     GQuark   quark)
1258 {
1259   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1260   
1261   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1262 }
1263
1264 void
1265 g_object_set_qdata (GObject *object,
1266                     GQuark   quark,
1267                     gpointer data)
1268 {
1269   g_return_if_fail (G_IS_OBJECT (object));
1270   g_return_if_fail (quark > 0);
1271   
1272   g_datalist_id_set_data (&object->qdata, quark, data);
1273 }
1274
1275 void
1276 g_object_set_qdata_full (GObject       *object,
1277                          GQuark         quark,
1278                          gpointer       data,
1279                          GDestroyNotify destroy)
1280 {
1281   g_return_if_fail (G_IS_OBJECT (object));
1282   g_return_if_fail (quark > 0);
1283   
1284   g_datalist_id_set_data_full (&object->qdata, quark, data,
1285                                data ? destroy : (GDestroyNotify) NULL);
1286 }
1287
1288 gpointer
1289 g_object_steal_qdata (GObject *object,
1290                       GQuark   quark)
1291 {
1292   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1293   g_return_val_if_fail (quark > 0, NULL);
1294   
1295   return g_datalist_id_remove_no_notify (&object->qdata, quark);
1296 }
1297
1298 gpointer
1299 g_object_get_data (GObject     *object,
1300                    const gchar *key)
1301 {
1302   GQuark quark;
1303
1304   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1305   g_return_val_if_fail (key != NULL, NULL);
1306
1307   quark = g_quark_try_string (key);
1308
1309   return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1310 }
1311
1312 void
1313 g_object_set_data (GObject     *object,
1314                    const gchar *key,
1315                    gpointer     data)
1316 {
1317   g_return_if_fail (G_IS_OBJECT (object));
1318   g_return_if_fail (key != NULL);
1319
1320   g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1321 }
1322
1323 void
1324 g_object_set_data_full (GObject       *object,
1325                         const gchar   *key,
1326                         gpointer       data,
1327                         GDestroyNotify destroy)
1328 {
1329   g_return_if_fail (G_IS_OBJECT (object));
1330   g_return_if_fail (key != NULL);
1331
1332   g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1333                                data ? destroy : (GDestroyNotify) NULL);
1334 }
1335
1336 gpointer
1337 g_object_steal_data (GObject     *object,
1338                      const gchar *key)
1339 {
1340   GQuark quark;
1341
1342   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1343   g_return_val_if_fail (key != NULL, NULL);
1344
1345   quark = g_quark_try_string (key);
1346
1347   return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1348 }
1349
1350 static void
1351 g_value_object_init (GValue *value)
1352 {
1353   value->data[0].v_pointer = NULL;
1354 }
1355
1356 static void
1357 g_value_object_free_value (GValue *value)
1358 {
1359   if (value->data[0].v_pointer)
1360     g_object_unref (value->data[0].v_pointer);
1361 }
1362
1363 static void
1364 g_value_object_copy_value (const GValue *src_value,
1365                            GValue       *dest_value)
1366 {
1367   if (src_value->data[0].v_pointer)
1368     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1369   else
1370     dest_value->data[0].v_pointer = NULL;
1371 }
1372
1373 static void
1374 g_value_object_transform_value (const GValue *src_value,
1375                                 GValue       *dest_value)
1376 {
1377   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)))
1378     dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1379   else
1380     dest_value->data[0].v_pointer = NULL;
1381 }
1382
1383 static gpointer
1384 g_value_object_peek_pointer (const GValue *value)
1385 {
1386   return value->data[0].v_pointer;
1387 }
1388
1389 static gchar*
1390 g_value_object_collect_value (GValue      *value,
1391                               guint        n_collect_values,
1392                               GTypeCValue *collect_values,
1393                               guint        collect_flags)
1394 {
1395   if (collect_values[0].v_pointer)
1396     {
1397       GObject *object = collect_values[0].v_pointer;
1398       
1399       if (object->g_type_instance.g_class == NULL)
1400         return g_strconcat ("invalid unclassed object pointer for value type `",
1401                             G_VALUE_TYPE_NAME (value),
1402                             "'",
1403                             NULL);
1404       else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1405         return g_strconcat ("invalid object type `",
1406                             G_OBJECT_TYPE_NAME (object),
1407                             "' for value type `",
1408                             G_VALUE_TYPE_NAME (value),
1409                             "'",
1410                             NULL);
1411       /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1412       value->data[0].v_pointer = g_object_ref (object);
1413     }
1414   else
1415     value->data[0].v_pointer = NULL;
1416   
1417   return NULL;
1418 }
1419
1420 static gchar*
1421 g_value_object_lcopy_value (const GValue *value,
1422                             guint        n_collect_values,
1423                             GTypeCValue *collect_values,
1424                             guint        collect_flags)
1425 {
1426   GObject **object_p = collect_values[0].v_pointer;
1427   
1428   if (!object_p)
1429     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1430
1431   if (!value->data[0].v_pointer)
1432     *object_p = NULL;
1433   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1434     *object_p = value->data[0].v_pointer;
1435   else
1436     *object_p = g_object_ref (value->data[0].v_pointer);
1437   
1438   return NULL;
1439 }
1440
1441 void
1442 g_value_set_object (GValue   *value,
1443                     gpointer  v_object)
1444 {
1445   g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1446   
1447   if (value->data[0].v_pointer)
1448     {
1449       g_object_unref (value->data[0].v_pointer);
1450       value->data[0].v_pointer = NULL;
1451     }
1452
1453   if (v_object)
1454     {
1455       g_return_if_fail (G_IS_OBJECT (v_object));
1456       g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1457
1458       value->data[0].v_pointer = v_object;
1459       g_object_ref (value->data[0].v_pointer);
1460     }
1461 }
1462
1463 gpointer
1464 g_value_get_object (const GValue *value)
1465 {
1466   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1467   
1468   return value->data[0].v_pointer;
1469 }
1470
1471 GObject*
1472 g_value_dup_object (const GValue *value)
1473 {
1474   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1475   
1476   return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1477 }
1478
1479 guint
1480 g_signal_connect_object (gpointer     instance,
1481                          const gchar *detailed_signal,
1482                          GCallback    c_handler,
1483                          gpointer     gobject,
1484                          gboolean     swapped,
1485                          gboolean     after)
1486 {
1487   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1488   g_return_val_if_fail (detailed_signal != NULL, 0);
1489   g_return_val_if_fail (c_handler != NULL, 0);
1490
1491   if (gobject)
1492     {
1493       GClosure *closure;
1494
1495       g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1496
1497       closure = (swapped ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1498
1499       return g_signal_connect_closure (instance, detailed_signal, closure, after);
1500     }
1501   else
1502     return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, swapped, after);
1503 }
1504
1505 typedef struct {
1506   GObject  *object;
1507   guint     n_closures;
1508   GClosure *closures[1]; /* flexible array */
1509 } CArray;
1510
1511 static void
1512 object_remove_closure (gpointer  data,
1513                        GClosure *closure)
1514 {
1515   GObject *object = data;
1516   CArray *carray = g_object_get_qdata (object, quark_closure_array);
1517   guint i;
1518   
1519   for (i = 0; i < carray->n_closures; i++)
1520     if (carray->closures[i] == closure)
1521       {
1522         carray->n_closures--;
1523         if (i < carray->n_closures)
1524           carray->closures[i] = carray->closures[carray->n_closures];
1525         return;
1526       }
1527   g_assert_not_reached ();
1528 }
1529
1530 static void
1531 destroy_closure_array (gpointer data)
1532 {
1533   CArray *carray = data;
1534   GObject *object = carray->object;
1535   guint i, n = carray->n_closures;
1536   
1537   for (i = 0; i < n; i++)
1538     {
1539       GClosure *closure = carray->closures[i];
1540       
1541       /* removing object_remove_closure() upfront is probably faster than
1542        * letting it fiddle with quark_closure_array which is empty anyways
1543        */
1544       g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1545       g_closure_invalidate (closure);
1546     }
1547   g_free (carray);
1548 }
1549
1550 void
1551 g_object_watch_closure (GObject  *object,
1552                         GClosure *closure)
1553 {
1554   CArray *carray;
1555   
1556   g_return_if_fail (G_IS_OBJECT (object));
1557   g_return_if_fail (closure != NULL);
1558   g_return_if_fail (closure->is_invalid == FALSE);
1559   g_return_if_fail (closure->in_marshal == FALSE);
1560   g_return_if_fail (object->ref_count > 0);     /* this doesn't work on finalizing objects */
1561   
1562   g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1563   g_closure_add_marshal_guards (closure,
1564                                 object, (GClosureNotify) g_object_ref,
1565                                 object, (GClosureNotify) g_object_unref);
1566   carray = g_object_steal_qdata (object, quark_closure_array);
1567   if (!carray)
1568     {
1569       carray = g_renew (CArray, NULL, 1);
1570       carray->object = object;
1571       carray->n_closures = 1;
1572       carray->closures[0] = closure;
1573       g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1574     }
1575   else
1576     {
1577       guint i = carray->n_closures++;
1578       
1579       carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1580       carray->closures[i] = closure;
1581       g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1582     }
1583 }
1584
1585 GClosure*
1586 g_closure_new_object (guint    sizeof_closure,
1587                       GObject *object)
1588 {
1589   GClosure *closure;
1590
1591   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1592   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
1593
1594   closure = g_closure_new_simple (sizeof_closure, object);
1595   g_object_watch_closure (object, closure);
1596
1597   return closure;
1598 }
1599
1600 GClosure*
1601 g_cclosure_new_object (GCallback callback_func,
1602                        gpointer  _object)
1603 {
1604   GObject *object = _object;
1605   GClosure *closure;
1606
1607   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1608   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
1609   g_return_val_if_fail (callback_func != NULL, NULL);
1610
1611   closure = g_cclosure_new (callback_func, object, NULL);
1612   g_object_watch_closure (object, closure);
1613
1614   return closure;
1615 }
1616
1617 GClosure*
1618 g_cclosure_new_object_swap (GCallback callback_func,
1619                             gpointer  _object)
1620 {
1621   GObject *object = _object;
1622   GClosure *closure;
1623
1624   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1625   g_return_val_if_fail (object->ref_count > 0, NULL);     /* this doesn't work on finalizing objects */
1626   g_return_val_if_fail (callback_func != NULL, NULL);
1627
1628   closure = g_cclosure_new_swap (callback_func, object, NULL);
1629   g_object_watch_closure (object, closure);
1630
1631   return closure;
1632 }