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