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))
50 /* --- properties --- */
56 /* --- prototypes --- */
57 static void g_object_base_class_init (GObjectClass *class);
58 static void g_object_base_class_finalize (GObjectClass *class);
59 static void g_object_do_class_init (GObjectClass *class);
60 static void g_object_init (GObject *object);
61 static GObject* g_object_constructor (GType type,
62 guint n_construct_properties,
63 GObjectConstructParam *construct_params);
64 static void g_object_last_unref (GObject *object);
65 static void g_object_real_dispose (GObject *object);
66 static void g_object_finalize (GObject *object);
67 static void g_object_do_set_property (GObject *object,
71 static void g_object_do_get_property (GObject *object,
75 static void g_value_object_init (GValue *value);
76 static void g_value_object_free_value (GValue *value);
77 static void g_value_object_copy_value (const GValue *src_value,
79 static void g_value_object_transform_value (const GValue *src_value,
81 static gpointer g_value_object_peek_pointer (const GValue *value);
82 static gchar* g_value_object_collect_value (GValue *value,
83 guint n_collect_values,
84 GTypeCValue *collect_values,
86 static gchar* g_value_object_lcopy_value (const GValue *value,
87 guint n_collect_values,
88 GTypeCValue *collect_values,
90 static void g_object_dispatch_properties_changed (GObject *object,
93 static inline void object_get_property (GObject *object,
96 static inline void object_set_property (GObject *object,
99 GObjectNotifyQueue *nqueue);
102 /* --- variables --- */
103 static GQuark quark_closure_array = 0;
104 static GQuark quark_weak_refs = 0;
105 static GParamSpecPool *pspec_pool = NULL;
106 static GObjectNotifyContext property_notify_context = { 0, };
107 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
110 /* --- functions --- */
111 #ifdef G_ENABLE_DEBUG
112 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
113 G_LOCK_DEFINE_STATIC (debug_objects);
114 static volatile GObject *g_trap_object_ref = NULL;
115 static guint debug_objects_count = 0;
116 static GHashTable *debug_objects_ht = NULL;
118 debug_objects_foreach (gpointer key,
122 GObject *object = value;
124 g_message ("[%p] stale %s\tref_count=%u",
126 G_OBJECT_TYPE_NAME (object),
130 debug_objects_atexit (void)
134 G_LOCK (debug_objects);
135 g_message ("stale GObjects: %u", debug_objects_count);
136 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
137 G_UNLOCK (debug_objects);
140 #endif /* G_ENABLE_DEBUG */
143 g_object_type_init (void) /* sync with gtype.c */
145 static gboolean initialized = FALSE;
146 static const GTypeFundamentalInfo finfo = {
147 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
149 static GTypeInfo info = {
150 sizeof (GObjectClass),
151 (GBaseInitFunc) g_object_base_class_init,
152 (GBaseFinalizeFunc) g_object_base_class_finalize,
153 (GClassInitFunc) g_object_do_class_init,
154 NULL /* class_destroy */,
155 NULL /* class_data */,
158 (GInstanceInitFunc) g_object_init,
159 NULL, /* value_table */
161 static const GTypeValueTable value_table = {
162 g_value_object_init, /* value_init */
163 g_value_object_free_value, /* value_free */
164 g_value_object_copy_value, /* value_copy */
165 g_value_object_peek_pointer, /* value_peek_pointer */
166 "p", /* collect_format */
167 g_value_object_collect_value, /* collect_value */
168 "p", /* lcopy_format */
169 g_value_object_lcopy_value, /* lcopy_value */
173 g_return_if_fail (initialized == FALSE);
178 info.value_table = &value_table;
179 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
180 g_assert (type == G_TYPE_OBJECT);
181 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
183 #ifdef G_ENABLE_DEBUG
186 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
187 g_atexit (debug_objects_atexit);
189 #endif /* G_ENABLE_DEBUG */
193 g_object_base_class_init (GObjectClass *class)
195 GObjectClass *pclass = g_type_class_peek_parent (class);
197 /* reset instance specific fields and methods that don't get inherited */
198 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
199 class->get_property = NULL;
200 class->set_property = NULL;
204 g_object_base_class_finalize (GObjectClass *class)
208 g_message ("finalizing base class of %s", G_OBJECT_CLASS_NAME (class));
210 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
212 g_slist_free (class->construct_properties);
213 class->construct_properties = NULL;
214 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
215 for (node = list; node; node = node->next)
217 GParamSpec *pspec = node->data;
219 g_param_spec_pool_remove (pspec_pool, pspec);
220 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
221 g_param_spec_unref (pspec);
227 g_object_notify_dispatcher (GObject *object,
231 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
235 g_object_do_class_init (GObjectClass *class)
237 /* read the comment about typedef struct CArray; on why not to change this quark */
238 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
240 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
241 pspec_pool = g_param_spec_pool_new (TRUE);
242 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
243 property_notify_context.dispatcher = g_object_notify_dispatcher;
245 class->constructor = g_object_constructor;
246 class->set_property = g_object_do_set_property;
247 class->get_property = g_object_do_get_property;
248 class->dispose = g_object_real_dispose;
249 class->finalize = g_object_finalize;
250 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
251 class->notify = NULL;
253 gobject_signals[NOTIFY] =
254 g_signal_new ("notify",
255 G_TYPE_FROM_CLASS (class),
256 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
257 G_STRUCT_OFFSET (GObjectClass, notify),
259 g_cclosure_marshal_VOID__PARAM,
265 g_object_class_install_property (GObjectClass *class,
269 g_return_if_fail (G_IS_OBJECT_CLASS (class));
270 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
271 if (pspec->flags & G_PARAM_WRITABLE)
272 g_return_if_fail (class->set_property != NULL);
273 if (pspec->flags & G_PARAM_READABLE)
274 g_return_if_fail (class->get_property != NULL);
275 g_return_if_fail (property_id > 0);
276 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
277 if (pspec->flags & G_PARAM_CONSTRUCT)
278 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
279 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
280 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
282 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
284 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
285 G_OBJECT_CLASS_NAME (class),
290 g_param_spec_ref (pspec);
291 g_param_spec_sink (pspec);
292 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
293 g_param_spec_pool_insert (pspec_pool, pspec, G_OBJECT_CLASS_TYPE (class));
294 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
295 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
297 /* for property overrides of construct poperties, we have to get rid
298 * of the overidden inherited construct property
300 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
301 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
302 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
306 g_object_class_find_property (GObjectClass *class,
307 const gchar *property_name)
309 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
310 g_return_val_if_fail (property_name != NULL, NULL);
312 return g_param_spec_pool_lookup (pspec_pool,
314 G_OBJECT_CLASS_TYPE (class),
318 GParamSpec** /* free result */
319 g_object_class_list_properties (GObjectClass *class,
320 guint *n_properties_p)
325 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
327 pspecs = g_param_spec_pool_list (pspec_pool,
328 G_OBJECT_CLASS_TYPE (class),
337 g_object_init (GObject *object)
339 object->ref_count = 1;
340 g_datalist_init (&object->qdata);
342 /* freeze object's notification queue, g_object_newv() preserves pairedness */
343 g_object_notify_queue_freeze (object, &property_notify_context);
345 #ifdef G_ENABLE_DEBUG
348 G_LOCK (debug_objects);
349 debug_objects_count++;
350 g_hash_table_insert (debug_objects_ht, object, object);
351 G_UNLOCK (debug_objects);
353 #endif /* G_ENABLE_DEBUG */
357 g_object_do_set_property (GObject *object,
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
371 g_object_do_get_property (GObject *object,
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
385 g_object_real_dispose (GObject *object)
389 g_signal_handlers_destroy (object);
390 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
392 /* yes, temporarily altering the ref_count is hackish, but that
393 * enforces people not jerking around with weak_ref notifiers
395 ref_count = object->ref_count;
396 object->ref_count = 0;
397 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
398 object->ref_count = ref_count;
402 g_object_finalize (GObject *object)
404 g_datalist_clear (&object->qdata);
406 #ifdef G_ENABLE_DEBUG
409 G_LOCK (debug_objects);
410 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
411 g_hash_table_remove (debug_objects_ht, object);
412 debug_objects_count--;
413 G_UNLOCK (debug_objects);
415 #endif /* G_ENABLE_DEBUG */
419 g_object_last_unref (GObject *object)
421 g_return_if_fail (object->ref_count > 0);
423 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
424 G_OBJECT_GET_CLASS (object)->dispose (object);
426 #ifdef G_ENABLE_DEBUG
427 if (g_trap_object_ref == object)
429 #endif /* G_ENABLE_DEBUG */
431 object->ref_count -= 1;
433 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
435 g_signal_handlers_destroy (object);
436 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
437 G_OBJECT_GET_CLASS (object)->finalize (object);
438 #ifdef G_ENABLE_DEBUG
441 /* catch objects not chaining finalize handlers */
442 G_LOCK (debug_objects);
443 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
444 G_UNLOCK (debug_objects);
446 #endif /* G_ENABLE_DEBUG */
447 g_type_free_instance ((GTypeInstance*) object);
452 g_object_dispatch_properties_changed (GObject *object,
458 for (i = 0; i < n_pspecs; i++)
459 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
463 g_object_run_dispose (GObject *object)
465 g_return_if_fail (G_IS_OBJECT (object));
466 g_return_if_fail (object->ref_count > 0);
468 g_object_ref (object);
469 G_OBJECT_GET_CLASS (object)->dispose (object);
470 g_object_unref (object);
474 g_object_freeze_notify (GObject *object)
476 g_return_if_fail (G_IS_OBJECT (object));
477 if (!object->ref_count)
480 g_object_ref (object);
481 g_object_notify_queue_freeze (object, &property_notify_context);
482 g_object_unref (object);
486 g_object_notify (GObject *object,
487 const gchar *property_name)
491 g_return_if_fail (G_IS_OBJECT (object));
492 g_return_if_fail (property_name != NULL);
493 if (!object->ref_count)
496 g_object_ref (object);
497 pspec = g_param_spec_pool_lookup (pspec_pool,
499 G_OBJECT_TYPE (object),
502 g_warning ("%s: object class `%s' has no property named `%s'",
504 G_OBJECT_TYPE_NAME (object),
508 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
510 g_object_notify_queue_add (object, nqueue, pspec);
511 g_object_notify_queue_thaw (object, nqueue);
513 g_object_unref (object);
517 g_object_thaw_notify (GObject *object)
519 GObjectNotifyQueue *nqueue;
521 g_return_if_fail (G_IS_OBJECT (object));
522 if (!object->ref_count)
525 g_object_ref (object);
526 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
527 if (!nqueue || !nqueue->freeze_count)
528 g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
529 G_OBJECT_TYPE_NAME (object), object);
531 g_object_notify_queue_thaw (object, nqueue);
532 g_object_unref (object);
536 object_get_property (GObject *object,
540 GObjectClass *class = g_type_class_peek (pspec->owner_type);
542 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
546 object_set_property (GObject *object,
549 GObjectNotifyQueue *nqueue)
551 GValue tmp_value = { 0, };
552 GObjectClass *class = g_type_class_peek (pspec->owner_type);
554 /* provide a copy to work from, convert (if necessary) and validate */
555 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
556 if (!g_value_transform (value, &tmp_value))
557 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
559 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
560 G_VALUE_TYPE_NAME (value));
561 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
563 gchar *contents = g_strdup_value_contents (value);
565 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
567 G_VALUE_TYPE_NAME (value),
569 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
574 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
575 g_object_notify_queue_add (object, nqueue, pspec);
577 g_value_unset (&tmp_value);
581 g_object_new (GType object_type,
582 const gchar *first_property_name,
588 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
590 va_start (var_args, first_property_name);
591 object = g_object_new_valist (object_type, first_property_name, var_args);
598 g_object_newv (GType object_type,
600 GParameter *parameters)
602 GObjectConstructParam *cparams, *oparams;
603 GObjectNotifyQueue *nqueue;
607 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
612 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
614 class = g_type_class_ref (object_type);
615 for (slist = class->construct_properties; slist; slist = slist->next)
617 clist = g_list_prepend (clist, slist->data);
618 n_total_cparams += 1;
621 /* collect parameters, sort into construction and normal ones */
622 oparams = g_new (GObjectConstructParam, n_parameters);
623 cparams = g_new (GObjectConstructParam, n_total_cparams);
624 for (i = 0; i < n_parameters; i++)
626 GValue *value = ¶meters[i].value;
627 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
633 g_warning ("%s: object class `%s' has no property named `%s'",
635 g_type_name (object_type),
639 if (!(pspec->flags & G_PARAM_WRITABLE))
641 g_warning ("%s: property `%s' of object class `%s' is not writable",
644 g_type_name (object_type));
647 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
649 GList *list = g_list_find (clist, pspec);
653 g_warning (G_STRLOC ": construct property \"%s\" for object `%s' can't be set twice",
654 pspec->name, g_type_name (object_type));
657 cparams[n_cparams].pspec = pspec;
658 cparams[n_cparams].value = value;
663 list->prev->next = list->next;
665 list->next->prev = list->prev;
666 g_list_free_1 (list);
670 oparams[n_oparams].pspec = pspec;
671 oparams[n_oparams].value = value;
676 /* set remaining construction properties to default values */
677 n_cvalues = n_total_cparams - n_cparams;
678 cvalues = g_new (GValue, n_cvalues);
681 GList *tmp = clist->next;
682 GParamSpec *pspec = clist->data;
683 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
686 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
687 g_param_value_set_default (pspec, value);
689 cparams[n_cparams].pspec = pspec;
690 cparams[n_cparams].value = value;
693 g_list_free_1 (clist);
697 /* construct object from construction parameters */
698 object = class->constructor (object_type, n_total_cparams, cparams);
700 /* free construction values */
703 g_value_unset (cvalues + n_cvalues);
706 /* release g_object_init() notification queue freeze_count */
707 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
708 g_object_notify_queue_thaw (object, nqueue);
710 /* set remaining properties */
711 for (i = 0; i < n_oparams; i++)
712 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
715 g_type_class_unref (class);
717 /* release our own freeze count and handle notifications */
718 g_object_notify_queue_thaw (object, nqueue);
724 g_object_new_valist (GType object_type,
725 const gchar *first_property_name,
732 guint n_params = 0, n_alloced_params = 16;
734 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
736 if (!first_property_name)
737 return g_object_newv (object_type, 0, NULL);
739 class = g_type_class_ref (object_type);
741 params = g_new (GParameter, n_alloced_params);
742 name = first_property_name;
746 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
752 g_warning ("%s: object class `%s' has no property named `%s'",
754 g_type_name (object_type),
758 if (n_params >= n_alloced_params)
760 n_alloced_params += 16;
761 params = g_renew (GParameter, params, n_alloced_params);
763 params[n_params].name = name;
764 params[n_params].value.g_type = 0;
765 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
766 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
769 g_warning ("%s: %s", G_STRLOC, error);
772 /* we purposely leak the value here, it might not be
773 * in a sane state if an error condition occoured
778 name = va_arg (var_args, gchar*);
781 object = g_object_newv (object_type, n_params, params);
784 g_value_unset (¶ms[n_params].value);
787 g_type_class_unref (class);
793 g_object_constructor (GType type,
794 guint n_construct_properties,
795 GObjectConstructParam *construct_params)
800 object = (GObject*) g_type_create_instance (type);
802 /* set construction parameters */
803 if (n_construct_properties)
805 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
807 /* set construct properties */
808 while (n_construct_properties--)
810 GValue *value = construct_params->value;
811 GParamSpec *pspec = construct_params->pspec;
814 object_set_property (object, pspec, value, nqueue);
816 g_object_notify_queue_thaw (object, nqueue);
817 /* the notification queue is still frozen from g_object_init(), so
818 * we don't need to handle it here, g_object_newv() takes
827 g_object_set_valist (GObject *object,
828 const gchar *first_property_name,
831 GObjectNotifyQueue *nqueue;
834 g_return_if_fail (G_IS_OBJECT (object));
836 g_object_ref (object);
837 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
839 name = first_property_name;
842 GValue value = { 0, };
846 pspec = g_param_spec_pool_lookup (pspec_pool,
848 G_OBJECT_TYPE (object),
852 g_warning ("%s: object class `%s' has no property named `%s'",
854 G_OBJECT_TYPE_NAME (object),
858 if (!(pspec->flags & G_PARAM_WRITABLE))
860 g_warning ("%s: property `%s' of object class `%s' is not writable",
863 G_OBJECT_TYPE_NAME (object));
867 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
869 G_VALUE_COLLECT (&value, var_args, 0, &error);
872 g_warning ("%s: %s", G_STRLOC, error);
875 /* we purposely leak the value here, it might not be
876 * in a sane state if an error condition occoured
881 object_set_property (object, pspec, &value, nqueue);
882 g_value_unset (&value);
884 name = va_arg (var_args, gchar*);
887 g_object_notify_queue_thaw (object, nqueue);
888 g_object_unref (object);
892 g_object_get_valist (GObject *object,
893 const gchar *first_property_name,
898 g_return_if_fail (G_IS_OBJECT (object));
900 g_object_ref (object);
902 name = first_property_name;
906 GValue value = { 0, };
910 pspec = g_param_spec_pool_lookup (pspec_pool,
912 G_OBJECT_TYPE (object),
916 g_warning ("%s: object class `%s' has no property named `%s'",
918 G_OBJECT_TYPE_NAME (object),
922 if (!(pspec->flags & G_PARAM_READABLE))
924 g_warning ("%s: property `%s' of object class `%s' is not readable",
927 G_OBJECT_TYPE_NAME (object));
931 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
933 object_get_property (object, pspec, &value);
935 G_VALUE_LCOPY (&value, var_args, 0, &error);
938 g_warning ("%s: %s", G_STRLOC, error);
940 g_value_unset (&value);
944 g_value_unset (&value);
946 name = va_arg (var_args, gchar*);
949 g_object_unref (object);
953 g_object_set (gpointer _object,
954 const gchar *first_property_name,
957 GObject *object = _object;
960 g_return_if_fail (G_IS_OBJECT (object));
962 va_start (var_args, first_property_name);
963 g_object_set_valist (object, first_property_name, var_args);
968 g_object_get (gpointer _object,
969 const gchar *first_property_name,
972 GObject *object = _object;
975 g_return_if_fail (G_IS_OBJECT (object));
977 va_start (var_args, first_property_name);
978 g_object_get_valist (object, first_property_name, var_args);
983 g_object_set_property (GObject *object,
984 const gchar *property_name,
987 GObjectNotifyQueue *nqueue;
990 g_return_if_fail (G_IS_OBJECT (object));
991 g_return_if_fail (property_name != NULL);
992 g_return_if_fail (G_IS_VALUE (value));
994 g_object_ref (object);
995 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
997 pspec = g_param_spec_pool_lookup (pspec_pool,
999 G_OBJECT_TYPE (object),
1002 g_warning ("%s: object class `%s' has no property named `%s'",
1004 G_OBJECT_TYPE_NAME (object),
1007 object_set_property (object, pspec, value, nqueue);
1009 g_object_notify_queue_thaw (object, nqueue);
1010 g_object_unref (object);
1014 g_object_get_property (GObject *object,
1015 const gchar *property_name,
1020 g_return_if_fail (G_IS_OBJECT (object));
1021 g_return_if_fail (property_name != NULL);
1022 g_return_if_fail (G_IS_VALUE (value));
1024 g_object_ref (object);
1026 pspec = g_param_spec_pool_lookup (pspec_pool,
1028 G_OBJECT_TYPE (object),
1031 g_warning ("%s: object class `%s' has no property named `%s'",
1033 G_OBJECT_TYPE_NAME (object),
1037 GValue *prop_value, tmp_value = { 0, };
1039 /* auto-conversion of the callers value type
1041 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1043 g_value_reset (value);
1046 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1048 g_warning ("can't retrieve property `%s' of type `%s' as value of type `%s'",
1050 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1051 G_VALUE_TYPE_NAME (value));
1052 g_object_unref (object);
1057 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1058 prop_value = &tmp_value;
1060 object_get_property (object, pspec, prop_value);
1061 if (prop_value != value)
1063 g_value_transform (prop_value, value);
1064 g_value_unset (&tmp_value);
1068 g_object_unref (object);
1072 g_object_connect (gpointer _object,
1073 const gchar *signal_spec,
1076 GObject *object = _object;
1079 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1080 g_return_val_if_fail (object->ref_count > 0, object);
1082 va_start (var_args, signal_spec);
1085 GCallback callback = va_arg (var_args, GCallback);
1086 gpointer data = va_arg (var_args, gpointer);
1089 if (strncmp (signal_spec, "signal::", 8) == 0)
1090 sid = g_signal_connect_data (object, signal_spec + 8,
1091 callback, data, NULL,
1093 else if (strncmp (signal_spec, "object_signal::", 15) == 0)
1094 sid = g_signal_connect_object (object, signal_spec + 15,
1097 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0)
1098 sid = g_signal_connect_data (object, signal_spec + 16,
1099 callback, data, NULL,
1101 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0)
1102 sid = g_signal_connect_object (object, signal_spec + 23,
1105 else if (strncmp (signal_spec, "signal_after::", 14) == 0)
1106 sid = g_signal_connect_data (object, signal_spec + 14,
1107 callback, data, NULL,
1109 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0)
1110 sid = g_signal_connect_object (object, signal_spec + 21,
1113 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0)
1114 sid = g_signal_connect_data (object, signal_spec + 22,
1115 callback, data, NULL,
1116 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1117 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0)
1118 sid = g_signal_connect_object (object, signal_spec + 29,
1120 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1123 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1126 signal_spec = va_arg (var_args, gchar*);
1134 g_object_disconnect (gpointer _object,
1135 const gchar *signal_spec,
1138 GObject *object = _object;
1141 g_return_if_fail (G_IS_OBJECT (object));
1142 g_return_if_fail (object->ref_count > 0);
1144 va_start (var_args, signal_spec);
1147 GCallback callback = va_arg (var_args, GCallback);
1148 gpointer data = va_arg (var_args, gpointer);
1149 guint sid = 0, detail = 0, mask = 0;
1151 if (strncmp (signal_spec, "any_signal::", 12) == 0)
1154 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1156 else if (strcmp (signal_spec, "any_signal") == 0)
1159 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1163 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1167 if ((mask & G_SIGNAL_MATCH_ID) &&
1168 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1169 g_warning ("%s: invalid signal name \"%s\"", G_STRLOC, signal_spec);
1170 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1172 NULL, (gpointer)callback, data))
1173 g_warning (G_STRLOC ": signal handler %p(%p) is not connected", callback, data);
1174 signal_spec = va_arg (var_args, gchar*);
1185 } weak_refs[1]; /* flexible array */
1189 weak_refs_notify (gpointer data)
1191 WeakRefStack *wstack = data;
1194 for (i = 0; i < wstack->n_weak_refs; i++)
1195 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1200 g_object_weak_ref (GObject *object,
1204 WeakRefStack *wstack;
1207 g_return_if_fail (G_IS_OBJECT (object));
1208 g_return_if_fail (notify != NULL);
1209 g_return_if_fail (object->ref_count >= 1);
1211 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1214 i = wstack->n_weak_refs++;
1215 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1219 wstack = g_renew (WeakRefStack, NULL, 1);
1220 wstack->object = object;
1221 wstack->n_weak_refs = 1;
1224 wstack->weak_refs[i].notify = notify;
1225 wstack->weak_refs[i].data = data;
1226 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1230 g_object_weak_unref (GObject *object,
1234 WeakRefStack *wstack;
1235 gboolean found_one = FALSE;
1237 g_return_if_fail (G_IS_OBJECT (object));
1238 g_return_if_fail (notify != NULL);
1240 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1245 for (i = 0; i < wstack->n_weak_refs; i++)
1246 if (wstack->weak_refs[i].notify == notify &&
1247 wstack->weak_refs[i].data == data)
1250 wstack->n_weak_refs -= 1;
1251 if (i != wstack->n_weak_refs)
1253 wstack->weak_refs[i].notify = wstack->weak_refs[wstack->n_weak_refs].notify;
1254 wstack->weak_refs[i].data = wstack->weak_refs[wstack->n_weak_refs].data;
1260 g_warning (G_STRLOC ": couldn't find weak ref %p(%p)", notify, data);
1264 g_object_add_weak_pointer (GObject *object,
1265 gpointer *weak_pointer_location)
1267 g_return_if_fail (G_IS_OBJECT (object));
1268 g_return_if_fail (weak_pointer_location != NULL);
1270 g_object_weak_ref (object,
1271 (GWeakNotify) g_nullify_pointer,
1272 weak_pointer_location);
1276 g_object_remove_weak_pointer (GObject *object,
1277 gpointer *weak_pointer_location)
1279 g_return_if_fail (G_IS_OBJECT (object));
1280 g_return_if_fail (weak_pointer_location != NULL);
1282 g_object_weak_unref (object,
1283 (GWeakNotify) g_nullify_pointer,
1284 weak_pointer_location);
1288 g_object_ref (gpointer _object)
1290 GObject *object = _object;
1292 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1293 g_return_val_if_fail (object->ref_count > 0, NULL);
1295 #ifdef G_ENABLE_DEBUG
1296 if (g_trap_object_ref == object)
1298 #endif /* G_ENABLE_DEBUG */
1300 object->ref_count += 1;
1306 g_object_unref (gpointer _object)
1308 GObject *object = _object;
1310 g_return_if_fail (G_IS_OBJECT (object));
1311 g_return_if_fail (object->ref_count > 0);
1313 #ifdef G_ENABLE_DEBUG
1314 if (g_trap_object_ref == object)
1316 #endif /* G_ENABLE_DEBUG */
1318 if (object->ref_count > 1)
1319 object->ref_count -= 1;
1321 g_object_last_unref (object);
1325 g_object_get_qdata (GObject *object,
1328 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1330 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1334 g_object_set_qdata (GObject *object,
1338 g_return_if_fail (G_IS_OBJECT (object));
1339 g_return_if_fail (quark > 0);
1341 g_datalist_id_set_data (&object->qdata, quark, data);
1345 g_object_set_qdata_full (GObject *object,
1348 GDestroyNotify destroy)
1350 g_return_if_fail (G_IS_OBJECT (object));
1351 g_return_if_fail (quark > 0);
1353 g_datalist_id_set_data_full (&object->qdata, quark, data,
1354 data ? destroy : (GDestroyNotify) NULL);
1358 g_object_steal_qdata (GObject *object,
1361 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1362 g_return_val_if_fail (quark > 0, NULL);
1364 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1368 g_object_get_data (GObject *object,
1373 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1374 g_return_val_if_fail (key != NULL, NULL);
1376 quark = g_quark_try_string (key);
1378 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1382 g_object_set_data (GObject *object,
1386 g_return_if_fail (G_IS_OBJECT (object));
1387 g_return_if_fail (key != NULL);
1389 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1393 g_object_set_data_full (GObject *object,
1396 GDestroyNotify destroy)
1398 g_return_if_fail (G_IS_OBJECT (object));
1399 g_return_if_fail (key != NULL);
1401 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1402 data ? destroy : (GDestroyNotify) NULL);
1406 g_object_steal_data (GObject *object,
1411 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1412 g_return_val_if_fail (key != NULL, NULL);
1414 quark = g_quark_try_string (key);
1416 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1420 g_value_object_init (GValue *value)
1422 value->data[0].v_pointer = NULL;
1426 g_value_object_free_value (GValue *value)
1428 if (value->data[0].v_pointer)
1429 g_object_unref (value->data[0].v_pointer);
1433 g_value_object_copy_value (const GValue *src_value,
1436 if (src_value->data[0].v_pointer)
1437 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1439 dest_value->data[0].v_pointer = NULL;
1443 g_value_object_transform_value (const GValue *src_value,
1446 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)))
1447 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1449 dest_value->data[0].v_pointer = NULL;
1453 g_value_object_peek_pointer (const GValue *value)
1455 return value->data[0].v_pointer;
1459 g_value_object_collect_value (GValue *value,
1460 guint n_collect_values,
1461 GTypeCValue *collect_values,
1462 guint collect_flags)
1464 if (collect_values[0].v_pointer)
1466 GObject *object = collect_values[0].v_pointer;
1468 if (object->g_type_instance.g_class == NULL)
1469 return g_strconcat ("invalid unclassed object pointer for value type `",
1470 G_VALUE_TYPE_NAME (value),
1473 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1474 return g_strconcat ("invalid object type `",
1475 G_OBJECT_TYPE_NAME (object),
1476 "' for value type `",
1477 G_VALUE_TYPE_NAME (value),
1480 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1481 value->data[0].v_pointer = g_object_ref (object);
1484 value->data[0].v_pointer = NULL;
1490 g_value_object_lcopy_value (const GValue *value,
1491 guint n_collect_values,
1492 GTypeCValue *collect_values,
1493 guint collect_flags)
1495 GObject **object_p = collect_values[0].v_pointer;
1498 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1500 if (!value->data[0].v_pointer)
1502 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1503 *object_p = value->data[0].v_pointer;
1505 *object_p = g_object_ref (value->data[0].v_pointer);
1511 g_value_set_object (GValue *value,
1514 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1516 if (value->data[0].v_pointer)
1518 g_object_unref (value->data[0].v_pointer);
1519 value->data[0].v_pointer = NULL;
1524 g_return_if_fail (G_IS_OBJECT (v_object));
1525 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1527 value->data[0].v_pointer = v_object;
1528 g_object_ref (value->data[0].v_pointer);
1533 g_value_set_object_take_ownership (GValue *value,
1536 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1538 if (value->data[0].v_pointer)
1540 g_object_unref (value->data[0].v_pointer);
1541 value->data[0].v_pointer = NULL;
1546 g_return_if_fail (G_IS_OBJECT (v_object));
1547 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1549 value->data[0].v_pointer = v_object; /* we take over the reference count */
1554 g_value_get_object (const GValue *value)
1556 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1558 return value->data[0].v_pointer;
1562 g_value_dup_object (const GValue *value)
1564 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1566 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1570 g_signal_connect_object (gpointer instance,
1571 const gchar *detailed_signal,
1572 GCallback c_handler,
1574 GConnectFlags connect_flags)
1576 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1577 g_return_val_if_fail (detailed_signal != NULL, 0);
1578 g_return_val_if_fail (c_handler != NULL, 0);
1584 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1586 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1588 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1591 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1597 GClosure *closures[1]; /* flexible array */
1599 /* don't change this structure without supplying an accessor for
1600 * watched closures, e.g.:
1601 * GSList* g_object_list_watched_closures (GObject *object)
1604 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1605 * carray = g_object_get_data (object, "GObject-closure-array");
1608 * GSList *slist = NULL;
1610 * for (i = 0; i < carray->n_closures; i++)
1611 * slist = g_slist_prepend (slist, carray->closures[i]);
1619 object_remove_closure (gpointer data,
1622 GObject *object = data;
1623 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1626 for (i = 0; i < carray->n_closures; i++)
1627 if (carray->closures[i] == closure)
1629 carray->n_closures--;
1630 if (i < carray->n_closures)
1631 carray->closures[i] = carray->closures[carray->n_closures];
1634 g_assert_not_reached ();
1638 destroy_closure_array (gpointer data)
1640 CArray *carray = data;
1641 GObject *object = carray->object;
1642 guint i, n = carray->n_closures;
1644 for (i = 0; i < n; i++)
1646 GClosure *closure = carray->closures[i];
1648 /* removing object_remove_closure() upfront is probably faster than
1649 * letting it fiddle with quark_closure_array which is empty anyways
1651 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1652 g_closure_invalidate (closure);
1658 g_object_watch_closure (GObject *object,
1664 g_return_if_fail (G_IS_OBJECT (object));
1665 g_return_if_fail (closure != NULL);
1666 g_return_if_fail (closure->is_invalid == FALSE);
1667 g_return_if_fail (closure->in_marshal == FALSE);
1668 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1670 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1671 g_closure_add_marshal_guards (closure,
1672 object, (GClosureNotify) g_object_ref,
1673 object, (GClosureNotify) g_object_unref);
1674 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
1677 carray = g_renew (CArray, NULL, 1);
1678 carray->object = object;
1679 carray->n_closures = 1;
1684 i = carray->n_closures++;
1685 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1687 carray->closures[i] = closure;
1688 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
1692 g_closure_new_object (guint sizeof_closure,
1697 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1698 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1700 closure = g_closure_new_simple (sizeof_closure, object);
1701 g_object_watch_closure (object, closure);
1707 g_cclosure_new_object (GCallback callback_func,
1712 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1713 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1714 g_return_val_if_fail (callback_func != NULL, NULL);
1716 closure = g_cclosure_new (callback_func, object, NULL);
1717 g_object_watch_closure (object, closure);
1723 g_cclosure_new_object_swap (GCallback callback_func,
1728 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1729 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1730 g_return_val_if_fail (callback_func != NULL, NULL);
1732 closure = g_cclosure_new_swap (callback_func, object, NULL);
1733 g_object_watch_closure (object, closure);