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 GHashTable *pspec_hash_table = 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 /* reset instance specific fields and methods that don't get inherited */
222 class->n_property_specs = 0;
223 class->property_specs = NULL;
224 class->get_property = NULL;
225 class->set_property = NULL;
229 g_object_base_class_finalize (GObjectClass *class)
233 g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
235 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
237 for (i = 0; i < class->n_property_specs; i++)
239 GParamSpec *pspec = class->property_specs[i];
241 g_param_spec_hash_table_remove (pspec_hash_table, pspec);
242 g_param_spec_set_qdata (pspec, quark_property_id, NULL);
243 g_param_spec_unref (pspec);
245 class->n_property_specs = 0;
246 g_free (class->property_specs);
247 class->property_specs = NULL;
251 g_object_do_class_init (GObjectClass *class)
253 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
254 quark_property_id = g_quark_from_static_string ("GObject-property-id");
255 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
256 pspec_hash_table = g_param_spec_hash_table_new ();
258 class->constructor = g_object_constructor;
259 class->set_property = g_object_do_set_property;
260 class->get_property = g_object_do_get_property;
261 class->shutdown = g_object_shutdown;
262 class->finalize = g_object_finalize;
263 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
264 class->properties_changed = g_object_properties_changed;
265 class->notify = g_object_notify_property_changed;
267 g_object_class_install_property (class,
269 g_param_spec_pointer ("data", "Named Data",
270 "Named anonymous pointers",
271 G_PARAM_READABLE | G_PARAM_WRITABLE));
272 g_object_class_install_property (class,
274 g_param_spec_ccallback ("signal", "Signal Connection",
275 "Signal connection consisting of a callback function "
276 "and a data pointer",
278 g_object_class_install_property (class,
280 g_param_spec_ccallback ("swapped_signal", "Swapped Signal Connection",
281 "Signal connection consisting of a callback function "
282 "and a data pointer",
284 g_object_class_install_property (class,
286 g_param_spec_ccallback ("signal_after", "Signal After Connection",
287 "Signal connection consisting of a callback function "
288 "and a data pointer",
290 g_object_class_install_property (class,
291 PROP_SWAPPED_SIGNAL_AFTER,
292 g_param_spec_ccallback ("swapped_signal_after", "Swapped Signal After Connection",
293 "Signal connection consisting of a callback function "
294 "and a data pointer",
296 gobject_signals[PROPERTIES_CHANGED] =
297 g_signal_new ("properties_changed",
298 G_TYPE_FROM_CLASS (class),
299 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
300 g_signal_type_cclosure_new (G_TYPE_FROM_CLASS (class),
301 G_STRUCT_OFFSET (GObjectClass, properties_changed)),
302 NULL, /* accumulator */
303 g_cclosure_marshal_VOID__UINT_POINTER,
305 2, G_TYPE_UINT, G_TYPE_POINTER);
306 gobject_signals[NOTIFY] =
307 g_signal_new ("notify",
308 G_TYPE_FROM_CLASS (class),
309 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
310 g_signal_type_cclosure_new (G_TYPE_FROM_CLASS (class),
311 G_STRUCT_OFFSET (GObjectClass, notify)),
312 NULL, /* accumulator */
313 g_cclosure_marshal_VOID__PARAM,
319 g_object_class_install_property (GObjectClass *class,
325 g_return_if_fail (G_IS_OBJECT_CLASS (class));
326 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
327 if (pspec->flags & G_PARAM_WRITABLE)
328 g_return_if_fail (class->set_property != NULL);
329 if (pspec->flags & G_PARAM_READABLE)
330 g_return_if_fail (class->get_property != NULL);
331 g_return_if_fail (property_id > 0);
332 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
333 if (pspec->flags & G_PARAM_CONSTRUCT)
334 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
335 if (pspec->flags & (G_PARAM_CONSTRUCT || G_PARAM_CONSTRUCT_ONLY))
336 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
338 /* expensive paranoia checks ;( */
339 for (i = 0; i < class->n_property_specs; i++)
340 if (PARAM_SPEC_PARAM_ID (class->property_specs[i]) == property_id)
342 g_warning (G_STRLOC ": class `%s' already contains a property `%s' with id %u, "
343 "cannot install property `%s'",
344 G_OBJECT_CLASS_NAME (class),
345 class->property_specs[i]->name,
350 if (g_object_class_find_property (class, pspec->name))
352 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
353 G_OBJECT_CLASS_NAME (class),
358 g_param_spec_ref (pspec);
359 g_param_spec_sink (pspec);
360 g_param_spec_set_qdata (pspec, quark_property_id, GUINT_TO_POINTER (property_id));
361 g_param_spec_hash_table_insert (pspec_hash_table, pspec, G_OBJECT_CLASS_TYPE (class));
362 i = class->n_property_specs++;
363 class->property_specs = g_renew (GParamSpec*, class->property_specs, class->n_property_specs);
364 class->property_specs[i] = pspec;
368 g_object_class_find_property (GObjectClass *class,
369 const gchar *property_name)
371 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
372 g_return_val_if_fail (property_name != NULL, NULL);
374 return g_param_spec_hash_table_lookup (pspec_hash_table,
376 G_OBJECT_CLASS_TYPE (class),
381 free_notify_queue (gpointer data)
383 NotifyQueue *nqueue = data;
385 g_slist_free (nqueue->pspecs);
389 static inline NotifyQueue*
390 object_freeze_notifies (GObject *object)
394 nqueue = g_object_get_qdata (object, quark_notify_queue);
397 nqueue = g_new0 (NotifyQueue, 1);
398 g_object_set_qdata_full (object, quark_notify_queue, nqueue, free_notify_queue);
400 nqueue->freeze_count++;
406 object_queue_property (GObject *object,
410 /* we will dedup later */
411 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
416 g_object_init (GObject *object)
418 object->ref_count = 1;
419 object->qdata = NULL;
421 /* freeze object's notification queue, g_object_new_valist() takes care of that */
422 object_freeze_notifies (object);
425 if (glib_debug_objects)
427 if (!debug_objects_ht)
428 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
429 debug_objects_count++;
430 g_hash_table_insert (debug_objects_ht, object, object);
432 #endif /* DEBUG_OBJECTS */
436 g_object_do_set_property (GObject *object,
440 const gchar *trailer)
446 gboolean swapped, after;
447 gpointer callback, data;
449 g_return_if_fail (trailer[0] == ':' && trailer[1] == ':');
451 g_object_set_data (object, trailer + 2, g_value_get_pointer (value));
453 case PROP_SWAPPED_SIGNAL_AFTER:
455 case PROP_SIGNAL_AFTER:
457 case PROP_SWAPPED_SIGNAL:
462 g_return_if_fail (trailer[0] == ':' && trailer[1] == ':');
464 g_value_get_ccallback (value, &callback, &data);
465 g_signal_connect_data (object, trailer + 2,
466 callback, data, NULL,
470 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
476 g_object_do_get_property (GObject *object,
480 const gchar *trailer)
485 g_return_if_fail (trailer[0] == ':' && trailer[1] == ':');
487 g_value_set_pointer (value, g_object_get_data (object, trailer + 2));
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
496 g_object_last_unref (GObject *object)
498 g_return_if_fail (object->ref_count > 0);
500 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
501 G_OBJECT_GET_CLASS (object)->shutdown (object);
503 object->ref_count -= 1;
505 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
507 G_OBJECT_GET_CLASS (object)->finalize (object);
509 if (glib_debug_objects && debug_objects_ht)
510 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
511 #endif /* DEBUG_OBJECTS */
512 g_type_free_instance ((GTypeInstance*) object);
517 g_object_shutdown (GObject *object)
519 /* this function needs to be always present for unconditional
520 * chaining, we also might add some code here later.
521 * beware though, subclasses may invoke shutdown() arbitrarily.
526 g_object_finalize (GObject *object)
528 g_signal_handlers_destroy (object);
529 g_datalist_clear (&object->qdata);
532 if (glib_debug_objects)
534 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
536 g_hash_table_remove (debug_objects_ht, object);
537 debug_objects_count--;
539 #endif /* DEBUG_OBJECTS */
543 object_thaw_notifies (GObject *object,
550 nqueue->freeze_count--;
551 if (nqueue->freeze_count)
553 g_return_if_fail (object->ref_count > 0);
555 pspecs = g_new (GParamSpec*, nqueue->n_pspecs);
556 for (slist = nqueue->pspecs; slist; slist = slist->next)
558 GParamSpec *pspec = slist->data;
561 /* dedup, make pspecs in the list unique */
563 if (pspecs[i] == pspec)
566 goto redo_dedup_check;
568 pspecs[n_pspecs++] = pspec;
570 g_object_set_qdata (object, quark_notify_queue, NULL);
573 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
579 g_object_dispatch_properties_changed (GObject *object,
583 g_signal_emit (object, gobject_signals[PROPERTIES_CHANGED], 0, n_pspecs, pspecs);
587 g_object_properties_changed (GObject *object,
593 for (i = 0; i < n_pspecs; i++)
594 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
598 g_object_notify_property_changed (GObject *object,
601 g_message ("NOTIFICATION: %s property changed on object `%s'",
603 G_OBJECT_TYPE_NAME (object));
607 g_object_freeze_notify (GObject *object)
609 g_return_if_fail (G_IS_OBJECT (object));
610 if (!object->ref_count)
613 g_object_ref (object);
614 object_freeze_notifies (object);
615 g_object_unref (object);
619 g_object_notify (GObject *object,
620 const gchar *property_name)
624 g_return_if_fail (G_IS_OBJECT (object));
625 g_return_if_fail (property_name != NULL);
626 if (!object->ref_count)
629 g_object_ref (object);
630 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
632 G_OBJECT_TYPE (object),
635 g_warning ("%s: object class `%s' has no property named `%s'",
637 G_OBJECT_TYPE_NAME (object),
641 NotifyQueue *nqueue = object_freeze_notifies (object);
643 object_queue_property (object, pspec, nqueue);
644 object_thaw_notifies (object, nqueue);
646 g_object_unref (object);
650 g_object_thaw_notify (GObject *object)
654 g_return_if_fail (G_IS_OBJECT (object));
655 if (!object->ref_count)
658 g_object_ref (object);
659 nqueue = g_object_get_qdata (object, quark_notify_queue);
660 if (!nqueue || !nqueue->freeze_count)
661 g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
662 G_OBJECT_TYPE_NAME (object), object);
664 object_thaw_notifies (object, nqueue);
665 g_object_unref (object);
669 object_get_property (GObject *object,
672 const gchar *trailer)
676 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
678 class = g_type_class_peek (pspec->owner_type);
680 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
684 object_set_property (GObject *object,
687 const gchar *trailer,
692 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
694 class = g_type_class_peek (pspec->owner_type);
696 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
697 object_queue_property (object, pspec, nqueue);
701 g_object_new (GType object_type,
702 const gchar *first_property_name,
708 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
710 va_start (var_args, first_property_name);
711 object = g_object_new_valist (object_type, first_property_name, var_args);
718 g_object_new_valist (GType object_type,
719 const gchar *first_property_name,
726 GObjectConstructParam *cparams = NULL, *nparams = NULL;
727 guint n_cparams = 0, n_nparams = 0;
729 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
731 /* collect parameters, sort into construction and normal ones */
732 name = first_property_name;
735 const gchar *trailer = NULL;
740 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
747 g_warning ("%s: object class `%s' has no property named `%s'",
749 g_type_name (object_type),
753 if (!(pspec->flags & G_PARAM_WRITABLE))
755 g_warning ("%s: property `%s' of object class `%s' is not writable",
758 g_type_name (object_type));
762 value = g_new (GValue, 1);
764 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
765 G_VALUE_COLLECT (value, var_args, &error);
768 g_warning ("%s: %s", G_STRLOC, error);
771 /* we purposely leak the value here, it might not be
772 * in a sane state if an error condition occoured
776 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
778 if (!n_cparams || n_cparams >= PREALLOC_CPARAMS)
779 cparams = g_renew (GObjectConstructParam, cparams, n_cparams + 1);
780 cparams[n_cparams].pspec = pspec;
781 cparams[n_cparams].value = value;
782 cparams[n_cparams].trailer = trailer;
787 if (!n_nparams || n_nparams >= PREALLOC_CPARAMS)
788 nparams = g_renew (GObjectConstructParam, nparams, n_nparams + 1);
789 nparams[n_nparams].pspec = pspec;
790 nparams[n_nparams].value = value;
791 nparams[n_nparams].trailer = trailer;
795 name = va_arg (var_args, gchar*);
798 /* construct object from construction parameters */
799 class = g_type_class_ref (object_type);
800 object = class->constructor (object_type, n_cparams, cparams);
801 g_type_class_unref (class);
803 /* free construction values */
806 g_value_unset (cparams[n_cparams].value);
807 g_free (cparams[n_cparams].value);
811 /* release g_object_init() notification queue freeze_count */
812 nqueue = object_freeze_notifies (object);
813 nqueue->freeze_count--;
815 /* set remaining properties */
818 GValue *value = nparams[n_nparams].value;
819 GParamSpec *pspec = nparams[n_nparams].pspec;
820 const gchar *trailer = nparams[n_nparams].trailer;
822 /* convert if necessary */
823 if (!g_type_is_a (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
825 GValue tmp_value = { 0, };
827 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
828 if (!g_value_convert (value, &tmp_value) ||
829 g_param_value_validate (pspec, &tmp_value))
830 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
832 G_VALUE_TYPE_NAME (value),
834 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
836 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
837 g_value_unset (&tmp_value);
840 object_set_property (object, value, pspec, trailer, nqueue);
842 g_value_unset (value);
847 /* release our own freeze count and handle notifications */
848 object_thaw_notifies (object, nqueue);
854 g_object_constructor (GType type,
855 guint n_construct_properties,
856 GObjectConstructParam *construct_params)
861 object = (GObject*) g_type_create_instance (type);
863 /* set construction parameters */
864 if (n_construct_properties)
866 NotifyQueue *nqueue = object_freeze_notifies (object);
868 /* set construct properties */
869 while (n_construct_properties--)
871 GValue *value = construct_params[n_construct_properties].value;
872 GParamSpec *pspec = construct_params[n_construct_properties].pspec;
873 const gchar *trailer = construct_params[n_construct_properties].trailer;
875 /* convert if necessary */
876 if (!g_type_is_a (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
878 GValue tmp_value = { 0, };
880 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
881 if (!g_value_convert (value, &tmp_value) ||
882 g_param_value_validate (pspec, &tmp_value))
883 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
885 G_VALUE_TYPE_NAME (value),
887 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
889 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
890 g_value_unset (&tmp_value);
893 object_set_property (object, value, pspec, trailer, nqueue);
895 nqueue->freeze_count--;
896 /* the notification queue is still frozen from g_object_init(), so
897 * we don't need to handle it here, g_object_new_valist() takes
906 g_object_set_valist (GObject *object,
907 const gchar *first_property_name,
913 g_return_if_fail (G_IS_OBJECT (object));
915 g_object_ref (object);
916 nqueue = object_freeze_notifies (object);
918 name = first_property_name;
921 const gchar *trailer = NULL;
922 GValue value = { 0, };
926 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
928 G_OBJECT_TYPE (object),
933 g_warning ("%s: object class `%s' has no property named `%s'",
935 G_OBJECT_TYPE_NAME (object),
939 if (!(pspec->flags & G_PARAM_WRITABLE))
941 g_warning ("%s: property `%s' of object class `%s' is not writable",
944 G_OBJECT_TYPE_NAME (object));
948 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
950 G_VALUE_COLLECT (&value, var_args, &error);
953 g_warning ("%s: %s", G_STRLOC, error);
956 /* we purposely leak the value here, it might not be
957 * in a sane state if an error condition occoured
962 object_set_property (object, &value, pspec, trailer, nqueue);
964 g_value_unset (&value);
966 name = va_arg (var_args, gchar*);
969 object_thaw_notifies (object, nqueue);
970 g_object_unref (object);
974 g_object_get_valist (GObject *object,
975 const gchar *first_property_name,
980 g_return_if_fail (G_IS_OBJECT (object));
982 g_object_ref (object);
984 name = first_property_name;
988 const gchar *trailer = NULL;
989 GValue value = { 0, };
993 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
995 G_OBJECT_TYPE (object),
1000 g_warning ("%s: object class `%s' has no property named `%s'",
1002 G_OBJECT_TYPE_NAME (object),
1006 if (!(pspec->flags & G_PARAM_READABLE))
1008 g_warning ("%s: property `%s' of object class `%s' is not readable",
1011 G_OBJECT_TYPE_NAME (object));
1015 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1017 object_get_property (object, &value, pspec, trailer);
1019 G_VALUE_LCOPY (&value, var_args, &error);
1022 g_warning ("%s: %s", G_STRLOC, error);
1025 /* we purposely leak the value here, it might not be
1026 * in a sane state if an error condition occoured
1031 g_value_unset (&value);
1033 name = va_arg (var_args, gchar*);
1036 g_object_unref (object);
1040 g_object_set (GObject *object,
1041 const gchar *first_property_name,
1046 g_return_if_fail (G_IS_OBJECT (object));
1048 va_start (var_args, first_property_name);
1049 g_object_set_valist (object, first_property_name, var_args);
1054 g_object_get (GObject *object,
1055 const gchar *first_property_name,
1060 g_return_if_fail (G_IS_OBJECT (object));
1062 va_start (var_args, first_property_name);
1063 g_object_get_valist (object, first_property_name, var_args);
1068 g_object_set_property (GObject *object,
1069 const gchar *property_name,
1070 const GValue *value)
1072 NotifyQueue *nqueue;
1074 const gchar *trailer;
1076 g_return_if_fail (G_IS_OBJECT (object));
1077 g_return_if_fail (property_name != NULL);
1078 g_return_if_fail (G_IS_VALUE (value));
1080 g_object_ref (object);
1081 nqueue = object_freeze_notifies (object);
1083 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
1085 G_OBJECT_TYPE (object),
1089 g_warning ("%s: object class `%s' has no property named `%s'",
1091 G_OBJECT_TYPE_NAME (object),
1095 GValue tmp_value = { 0, };
1097 /* provide a copy to work from and convert if necessary */
1098 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1100 if (!g_value_convert (value, &tmp_value) ||
1101 g_param_value_validate (pspec, &tmp_value))
1102 g_warning ("%s: cannot convert `%s' value to property `%s' value of type `%s'",
1104 G_VALUE_TYPE_NAME (value),
1106 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
1108 object_set_property (object, &tmp_value, pspec, trailer, nqueue);
1110 g_value_unset (&tmp_value);
1113 object_thaw_notifies (object, nqueue);
1114 g_object_unref (object);
1118 g_object_get_property (GObject *object,
1119 const gchar *property_name,
1123 const gchar *trailer;
1125 g_return_if_fail (G_IS_OBJECT (object));
1126 g_return_if_fail (property_name != NULL);
1127 g_return_if_fail (G_IS_VALUE (value));
1129 g_object_ref (object);
1131 pspec = g_param_spec_hash_table_lookup (pspec_hash_table,
1133 G_OBJECT_TYPE (object),
1137 g_warning ("%s: object class `%s' has no property named `%s'",
1139 G_OBJECT_TYPE_NAME (object),
1143 GValue tmp_value = { 0, };
1145 /* provide a copy to work from and later convert if necessary, so
1146 * _get_property() implementations need *not* care about freeing values
1147 * that might be already set in the property to get.
1148 * (though, at this point, GValue should exclusively be modified
1149 * through the accessor functions anyways)
1151 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1153 if (!g_value_types_exchangable (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
1154 g_warning ("%s: can't retrive property `%s' value of type `%s' as value of type `%s'",
1157 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1158 G_VALUE_TYPE_NAME (value));
1161 object_get_property (object, &tmp_value, pspec, trailer);
1162 g_value_convert (&tmp_value, value);
1163 /* g_value_validate (value, pspec); */
1166 g_value_unset (&tmp_value);
1169 g_object_unref (object);
1173 g_object_ref (GObject *object)
1175 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1176 g_return_val_if_fail (object->ref_count > 0, NULL);
1178 object->ref_count += 1;
1184 g_object_unref (GObject *object)
1186 g_return_if_fail (G_IS_OBJECT (object));
1187 g_return_if_fail (object->ref_count > 0);
1189 if (object->ref_count > 1)
1190 object->ref_count -= 1;
1192 g_object_last_unref (object);
1196 g_object_get_qdata (GObject *object,
1199 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1201 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1205 g_object_set_qdata (GObject *object,
1209 g_return_if_fail (G_IS_OBJECT (object));
1210 g_return_if_fail (quark > 0);
1212 g_datalist_id_set_data (&object->qdata, quark, data);
1216 g_object_set_qdata_full (GObject *object,
1219 GDestroyNotify destroy)
1221 g_return_if_fail (G_IS_OBJECT (object));
1222 g_return_if_fail (quark > 0);
1224 g_datalist_id_set_data_full (&object->qdata, quark, data, data ? destroy : NULL);
1228 g_object_steal_qdata (GObject *object,
1231 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1232 g_return_val_if_fail (quark > 0, NULL);
1234 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1238 g_object_get_data (GObject *object,
1243 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1244 g_return_val_if_fail (key != NULL, NULL);
1246 quark = g_quark_try_string (key);
1248 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1252 g_object_set_data (GObject *object,
1256 g_return_if_fail (G_IS_OBJECT (object));
1257 g_return_if_fail (key != NULL);
1259 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1263 g_object_set_data_full (GObject *object,
1266 GDestroyNotify destroy)
1268 g_return_if_fail (G_IS_OBJECT (object));
1269 g_return_if_fail (key != NULL);
1271 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data, data ? destroy : NULL);
1275 g_object_steal_data (GObject *object,
1280 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1281 g_return_val_if_fail (key != NULL, NULL);
1283 quark = g_quark_try_string (key);
1285 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1289 g_value_object_init (GValue *value)
1291 value->data[0].v_pointer = NULL;
1295 g_value_object_free_value (GValue *value)
1297 if (value->data[0].v_pointer)
1298 g_object_unref (value->data[0].v_pointer);
1302 g_value_object_copy_value (const GValue *src_value,
1305 if (src_value->data[0].v_pointer)
1306 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1308 dest_value->data[0].v_pointer = NULL;
1312 g_value_object_peek_pointer (const GValue *value)
1314 return value->data[0].v_pointer;
1318 g_value_object_collect_value (GValue *value,
1320 GType *collect_type,
1321 GTypeCValue *collect_value)
1323 if (collect_value->v_pointer)
1325 GObject *object = collect_value->v_pointer;
1327 if (object->g_type_instance.g_class == NULL)
1328 return g_strconcat ("invalid unclassed object pointer for value type `",
1329 G_VALUE_TYPE_NAME (value),
1332 else if (!g_type_is_a (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1333 return g_strconcat ("invalid object type `",
1334 G_OBJECT_TYPE_NAME (object),
1335 "' for value type `",
1336 G_VALUE_TYPE_NAME (value),
1339 value->data[0].v_pointer = g_object_ref (object);
1342 value->data[0].v_pointer = NULL;
1349 g_value_object_lcopy_value (const GValue *value,
1351 GType *collect_type,
1352 GTypeCValue *collect_value)
1354 GObject **object_p = collect_value->v_pointer;
1357 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1359 *object_p = value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1366 g_value_set_object (GValue *value,
1369 g_return_if_fail (G_IS_VALUE_OBJECT (value));
1371 if (value->data[0].v_pointer)
1373 g_object_unref (value->data[0].v_pointer);
1374 value->data[0].v_pointer = NULL;
1379 g_return_if_fail (G_IS_OBJECT (v_object));
1380 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1382 value->data[0].v_pointer = v_object;
1383 g_object_ref (value->data[0].v_pointer);
1388 g_value_get_object (const GValue *value)
1390 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
1392 return value->data[0].v_pointer;
1396 g_value_dup_object (const GValue *value)
1398 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
1400 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1404 g_signal_connect_object (gpointer instance,
1405 const gchar *detailed_signal,
1406 GCallback c_handler,
1411 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1412 g_return_val_if_fail (detailed_signal != NULL, 0);
1413 g_return_val_if_fail (c_handler != NULL, 0);
1419 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1421 closure = (swapped ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1423 return g_signal_connect_closure (instance, detailed_signal, closure, after);
1426 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, swapped, after);
1432 GClosure *closures[1]; /* flexible array */
1436 object_remove_closure (gpointer data,
1439 GObject *object = data;
1440 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1443 for (i = 0; i < carray->n_closures; i++)
1444 if (carray->closures[i] == closure)
1446 carray->n_closures--;
1447 if (i < carray->n_closures)
1448 carray->closures[i] = carray->closures[carray->n_closures];
1451 g_assert_not_reached ();
1455 destroy_closure_array (gpointer data)
1457 CArray *carray = data;
1458 GObject *object = carray->object;
1459 guint i, n = carray->n_closures;
1461 for (i = 0; i < n; i++)
1463 GClosure *closure = carray->closures[i];
1465 /* removing object_remove_closure() upfront is probably faster than
1466 * letting it fiddle with quark_closure_array which is empty anyways
1468 g_closure_remove_inotify (closure, object, object_remove_closure);
1469 g_closure_invalidate (closure);
1475 g_object_watch_closure (GObject *object,
1480 g_return_if_fail (G_IS_OBJECT (object));
1481 g_return_if_fail (closure != NULL);
1482 g_return_if_fail (closure->is_invalid == FALSE);
1483 g_return_if_fail (closure->in_marshal == FALSE);
1484 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1486 g_closure_add_inotify (closure, object, object_remove_closure);
1487 g_closure_add_marshal_guards (closure,
1488 object, (GClosureNotify) g_object_ref,
1489 object, (GClosureNotify) g_object_unref);
1490 carray = g_object_steal_qdata (object, quark_closure_array);
1493 carray = g_renew (CArray, NULL, 1);
1494 carray->object = object;
1495 carray->n_closures = 1;
1496 carray->closures[0] = closure;
1497 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1501 guint i = carray->n_closures++;
1503 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1504 carray->closures[i] = closure;
1505 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1510 g_closure_new_object (guint sizeof_closure,
1515 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1516 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1518 closure = g_closure_new_simple (sizeof_closure, object);
1519 g_object_watch_closure (object, closure);
1525 g_cclosure_new_object (GCallback callback_func,
1528 GObject *object = _object;
1531 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1532 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1533 g_return_val_if_fail (callback_func != NULL, NULL);
1535 closure = g_cclosure_new (callback_func, object, NULL);
1536 g_object_watch_closure (object, closure);
1542 g_cclosure_new_object_swap (GCallback callback_func,
1545 GObject *object = _object;
1548 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1549 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1550 g_return_val_if_fail (callback_func != NULL, NULL);
1552 closure = g_cclosure_new_swap (callback_func, object, NULL);
1553 g_object_watch_closure (object, closure);