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