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