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:
480 * @object: a #GObject
482 * Stops emission of "notify" signals on @object. The signals are
483 * queued until g_object_thaw_notify() is called on @object.
486 g_object_freeze_notify (GObject *object)
488 g_return_if_fail (G_IS_OBJECT (object));
489 if (!object->ref_count)
492 g_object_ref (object);
493 g_object_notify_queue_freeze (object, &property_notify_context);
494 g_object_unref (object);
499 * @object: a #GObject
500 * @property_name: the name of a property installed on the class of @object.
502 * Emits a "notify" signal for the property @property_name on @object.
505 g_object_notify (GObject *object,
506 const gchar *property_name)
510 g_return_if_fail (G_IS_OBJECT (object));
511 g_return_if_fail (property_name != NULL);
512 if (!object->ref_count)
515 g_object_ref (object);
516 pspec = g_param_spec_pool_lookup (pspec_pool,
518 G_OBJECT_TYPE (object),
521 g_warning ("%s: object class `%s' has no property named `%s'",
523 G_OBJECT_TYPE_NAME (object),
527 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
529 g_object_notify_queue_add (object, nqueue, pspec);
530 g_object_notify_queue_thaw (object, nqueue);
532 g_object_unref (object);
536 * g_object_thaw_notify:
537 * @object: a #GObject
539 * Reverts the effect of a previous call to g_object_freeze_notify().
540 * This causes all queued "notify" signals on @object to be emitted.
544 g_object_thaw_notify (GObject *object)
546 GObjectNotifyQueue *nqueue;
548 g_return_if_fail (G_IS_OBJECT (object));
549 if (!object->ref_count)
552 g_object_ref (object);
553 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
554 if (!nqueue || !nqueue->freeze_count)
555 g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
556 G_OBJECT_TYPE_NAME (object), object);
558 g_object_notify_queue_thaw (object, nqueue);
559 g_object_unref (object);
563 object_get_property (GObject *object,
567 GObjectClass *class = g_type_class_peek (pspec->owner_type);
569 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
573 object_set_property (GObject *object,
576 GObjectNotifyQueue *nqueue)
578 GValue tmp_value = { 0, };
579 GObjectClass *class = g_type_class_peek (pspec->owner_type);
581 /* provide a copy to work from, convert (if necessary) and validate */
582 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
583 if (!g_value_transform (value, &tmp_value))
584 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
586 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
587 G_VALUE_TYPE_NAME (value));
588 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
590 gchar *contents = g_strdup_value_contents (value);
592 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
594 G_VALUE_TYPE_NAME (value),
596 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
601 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
602 g_object_notify_queue_add (object, nqueue, pspec);
604 g_value_unset (&tmp_value);
608 g_object_new (GType object_type,
609 const gchar *first_property_name,
615 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
617 va_start (var_args, first_property_name);
618 object = g_object_new_valist (object_type, first_property_name, var_args);
625 g_object_newv (GType object_type,
627 GParameter *parameters)
629 GObjectConstructParam *cparams, *oparams;
630 GObjectNotifyQueue *nqueue;
634 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
639 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
641 class = g_type_class_ref (object_type);
642 for (slist = class->construct_properties; slist; slist = slist->next)
644 clist = g_list_prepend (clist, slist->data);
645 n_total_cparams += 1;
648 /* collect parameters, sort into construction and normal ones */
649 oparams = g_new (GObjectConstructParam, n_parameters);
650 cparams = g_new (GObjectConstructParam, n_total_cparams);
651 for (i = 0; i < n_parameters; i++)
653 GValue *value = ¶meters[i].value;
654 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
660 g_warning ("%s: object class `%s' has no property named `%s'",
662 g_type_name (object_type),
666 if (!(pspec->flags & G_PARAM_WRITABLE))
668 g_warning ("%s: property `%s' of object class `%s' is not writable",
671 g_type_name (object_type));
674 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
676 GList *list = g_list_find (clist, pspec);
680 g_warning (G_STRLOC ": construct property \"%s\" for object `%s' can't be set twice",
681 pspec->name, g_type_name (object_type));
684 cparams[n_cparams].pspec = pspec;
685 cparams[n_cparams].value = value;
690 list->prev->next = list->next;
692 list->next->prev = list->prev;
693 g_list_free_1 (list);
697 oparams[n_oparams].pspec = pspec;
698 oparams[n_oparams].value = value;
703 /* set remaining construction properties to default values */
704 n_cvalues = n_total_cparams - n_cparams;
705 cvalues = g_new (GValue, n_cvalues);
708 GList *tmp = clist->next;
709 GParamSpec *pspec = clist->data;
710 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
713 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
714 g_param_value_set_default (pspec, value);
716 cparams[n_cparams].pspec = pspec;
717 cparams[n_cparams].value = value;
720 g_list_free_1 (clist);
724 /* construct object from construction parameters */
725 object = class->constructor (object_type, n_total_cparams, cparams);
727 /* free construction values */
730 g_value_unset (cvalues + n_cvalues);
733 /* release g_object_init() notification queue freeze_count */
734 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
735 g_object_notify_queue_thaw (object, nqueue);
737 /* set remaining properties */
738 for (i = 0; i < n_oparams; i++)
739 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
742 g_type_class_unref (class);
744 /* release our own freeze count and handle notifications */
745 g_object_notify_queue_thaw (object, nqueue);
751 g_object_new_valist (GType object_type,
752 const gchar *first_property_name,
759 guint n_params = 0, n_alloced_params = 16;
761 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
763 if (!first_property_name)
764 return g_object_newv (object_type, 0, NULL);
766 class = g_type_class_ref (object_type);
768 params = g_new (GParameter, n_alloced_params);
769 name = first_property_name;
773 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
779 g_warning ("%s: object class `%s' has no property named `%s'",
781 g_type_name (object_type),
785 if (n_params >= n_alloced_params)
787 n_alloced_params += 16;
788 params = g_renew (GParameter, params, n_alloced_params);
790 params[n_params].name = name;
791 params[n_params].value.g_type = 0;
792 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
793 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
796 g_warning ("%s: %s", G_STRLOC, error);
799 /* we purposely leak the value here, it might not be
800 * in a sane state if an error condition occoured
805 name = va_arg (var_args, gchar*);
808 object = g_object_newv (object_type, n_params, params);
811 g_value_unset (¶ms[n_params].value);
814 g_type_class_unref (class);
820 g_object_constructor (GType type,
821 guint n_construct_properties,
822 GObjectConstructParam *construct_params)
827 object = (GObject*) g_type_create_instance (type);
829 /* set construction parameters */
830 if (n_construct_properties)
832 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
834 /* set construct properties */
835 while (n_construct_properties--)
837 GValue *value = construct_params->value;
838 GParamSpec *pspec = construct_params->pspec;
841 object_set_property (object, pspec, value, nqueue);
843 g_object_notify_queue_thaw (object, nqueue);
844 /* the notification queue is still frozen from g_object_init(), so
845 * we don't need to handle it here, g_object_newv() takes
854 g_object_set_valist (GObject *object,
855 const gchar *first_property_name,
858 GObjectNotifyQueue *nqueue;
861 g_return_if_fail (G_IS_OBJECT (object));
863 g_object_ref (object);
864 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
866 name = first_property_name;
869 GValue value = { 0, };
873 pspec = g_param_spec_pool_lookup (pspec_pool,
875 G_OBJECT_TYPE (object),
879 g_warning ("%s: object class `%s' has no property named `%s'",
881 G_OBJECT_TYPE_NAME (object),
885 if (!(pspec->flags & G_PARAM_WRITABLE))
887 g_warning ("%s: property `%s' of object class `%s' is not writable",
890 G_OBJECT_TYPE_NAME (object));
894 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
896 G_VALUE_COLLECT (&value, var_args, 0, &error);
899 g_warning ("%s: %s", G_STRLOC, error);
902 /* we purposely leak the value here, it might not be
903 * in a sane state if an error condition occoured
908 object_set_property (object, pspec, &value, nqueue);
909 g_value_unset (&value);
911 name = va_arg (var_args, gchar*);
914 g_object_notify_queue_thaw (object, nqueue);
915 g_object_unref (object);
919 g_object_get_valist (GObject *object,
920 const gchar *first_property_name,
925 g_return_if_fail (G_IS_OBJECT (object));
927 g_object_ref (object);
929 name = first_property_name;
933 GValue value = { 0, };
937 pspec = g_param_spec_pool_lookup (pspec_pool,
939 G_OBJECT_TYPE (object),
943 g_warning ("%s: object class `%s' has no property named `%s'",
945 G_OBJECT_TYPE_NAME (object),
949 if (!(pspec->flags & G_PARAM_READABLE))
951 g_warning ("%s: property `%s' of object class `%s' is not readable",
954 G_OBJECT_TYPE_NAME (object));
958 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
960 object_get_property (object, pspec, &value);
962 G_VALUE_LCOPY (&value, var_args, 0, &error);
965 g_warning ("%s: %s", G_STRLOC, error);
967 g_value_unset (&value);
971 g_value_unset (&value);
973 name = va_arg (var_args, gchar*);
976 g_object_unref (object);
980 g_object_set (gpointer _object,
981 const gchar *first_property_name,
984 GObject *object = _object;
987 g_return_if_fail (G_IS_OBJECT (object));
989 va_start (var_args, first_property_name);
990 g_object_set_valist (object, first_property_name, var_args);
995 g_object_get (gpointer _object,
996 const gchar *first_property_name,
999 GObject *object = _object;
1002 g_return_if_fail (G_IS_OBJECT (object));
1004 va_start (var_args, first_property_name);
1005 g_object_get_valist (object, first_property_name, var_args);
1010 g_object_set_property (GObject *object,
1011 const gchar *property_name,
1012 const GValue *value)
1014 GObjectNotifyQueue *nqueue;
1017 g_return_if_fail (G_IS_OBJECT (object));
1018 g_return_if_fail (property_name != NULL);
1019 g_return_if_fail (G_IS_VALUE (value));
1021 g_object_ref (object);
1022 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1024 pspec = g_param_spec_pool_lookup (pspec_pool,
1026 G_OBJECT_TYPE (object),
1029 g_warning ("%s: object class `%s' has no property named `%s'",
1031 G_OBJECT_TYPE_NAME (object),
1034 object_set_property (object, pspec, value, nqueue);
1036 g_object_notify_queue_thaw (object, nqueue);
1037 g_object_unref (object);
1041 g_object_get_property (GObject *object,
1042 const gchar *property_name,
1047 g_return_if_fail (G_IS_OBJECT (object));
1048 g_return_if_fail (property_name != NULL);
1049 g_return_if_fail (G_IS_VALUE (value));
1051 g_object_ref (object);
1053 pspec = g_param_spec_pool_lookup (pspec_pool,
1055 G_OBJECT_TYPE (object),
1058 g_warning ("%s: object class `%s' has no property named `%s'",
1060 G_OBJECT_TYPE_NAME (object),
1064 GValue *prop_value, tmp_value = { 0, };
1066 /* auto-conversion of the callers value type
1068 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1070 g_value_reset (value);
1073 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1075 g_warning ("can't retrieve property `%s' of type `%s' as value of type `%s'",
1077 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1078 G_VALUE_TYPE_NAME (value));
1079 g_object_unref (object);
1084 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1085 prop_value = &tmp_value;
1087 object_get_property (object, pspec, prop_value);
1088 if (prop_value != value)
1090 g_value_transform (prop_value, value);
1091 g_value_unset (&tmp_value);
1095 g_object_unref (object);
1099 g_object_connect (gpointer _object,
1100 const gchar *signal_spec,
1103 GObject *object = _object;
1106 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1107 g_return_val_if_fail (object->ref_count > 0, object);
1109 va_start (var_args, signal_spec);
1112 GCallback callback = va_arg (var_args, GCallback);
1113 gpointer data = va_arg (var_args, gpointer);
1116 if (strncmp (signal_spec, "signal::", 8) == 0)
1117 sid = g_signal_connect_data (object, signal_spec + 8,
1118 callback, data, NULL,
1120 else if (strncmp (signal_spec, "object_signal::", 15) == 0)
1121 sid = g_signal_connect_object (object, signal_spec + 15,
1124 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0)
1125 sid = g_signal_connect_data (object, signal_spec + 16,
1126 callback, data, NULL,
1128 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0)
1129 sid = g_signal_connect_object (object, signal_spec + 23,
1132 else if (strncmp (signal_spec, "signal_after::", 14) == 0)
1133 sid = g_signal_connect_data (object, signal_spec + 14,
1134 callback, data, NULL,
1136 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0)
1137 sid = g_signal_connect_object (object, signal_spec + 21,
1140 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0)
1141 sid = g_signal_connect_data (object, signal_spec + 22,
1142 callback, data, NULL,
1143 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1144 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0)
1145 sid = g_signal_connect_object (object, signal_spec + 29,
1147 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1150 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1153 signal_spec = va_arg (var_args, gchar*);
1161 g_object_disconnect (gpointer _object,
1162 const gchar *signal_spec,
1165 GObject *object = _object;
1168 g_return_if_fail (G_IS_OBJECT (object));
1169 g_return_if_fail (object->ref_count > 0);
1171 va_start (var_args, signal_spec);
1174 GCallback callback = va_arg (var_args, GCallback);
1175 gpointer data = va_arg (var_args, gpointer);
1176 guint sid = 0, detail = 0, mask = 0;
1178 if (strncmp (signal_spec, "any_signal::", 12) == 0)
1181 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1183 else if (strcmp (signal_spec, "any_signal") == 0)
1186 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1190 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1194 if ((mask & G_SIGNAL_MATCH_ID) &&
1195 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1196 g_warning ("%s: invalid signal name \"%s\"", G_STRLOC, signal_spec);
1197 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1199 NULL, (gpointer)callback, data))
1200 g_warning (G_STRLOC ": signal handler %p(%p) is not connected", callback, data);
1201 signal_spec = va_arg (var_args, gchar*);
1212 } weak_refs[1]; /* flexible array */
1216 weak_refs_notify (gpointer data)
1218 WeakRefStack *wstack = data;
1221 for (i = 0; i < wstack->n_weak_refs; i++)
1222 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1227 g_object_weak_ref (GObject *object,
1231 WeakRefStack *wstack;
1234 g_return_if_fail (G_IS_OBJECT (object));
1235 g_return_if_fail (notify != NULL);
1236 g_return_if_fail (object->ref_count >= 1);
1238 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1241 i = wstack->n_weak_refs++;
1242 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1246 wstack = g_renew (WeakRefStack, NULL, 1);
1247 wstack->object = object;
1248 wstack->n_weak_refs = 1;
1251 wstack->weak_refs[i].notify = notify;
1252 wstack->weak_refs[i].data = data;
1253 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1257 g_object_weak_unref (GObject *object,
1261 WeakRefStack *wstack;
1262 gboolean found_one = FALSE;
1264 g_return_if_fail (G_IS_OBJECT (object));
1265 g_return_if_fail (notify != NULL);
1267 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1272 for (i = 0; i < wstack->n_weak_refs; i++)
1273 if (wstack->weak_refs[i].notify == notify &&
1274 wstack->weak_refs[i].data == data)
1277 wstack->n_weak_refs -= 1;
1278 if (i != wstack->n_weak_refs)
1280 wstack->weak_refs[i].notify = wstack->weak_refs[wstack->n_weak_refs].notify;
1281 wstack->weak_refs[i].data = wstack->weak_refs[wstack->n_weak_refs].data;
1287 g_warning (G_STRLOC ": couldn't find weak ref %p(%p)", notify, data);
1291 g_object_add_weak_pointer (GObject *object,
1292 gpointer *weak_pointer_location)
1294 g_return_if_fail (G_IS_OBJECT (object));
1295 g_return_if_fail (weak_pointer_location != NULL);
1297 g_object_weak_ref (object,
1298 (GWeakNotify) g_nullify_pointer,
1299 weak_pointer_location);
1303 g_object_remove_weak_pointer (GObject *object,
1304 gpointer *weak_pointer_location)
1306 g_return_if_fail (G_IS_OBJECT (object));
1307 g_return_if_fail (weak_pointer_location != NULL);
1309 g_object_weak_unref (object,
1310 (GWeakNotify) g_nullify_pointer,
1311 weak_pointer_location);
1315 g_object_ref (gpointer _object)
1317 GObject *object = _object;
1319 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1320 g_return_val_if_fail (object->ref_count > 0, NULL);
1322 #ifdef G_ENABLE_DEBUG
1323 if (g_trap_object_ref == object)
1325 #endif /* G_ENABLE_DEBUG */
1327 object->ref_count += 1;
1333 g_object_unref (gpointer _object)
1335 GObject *object = _object;
1337 g_return_if_fail (G_IS_OBJECT (object));
1338 g_return_if_fail (object->ref_count > 0);
1340 #ifdef G_ENABLE_DEBUG
1341 if (g_trap_object_ref == object)
1343 #endif /* G_ENABLE_DEBUG */
1345 if (object->ref_count > 1)
1346 object->ref_count -= 1;
1348 g_object_last_unref (object);
1352 g_object_get_qdata (GObject *object,
1355 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1357 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1361 g_object_set_qdata (GObject *object,
1365 g_return_if_fail (G_IS_OBJECT (object));
1366 g_return_if_fail (quark > 0);
1368 g_datalist_id_set_data (&object->qdata, quark, data);
1372 g_object_set_qdata_full (GObject *object,
1375 GDestroyNotify destroy)
1377 g_return_if_fail (G_IS_OBJECT (object));
1378 g_return_if_fail (quark > 0);
1380 g_datalist_id_set_data_full (&object->qdata, quark, data,
1381 data ? destroy : (GDestroyNotify) NULL);
1385 g_object_steal_qdata (GObject *object,
1388 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1389 g_return_val_if_fail (quark > 0, NULL);
1391 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1395 g_object_get_data (GObject *object,
1400 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1401 g_return_val_if_fail (key != NULL, NULL);
1403 quark = g_quark_try_string (key);
1405 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1409 g_object_set_data (GObject *object,
1413 g_return_if_fail (G_IS_OBJECT (object));
1414 g_return_if_fail (key != NULL);
1416 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1420 g_object_set_data_full (GObject *object,
1423 GDestroyNotify destroy)
1425 g_return_if_fail (G_IS_OBJECT (object));
1426 g_return_if_fail (key != NULL);
1428 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1429 data ? destroy : (GDestroyNotify) NULL);
1433 g_object_steal_data (GObject *object,
1438 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1439 g_return_val_if_fail (key != NULL, NULL);
1441 quark = g_quark_try_string (key);
1443 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1447 g_value_object_init (GValue *value)
1449 value->data[0].v_pointer = NULL;
1453 g_value_object_free_value (GValue *value)
1455 if (value->data[0].v_pointer)
1456 g_object_unref (value->data[0].v_pointer);
1460 g_value_object_copy_value (const GValue *src_value,
1463 if (src_value->data[0].v_pointer)
1464 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1466 dest_value->data[0].v_pointer = NULL;
1470 g_value_object_transform_value (const GValue *src_value,
1473 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)))
1474 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1476 dest_value->data[0].v_pointer = NULL;
1480 g_value_object_peek_pointer (const GValue *value)
1482 return value->data[0].v_pointer;
1486 g_value_object_collect_value (GValue *value,
1487 guint n_collect_values,
1488 GTypeCValue *collect_values,
1489 guint collect_flags)
1491 if (collect_values[0].v_pointer)
1493 GObject *object = collect_values[0].v_pointer;
1495 if (object->g_type_instance.g_class == NULL)
1496 return g_strconcat ("invalid unclassed object pointer for value type `",
1497 G_VALUE_TYPE_NAME (value),
1500 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1501 return g_strconcat ("invalid object type `",
1502 G_OBJECT_TYPE_NAME (object),
1503 "' for value type `",
1504 G_VALUE_TYPE_NAME (value),
1507 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1508 value->data[0].v_pointer = g_object_ref (object);
1511 value->data[0].v_pointer = NULL;
1517 g_value_object_lcopy_value (const GValue *value,
1518 guint n_collect_values,
1519 GTypeCValue *collect_values,
1520 guint collect_flags)
1522 GObject **object_p = collect_values[0].v_pointer;
1525 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1527 if (!value->data[0].v_pointer)
1529 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1530 *object_p = value->data[0].v_pointer;
1532 *object_p = g_object_ref (value->data[0].v_pointer);
1538 g_value_set_object (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;
1555 g_object_ref (value->data[0].v_pointer);
1560 g_value_set_object_take_ownership (GValue *value,
1563 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1565 if (value->data[0].v_pointer)
1567 g_object_unref (value->data[0].v_pointer);
1568 value->data[0].v_pointer = NULL;
1573 g_return_if_fail (G_IS_OBJECT (v_object));
1574 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1576 value->data[0].v_pointer = v_object; /* we take over the reference count */
1581 g_value_get_object (const GValue *value)
1583 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1585 return value->data[0].v_pointer;
1589 g_value_dup_object (const GValue *value)
1591 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1593 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1597 * g_signal_connect_object:
1598 * @instance: the instance to connect to.
1599 * @detailed_signal: a string of the form "signal-name::detail".
1600 * @c_handler: the #GCallback to connect.
1601 * @gobject: the object to pass as data to @c_handler.
1602 * @connect_flags: a combination of #GConnnectFlags.
1604 * This is similar to g_signal_connect_data(), but uses a closure which
1605 * ensures that the object stays alive during the call to @c_handler.
1607 * Return value: the handler id.
1610 g_signal_connect_object (gpointer instance,
1611 const gchar *detailed_signal,
1612 GCallback c_handler,
1614 GConnectFlags connect_flags)
1616 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1617 g_return_val_if_fail (detailed_signal != NULL, 0);
1618 g_return_val_if_fail (c_handler != NULL, 0);
1624 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1626 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1628 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1631 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1637 GClosure *closures[1]; /* flexible array */
1639 /* don't change this structure without supplying an accessor for
1640 * watched closures, e.g.:
1641 * GSList* g_object_list_watched_closures (GObject *object)
1644 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1645 * carray = g_object_get_data (object, "GObject-closure-array");
1648 * GSList *slist = NULL;
1650 * for (i = 0; i < carray->n_closures; i++)
1651 * slist = g_slist_prepend (slist, carray->closures[i]);
1659 object_remove_closure (gpointer data,
1662 GObject *object = data;
1663 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1666 for (i = 0; i < carray->n_closures; i++)
1667 if (carray->closures[i] == closure)
1669 carray->n_closures--;
1670 if (i < carray->n_closures)
1671 carray->closures[i] = carray->closures[carray->n_closures];
1674 g_assert_not_reached ();
1678 destroy_closure_array (gpointer data)
1680 CArray *carray = data;
1681 GObject *object = carray->object;
1682 guint i, n = carray->n_closures;
1684 for (i = 0; i < n; i++)
1686 GClosure *closure = carray->closures[i];
1688 /* removing object_remove_closure() upfront is probably faster than
1689 * letting it fiddle with quark_closure_array which is empty anyways
1691 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1692 g_closure_invalidate (closure);
1698 g_object_watch_closure (GObject *object,
1704 g_return_if_fail (G_IS_OBJECT (object));
1705 g_return_if_fail (closure != NULL);
1706 g_return_if_fail (closure->is_invalid == FALSE);
1707 g_return_if_fail (closure->in_marshal == FALSE);
1708 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1710 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1711 g_closure_add_marshal_guards (closure,
1712 object, (GClosureNotify) g_object_ref,
1713 object, (GClosureNotify) g_object_unref);
1714 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
1717 carray = g_renew (CArray, NULL, 1);
1718 carray->object = object;
1719 carray->n_closures = 1;
1724 i = carray->n_closures++;
1725 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1727 carray->closures[i] = closure;
1728 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
1732 g_closure_new_object (guint sizeof_closure,
1737 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1738 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1740 closure = g_closure_new_simple (sizeof_closure, object);
1741 g_object_watch_closure (object, closure);
1747 g_cclosure_new_object (GCallback callback_func,
1752 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1753 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1754 g_return_val_if_fail (callback_func != NULL, NULL);
1756 closure = g_cclosure_new (callback_func, object, NULL);
1757 g_object_watch_closure (object, closure);
1763 g_cclosure_new_object_swap (GCallback callback_func,
1768 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1769 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1770 g_return_val_if_fail (callback_func != NULL, NULL);
1772 closure = g_cclosure_new_swap (callback_func, object, NULL);
1773 g_object_watch_closure (object, closure);