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