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