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"
33 #define PREALLOC_CPARAMS (8)
37 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
38 #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_shutdown (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 /* --- structures --- */
104 /* --- variables --- */
105 static GQuark quark_closure_array = 0;
106 static GParamSpecPool *pspec_pool = NULL;
107 static GObjectNotifyContext property_notify_context = { 0, };
108 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
111 /* --- functions --- */
112 #ifdef G_ENABLE_DEBUG
113 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
114 G_LOCK_DEFINE_STATIC (debug_objects);
115 static volatile GObject *g_trap_object_ref = NULL;
116 static guint debug_objects_count = 0;
117 static GHashTable *debug_objects_ht = NULL;
119 debug_objects_foreach (gpointer key,
123 GObject *object = value;
125 g_message ("[%p] stale %s\tref_count=%u",
127 G_OBJECT_TYPE_NAME (object),
131 debug_objects_atexit (void)
135 G_LOCK (debug_objects);
136 if (debug_objects_ht)
138 g_message ("stale GObjects: %u", debug_objects_count);
139 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
141 G_UNLOCK (debug_objects);
144 #endif /* G_ENABLE_DEBUG */
147 g_object_type_init (void) /* sync with gtype.c */
149 static gboolean initialized = FALSE;
150 static const GTypeFundamentalInfo finfo = {
151 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
153 static GTypeInfo info = {
154 sizeof (GObjectClass),
155 (GBaseInitFunc) g_object_base_class_init,
156 (GBaseFinalizeFunc) g_object_base_class_finalize,
157 (GClassInitFunc) g_object_do_class_init,
158 NULL /* class_destroy */,
159 NULL /* class_data */,
162 (GInstanceInitFunc) g_object_init,
163 NULL, /* value_table */
165 static const GTypeValueTable value_table = {
166 g_value_object_init, /* value_init */
167 g_value_object_free_value, /* value_free */
168 g_value_object_copy_value, /* value_copy */
169 g_value_object_peek_pointer, /* value_peek_pointer */
170 "p", /* collect_format */
171 g_value_object_collect_value, /* collect_value */
172 "p", /* lcopy_format */
173 g_value_object_lcopy_value, /* lcopy_value */
177 g_return_if_fail (initialized == FALSE);
182 info.value_table = &value_table;
183 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
184 g_assert (type == G_TYPE_OBJECT);
185 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
187 #ifdef G_ENABLE_DEBUG
189 g_atexit (debug_objects_atexit);
190 #endif /* G_ENABLE_DEBUG */
194 g_object_base_class_init (GObjectClass *class)
196 GObjectClass *pclass = g_type_class_peek_parent (class);
198 /* reset instance specific fields and methods that don't get inherited */
199 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
200 class->get_property = NULL;
201 class->set_property = NULL;
205 g_object_base_class_finalize (GObjectClass *class)
209 g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
211 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
213 g_slist_free (class->construct_properties);
214 class->construct_properties = NULL;
215 list = g_param_spec_pool_belongings (pspec_pool, G_OBJECT_CLASS_TYPE (class));
216 for (node = list; node; node = node->next)
218 GParamSpec *pspec = node->data;
220 g_param_spec_pool_remove (pspec_pool, pspec);
221 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
222 g_param_spec_unref (pspec);
228 g_object_notify_dispatcher (GObject *object,
232 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
236 g_object_do_class_init (GObjectClass *class)
238 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
239 pspec_pool = g_param_spec_pool_new (TRUE);
240 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
241 property_notify_context.dispatcher = g_object_notify_dispatcher;
243 class->constructor = g_object_constructor;
244 class->set_property = g_object_do_set_property;
245 class->get_property = g_object_do_get_property;
246 class->shutdown = g_object_shutdown;
247 class->finalize = g_object_finalize;
248 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
249 class->notify = NULL;
251 gobject_signals[NOTIFY] =
252 g_signal_newc ("notify",
253 G_TYPE_FROM_CLASS (class),
254 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
255 G_STRUCT_OFFSET (GObjectClass, notify),
257 g_cclosure_marshal_VOID__PARAM,
263 g_object_class_install_property (GObjectClass *class,
267 g_return_if_fail (G_IS_OBJECT_CLASS (class));
268 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
269 if (pspec->flags & G_PARAM_WRITABLE)
270 g_return_if_fail (class->set_property != NULL);
271 if (pspec->flags & G_PARAM_READABLE)
272 g_return_if_fail (class->get_property != NULL);
273 g_return_if_fail (property_id > 0);
274 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
275 if (pspec->flags & G_PARAM_CONSTRUCT)
276 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
277 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
278 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
280 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
282 g_warning (G_STRLOC ": class `%s' already contains a property named `%s'",
283 G_OBJECT_CLASS_NAME (class),
288 g_param_spec_ref (pspec);
289 g_param_spec_sink (pspec);
290 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
291 g_param_spec_pool_insert (pspec_pool, pspec, G_OBJECT_CLASS_TYPE (class));
292 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
293 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
295 /* for property overrides of construct poperties, we have to get rid
296 * of the overidden inherited construct property
298 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
299 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
300 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
304 g_object_class_find_property (GObjectClass *class,
305 const gchar *property_name)
307 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
308 g_return_val_if_fail (property_name != NULL, NULL);
310 return g_param_spec_pool_lookup (pspec_pool,
312 G_OBJECT_CLASS_TYPE (class),
316 GParamSpec** /* free result */
317 g_object_class_list_properties (GObjectClass *class,
318 guint *n_properties_p)
323 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
325 pspecs = g_param_spec_pool_list (pspec_pool,
326 G_OBJECT_CLASS_TYPE (class),
335 g_object_init (GObject *object)
337 object->ref_count = 1;
338 g_datalist_init (&object->qdata);
340 /* freeze object's notification queue, g_object_newv() preserves pairedness */
341 g_object_notify_queue_freeze (object, &property_notify_context);
343 #ifdef G_ENABLE_DEBUG
346 G_LOCK (debug_objects);
347 if (!debug_objects_ht)
348 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
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_last_unref (GObject *object)
387 g_return_if_fail (object->ref_count > 0);
389 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
390 G_OBJECT_GET_CLASS (object)->shutdown (object);
392 #ifdef G_ENABLE_DEBUG
393 if (g_trap_object_ref == object)
395 #endif /* G_ENABLE_DEBUG */
397 object->ref_count -= 1;
399 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
401 g_signal_handlers_destroy (object);
402 g_object_set_qdata (object, quark_closure_array, NULL);
403 G_OBJECT_GET_CLASS (object)->finalize (object);
404 #ifdef G_ENABLE_DEBUG
407 G_LOCK (debug_objects);
408 if (debug_objects_ht)
409 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
410 G_UNLOCK (debug_objects);
412 #endif /* G_ENABLE_DEBUG */
413 g_type_free_instance ((GTypeInstance*) object);
418 g_object_shutdown (GObject *object)
420 /* this function needs to be always present for unconditional
421 * chaining, we also might add some code here later.
422 * beware though, subclasses may invoke shutdown() arbitrarily.
427 g_object_finalize (GObject *object)
429 g_signal_handlers_destroy (object);
430 g_datalist_clear (&object->qdata);
432 #ifdef G_ENABLE_DEBUG
435 G_LOCK (debug_objects);
436 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
437 g_hash_table_remove (debug_objects_ht, object);
438 debug_objects_count--;
439 G_UNLOCK (debug_objects);
441 #endif /* G_ENABLE_DEBUG */
445 g_object_dispatch_properties_changed (GObject *object,
451 for (i = 0; i < n_pspecs; i++)
452 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
456 g_object_freeze_notify (GObject *object)
458 g_return_if_fail (G_IS_OBJECT (object));
459 if (!object->ref_count)
462 g_object_ref (object);
463 g_object_notify_queue_freeze (object, &property_notify_context);
464 g_object_unref (object);
468 g_object_notify (GObject *object,
469 const gchar *property_name)
473 g_return_if_fail (G_IS_OBJECT (object));
474 g_return_if_fail (property_name != NULL);
475 if (!object->ref_count)
478 g_object_ref (object);
479 pspec = g_param_spec_pool_lookup (pspec_pool,
481 G_OBJECT_TYPE (object),
484 g_warning ("%s: object class `%s' has no property named `%s'",
486 G_OBJECT_TYPE_NAME (object),
490 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
492 g_object_notify_queue_add (object, nqueue, pspec);
493 g_object_notify_queue_thaw (object, nqueue);
495 g_object_unref (object);
499 g_object_thaw_notify (GObject *object)
501 GObjectNotifyQueue *nqueue;
503 g_return_if_fail (G_IS_OBJECT (object));
504 if (!object->ref_count)
507 g_object_ref (object);
508 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
509 if (!nqueue || !nqueue->freeze_count)
510 g_warning (G_STRLOC ": property-changed notification for %s(%p) is not frozen",
511 G_OBJECT_TYPE_NAME (object), object);
513 g_object_notify_queue_thaw (object, nqueue);
514 g_object_unref (object);
518 object_get_property (GObject *object,
522 GObjectClass *class = g_type_class_peek (pspec->owner_type);
524 class->get_property (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
528 object_set_property (GObject *object,
531 GObjectNotifyQueue *nqueue)
533 GValue tmp_value = { 0, };
534 GObjectClass *class = g_type_class_peek (pspec->owner_type);
536 /* provide a copy to work from, convert (if necessary) and validate */
537 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
538 if (!g_value_transform (value, &tmp_value))
539 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
541 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
542 G_VALUE_TYPE_NAME (value));
543 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
545 gchar *contents = g_strdup_value_contents (value);
547 g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
549 G_VALUE_TYPE_NAME (value),
551 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
556 class->set_property (object, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
557 g_object_notify_queue_add (object, nqueue, pspec);
559 g_value_unset (&tmp_value);
563 g_object_new (GType object_type,
564 const gchar *first_property_name,
570 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
572 va_start (var_args, first_property_name);
573 object = g_object_new_valist (object_type, first_property_name, var_args);
580 g_object_newv (GType object_type,
582 GParameter *parameters)
584 GObjectConstructParam *cparams, *oparams;
585 GObjectNotifyQueue *nqueue;
589 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
594 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
596 class = g_type_class_ref (object_type);
597 for (slist = class->construct_properties; slist; slist = slist->next)
599 clist = g_list_prepend (clist, slist->data);
600 n_total_cparams += 1;
603 /* collect parameters, sort into construction and normal ones */
604 oparams = g_new (GObjectConstructParam, n_parameters);
605 cparams = g_new (GObjectConstructParam, n_total_cparams);
606 for (i = 0; i < n_parameters; i++)
608 GValue *value = ¶meters[i].value;
609 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
615 g_warning ("%s: object class `%s' has no property named `%s'",
617 g_type_name (object_type),
621 if (!(pspec->flags & G_PARAM_WRITABLE))
623 g_warning ("%s: property `%s' of object class `%s' is not writable",
626 g_type_name (object_type));
629 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
631 GList *list = g_list_find (clist, pspec);
635 g_warning (G_STRLOC ": construct property \"%s\" for object `%s' can't be set twice",
636 pspec->name, g_type_name (object_type));
639 cparams[n_cparams].pspec = pspec;
640 cparams[n_cparams].value = value;
645 list->prev->next = list->next;
647 list->next->prev = list->prev;
648 g_list_free_1 (list);
652 oparams[n_oparams].pspec = pspec;
653 oparams[n_oparams].value = value;
658 /* set remaining construction properties to default values */
659 n_cvalues = n_total_cparams - n_cparams;
660 cvalues = g_new (GValue, n_cvalues);
663 GList *tmp = clist->next;
664 GParamSpec *pspec = clist->data;
665 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
668 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
669 g_param_value_set_default (pspec, value);
671 cparams[n_cparams].pspec = pspec;
672 cparams[n_cparams].value = value;
675 g_list_free_1 (clist);
679 /* construct object from construction parameters */
680 object = class->constructor (object_type, n_total_cparams, cparams);
682 /* free construction values */
685 g_value_unset (cvalues + n_cvalues);
688 /* release g_object_init() notification queue freeze_count */
689 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
690 g_object_notify_queue_thaw (object, nqueue);
692 /* set remaining properties */
693 for (i = 0; i < n_oparams; i++)
694 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
697 g_type_class_unref (class);
699 /* release our own freeze count and handle notifications */
700 g_object_notify_queue_thaw (object, nqueue);
706 g_object_new_valist (GType object_type,
707 const gchar *first_property_name,
714 guint n_params = 0, n_alloced_params = 16;
716 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
718 if (!first_property_name)
719 return g_object_newv (object_type, 0, NULL);
721 class = g_type_class_ref (object_type);
723 params = g_new (GParameter, n_alloced_params);
724 name = first_property_name;
728 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
734 g_warning ("%s: object class `%s' has no property named `%s'",
736 g_type_name (object_type),
740 if (n_params >= n_alloced_params)
742 n_alloced_params += 16;
743 params = g_renew (GParameter, params, n_alloced_params);
745 params[n_params].name = name;
746 params[n_params].value.g_type = 0;
747 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
748 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
751 g_warning ("%s: %s", G_STRLOC, error);
754 /* we purposely leak the value here, it might not be
755 * in a sane state if an error condition occoured
760 name = va_arg (var_args, gchar*);
763 object = g_object_newv (object_type, n_params, params);
766 g_value_unset (¶ms[n_params].value);
769 g_type_class_unref (class);
775 g_object_constructor (GType type,
776 guint n_construct_properties,
777 GObjectConstructParam *construct_params)
782 object = (GObject*) g_type_create_instance (type);
784 /* set construction parameters */
785 if (n_construct_properties)
787 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
789 /* set construct properties */
790 while (n_construct_properties--)
792 GValue *value = construct_params->value;
793 GParamSpec *pspec = construct_params->pspec;
796 object_set_property (object, pspec, value, nqueue);
798 g_object_notify_queue_thaw (object, nqueue);
799 /* the notification queue is still frozen from g_object_init(), so
800 * we don't need to handle it here, g_object_newv() takes
809 g_object_set_valist (GObject *object,
810 const gchar *first_property_name,
813 GObjectNotifyQueue *nqueue;
816 g_return_if_fail (G_IS_OBJECT (object));
818 g_object_ref (object);
819 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
821 name = first_property_name;
824 GValue value = { 0, };
828 pspec = g_param_spec_pool_lookup (pspec_pool,
830 G_OBJECT_TYPE (object),
834 g_warning ("%s: object class `%s' has no property named `%s'",
836 G_OBJECT_TYPE_NAME (object),
840 if (!(pspec->flags & G_PARAM_WRITABLE))
842 g_warning ("%s: property `%s' of object class `%s' is not writable",
845 G_OBJECT_TYPE_NAME (object));
849 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
851 G_VALUE_COLLECT (&value, var_args, 0, &error);
854 g_warning ("%s: %s", G_STRLOC, error);
857 /* we purposely leak the value here, it might not be
858 * in a sane state if an error condition occoured
863 object_set_property (object, pspec, &value, nqueue);
864 g_value_unset (&value);
866 name = va_arg (var_args, gchar*);
869 g_object_notify_queue_thaw (object, nqueue);
870 g_object_unref (object);
874 g_object_get_valist (GObject *object,
875 const gchar *first_property_name,
880 g_return_if_fail (G_IS_OBJECT (object));
882 g_object_ref (object);
884 name = first_property_name;
888 GValue value = { 0, };
892 pspec = g_param_spec_pool_lookup (pspec_pool,
894 G_OBJECT_TYPE (object),
898 g_warning ("%s: object class `%s' has no property named `%s'",
900 G_OBJECT_TYPE_NAME (object),
904 if (!(pspec->flags & G_PARAM_READABLE))
906 g_warning ("%s: property `%s' of object class `%s' is not readable",
909 G_OBJECT_TYPE_NAME (object));
913 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
915 object_get_property (object, pspec, &value);
917 G_VALUE_LCOPY (&value, var_args, 0, &error);
920 g_warning ("%s: %s", G_STRLOC, error);
922 g_value_unset (&value);
926 g_value_unset (&value);
928 name = va_arg (var_args, gchar*);
931 g_object_unref (object);
935 g_object_set (gpointer _object,
936 const gchar *first_property_name,
939 GObject *object = _object;
942 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
944 va_start (var_args, first_property_name);
945 g_object_set_valist (object, first_property_name, var_args);
952 g_object_get (gpointer _object,
953 const gchar *first_property_name,
956 GObject *object = _object;
959 g_return_if_fail (G_IS_OBJECT (object));
961 va_start (var_args, first_property_name);
962 g_object_get_valist (object, first_property_name, var_args);
967 g_object_set_property (GObject *object,
968 const gchar *property_name,
971 GObjectNotifyQueue *nqueue;
974 g_return_if_fail (G_IS_OBJECT (object));
975 g_return_if_fail (property_name != NULL);
976 g_return_if_fail (G_IS_VALUE (value));
978 g_object_ref (object);
979 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
981 pspec = g_param_spec_pool_lookup (pspec_pool,
983 G_OBJECT_TYPE (object),
986 g_warning ("%s: object class `%s' has no property named `%s'",
988 G_OBJECT_TYPE_NAME (object),
991 object_set_property (object, pspec, value, nqueue);
993 g_object_notify_queue_thaw (object, nqueue);
994 g_object_unref (object);
998 g_object_get_property (GObject *object,
999 const gchar *property_name,
1004 g_return_if_fail (G_IS_OBJECT (object));
1005 g_return_if_fail (property_name != NULL);
1006 g_return_if_fail (G_IS_VALUE (value));
1008 g_object_ref (object);
1010 pspec = g_param_spec_pool_lookup (pspec_pool,
1012 G_OBJECT_TYPE (object),
1015 g_warning ("%s: object class `%s' has no property named `%s'",
1017 G_OBJECT_TYPE_NAME (object),
1021 GValue *prop_value, tmp_value = { 0, };
1023 /* auto-conversion of the callers value type
1025 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1027 g_value_reset (value);
1030 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1032 g_warning ("can't retrive property `%s' of type `%s' as value of type `%s'",
1034 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1035 G_VALUE_TYPE_NAME (value));
1036 g_object_unref (object);
1041 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1042 prop_value = &tmp_value;
1044 object_get_property (object, pspec, prop_value);
1045 if (prop_value != value)
1047 g_value_transform (prop_value, value);
1048 g_value_unset (&tmp_value);
1052 g_object_unref (object);
1056 g_object_connect (gpointer _object,
1057 const gchar *signal_spec,
1060 GObject *object = _object;
1063 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1064 g_return_val_if_fail (object->ref_count > 0, object);
1066 va_start (var_args, signal_spec);
1069 gpointer callback = va_arg (var_args, gpointer);
1070 gpointer data = va_arg (var_args, gpointer);
1073 if (strncmp (signal_spec, "signal::", 8) == 0)
1074 sid = g_signal_connect_data (object, signal_spec + 8,
1075 callback, data, NULL,
1077 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0)
1078 sid = g_signal_connect_data (object, signal_spec + 16,
1079 callback, data, NULL,
1081 else if (strncmp (signal_spec, "signal_after::", 14) == 0)
1082 sid = g_signal_connect_data (object, signal_spec + 14,
1083 callback, data, NULL,
1085 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0)
1086 sid = g_signal_connect_data (object, signal_spec + 22,
1087 callback, data, NULL,
1091 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1094 signal_spec = va_arg (var_args, gchar*);
1102 g_object_disconnect (gpointer _object,
1103 const gchar *signal_spec,
1106 GObject *object = _object;
1109 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1110 g_return_val_if_fail (object->ref_count > 0, object);
1112 va_start (var_args, signal_spec);
1115 gpointer callback = va_arg (var_args, gpointer);
1116 gpointer data = va_arg (var_args, gpointer);
1117 guint sid = 0, detail = 0, mask = 0;
1119 if (strncmp (signal_spec, "any_signal::", 12) == 0)
1122 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1124 else if (strcmp (signal_spec, "any_signal") == 0)
1127 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1131 g_warning ("%s: invalid signal spec \"%s\"", G_STRLOC, signal_spec);
1135 if ((mask & G_SIGNAL_MATCH_ID) &&
1136 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1137 g_warning ("%s: invalid signal name \"%s\"", G_STRLOC, signal_spec);
1138 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1140 NULL, callback, data))
1141 g_warning (G_STRLOC ": signal handler %p(%p) is not connected", callback, data);
1142 signal_spec = va_arg (var_args, gchar*);
1150 g_object_ref (gpointer _object)
1152 GObject *object = _object;
1154 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1155 g_return_val_if_fail (object->ref_count > 0, NULL);
1157 #ifdef G_ENABLE_DEBUG
1158 if (g_trap_object_ref == object)
1160 #endif /* G_ENABLE_DEBUG */
1162 object->ref_count += 1;
1168 g_object_unref (gpointer _object)
1170 GObject *object = _object;
1172 g_return_if_fail (G_IS_OBJECT (object));
1173 g_return_if_fail (object->ref_count > 0);
1175 #ifdef G_ENABLE_DEBUG
1176 if (g_trap_object_ref == object)
1178 #endif /* G_ENABLE_DEBUG */
1180 if (object->ref_count > 1)
1181 object->ref_count -= 1;
1183 g_object_last_unref (object);
1187 g_object_get_qdata (GObject *object,
1190 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1192 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1196 g_object_set_qdata (GObject *object,
1200 g_return_if_fail (G_IS_OBJECT (object));
1201 g_return_if_fail (quark > 0);
1203 g_datalist_id_set_data (&object->qdata, quark, data);
1207 g_object_set_qdata_full (GObject *object,
1210 GDestroyNotify destroy)
1212 g_return_if_fail (G_IS_OBJECT (object));
1213 g_return_if_fail (quark > 0);
1215 g_datalist_id_set_data_full (&object->qdata, quark, data,
1216 data ? destroy : (GDestroyNotify) NULL);
1220 g_object_steal_qdata (GObject *object,
1223 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1224 g_return_val_if_fail (quark > 0, NULL);
1226 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1230 g_object_get_data (GObject *object,
1235 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1236 g_return_val_if_fail (key != NULL, NULL);
1238 quark = g_quark_try_string (key);
1240 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1244 g_object_set_data (GObject *object,
1248 g_return_if_fail (G_IS_OBJECT (object));
1249 g_return_if_fail (key != NULL);
1251 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1255 g_object_set_data_full (GObject *object,
1258 GDestroyNotify destroy)
1260 g_return_if_fail (G_IS_OBJECT (object));
1261 g_return_if_fail (key != NULL);
1263 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1264 data ? destroy : (GDestroyNotify) NULL);
1268 g_object_steal_data (GObject *object,
1273 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1274 g_return_val_if_fail (key != NULL, NULL);
1276 quark = g_quark_try_string (key);
1278 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1282 g_value_object_init (GValue *value)
1284 value->data[0].v_pointer = NULL;
1288 g_value_object_free_value (GValue *value)
1290 if (value->data[0].v_pointer)
1291 g_object_unref (value->data[0].v_pointer);
1295 g_value_object_copy_value (const GValue *src_value,
1298 if (src_value->data[0].v_pointer)
1299 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1301 dest_value->data[0].v_pointer = NULL;
1305 g_value_object_transform_value (const GValue *src_value,
1308 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)))
1309 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1311 dest_value->data[0].v_pointer = NULL;
1315 g_value_object_peek_pointer (const GValue *value)
1317 return value->data[0].v_pointer;
1321 g_value_object_collect_value (GValue *value,
1322 guint n_collect_values,
1323 GTypeCValue *collect_values,
1324 guint collect_flags)
1326 if (collect_values[0].v_pointer)
1328 GObject *object = collect_values[0].v_pointer;
1330 if (object->g_type_instance.g_class == NULL)
1331 return g_strconcat ("invalid unclassed object pointer for value type `",
1332 G_VALUE_TYPE_NAME (value),
1335 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1336 return g_strconcat ("invalid object type `",
1337 G_OBJECT_TYPE_NAME (object),
1338 "' for value type `",
1339 G_VALUE_TYPE_NAME (value),
1342 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1343 value->data[0].v_pointer = g_object_ref (object);
1346 value->data[0].v_pointer = NULL;
1352 g_value_object_lcopy_value (const GValue *value,
1353 guint n_collect_values,
1354 GTypeCValue *collect_values,
1355 guint collect_flags)
1357 GObject **object_p = collect_values[0].v_pointer;
1360 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1362 if (!value->data[0].v_pointer)
1364 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1365 *object_p = value->data[0].v_pointer;
1367 *object_p = g_object_ref (value->data[0].v_pointer);
1373 g_value_set_object (GValue *value,
1376 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1378 if (value->data[0].v_pointer)
1380 g_object_unref (value->data[0].v_pointer);
1381 value->data[0].v_pointer = NULL;
1386 g_return_if_fail (G_IS_OBJECT (v_object));
1387 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1389 value->data[0].v_pointer = v_object;
1390 g_object_ref (value->data[0].v_pointer);
1395 g_value_get_object (const GValue *value)
1397 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1399 return value->data[0].v_pointer;
1403 g_value_dup_object (const GValue *value)
1405 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1407 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1411 g_signal_connect_object (gpointer instance,
1412 const gchar *detailed_signal,
1413 GCallback c_handler,
1418 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1419 g_return_val_if_fail (detailed_signal != NULL, 0);
1420 g_return_val_if_fail (c_handler != NULL, 0);
1426 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1428 closure = (swapped ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1430 return g_signal_connect_closure (instance, detailed_signal, closure, after);
1433 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, swapped, after);
1439 GClosure *closures[1]; /* flexible array */
1443 object_remove_closure (gpointer data,
1446 GObject *object = data;
1447 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1450 for (i = 0; i < carray->n_closures; i++)
1451 if (carray->closures[i] == closure)
1453 carray->n_closures--;
1454 if (i < carray->n_closures)
1455 carray->closures[i] = carray->closures[carray->n_closures];
1458 g_assert_not_reached ();
1462 destroy_closure_array (gpointer data)
1464 CArray *carray = data;
1465 GObject *object = carray->object;
1466 guint i, n = carray->n_closures;
1468 for (i = 0; i < n; i++)
1470 GClosure *closure = carray->closures[i];
1472 /* removing object_remove_closure() upfront is probably faster than
1473 * letting it fiddle with quark_closure_array which is empty anyways
1475 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1476 g_closure_invalidate (closure);
1482 g_object_watch_closure (GObject *object,
1487 g_return_if_fail (G_IS_OBJECT (object));
1488 g_return_if_fail (closure != NULL);
1489 g_return_if_fail (closure->is_invalid == FALSE);
1490 g_return_if_fail (closure->in_marshal == FALSE);
1491 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1493 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1494 g_closure_add_marshal_guards (closure,
1495 object, (GClosureNotify) g_object_ref,
1496 object, (GClosureNotify) g_object_unref);
1497 carray = g_object_steal_qdata (object, quark_closure_array);
1500 carray = g_renew (CArray, NULL, 1);
1501 carray->object = object;
1502 carray->n_closures = 1;
1503 carray->closures[0] = closure;
1504 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1508 guint i = carray->n_closures++;
1510 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1511 carray->closures[i] = closure;
1512 g_object_set_qdata_full (object, quark_closure_array, carray, destroy_closure_array);
1517 g_closure_new_object (guint sizeof_closure,
1522 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1523 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1525 closure = g_closure_new_simple (sizeof_closure, object);
1526 g_object_watch_closure (object, closure);
1532 g_cclosure_new_object (GCallback callback_func,
1535 GObject *object = _object;
1538 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1539 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1540 g_return_val_if_fail (callback_func != NULL, NULL);
1542 closure = g_cclosure_new (callback_func, object, NULL);
1543 g_object_watch_closure (object, closure);
1549 g_cclosure_new_object_swap (GCallback callback_func,
1552 GObject *object = _object;
1555 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1556 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1557 g_return_val_if_fail (callback_func != NULL, NULL);
1559 closure = g_cclosure_new_swap (callback_func, object, NULL);
1560 g_object_watch_closure (object, closure);