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