1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 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.
25 #include "gvaluecollector.h"
27 #include "gparamspecs.h"
28 #include "gvaluetypes.h"
29 #include "gobjectnotifyqueue.c"
34 #define PREALLOC_CPARAMS (8)
38 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
39 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
49 /* --- properties --- */
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,
70 static void g_object_do_get_property (GObject *object,
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,
78 static void g_value_object_transform_value (const GValue *src_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,
85 static gchar* g_value_object_lcopy_value (const GValue *value,
86 guint n_collect_values,
87 GTypeCValue *collect_values,
89 static void g_object_dispatch_properties_changed (GObject *object,
92 static inline void object_get_property (GObject *object,
95 static inline void object_set_property (GObject *object,
98 GObjectNotifyQueue *nqueue);
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, };
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;
117 debug_objects_foreach (gpointer key,
121 GObject *object = value;
123 g_message ("[%p] stale %s\tref_count=%u",
125 G_OBJECT_TYPE_NAME (object),
129 debug_objects_atexit (void)
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);
139 #endif /* G_ENABLE_DEBUG */
142 g_object_type_init (void) /* sync with gtype.c */
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,
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 */,
157 (GInstanceInitFunc) g_object_init,
158 NULL, /* value_table */
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 */
172 g_return_if_fail (initialized == FALSE);
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);
182 #ifdef G_ENABLE_DEBUG
185 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
186 g_atexit (debug_objects_atexit);
188 #endif /* G_ENABLE_DEBUG */
192 g_object_base_class_init (GObjectClass *class)
194 GObjectClass *pclass = g_type_class_peek_parent (class);
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;
203 g_object_base_class_finalize (GObjectClass *class)
207 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
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)
214 GParamSpec *pspec = node->data;
216 g_param_spec_pool_remove (pspec_pool, pspec);
217 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
218 g_param_spec_unref (pspec);
224 g_object_notify_dispatcher (GObject *object,
228 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
232 g_object_do_class_init (GObjectClass *class)
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");
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;
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;
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),
256 g_cclosure_marshal_VOID__PARAM,
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
267 * Installs a new property. This is usually done in the class initializer.
270 g_object_class_install_property (GObjectClass *class,
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);
287 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
289 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
290 G_OBJECT_CLASS_NAME (class),
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);
302 /* for property overrides of construct poperties, we have to get rid
303 * of the overidden inherited construct property
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);
311 g_object_class_find_property (GObjectClass *class,
312 const gchar *property_name)
314 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
315 g_return_val_if_fail (property_name != NULL, NULL);
317 return g_param_spec_pool_lookup (pspec_pool,
319 G_OBJECT_CLASS_TYPE (class),
323 GParamSpec** /* free result */
324 g_object_class_list_properties (GObjectClass *class,
325 guint *n_properties_p)
330 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
332 pspecs = g_param_spec_pool_list (pspec_pool,
333 G_OBJECT_CLASS_TYPE (class),
342 g_object_init (GObject *object)
344 object->ref_count = 1;
345 g_datalist_init (&object->qdata);
347 /* freeze object's notification queue, g_object_newv() preserves pairedness */
348 g_object_notify_queue_freeze (object, &property_notify_context);
350 #ifdef G_ENABLE_DEBUG
353 G_LOCK (debug_objects);
354 debug_objects_count++;
355 g_hash_table_insert (debug_objects_ht, object, object);
356 G_UNLOCK (debug_objects);
358 #endif /* G_ENABLE_DEBUG */
362 g_object_do_set_property (GObject *object,
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
376 g_object_do_get_property (GObject *object,
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390 g_object_real_dispose (GObject *object)
394 g_signal_handlers_destroy (object);
395 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
397 /* yes, temporarily altering the ref_count is hackish, but that
398 * enforces people not jerking around with weak_ref notifiers
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;
407 g_object_finalize (GObject *object)
409 g_datalist_clear (&object->qdata);
411 #ifdef G_ENABLE_DEBUG
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);
420 #endif /* G_ENABLE_DEBUG */
424 g_object_last_unref (GObject *object)
426 g_return_if_fail (object->ref_count > 0);
428 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
429 G_OBJECT_GET_CLASS (object)->dispose (object);
431 #ifdef G_ENABLE_DEBUG
432 if (g_trap_object_ref == object)
434 #endif /* G_ENABLE_DEBUG */
436 object->ref_count -= 1;
438 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
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
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);
451 #endif /* G_ENABLE_DEBUG */
452 g_type_free_instance ((GTypeInstance*) object);
457 g_object_dispatch_properties_changed (GObject *object,
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]);
468 g_object_run_dispose (GObject *object)
470 g_return_if_fail (G_IS_OBJECT (object));
471 g_return_if_fail (object->ref_count > 0);
473 g_object_ref (object);
474 G_OBJECT_GET_CLASS (object)->dispose (object);
475 g_object_unref (object);
479 g_object_freeze_notify (GObject *object)
481 g_return_if_fail (G_IS_OBJECT (object));
482 if (!object->ref_count)
485 g_object_ref (object);
486 g_object_notify_queue_freeze (object, &property_notify_context);
487 g_object_unref (object);
491 g_object_notify (GObject *object,
492 const gchar *property_name)
496 g_return_if_fail (G_IS_OBJECT (object));
497 g_return_if_fail (property_name != NULL);
498 if (!object->ref_count)
501 g_object_ref (object);
502 pspec = g_param_spec_pool_lookup (pspec_pool,
504 G_OBJECT_TYPE (object),
507 g_warning ("%s: object class `%s' has no property named `%s'",
509 G_OBJECT_TYPE_NAME (object),
513 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
515 g_object_notify_queue_add (object, nqueue, pspec);
516 g_object_notify_queue_thaw (object, nqueue);
518 g_object_unref (object);
522 g_object_thaw_notify (GObject *object)
524 GObjectNotifyQueue *nqueue;
526 g_return_if_fail (G_IS_OBJECT (object));
527 if (!object->ref_count)
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);
536 g_object_notify_queue_thaw (object, nqueue);
537 g_object_unref (object);
541 object_get_property (GObject *object,
545 GObjectClass *class = g_type_class_peek (pspec->owner_type);
547 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
551 object_set_property (GObject *object,
554 GObjectNotifyQueue *nqueue)
556 GValue tmp_value = { 0, };
557 GObjectClass *class = g_type_class_peek (pspec->owner_type);
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'",
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))
568 gchar *contents = g_strdup_value_contents (value);
570 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
572 G_VALUE_TYPE_NAME (value),
574 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
579 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
580 g_object_notify_queue_add (object, nqueue, pspec);
582 g_value_unset (&tmp_value);
586 g_object_new (GType object_type,
587 const gchar *first_property_name,
593 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
595 va_start (var_args, first_property_name);
596 object = g_object_new_valist (object_type, first_property_name, var_args);
603 g_object_newv (GType object_type,
605 GParameter *parameters)
607 GObjectConstructParam *cparams, *oparams;
608 GObjectNotifyQueue *nqueue;
612 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
617 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
619 class = g_type_class_ref (object_type);
620 for (slist = class->construct_properties; slist; slist = slist->next)
622 clist = g_list_prepend (clist, slist->data);
623 n_total_cparams += 1;
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++)
631 GValue *value = ¶meters[i].value;
632 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
638 g_warning ("%s: object class `%s' has no property named `%s'",
640 g_type_name (object_type),
644 if (!(pspec->flags & G_PARAM_WRITABLE))
646 g_warning ("%s: property `%s' of object class `%s' is not writable",
649 g_type_name (object_type));
652 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
654 GList *list = g_list_find (clist, pspec);
658 g_warning (G_STRLOC ": construct property \"%s\" for object `%s' can't be set twice",
659 pspec->name, g_type_name (object_type));
662 cparams[n_cparams].pspec = pspec;
663 cparams[n_cparams].value = value;
668 list->prev->next = list->next;
670 list->next->prev = list->prev;
671 g_list_free_1 (list);
675 oparams[n_oparams].pspec = pspec;
676 oparams[n_oparams].value = value;
681 /* set remaining construction properties to default values */
682 n_cvalues = n_total_cparams - n_cparams;
683 cvalues = g_new (GValue, n_cvalues);
686 GList *tmp = clist->next;
687 GParamSpec *pspec = clist->data;
688 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
691 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
692 g_param_value_set_default (pspec, value);
694 cparams[n_cparams].pspec = pspec;
695 cparams[n_cparams].value = value;
698 g_list_free_1 (clist);
702 /* construct object from construction parameters */
703 object = class->constructor (object_type, n_total_cparams, cparams);
705 /* free construction values */
708 g_value_unset (cvalues + n_cvalues);
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);
715 /* set remaining properties */
716 for (i = 0; i < n_oparams; i++)
717 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
720 g_type_class_unref (class);
722 /* release our own freeze count and handle notifications */
723 g_object_notify_queue_thaw (object, nqueue);
729 g_object_new_valist (GType object_type,
730 const gchar *first_property_name,
737 guint n_params = 0, n_alloced_params = 16;
739 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
741 if (!first_property_name)
742 return g_object_newv (object_type, 0, NULL);
744 class = g_type_class_ref (object_type);
746 params = g_new (GParameter, n_alloced_params);
747 name = first_property_name;
751 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
757 g_warning ("%s: object class `%s' has no property named `%s'",
759 g_type_name (object_type),
763 if (n_params >= n_alloced_params)
765 n_alloced_params += 16;
766 params = g_renew (GParameter, params, n_alloced_params);
768 params[n_params].name = name;
769 params[n_params].value.g_type = 0;
770 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
771 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
774 g_warning ("%s: %s", G_STRLOC, error);
777 /* we purposely leak the value here, it might not be
778 * in a sane state if an error condition occoured
783 name = va_arg (var_args, gchar*);
786 object = g_object_newv (object_type, n_params, params);
789 g_value_unset (¶ms[n_params].value);
792 g_type_class_unref (class);
798 g_object_constructor (GType type,
799 guint n_construct_properties,
800 GObjectConstructParam *construct_params)
805 object = (GObject*) g_type_create_instance (type);
807 /* set construction parameters */
808 if (n_construct_properties)
810 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
812 /* set construct properties */
813 while (n_construct_properties--)
815 GValue *value = construct_params->value;
816 GParamSpec *pspec = construct_params->pspec;
819 object_set_property (object, pspec, value, nqueue);
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
832 g_object_set_valist (GObject *object,
833 const gchar *first_property_name,
836 GObjectNotifyQueue *nqueue;
839 g_return_if_fail (G_IS_OBJECT (object));
841 g_object_ref (object);
842 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
844 name = first_property_name;
847 GValue value = { 0, };
851 pspec = g_param_spec_pool_lookup (pspec_pool,
853 G_OBJECT_TYPE (object),
857 g_warning ("%s: object class `%s' has no property named `%s'",
859 G_OBJECT_TYPE_NAME (object),
863 if (!(pspec->flags & G_PARAM_WRITABLE))
865 g_warning ("%s: property `%s' of object class `%s' is not writable",
868 G_OBJECT_TYPE_NAME (object));
872 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
874 G_VALUE_COLLECT (&value, var_args, 0, &error);
877 g_warning ("%s: %s", G_STRLOC, error);
880 /* we purposely leak the value here, it might not be
881 * in a sane state if an error condition occoured
886 object_set_property (object, pspec, &value, nqueue);
887 g_value_unset (&value);
889 name = va_arg (var_args, gchar*);
892 g_object_notify_queue_thaw (object, nqueue);
893 g_object_unref (object);
897 g_object_get_valist (GObject *object,
898 const gchar *first_property_name,
903 g_return_if_fail (G_IS_OBJECT (object));
905 g_object_ref (object);
907 name = first_property_name;
911 GValue value = { 0, };
915 pspec = g_param_spec_pool_lookup (pspec_pool,
917 G_OBJECT_TYPE (object),
921 g_warning ("%s: object class `%s' has no property named `%s'",
923 G_OBJECT_TYPE_NAME (object),
927 if (!(pspec->flags & G_PARAM_READABLE))
929 g_warning ("%s: property `%s' of object class `%s' is not readable",
932 G_OBJECT_TYPE_NAME (object));
936 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
938 object_get_property (object, pspec, &value);
940 G_VALUE_LCOPY (&value, var_args, 0, &error);
943 g_warning ("%s: %s", G_STRLOC, error);
945 g_value_unset (&value);
949 g_value_unset (&value);
951 name = va_arg (var_args, gchar*);
954 g_object_unref (object);
958 g_object_set (gpointer _object,
959 const gchar *first_property_name,
962 GObject *object = _object;
965 g_return_if_fail (G_IS_OBJECT (object));
967 va_start (var_args, first_property_name);
968 g_object_set_valist (object, first_property_name, var_args);
973 g_object_get (gpointer _object,
974 const gchar *first_property_name,
977 GObject *object = _object;
980 g_return_if_fail (G_IS_OBJECT (object));
982 va_start (var_args, first_property_name);
983 g_object_get_valist (object, first_property_name, var_args);
988 g_object_set_property (GObject *object,
989 const gchar *property_name,
992 GObjectNotifyQueue *nqueue;
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));
999 g_object_ref (object);
1000 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1002 pspec = g_param_spec_pool_lookup (pspec_pool,
1004 G_OBJECT_TYPE (object),
1007 g_warning ("%s: object class `%s' has no property named `%s'",
1009 G_OBJECT_TYPE_NAME (object),
1012 object_set_property (object, pspec, value, nqueue);
1014 g_object_notify_queue_thaw (object, nqueue);
1015 g_object_unref (object);
1019 g_object_get_property (GObject *object,
1020 const gchar *property_name,
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));
1029 g_object_ref (object);
1031 pspec = g_param_spec_pool_lookup (pspec_pool,
1033 G_OBJECT_TYPE (object),
1036 g_warning ("%s: object class `%s' has no property named `%s'",
1038 G_OBJECT_TYPE_NAME (object),
1042 GValue *prop_value, tmp_value = { 0, };
1044 /* auto-conversion of the callers value type
1046 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1048 g_value_reset (value);
1051 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1053 g_warning ("can't retrieve property `%s' of type `%s' as value of type `%s'",
1055 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1056 G_VALUE_TYPE_NAME (value));
1057 g_object_unref (object);
1062 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1063 prop_value = &tmp_value;
1065 object_get_property (object, pspec, prop_value);
1066 if (prop_value != value)
1068 g_value_transform (prop_value, value);
1069 g_value_unset (&tmp_value);
1073 g_object_unref (object);
1077 g_object_connect (gpointer _object,
1078 const gchar *signal_spec,
1081 GObject *object = _object;
1084 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1085 g_return_val_if_fail (object->ref_count > 0, object);
1087 va_start (var_args, signal_spec);
1090 GCallback callback = va_arg (var_args, GCallback);
1091 gpointer data = va_arg (var_args, gpointer);
1094 if (strncmp (signal_spec, "signal::", 8) == 0)
1095 sid = g_signal_connect_data (object, signal_spec + 8,
1096 callback, data, NULL,
1098 else if (strncmp (signal_spec, "object_signal::", 15) == 0)
1099 sid = g_signal_connect_object (object, signal_spec + 15,
1102 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0)
1103 sid = g_signal_connect_data (object, signal_spec + 16,
1104 callback, data, NULL,
1106 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0)
1107 sid = g_signal_connect_object (object, signal_spec + 23,
1110 else if (strncmp (signal_spec, "signal_after::", 14) == 0)
1111 sid = g_signal_connect_data (object, signal_spec + 14,
1112 callback, data, NULL,
1114 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0)
1115 sid = g_signal_connect_object (object, signal_spec + 21,
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,
1125 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1128 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1131 signal_spec = va_arg (var_args, gchar*);
1139 g_object_disconnect (gpointer _object,
1140 const gchar *signal_spec,
1143 GObject *object = _object;
1146 g_return_if_fail (G_IS_OBJECT (object));
1147 g_return_if_fail (object->ref_count > 0);
1149 va_start (var_args, signal_spec);
1152 GCallback callback = va_arg (var_args, GCallback);
1153 gpointer data = va_arg (var_args, gpointer);
1154 guint sid = 0, detail = 0, mask = 0;
1156 if (strncmp (signal_spec, "any_signal::", 12) == 0)
1159 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1161 else if (strcmp (signal_spec, "any_signal") == 0)
1164 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1168 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
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),
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*);
1190 } weak_refs[1]; /* flexible array */
1194 weak_refs_notify (gpointer data)
1196 WeakRefStack *wstack = data;
1199 for (i = 0; i < wstack->n_weak_refs; i++)
1200 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1205 g_object_weak_ref (GObject *object,
1209 WeakRefStack *wstack;
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);
1216 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1219 i = wstack->n_weak_refs++;
1220 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1224 wstack = g_renew (WeakRefStack, NULL, 1);
1225 wstack->object = object;
1226 wstack->n_weak_refs = 1;
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);
1235 g_object_weak_unref (GObject *object,
1239 WeakRefStack *wstack;
1240 gboolean found_one = FALSE;
1242 g_return_if_fail (G_IS_OBJECT (object));
1243 g_return_if_fail (notify != NULL);
1245 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
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)
1255 wstack->n_weak_refs -= 1;
1256 if (i != wstack->n_weak_refs)
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;
1265 g_warning (G_STRLOC ": couldn't find weak ref %p(%p)", notify, data);
1269 g_object_add_weak_pointer (GObject *object,
1270 gpointer *weak_pointer_location)
1272 g_return_if_fail (G_IS_OBJECT (object));
1273 g_return_if_fail (weak_pointer_location != NULL);
1275 g_object_weak_ref (object,
1276 (GWeakNotify) g_nullify_pointer,
1277 weak_pointer_location);
1281 g_object_remove_weak_pointer (GObject *object,
1282 gpointer *weak_pointer_location)
1284 g_return_if_fail (G_IS_OBJECT (object));
1285 g_return_if_fail (weak_pointer_location != NULL);
1287 g_object_weak_unref (object,
1288 (GWeakNotify) g_nullify_pointer,
1289 weak_pointer_location);
1293 g_object_ref (gpointer _object)
1295 GObject *object = _object;
1297 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1298 g_return_val_if_fail (object->ref_count > 0, NULL);
1300 #ifdef G_ENABLE_DEBUG
1301 if (g_trap_object_ref == object)
1303 #endif /* G_ENABLE_DEBUG */
1305 object->ref_count += 1;
1311 g_object_unref (gpointer _object)
1313 GObject *object = _object;
1315 g_return_if_fail (G_IS_OBJECT (object));
1316 g_return_if_fail (object->ref_count > 0);
1318 #ifdef G_ENABLE_DEBUG
1319 if (g_trap_object_ref == object)
1321 #endif /* G_ENABLE_DEBUG */
1323 if (object->ref_count > 1)
1324 object->ref_count -= 1;
1326 g_object_last_unref (object);
1330 g_object_get_qdata (GObject *object,
1333 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1335 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1339 g_object_set_qdata (GObject *object,
1343 g_return_if_fail (G_IS_OBJECT (object));
1344 g_return_if_fail (quark > 0);
1346 g_datalist_id_set_data (&object->qdata, quark, data);
1350 g_object_set_qdata_full (GObject *object,
1353 GDestroyNotify destroy)
1355 g_return_if_fail (G_IS_OBJECT (object));
1356 g_return_if_fail (quark > 0);
1358 g_datalist_id_set_data_full (&object->qdata, quark, data,
1359 data ? destroy : (GDestroyNotify) NULL);
1363 g_object_steal_qdata (GObject *object,
1366 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1367 g_return_val_if_fail (quark > 0, NULL);
1369 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1373 g_object_get_data (GObject *object,
1378 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1379 g_return_val_if_fail (key != NULL, NULL);
1381 quark = g_quark_try_string (key);
1383 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1387 g_object_set_data (GObject *object,
1391 g_return_if_fail (G_IS_OBJECT (object));
1392 g_return_if_fail (key != NULL);
1394 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1398 g_object_set_data_full (GObject *object,
1401 GDestroyNotify destroy)
1403 g_return_if_fail (G_IS_OBJECT (object));
1404 g_return_if_fail (key != NULL);
1406 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1407 data ? destroy : (GDestroyNotify) NULL);
1411 g_object_steal_data (GObject *object,
1416 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1417 g_return_val_if_fail (key != NULL, NULL);
1419 quark = g_quark_try_string (key);
1421 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1425 g_value_object_init (GValue *value)
1427 value->data[0].v_pointer = NULL;
1431 g_value_object_free_value (GValue *value)
1433 if (value->data[0].v_pointer)
1434 g_object_unref (value->data[0].v_pointer);
1438 g_value_object_copy_value (const GValue *src_value,
1441 if (src_value->data[0].v_pointer)
1442 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1444 dest_value->data[0].v_pointer = NULL;
1448 g_value_object_transform_value (const GValue *src_value,
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);
1454 dest_value->data[0].v_pointer = NULL;
1458 g_value_object_peek_pointer (const GValue *value)
1460 return value->data[0].v_pointer;
1464 g_value_object_collect_value (GValue *value,
1465 guint n_collect_values,
1466 GTypeCValue *collect_values,
1467 guint collect_flags)
1469 if (collect_values[0].v_pointer)
1471 GObject *object = collect_values[0].v_pointer;
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),
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),
1485 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1486 value->data[0].v_pointer = g_object_ref (object);
1489 value->data[0].v_pointer = NULL;
1495 g_value_object_lcopy_value (const GValue *value,
1496 guint n_collect_values,
1497 GTypeCValue *collect_values,
1498 guint collect_flags)
1500 GObject **object_p = collect_values[0].v_pointer;
1503 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1505 if (!value->data[0].v_pointer)
1507 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1508 *object_p = value->data[0].v_pointer;
1510 *object_p = g_object_ref (value->data[0].v_pointer);
1516 g_value_set_object (GValue *value,
1519 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1521 if (value->data[0].v_pointer)
1523 g_object_unref (value->data[0].v_pointer);
1524 value->data[0].v_pointer = NULL;
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)));
1532 value->data[0].v_pointer = v_object;
1533 g_object_ref (value->data[0].v_pointer);
1538 g_value_set_object_take_ownership (GValue *value,
1541 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1543 if (value->data[0].v_pointer)
1545 g_object_unref (value->data[0].v_pointer);
1546 value->data[0].v_pointer = NULL;
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)));
1554 value->data[0].v_pointer = v_object; /* we take over the reference count */
1559 g_value_get_object (const GValue *value)
1561 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1563 return value->data[0].v_pointer;
1567 g_value_dup_object (const GValue *value)
1569 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1571 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
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.
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.
1585 * Return value: the handler id.
1588 g_signal_connect_object (gpointer instance,
1589 const gchar *detailed_signal,
1590 GCallback c_handler,
1592 GConnectFlags connect_flags)
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);
1602 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1604 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1606 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1609 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1615 GClosure *closures[1]; /* flexible array */
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)
1622 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1623 * carray = g_object_get_data (object, "GObject-closure-array");
1626 * GSList *slist = NULL;
1628 * for (i = 0; i < carray->n_closures; i++)
1629 * slist = g_slist_prepend (slist, carray->closures[i]);
1637 object_remove_closure (gpointer data,
1640 GObject *object = data;
1641 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1644 for (i = 0; i < carray->n_closures; i++)
1645 if (carray->closures[i] == closure)
1647 carray->n_closures--;
1648 if (i < carray->n_closures)
1649 carray->closures[i] = carray->closures[carray->n_closures];
1652 g_assert_not_reached ();
1656 destroy_closure_array (gpointer data)
1658 CArray *carray = data;
1659 GObject *object = carray->object;
1660 guint i, n = carray->n_closures;
1662 for (i = 0; i < n; i++)
1664 GClosure *closure = carray->closures[i];
1666 /* removing object_remove_closure() upfront is probably faster than
1667 * letting it fiddle with quark_closure_array which is empty anyways
1669 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1670 g_closure_invalidate (closure);
1676 g_object_watch_closure (GObject *object,
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 */
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);
1695 carray = g_renew (CArray, NULL, 1);
1696 carray->object = object;
1697 carray->n_closures = 1;
1702 i = carray->n_closures++;
1703 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1705 carray->closures[i] = closure;
1706 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
1710 g_closure_new_object (guint sizeof_closure,
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 */
1718 closure = g_closure_new_simple (sizeof_closure, object);
1719 g_object_watch_closure (object, closure);
1725 g_cclosure_new_object (GCallback callback_func,
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);
1734 closure = g_cclosure_new (callback_func, object, NULL);
1735 g_object_watch_closure (object, closure);
1741 g_cclosure_new_object_swap (GCallback callback_func,
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);
1750 closure = g_cclosure_new_swap (callback_func, object, NULL);
1751 g_object_watch_closure (object, closure);