1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998, 1999, 2000 Tim Janik and Red Hat, Inc.
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.
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.
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.
22 #include "gvaluecollector.h"
24 #include "gparamspecs.h"
25 #include "gvaluetypes.h"
29 #define PREALLOC_CPARAMS (8)
34 #define PARAM_SPEC_PARAM_ID(pspec) (GPOINTER_TO_UINT (g_param_spec_get_qdata ((pspec), quark_property_id)))
45 /* --- properties --- */
52 PROP_SWAPPED_SIGNAL_AFTER
56 /* --- typedefs --- */
57 typedef struct _NotifyQueue NotifyQueue;
60 /* --- prototypes --- */
61 static void g_object_base_class_init (GObjectClass *class);
62 static void g_object_base_class_finalize (GObjectClass *class);
63 static void g_object_do_class_init (GObjectClass *class);
64 static void g_object_init (GObject *object);
65 static GObject* g_object_constructor (GType type,
66 guint n_construct_properties,
67 GObjectConstructParam *construct_params);
68 static void g_object_last_unref (GObject *object);
69 static void g_object_shutdown (GObject *object);
70 static void g_object_finalize (GObject *object);
71 static void g_object_do_set_property (GObject *object,
75 const gchar *trailer);
76 static void g_object_do_get_property (GObject *object,
80 const gchar *trailer);
81 static void g_value_object_init (GValue *value);
82 static void g_value_object_free_value (GValue *value);
83 static void g_value_object_copy_value (const GValue *src_value,
85 static gpointer g_value_object_peek_pointer (const GValue *value);
86 static gchar* g_value_object_collect_value (GValue *value,
89 GTypeCValue *collect_value);
90 static gchar* g_value_object_lcopy_value (const GValue *value,
93 GTypeCValue *collect_value);
94 static void g_object_dispatch_properties_changed (GObject *object,
97 static void g_object_properties_changed (GObject *object,
100 static void g_object_notify_property_changed (GObject *object,
102 static inline NotifyQueue* object_freeze_notifies (GObject *object);
103 static inline void object_queue_property (GObject *object,
105 NotifyQueue *nqueue);
106 static inline void object_thaw_notifies (GObject *object,
107 NotifyQueue *nqueue);
108 static inline void object_get_property (GObject *object,
111 const gchar *trailer);
112 static inline void object_set_property (GObject *object,
115 const gchar *trailer,
116 NotifyQueue *nqueue);
119 /* --- structures --- */
128 /* --- variables --- */
129 static GQuark quark_notify_queue = 0;
130 static GQuark quark_property_id = 0;
131 static GQuark quark_closure_array = 0;
132 static GParamSpecPool *pspec_pool = NULL;
133 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
136 /* --- functions --- */
139 /* We need an actual method for handling debug keys in GLib.
140 * For now, we'll simply use, as a method
141 * 'extern gboolean glib_debug_objects'
143 gboolean glib_debug_objects = FALSE;
145 static guint debug_objects_count = 0;
146 static GHashTable *debug_objects_ht = NULL;
148 debug_objects_foreach (gpointer key,
152 GObject *object = value;
154 g_message ("[%p] stale %s\tref_count=%u",
156 G_OBJECT_TYPE_NAME (object),
160 debug_objects_atexit (void)
162 if (glib_debug_objects)
164 if (debug_objects_ht)
166 g_message ("stale GObjects: %u", debug_objects_count);
167 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
171 #endif /* DEBUG_OBJECTS */
174 g_object_type_init (void) /* sync with gtype.c */
176 static gboolean initialized = FALSE;
177 static const GTypeFundamentalInfo finfo = {
178 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
180 static GTypeInfo info = {
181 sizeof (GObjectClass),
182 (GBaseInitFunc) g_object_base_class_init,
183 (GBaseFinalizeFunc) g_object_base_class_finalize,
184 (GClassInitFunc) g_object_do_class_init,
185 NULL /* class_destroy */,
186 NULL /* class_data */,
189 (GInstanceInitFunc) g_object_init,
190 NULL, /* value_table */
192 static const GTypeValueTable value_table = {
193 g_value_object_init, /* value_init */
194 g_value_object_free_value, /* value_free */
195 g_value_object_copy_value, /* value_copy */
196 g_value_object_peek_pointer, /* value_peek_pointer */
197 G_VALUE_COLLECT_POINTER, /* collect_type */
198 g_value_object_collect_value, /* collect_value */
199 G_VALUE_COLLECT_POINTER, /* lcopy_type */
200 g_value_object_lcopy_value, /* lcopy_value */
204 g_return_if_fail (initialized == FALSE);
209 info.value_table = &value_table;
210 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
211 g_assert (type == G_TYPE_OBJECT);
214 g_atexit (debug_objects_atexit);
215 #endif /* DEBUG_OBJECTS */
219 g_object_base_class_init (GObjectClass *class)
221 GObjectClass *pclass = g_type_class_peek_parent (class);
223 /* reset instance specific fields and methods that don't get inherited */
224 class->n_property_specs = 0;
225 class->property_specs = NULL;
226 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
227 class->get_property = NULL;
228 class->set_property = NULL;
232 g_object_base_class_finalize (GObjectClass *class)
236 g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
238 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
240 g_slist_free (class->construct_properties);
241 class->construct_properties = NULL;
242 for (i = 0; i < class->n_property_specs; i++)
244 GParamSpec *pspec = class->property_specs[i];
246 g_param_spec_pool_remove (pspec_pool, pspec);
247 g_param_spec_set_qdata (pspec, quark_property_id, NULL);
248 g_param_spec_unref (pspec);
250 class->n_property_specs = 0;
251 g_free (class->property_specs);
252 class->property_specs = NULL;
256 g_object_do_class_init (GObjectClass *class)
258 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
259 quark_property_id = g_quark_from_static_string ("GObject-property-id");
260 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
261 pspec_pool = g_param_spec_pool_new (TRUE);
263 class->constructor = g_object_constructor;
264 class->set_property = g_object_do_set_property;
265 class->get_property = g_object_do_get_property;
266 class->shutdown = g_object_shutdown;
267 class->finalize = g_object_finalize;
268 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
269 class->properties_changed = g_object_properties_changed;
270 class->notify = g_object_notify_property_changed;
272 g_object_class_install_property (class,
274 g_param_spec_pointer ("data", "Named Data",
275 "Named anonymous pointers",
276 G_PARAM_READABLE | G_PARAM_WRITABLE));
277 g_object_class_install_property (class,
279 g_param_spec_ccallback ("signal", "Signal Connection",
280 "Signal connection consisting of a callback function "
281 "and a data pointer",
283 g_object_class_install_property (class,
285 g_param_spec_ccallback ("swapped_signal", "Swapped Signal Connection",
286 "Signal connection consisting of a callback function "
287 "and a data pointer",
289 g_object_class_install_property (class,
291 g_param_spec_ccallback ("signal_after", "Signal After Connection",
292 "Signal connection consisting of a callback function "
293 "and a data pointer",
295 g_object_class_install_property (class,
296 PROP_SWAPPED_SIGNAL_AFTER,
297 g_param_spec_ccallback ("swapped_signal_after", "Swapped Signal After Connection",
298 "Signal connection consisting of a callback function "
299 "and a data pointer",
301 gobject_signals[PROPERTIES_CHANGED] =
302 g_signal_newc ("properties_changed",
303 G_TYPE_FROM_CLASS (class),
304 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
305 G_STRUCT_OFFSET (GObjectClass, properties_changed),
306 NULL, /* accumulator */
307 g_cclosure_marshal_VOID__UINT_POINTER,
309 2, G_TYPE_UINT, G_TYPE_POINTER);
310 gobject_signals[NOTIFY] =
311 g_signal_newc ("notify",
312 G_TYPE_FROM_CLASS (class),
313 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
314 G_STRUCT_OFFSET (GObjectClass, notify),
315 NULL, /* accumulator */
316 g_cclosure_marshal_VOID__PARAM,
322 g_object_class_install_property (GObjectClass *class,
328 g_return_if_fail (G_IS_OBJECT_CLASS (class));
329 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
330 if (pspec->flags & G_PARAM_WRITABLE)
331 g_return_if_fail (class->set_property != NULL);
332 if (pspec->flags & G_PARAM_READABLE)
333 g_return_if_fail (class->get_property != NULL);
334 g_return_if_fail (property_id > 0);
335 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
336 if (pspec->flags & G_PARAM_CONSTRUCT)
337 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
338 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
339 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
341 /* expensive paranoia checks ;( */
342 for (i = 0; i < class->n_property_specs; i++)
343 if (PARAM_SPEC_PARAM_ID (class->property_specs[i]) == property_id)
345 g_warning (G_STRLOC ": class `%s' already contains a property `%s' with id %u, "
346 "cannot install property `%s'",
347 G_OBJECT_CLASS_NAME (class),
348 class->property_specs[i]->name,
353 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE, NULL))
355 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
356 G_OBJECT_CLASS_NAME (class),
361 g_param_spec_ref (pspec);
362 g_param_spec_sink (pspec);
363 g_param_spec_set_qdata (pspec, quark_property_id, GUINT_TO_POINTER (property_id));
364 g_param_spec_pool_insert (pspec_pool, pspec, G_OBJECT_CLASS_TYPE (class));
365 i = class->n_property_specs++;
366 class->property_specs = g_renew (GParamSpec*, class->property_specs, class->n_property_specs);
367 class->property_specs[i] = pspec;
368 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
369 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
371 /* for property overrides of construct poperties, we have to get rid
372 * of the overidden inherited construct property
374 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE, NULL);
375 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
376 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
380 g_object_class_find_property (GObjectClass *class,
381 const gchar *property_name)
383 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
384 g_return_val_if_fail (property_name != NULL, NULL);
386 return g_param_spec_pool_lookup (pspec_pool,
388 G_OBJECT_CLASS_TYPE (class),
393 free_notify_queue (gpointer data)
395 NotifyQueue *nqueue = data;
397 g_slist_free (nqueue->pspecs);
401 static inline NotifyQueue*
402 object_freeze_notifies (GObject *object)
406 nqueue = g_object_get_qdata (object, quark_notify_queue);
409 nqueue = g_new0 (NotifyQueue, 1);
410 g_object_set_qdata_full (object, quark_notify_queue, nqueue, free_notify_queue);
412 nqueue->freeze_count++;
418 object_queue_property (GObject *object,
422 /* we will dedup later */
423 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
428 g_object_init (GObject *object)
430 object->ref_count = 1;
431 g_datalist_init (&object->qdata);
433 /* freeze object's notification queue, g_object_new_valist() takes care of that */
434 object_freeze_notifies (object);
437 if (glib_debug_objects)
439 if (!debug_objects_ht)
440 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
441 debug_objects_count++;
442 g_hash_table_insert (debug_objects_ht, object, object);
444 #endif /* DEBUG_OBJECTS */
448 g_object_do_set_property (GObject *object,
452 const gchar *trailer)
458 gboolean swapped, after;
459 gpointer callback, data;
461 g_return_if_fail (trailer != NULL);
463 g_object_set_data (object, trailer, g_value_get_pointer (value));
465 case PROP_SWAPPED_SIGNAL_AFTER:
467 case PROP_SIGNAL_AFTER:
469 case PROP_SWAPPED_SIGNAL:
474 g_return_if_fail (trailer != NULL);
476 g_value_get_ccallback (value, &callback, &data);
477 g_signal_connect_data (object, trailer,
478 callback, data, NULL,
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
488 g_object_do_get_property (GObject *object,
492 const gchar *trailer)
497 g_return_if_fail (trailer != NULL);
499 g_value_set_pointer (value, g_object_get_data (object, trailer));
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
508 g_object_last_unref (GObject *object)
510 g_return_if_fail (object->ref_count > 0);
512 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
513 G_OBJECT_GET_CLASS (object)->shutdown (object);
515 object->ref_count -= 1;
517 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
519 G_OBJECT_GET_CLASS (object)->finalize (object);
521 if (glib_debug_objects && debug_objects_ht)
522 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
523 #endif /* DEBUG_OBJECTS */
524 g_type_free_instance ((GTypeInstance*) object);
529 g_object_shutdown (GObject *object)
531 /* this function needs to be always present for unconditional
532 * chaining, we also might add some code here later.
533 * beware though, subclasses may invoke shutdown() arbitrarily.
538 g_object_finalize (GObject *object)
540 g_signal_handlers_destroy (object);
541 g_datalist_clear (&object->qdata);
544 if (glib_debug_objects)
546 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
548 g_hash_table_remove (debug_objects_ht, object);
549 debug_objects_count--;
551 #endif /* DEBUG_OBJECTS */
555 object_thaw_notifies (GObject *object,
562 nqueue->freeze_count--;
563 if (nqueue->freeze_count)
565 g_return_if_fail (object->ref_count > 0);
567 pspecs = g_new (GParamSpec*, nqueue->n_pspecs);
568 for (slist = nqueue->pspecs; slist; slist = slist->next)
570 GParamSpec *pspec = slist->data;
573 /* dedup, make pspecs in the list unique */
575 if (pspecs[i] == pspec)
578 goto redo_dedup_check;
580 pspecs[n_pspecs++] = pspec;
582 g_object_set_qdata (object, quark_notify_queue, NULL);
585 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
591 g_object_dispatch_properties_changed (GObject *object,
595 g_signal_emit (object, gobject_signals[PROPERTIES_CHANGED], 0, n_pspecs, pspecs);
599 g_object_properties_changed (GObject *object,
605 for (i = 0; i < n_pspecs; i++)
606 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
610 g_object_notify_property_changed (GObject *object,
614 g_message ("NOTIFICATION: property `%s' changed on object `%s'",
616 G_OBJECT_TYPE_NAME (object));
620 g_object_freeze_notify (GObject *object)
622 g_return_if_fail (G_IS_OBJECT (object));
623 if (!object->ref_count)
626 g_object_ref (object);
627 object_freeze_notifies (object);
628 g_object_unref (object);
632 g_object_notify (GObject *object,
633 const gchar *property_name)
637 g_return_if_fail (G_IS_OBJECT (object));
638 g_return_if_fail (property_name != NULL);
639 if (!object->ref_count)
642 g_object_ref (object);
643 pspec = g_param_spec_pool_lookup (pspec_pool,
645 G_OBJECT_TYPE (object),
648 g_warning ("%s: object class `%s' has no property named `%s'",
650 G_OBJECT_TYPE_NAME (object),
654 NotifyQueue *nqueue = object_freeze_notifies (object);
656 object_queue_property (object, pspec, nqueue);
657 object_thaw_notifies (object, nqueue);
659 g_object_unref (object);
663 g_object_thaw_notify (GObject *object)
667 g_return_if_fail (G_IS_OBJECT (object));
668 if (!object->ref_count)
671 g_object_ref (object);
672 nqueue = g_object_get_qdata (object, quark_notify_queue);
673 if (!nqueue || !nqueue->freeze_count)
674 g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
675 G_OBJECT_TYPE_NAME (object), object);
677 object_thaw_notifies (object, nqueue);
678 g_object_unref (object);
682 object_get_property (GObject *object,
685 const gchar *trailer)
689 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
691 class = g_type_class_peek (pspec->owner_type);
693 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
697 object_set_property (GObject *object,
700 const gchar *trailer,
705 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
707 class = g_type_class_peek (pspec->owner_type);
709 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
710 object_queue_property (object, pspec, nqueue);
714 g_object_new (GType object_type,
715 const gchar *first_property_name,
721 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
723 va_start (var_args, first_property_name);
724 object = g_object_new_valist (object_type, first_property_name, var_args);
731 g_object_new_valist (GType object_type,
732 const gchar *first_property_name,
739 GObjectConstructParam *cparams = NULL, *nparams = NULL;
740 guint n_cparams = 0, n_nparams = 0;
743 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
745 class = g_type_class_ref (object_type);
746 clist = g_slist_copy (class->construct_properties);
748 /* collect parameters, sort into construction and normal ones */
749 name = first_property_name;
752 const gchar *trailer = NULL;
757 pspec = g_param_spec_pool_lookup (pspec_pool,
764 g_warning ("%s: object class `%s' has no property named `%s'",
766 g_type_name (object_type),
770 if (!(pspec->flags & G_PARAM_WRITABLE))
772 g_warning ("%s: property `%s' of object class `%s' is not writable",
775 g_type_name (object_type));
779 value = g_new (GValue, 1);
781 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
782 G_VALUE_COLLECT (value, var_args, &error);
785 g_warning ("%s: %s", G_STRLOC, error);
788 /* we purposely leak the value here, it might not be
789 * in a sane state if an error condition occoured
793 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
797 if (!n_cparams || n_cparams >= PREALLOC_CPARAMS)
798 cparams = g_renew (GObjectConstructParam, cparams, MAX (n_cparams + 1, PREALLOC_CPARAMS));
799 cparams[n_cparams].pspec = pspec;
800 cparams[n_cparams].value = value;
801 cparams[n_cparams].trailer = trailer;
802 for (i = 0; i < n_cparams; i++) /* picky, aren't we? ;) */
803 if (cparams[i].pspec == pspec)
804 g_warning (G_STRLOC ": construct property \"%s\" for object `%s' is being set twice",
805 pspec->name, g_type_name (object_type));
807 clist = g_slist_remove (clist, pspec); /* FIXME: unique */
811 if (!n_nparams || n_nparams >= PREALLOC_CPARAMS)
812 nparams = g_renew (GObjectConstructParam, nparams, MAX (n_nparams + 1, PREALLOC_CPARAMS));
813 nparams[n_nparams].pspec = pspec;
814 nparams[n_nparams].value = value;
815 nparams[n_nparams].trailer = trailer;
819 name = va_arg (var_args, gchar*);
822 /* construct object from construction parameters */
825 GSList *tmp = clist->next;
826 GParamSpec *pspec = clist->data;
827 GValue *value = g_new (GValue, 1);
830 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
831 g_param_value_set_default (pspec, value);
833 if (!n_cparams || n_cparams >= PREALLOC_CPARAMS)
834 cparams = g_renew (GObjectConstructParam, cparams, MAX (n_cparams + 1, PREALLOC_CPARAMS));
835 cparams[n_cparams].pspec = pspec;
836 cparams[n_cparams].value = value;
837 cparams[n_cparams].trailer = NULL;
840 g_slist_free_1 (clist);
843 object = class->constructor (object_type, n_cparams, cparams);
845 /* free construction values */
848 g_value_unset (cparams[n_cparams].value);
849 g_free (cparams[n_cparams].value);
853 /* release g_object_init() notification queue freeze_count */
854 nqueue = object_freeze_notifies (object);
855 nqueue->freeze_count--;
857 /* set remaining properties */
861 GValue *value = nparams->value;
862 GParamSpec *pspec = nparams->pspec;
863 const gchar *trailer = nparams++->trailer;
865 /* convert if necessary */
866 if (!g_type_is_a (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
868 GValue tmp_value = { 0, };
870 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
871 if (!g_value_convert (value, &tmp_value) ||
872 g_param_value_validate (pspec, &tmp_value))
873 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
875 G_VALUE_TYPE_NAME (value),
877 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
879 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
880 g_value_unset (&tmp_value);
883 object_set_property (object, value, pspec, trailer, nqueue);
885 g_value_unset (value);
890 g_type_class_unref (class);
892 /* release our own freeze count and handle notifications */
893 object_thaw_notifies (object, nqueue);
899 g_object_constructor (GType type,
900 guint n_construct_properties,
901 GObjectConstructParam *construct_params)
906 object = (GObject*) g_type_create_instance (type);
908 /* set construction parameters */
909 if (n_construct_properties)
911 NotifyQueue *nqueue = object_freeze_notifies (object);
913 /* set construct properties */
914 while (n_construct_properties--)
916 GValue *value = construct_params->value;
917 GParamSpec *pspec = construct_params->pspec;
918 const gchar *trailer = construct_params++->trailer;
920 /* convert if necessary */
921 if (!g_type_is_a (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
923 GValue tmp_value = { 0, };
925 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
926 if (!g_value_convert (value, &tmp_value) ||
927 g_param_value_validate (pspec, &tmp_value))
928 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
930 G_VALUE_TYPE_NAME (value),
932 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
934 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
935 g_value_unset (&tmp_value);
938 object_set_property (object, value, pspec, trailer, nqueue);
940 nqueue->freeze_count--;
941 /* the notification queue is still frozen from g_object_init(), so
942 * we don't need to handle it here, g_object_new_valist() takes
951 g_object_set_valist (GObject *object,
952 const gchar *first_property_name,
958 g_return_if_fail (G_IS_OBJECT (object));
960 g_object_ref (object);
961 nqueue = object_freeze_notifies (object);
963 name = first_property_name;
966 const gchar *trailer = NULL;
967 GValue value = { 0, };
971 pspec = g_param_spec_pool_lookup (pspec_pool,
973 G_OBJECT_TYPE (object),
978 g_warning ("%s: object class `%s' has no property named `%s'",
980 G_OBJECT_TYPE_NAME (object),
984 if (!(pspec->flags & G_PARAM_WRITABLE))
986 g_warning ("%s: property `%s' of object class `%s' is not writable",
989 G_OBJECT_TYPE_NAME (object));
993 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
995 G_VALUE_COLLECT (&value, var_args, &error);
998 g_warning ("%s: %s", G_STRLOC, error);
1001 /* we purposely leak the value here, it might not be
1002 * in a sane state if an error condition occoured
1007 object_set_property (object, &value, pspec, trailer, nqueue);
1009 g_value_unset (&value);
1011 name = va_arg (var_args, gchar*);
1014 object_thaw_notifies (object, nqueue);
1015 g_object_unref (object);
1019 g_object_get_valist (GObject *object,
1020 const gchar *first_property_name,
1025 g_return_if_fail (G_IS_OBJECT (object));
1027 g_object_ref (object);
1029 name = first_property_name;
1033 const gchar *trailer = NULL;
1034 GValue value = { 0, };
1038 pspec = g_param_spec_pool_lookup (pspec_pool,
1040 G_OBJECT_TYPE (object),
1045 g_warning ("%s: object class `%s' has no property named `%s'",
1047 G_OBJECT_TYPE_NAME (object),
1051 if (!(pspec->flags & G_PARAM_READABLE))
1053 g_warning ("%s: property `%s' of object class `%s' is not readable",
1056 G_OBJECT_TYPE_NAME (object));
1060 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1062 object_get_property (object, &value, pspec, trailer);
1064 G_VALUE_LCOPY (&value, var_args, &error);
1067 g_warning ("%s: %s", G_STRLOC, error);
1070 /* we purposely leak the value here, it might not be
1071 * in a sane state if an error condition occoured
1076 g_value_unset (&value);
1078 name = va_arg (var_args, gchar*);
1081 g_object_unref (object);
1085 g_object_set (gpointer _object,
1086 const gchar *first_property_name,
1089 GObject *object = _object;
1092 g_return_if_fail (G_IS_OBJECT (object));
1094 va_start (var_args, first_property_name);
1095 g_object_set_valist (object, first_property_name, var_args);
1100 g_object_get (gpointer _object,
1101 const gchar *first_property_name,
1104 GObject *object = _object;
1107 g_return_if_fail (G_IS_OBJECT (object));
1109 va_start (var_args, first_property_name);
1110 g_object_get_valist (object, first_property_name, var_args);
1115 g_object_set_property (GObject *object,
1116 const gchar *property_name,
1117 const GValue *value)
1119 NotifyQueue *nqueue;
1121 const gchar *trailer;
1123 g_return_if_fail (G_IS_OBJECT (object));
1124 g_return_if_fail (property_name != NULL);
1125 g_return_if_fail (G_IS_VALUE (value));
1127 g_object_ref (object);
1128 nqueue = object_freeze_notifies (object);
1130 pspec = g_param_spec_pool_lookup (pspec_pool,
1132 G_OBJECT_TYPE (object),
1136 g_warning ("%s: object class `%s' has no property named `%s'",
1138 G_OBJECT_TYPE_NAME (object),
1142 GValue tmp_value = { 0, };
1144 /* provide a copy to work from and convert if necessary */
1145 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1147 if (!g_value_convert (value, &tmp_value) ||
1148 g_param_value_validate (pspec, &tmp_value))
1149 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
1151 G_VALUE_TYPE_NAME (value),
1153 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
1155 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
1157 g_value_unset (&tmp_value);
1160 object_thaw_notifies (object, nqueue);
1161 g_object_unref (object);
1165 g_object_get_property (GObject *object,
1166 const gchar *property_name,
1170 const gchar *trailer;
1172 g_return_if_fail (G_IS_OBJECT (object));
1173 g_return_if_fail (property_name != NULL);
1174 g_return_if_fail (G_IS_VALUE (value));
1176 g_object_ref (object);
1178 pspec = g_param_spec_pool_lookup (pspec_pool,
1180 G_OBJECT_TYPE (object),
1184 g_warning ("%s: object class `%s' has no property named `%s'",
1186 G_OBJECT_TYPE_NAME (object),
1190 GValue tmp_value = { 0, };
1192 /* provide a copy to work from and later convert if necessary, so
1193 * _get_property() implementations need *not* care about freeing values
1194 * that might be already set in the property to get.
1195 * (though, at this point, GValue should exclusively be modified
1196 * through the accessor functions anyways)
1198 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1200 if (!g_value_types_exchangable (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
1201 g_warning ("%s: can't retrive property `%s' value of type `%s' as value of type `%s'",
1204 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1205 G_VALUE_TYPE_NAME (value));
1208 object_get_property (object, &tmp_value, pspec, trailer);
1209 g_value_convert (&tmp_value, value);
1210 /* g_value_validate (value, pspec); */
1213 g_value_unset (&tmp_value);
1216 g_object_unref (object);
1220 g_object_ref (gpointer _object)
1222 GObject *object = _object;
1224 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1225 g_return_val_if_fail (object->ref_count > 0, NULL);
1227 object->ref_count += 1;
1233 g_object_unref (gpointer _object)
1235 GObject *object = _object;
1237 g_return_if_fail (G_IS_OBJECT (object));
1238 g_return_if_fail (object->ref_count > 0);
1240 if (object->ref_count > 1)
1241 object->ref_count -= 1;
1243 g_object_last_unref (object);
1247 g_object_get_qdata (GObject *object,
1250 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1252 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1256 g_object_set_qdata (GObject *object,
1260 g_return_if_fail (G_IS_OBJECT (object));
1261 g_return_if_fail (quark > 0);
1263 g_datalist_id_set_data (&object->qdata, quark, data);
1267 g_object_set_qdata_full (GObject *object,
1270 GDestroyNotify destroy)
1272 g_return_if_fail (G_IS_OBJECT (object));
1273 g_return_if_fail (quark > 0);
1275 g_datalist_id_set_data_full (&object->qdata, quark, data, data ? destroy : NULL);
1279 g_object_steal_qdata (GObject *object,
1282 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1283 g_return_val_if_fail (quark > 0, NULL);
1285 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1289 g_object_get_data (GObject *object,
1294 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1295 g_return_val_if_fail (key != NULL, NULL);
1297 quark = g_quark_try_string (key);
1299 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1303 g_object_set_data (GObject *object,
1307 g_return_if_fail (G_IS_OBJECT (object));
1308 g_return_if_fail (key != NULL);
1310 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1314 g_object_set_data_full (GObject *object,
1317 GDestroyNotify destroy)
1319 g_return_if_fail (G_IS_OBJECT (object));
1320 g_return_if_fail (key != NULL);
1322 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data, data ? destroy : NULL);
1326 g_object_steal_data (GObject *object,
1331 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1332 g_return_val_if_fail (key != NULL, NULL);
1334 quark = g_quark_try_string (key);
1336 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1340 g_value_object_init (GValue *value)
1342 value->data[0].v_pointer = NULL;
1346 g_value_object_free_value (GValue *value)
1348 if (value->data[0].v_pointer)
1349 g_object_unref (value->data[0].v_pointer);
1353 g_value_object_copy_value (const GValue *src_value,
1356 if (src_value->data[0].v_pointer)
1357 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1359 dest_value->data[0].v_pointer = NULL;
1363 g_value_object_peek_pointer (const GValue *value)
1365 return value->data[0].v_pointer;
1369 g_value_object_collect_value (GValue *value,
1371 GType *collect_type,
1372 GTypeCValue *collect_value)
1374 if (collect_value->v_pointer)
1376 GObject *object = collect_value->v_pointer;
1378 if (object->g_type_instance.g_class == NULL)
1379 return g_strconcat ("invalid unclassed object pointer for value type `",
1380 G_VALUE_TYPE_NAME (value),
1383 else if (!g_type_is_a (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1384 return g_strconcat ("invalid object type `",
1385 G_OBJECT_TYPE_NAME (object),
1386 "' for value type `",
1387 G_VALUE_TYPE_NAME (value),
1390 value->data[0].v_pointer = g_object_ref (object);
1393 value->data[0].v_pointer = NULL;
1400 g_value_object_lcopy_value (const GValue *value,
1402 GType *collect_type,
1403 GTypeCValue *collect_value)
1405 GObject **object_p = collect_value->v_pointer;
1408 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1410 *object_p = value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1417 g_value_set_object (GValue *value,
1420 g_return_if_fail (G_IS_VALUE_OBJECT (value));
1422 if (value->data[0].v_pointer)
1424 g_object_unref (value->data[0].v_pointer);
1425 value->data[0].v_pointer = NULL;
1430 g_return_if_fail (G_IS_OBJECT (v_object));
1431 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1433 value->data[0].v_pointer = v_object;
1434 g_object_ref (value->data[0].v_pointer);
1439 g_value_get_object (const GValue *value)
1441 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
1443 return value->data[0].v_pointer;
1447 g_value_dup_object (const GValue *value)
1449 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
1451 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1455 g_signal_connect_object (gpointer instance,
1456 const gchar *detailed_signal,
1457 GCallback c_handler,
1462 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1463 g_return_val_if_fail (detailed_signal != NULL, 0);
1464 g_return_val_if_fail (c_handler != NULL, 0);
1470 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1472 closure = (swapped ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1474 return g_signal_connect_closure (instance, detailed_signal, closure, after);
1477 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, swapped, after);
1483 GClosure *closures[1]; /* flexible array */
1487 object_remove_closure (gpointer data,
1490 GObject *object = data;
1491 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1494 for (i = 0; i < carray->n_closures; i++)
1495 if (carray->closures[i] == closure)
1497 carray->n_closures--;
1498 if (i < carray->n_closures)
1499 carray->closures[i] = carray->closures[carray->n_closures];
1502 g_assert_not_reached ();
1506 destroy_closure_array (gpointer data)
1508 CArray *carray = data;
1509 GObject *object = carray->object;
1510 guint i, n = carray->n_closures;
1512 for (i = 0; i < n; i++)
1514 GClosure *closure = carray->closures[i];
1516 /* removing object_remove_closure() upfront is probably faster than
1517 * letting it fiddle with quark_closure_array which is empty anyways
1519 g_closure_remove_inotify (closure, object, object_remove_closure);
1520 g_closure_invalidate (closure);
1526 g_object_watch_closure (GObject *object,
1531 g_return_if_fail (G_IS_OBJECT (object));
1532 g_return_if_fail (closure != NULL);
1533 g_return_if_fail (closure->is_invalid == FALSE);
1534 g_return_if_fail (closure->in_marshal == FALSE);
1535 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1537 g_closure_add_inotify (closure, object, object_remove_closure);
1538 g_closure_add_marshal_guards (closure,
1539 object, (GClosureNotify) g_object_ref,
1540 object, (GClosureNotify) g_object_unref);
1541 carray = g_object_steal_qdata (object, quark_closure_array);
1544 carray = g_renew (CArray, NULL, 1);
1545 carray->object = object;
1546 carray->n_closures = 1;
1547 carray->closures[0] = closure;
1548 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1552 guint i = carray->n_closures++;
1554 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1555 carray->closures[i] = closure;
1556 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1561 g_closure_new_object (guint sizeof_closure,
1566 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1567 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1569 closure = g_closure_new_simple (sizeof_closure, object);
1570 g_object_watch_closure (object, closure);
1576 g_cclosure_new_object (GCallback callback_func,
1579 GObject *object = _object;
1582 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1583 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1584 g_return_val_if_fail (callback_func != NULL, NULL);
1586 closure = g_cclosure_new (callback_func, object, NULL);
1587 g_object_watch_closure (object, closure);
1593 g_cclosure_new_object_swap (GCallback callback_func,
1596 GObject *object = _object;
1599 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1600 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1601 g_return_val_if_fail (callback_func != NULL, NULL);
1603 closure = g_cclosure_new_swap (callback_func, object, NULL);
1604 g_object_watch_closure (object, closure);