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.
20 #include "gobjectalias.h"
21 #include <glib/gdatasetprivate.h>
27 #include "gvaluecollector.h"
29 #include "gparamspecs.h"
30 #include "gvaluetypes.h"
31 #include "gobjectnotifyqueue.c"
36 #define PREALLOC_CPARAMS (8)
40 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
41 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
43 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
44 #define OBJECT_HAS_TOGGLE_REF(object) \
45 ((G_DATALIST_GET_FLAGS(&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
55 /* --- properties --- */
61 /* --- prototypes --- */
62 static void g_object_base_class_init (GObjectClass *class);
63 static void g_object_base_class_finalize (GObjectClass *class);
64 static void g_object_do_class_init (GObjectClass *class);
65 static void g_object_init (GObject *object);
66 static GObject* g_object_constructor (GType type,
67 guint n_construct_properties,
68 GObjectConstructParam *construct_params);
69 static void g_object_last_unref (GObject *object);
70 static void g_object_real_dispose (GObject *object);
71 static void g_object_finalize (GObject *object);
72 static void g_object_do_set_property (GObject *object,
76 static void g_object_do_get_property (GObject *object,
80 static void g_value_object_init (GValue *value);
81 static void g_value_object_free_value (GValue *value);
82 static void g_value_object_copy_value (const GValue *src_value,
84 static void g_value_object_transform_value (const GValue *src_value,
86 static gpointer g_value_object_peek_pointer (const GValue *value);
87 static gchar* g_value_object_collect_value (GValue *value,
88 guint n_collect_values,
89 GTypeCValue *collect_values,
91 static gchar* g_value_object_lcopy_value (const GValue *value,
92 guint n_collect_values,
93 GTypeCValue *collect_values,
95 static void g_object_dispatch_properties_changed (GObject *object,
98 static inline void object_get_property (GObject *object,
101 static inline void object_set_property (GObject *object,
104 GObjectNotifyQueue *nqueue);
106 static void object_interface_check_properties (gpointer func_data,
110 /* --- variables --- */
111 static GQuark quark_closure_array = 0;
112 static GQuark quark_weak_refs = 0;
113 static GQuark quark_toggle_refs = 0;
114 static GParamSpecPool *pspec_pool = NULL;
115 static GObjectNotifyContext property_notify_context = { 0, };
116 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
117 G_LOCK_DEFINE_STATIC (construct_objects_lock);
118 static GSList *construct_objects = NULL;
121 /* --- functions --- */
122 #ifdef G_ENABLE_DEBUG
123 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
124 G_LOCK_DEFINE_STATIC (debug_objects);
125 static volatile GObject *g_trap_object_ref = NULL;
126 static guint debug_objects_count = 0;
127 static GHashTable *debug_objects_ht = NULL;
129 debug_objects_foreach (gpointer key,
133 GObject *object = value;
135 g_message ("[%p] stale %s\tref_count=%u",
137 G_OBJECT_TYPE_NAME (object),
141 debug_objects_atexit (void)
145 G_LOCK (debug_objects);
146 g_message ("stale GObjects: %u", debug_objects_count);
147 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
148 G_UNLOCK (debug_objects);
151 #endif /* G_ENABLE_DEBUG */
154 g_object_type_init (void)
156 static gboolean initialized = FALSE;
157 static const GTypeFundamentalInfo finfo = {
158 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
160 static GTypeInfo info = {
161 sizeof (GObjectClass),
162 (GBaseInitFunc) g_object_base_class_init,
163 (GBaseFinalizeFunc) g_object_base_class_finalize,
164 (GClassInitFunc) g_object_do_class_init,
165 NULL /* class_destroy */,
166 NULL /* class_data */,
169 (GInstanceInitFunc) g_object_init,
170 NULL, /* value_table */
172 static const GTypeValueTable value_table = {
173 g_value_object_init, /* value_init */
174 g_value_object_free_value, /* value_free */
175 g_value_object_copy_value, /* value_copy */
176 g_value_object_peek_pointer, /* value_peek_pointer */
177 "p", /* collect_format */
178 g_value_object_collect_value, /* collect_value */
179 "p", /* lcopy_format */
180 g_value_object_lcopy_value, /* lcopy_value */
184 g_return_if_fail (initialized == FALSE);
189 info.value_table = &value_table;
190 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
191 g_assert (type == G_TYPE_OBJECT);
192 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
194 #ifdef G_ENABLE_DEBUG
197 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
198 g_atexit (debug_objects_atexit);
200 #endif /* G_ENABLE_DEBUG */
204 g_object_base_class_init (GObjectClass *class)
206 GObjectClass *pclass = g_type_class_peek_parent (class);
208 /* reset instance specific fields and methods that don't get inherited */
209 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
210 class->get_property = NULL;
211 class->set_property = NULL;
215 g_object_base_class_finalize (GObjectClass *class)
219 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
221 g_slist_free (class->construct_properties);
222 class->construct_properties = NULL;
223 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
224 for (node = list; node; node = node->next)
226 GParamSpec *pspec = node->data;
228 g_param_spec_pool_remove (pspec_pool, pspec);
229 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
230 g_param_spec_unref (pspec);
236 g_object_notify_dispatcher (GObject *object,
240 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
244 g_object_do_class_init (GObjectClass *class)
246 /* read the comment about typedef struct CArray; on why not to change this quark */
247 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
249 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
250 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
251 pspec_pool = g_param_spec_pool_new (TRUE);
252 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
253 property_notify_context.dispatcher = g_object_notify_dispatcher;
255 class->constructor = g_object_constructor;
256 class->set_property = g_object_do_set_property;
257 class->get_property = g_object_do_get_property;
258 class->dispose = g_object_real_dispose;
259 class->finalize = g_object_finalize;
260 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
261 class->notify = NULL;
263 gobject_signals[NOTIFY] =
264 g_signal_new ("notify",
265 G_TYPE_FROM_CLASS (class),
266 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
267 G_STRUCT_OFFSET (GObjectClass, notify),
269 g_cclosure_marshal_VOID__PARAM,
273 /* Install a check function that we'll use to verify that classes that
274 * implement an interface implement all properties for that interface
276 g_type_add_interface_check (NULL, object_interface_check_properties);
280 install_property_internal (GType g_type,
284 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
286 g_warning ("When installing property: type `%s' already has a property named `%s'",
287 g_type_name (g_type),
292 g_param_spec_ref (pspec);
293 g_param_spec_sink (pspec);
294 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
295 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
299 g_object_class_install_property (GObjectClass *class,
303 g_return_if_fail (G_IS_OBJECT_CLASS (class));
304 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
305 if (pspec->flags & G_PARAM_WRITABLE)
306 g_return_if_fail (class->set_property != NULL);
307 if (pspec->flags & G_PARAM_READABLE)
308 g_return_if_fail (class->get_property != NULL);
309 g_return_if_fail (property_id > 0);
310 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
311 if (pspec->flags & G_PARAM_CONSTRUCT)
312 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
313 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
314 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
316 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
318 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
319 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
321 /* for property overrides of construct poperties, we have to get rid
322 * of the overidden inherited construct property
324 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
325 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
326 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
330 g_object_interface_install_property (gpointer g_iface,
333 GTypeInterface *iface_class = g_iface;
335 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
336 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
337 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
338 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
340 install_property_internal (iface_class->g_type, 0, pspec);
344 g_object_class_find_property (GObjectClass *class,
345 const gchar *property_name)
348 GParamSpec *redirect;
350 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
351 g_return_val_if_fail (property_name != NULL, NULL);
353 pspec = g_param_spec_pool_lookup (pspec_pool,
355 G_OBJECT_CLASS_TYPE (class),
359 redirect = g_param_spec_get_redirect_target (pspec);
370 g_object_interface_find_property (gpointer g_iface,
371 const gchar *property_name)
373 GTypeInterface *iface_class = g_iface;
375 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
376 g_return_val_if_fail (property_name != NULL, NULL);
378 return g_param_spec_pool_lookup (pspec_pool,
385 g_object_class_override_property (GObjectClass *oclass,
389 GParamSpec *overridden = NULL;
393 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
394 g_return_if_fail (property_id > 0);
395 g_return_if_fail (name != NULL);
397 /* Find the overridden property; first check parent types
399 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
400 if (parent_type != G_TYPE_NONE)
401 overridden = g_param_spec_pool_lookup (pspec_pool,
410 /* Now check interfaces
412 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
413 while (n_ifaces-- && !overridden)
415 overridden = g_param_spec_pool_lookup (pspec_pool,
426 g_warning ("%s: Can't find property to override for '%s::%s'",
427 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
431 new = g_param_spec_override (name, overridden);
432 g_object_class_install_property (oclass, property_id, new);
435 GParamSpec** /* free result */
436 g_object_class_list_properties (GObjectClass *class,
437 guint *n_properties_p)
442 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
444 pspecs = g_param_spec_pool_list (pspec_pool,
445 G_OBJECT_CLASS_TYPE (class),
453 GParamSpec** /* free result */
454 g_object_interface_list_properties (gpointer g_iface,
455 guint *n_properties_p)
457 GTypeInterface *iface_class = g_iface;
461 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
463 pspecs = g_param_spec_pool_list (pspec_pool,
473 g_object_init (GObject *object)
475 object->ref_count = 1;
476 g_datalist_init (&object->qdata);
478 /* freeze object's notification queue, g_object_newv() preserves pairedness */
479 g_object_notify_queue_freeze (object, &property_notify_context);
481 /* allow construct-only properties to be set */
482 G_LOCK (construct_objects_lock);
483 construct_objects = g_slist_prepend (construct_objects, object);
484 G_UNLOCK (construct_objects_lock);
486 #ifdef G_ENABLE_DEBUG
489 G_LOCK (debug_objects);
490 debug_objects_count++;
491 g_hash_table_insert (debug_objects_ht, object, object);
492 G_UNLOCK (debug_objects);
494 #endif /* G_ENABLE_DEBUG */
498 g_object_do_set_property (GObject *object,
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
512 g_object_do_get_property (GObject *object,
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
526 g_object_real_dispose (GObject *object)
530 g_signal_handlers_destroy (object);
531 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
533 /* yes, temporarily altering the ref_count is hackish, but that
534 * enforces people not jerking around with weak_ref notifiers
536 ref_count = object->ref_count;
537 object->ref_count = 0;
538 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
539 object->ref_count = ref_count;
543 g_object_finalize (GObject *object)
545 g_datalist_clear (&object->qdata);
547 #ifdef G_ENABLE_DEBUG
550 G_LOCK (debug_objects);
551 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
552 g_hash_table_remove (debug_objects_ht, object);
553 debug_objects_count--;
554 G_UNLOCK (debug_objects);
556 #endif /* G_ENABLE_DEBUG */
560 g_object_last_unref (GObject *object)
562 g_return_if_fail (object->ref_count > 0);
564 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
565 G_OBJECT_GET_CLASS (object)->dispose (object);
567 #ifdef G_ENABLE_DEBUG
568 if (g_trap_object_ref == object)
570 #endif /* G_ENABLE_DEBUG */
572 object->ref_count -= 1;
574 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
576 g_signal_handlers_destroy (object);
577 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
578 G_OBJECT_GET_CLASS (object)->finalize (object);
579 #ifdef G_ENABLE_DEBUG
582 /* catch objects not chaining finalize handlers */
583 G_LOCK (debug_objects);
584 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
585 G_UNLOCK (debug_objects);
587 #endif /* G_ENABLE_DEBUG */
588 g_type_free_instance ((GTypeInstance*) object);
593 g_object_dispatch_properties_changed (GObject *object,
599 for (i = 0; i < n_pspecs; i++)
600 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
604 g_object_run_dispose (GObject *object)
606 g_return_if_fail (G_IS_OBJECT (object));
607 g_return_if_fail (object->ref_count > 0);
609 g_object_ref (object);
610 G_OBJECT_GET_CLASS (object)->dispose (object);
611 g_object_unref (object);
615 g_object_freeze_notify (GObject *object)
617 g_return_if_fail (G_IS_OBJECT (object));
618 if (!object->ref_count)
621 g_object_ref (object);
622 g_object_notify_queue_freeze (object, &property_notify_context);
623 g_object_unref (object);
627 g_object_notify (GObject *object,
628 const gchar *property_name)
632 g_return_if_fail (G_IS_OBJECT (object));
633 g_return_if_fail (property_name != NULL);
634 if (!object->ref_count)
637 g_object_ref (object);
638 /* We don't need to get the redirect target
639 * (by, e.g. calling g_object_class_find_property())
640 * because g_object_notify_queue_add() does that
642 pspec = g_param_spec_pool_lookup (pspec_pool,
644 G_OBJECT_TYPE (object),
648 g_warning ("%s: object class `%s' has no property named `%s'",
650 G_OBJECT_TYPE_NAME (object),
654 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
656 g_object_notify_queue_add (object, nqueue, pspec);
657 g_object_notify_queue_thaw (object, nqueue);
659 g_object_unref (object);
663 g_object_thaw_notify (GObject *object)
665 GObjectNotifyQueue *nqueue;
667 g_return_if_fail (G_IS_OBJECT (object));
668 if (!object->ref_count)
671 g_object_ref (object);
672 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
673 if (!nqueue || !nqueue->freeze_count)
674 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
675 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
677 g_object_notify_queue_thaw (object, nqueue);
678 g_object_unref (object);
682 object_get_property (GObject *object,
686 GObjectClass *class = g_type_class_peek (pspec->owner_type);
687 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
688 GParamSpec *redirect;
690 redirect = g_param_spec_get_redirect_target (pspec);
694 class->get_property (object, param_id, value, pspec);
698 object_set_property (GObject *object,
701 GObjectNotifyQueue *nqueue)
703 GValue tmp_value = { 0, };
704 GObjectClass *class = g_type_class_peek (pspec->owner_type);
705 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
706 GParamSpec *redirect;
708 redirect = g_param_spec_get_redirect_target (pspec);
712 /* provide a copy to work from, convert (if necessary) and validate */
713 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
714 if (!g_value_transform (value, &tmp_value))
715 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
717 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
718 G_VALUE_TYPE_NAME (value));
719 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
721 gchar *contents = g_strdup_value_contents (value);
723 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
725 G_VALUE_TYPE_NAME (value),
727 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
732 class->set_property (object, param_id, &tmp_value, pspec);
733 g_object_notify_queue_add (object, nqueue, pspec);
735 g_value_unset (&tmp_value);
739 object_interface_check_properties (gpointer func_data,
742 GTypeInterface *iface_class = g_iface;
743 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
744 GType iface_type = iface_class->g_type;
748 if (!G_IS_OBJECT_CLASS (class))
751 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
755 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
757 G_OBJECT_CLASS_TYPE (class),
762 g_critical ("Object class %s doesn't implement property "
763 "'%s' from interface '%s'",
764 g_type_name (G_OBJECT_CLASS_TYPE (class)),
766 g_type_name (iface_type));
771 /* The implementation paramspec must have a less restrictive
772 * type than the interface parameter spec for set() and a
773 * more restrictive type for get(). We just require equality,
774 * rather than doing something more complicated checking
775 * the READABLE and WRITABLE flags. We also simplify here
776 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
779 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
780 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
782 g_critical ("Property '%s' on class '%s' has type '%s' "
783 "which is different from the type '%s', "
784 "of the property on interface '%s'\n",
786 g_type_name (G_OBJECT_CLASS_TYPE (class)),
787 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
788 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
789 g_type_name (iface_type));
792 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
794 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
795 * READABLE and WRITABLE remove restrictions. The implementation
796 * paramspec must have less restrictive flags.
799 (!SUBSET (class_pspec->flags,
801 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
802 !SUBSET (pspecs[n]->flags,
804 G_PARAM_READABLE | G_PARAM_WRITABLE)))
806 g_critical ("Flags for property '%s' on class '%s' "
807 "are not compatible with the property on"
810 g_type_name (G_OBJECT_CLASS_TYPE (class)),
811 g_type_name (iface_type));
820 g_object_new (GType object_type,
821 const gchar *first_property_name,
827 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
829 va_start (var_args, first_property_name);
830 object = g_object_new_valist (object_type, first_property_name, var_args);
837 object_in_construction (GObject *object)
839 gboolean in_construction;
840 G_LOCK (construct_objects_lock);
841 in_construction = g_slist_find (construct_objects, object) != NULL;
842 G_UNLOCK (construct_objects_lock);
843 return in_construction;
847 g_object_newv (GType object_type,
849 GParameter *parameters)
851 GObjectConstructParam *cparams, *oparams;
852 GObjectNotifyQueue *nqueue;
854 GObjectClass *class, *unref_class = NULL;
856 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
861 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
863 class = g_type_class_peek_static (object_type);
865 class = unref_class = g_type_class_ref (object_type);
866 for (slist = class->construct_properties; slist; slist = slist->next)
868 clist = g_list_prepend (clist, slist->data);
869 n_total_cparams += 1;
872 /* collect parameters, sort into construction and normal ones */
873 oparams = g_new (GObjectConstructParam, n_parameters);
874 cparams = g_new (GObjectConstructParam, n_total_cparams);
875 for (i = 0; i < n_parameters; i++)
877 GValue *value = ¶meters[i].value;
878 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
884 g_warning ("%s: object class `%s' has no property named `%s'",
886 g_type_name (object_type),
890 if (!(pspec->flags & G_PARAM_WRITABLE))
892 g_warning ("%s: property `%s' of object class `%s' is not writable",
895 g_type_name (object_type));
898 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
900 GList *list = g_list_find (clist, pspec);
904 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
905 G_STRFUNC, pspec->name, g_type_name (object_type));
908 cparams[n_cparams].pspec = pspec;
909 cparams[n_cparams].value = value;
914 list->prev->next = list->next;
916 list->next->prev = list->prev;
917 g_list_free_1 (list);
921 oparams[n_oparams].pspec = pspec;
922 oparams[n_oparams].value = value;
927 /* set remaining construction properties to default values */
928 n_cvalues = n_total_cparams - n_cparams;
929 cvalues = g_new (GValue, n_cvalues);
932 GList *tmp = clist->next;
933 GParamSpec *pspec = clist->data;
934 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
937 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
938 g_param_value_set_default (pspec, value);
940 cparams[n_cparams].pspec = pspec;
941 cparams[n_cparams].value = value;
944 g_list_free_1 (clist);
948 /* construct object from construction parameters */
949 object = class->constructor (object_type, n_total_cparams, cparams);
950 G_LOCK (construct_objects_lock);
951 construct_objects = g_slist_remove (construct_objects, object);
952 G_UNLOCK (construct_objects_lock);
954 /* free construction values */
957 g_value_unset (cvalues + n_cvalues);
960 /* release g_object_init() notification queue freeze_count */
961 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
962 g_object_notify_queue_thaw (object, nqueue);
964 /* set remaining properties */
965 for (i = 0; i < n_oparams; i++)
966 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
970 g_type_class_unref (unref_class);
972 /* release our own freeze count and handle notifications */
973 g_object_notify_queue_thaw (object, nqueue);
979 g_object_new_valist (GType object_type,
980 const gchar *first_property_name,
987 guint n_params = 0, n_alloced_params = 16;
989 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
991 if (!first_property_name)
992 return g_object_newv (object_type, 0, NULL);
994 class = g_type_class_ref (object_type);
996 params = g_new (GParameter, n_alloced_params);
997 name = first_property_name;
1000 gchar *error = NULL;
1001 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1007 g_warning ("%s: object class `%s' has no property named `%s'",
1009 g_type_name (object_type),
1013 if (n_params >= n_alloced_params)
1015 n_alloced_params += 16;
1016 params = g_renew (GParameter, params, n_alloced_params);
1018 params[n_params].name = name;
1019 params[n_params].value.g_type = 0;
1020 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1021 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1024 g_warning ("%s: %s", G_STRFUNC, error);
1026 g_value_unset (¶ms[n_params].value);
1030 name = va_arg (var_args, gchar*);
1033 object = g_object_newv (object_type, n_params, params);
1036 g_value_unset (¶ms[n_params].value);
1039 g_type_class_unref (class);
1045 g_object_constructor (GType type,
1046 guint n_construct_properties,
1047 GObjectConstructParam *construct_params)
1052 object = (GObject*) g_type_create_instance (type);
1054 /* set construction parameters */
1055 if (n_construct_properties)
1057 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1059 /* set construct properties */
1060 while (n_construct_properties--)
1062 GValue *value = construct_params->value;
1063 GParamSpec *pspec = construct_params->pspec;
1066 object_set_property (object, pspec, value, nqueue);
1068 g_object_notify_queue_thaw (object, nqueue);
1069 /* the notification queue is still frozen from g_object_init(), so
1070 * we don't need to handle it here, g_object_newv() takes
1079 g_object_set_valist (GObject *object,
1080 const gchar *first_property_name,
1083 GObjectNotifyQueue *nqueue;
1086 g_return_if_fail (G_IS_OBJECT (object));
1088 g_object_ref (object);
1089 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1091 name = first_property_name;
1094 GValue value = { 0, };
1096 gchar *error = NULL;
1098 pspec = g_param_spec_pool_lookup (pspec_pool,
1100 G_OBJECT_TYPE (object),
1104 g_warning ("%s: object class `%s' has no property named `%s'",
1106 G_OBJECT_TYPE_NAME (object),
1110 if (!(pspec->flags & G_PARAM_WRITABLE))
1112 g_warning ("%s: property `%s' of object class `%s' is not writable",
1115 G_OBJECT_TYPE_NAME (object));
1118 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1120 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1121 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1125 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1127 G_VALUE_COLLECT (&value, var_args, 0, &error);
1130 g_warning ("%s: %s", G_STRFUNC, error);
1132 g_value_unset (&value);
1136 object_set_property (object, pspec, &value, nqueue);
1137 g_value_unset (&value);
1139 name = va_arg (var_args, gchar*);
1142 g_object_notify_queue_thaw (object, nqueue);
1143 g_object_unref (object);
1147 g_object_get_valist (GObject *object,
1148 const gchar *first_property_name,
1153 g_return_if_fail (G_IS_OBJECT (object));
1155 g_object_ref (object);
1157 name = first_property_name;
1161 GValue value = { 0, };
1165 pspec = g_param_spec_pool_lookup (pspec_pool,
1167 G_OBJECT_TYPE (object),
1171 g_warning ("%s: object class `%s' has no property named `%s'",
1173 G_OBJECT_TYPE_NAME (object),
1177 if (!(pspec->flags & G_PARAM_READABLE))
1179 g_warning ("%s: property `%s' of object class `%s' is not readable",
1182 G_OBJECT_TYPE_NAME (object));
1186 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1188 object_get_property (object, pspec, &value);
1190 G_VALUE_LCOPY (&value, var_args, 0, &error);
1193 g_warning ("%s: %s", G_STRFUNC, error);
1195 g_value_unset (&value);
1199 g_value_unset (&value);
1201 name = va_arg (var_args, gchar*);
1204 g_object_unref (object);
1208 g_object_set (gpointer _object,
1209 const gchar *first_property_name,
1212 GObject *object = _object;
1215 g_return_if_fail (G_IS_OBJECT (object));
1217 va_start (var_args, first_property_name);
1218 g_object_set_valist (object, first_property_name, var_args);
1223 g_object_get (gpointer _object,
1224 const gchar *first_property_name,
1227 GObject *object = _object;
1230 g_return_if_fail (G_IS_OBJECT (object));
1232 va_start (var_args, first_property_name);
1233 g_object_get_valist (object, first_property_name, var_args);
1238 g_object_set_property (GObject *object,
1239 const gchar *property_name,
1240 const GValue *value)
1242 GObjectNotifyQueue *nqueue;
1245 g_return_if_fail (G_IS_OBJECT (object));
1246 g_return_if_fail (property_name != NULL);
1247 g_return_if_fail (G_IS_VALUE (value));
1249 g_object_ref (object);
1250 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1252 pspec = g_param_spec_pool_lookup (pspec_pool,
1254 G_OBJECT_TYPE (object),
1257 g_warning ("%s: object class `%s' has no property named `%s'",
1259 G_OBJECT_TYPE_NAME (object),
1261 else if (!(pspec->flags & G_PARAM_WRITABLE))
1262 g_warning ("%s: property `%s' of object class `%s' is not writable",
1265 G_OBJECT_TYPE_NAME (object));
1266 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1267 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1268 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1270 object_set_property (object, pspec, value, nqueue);
1272 g_object_notify_queue_thaw (object, nqueue);
1273 g_object_unref (object);
1277 g_object_get_property (GObject *object,
1278 const gchar *property_name,
1283 g_return_if_fail (G_IS_OBJECT (object));
1284 g_return_if_fail (property_name != NULL);
1285 g_return_if_fail (G_IS_VALUE (value));
1287 g_object_ref (object);
1289 pspec = g_param_spec_pool_lookup (pspec_pool,
1291 G_OBJECT_TYPE (object),
1294 g_warning ("%s: object class `%s' has no property named `%s'",
1296 G_OBJECT_TYPE_NAME (object),
1298 else if (!(pspec->flags & G_PARAM_READABLE))
1299 g_warning ("%s: property `%s' of object class `%s' is not readable",
1302 G_OBJECT_TYPE_NAME (object));
1305 GValue *prop_value, tmp_value = { 0, };
1307 /* auto-conversion of the callers value type
1309 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1311 g_value_reset (value);
1314 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1316 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1317 G_STRFUNC, pspec->name,
1318 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1319 G_VALUE_TYPE_NAME (value));
1320 g_object_unref (object);
1325 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1326 prop_value = &tmp_value;
1328 object_get_property (object, pspec, prop_value);
1329 if (prop_value != value)
1331 g_value_transform (prop_value, value);
1332 g_value_unset (&tmp_value);
1336 g_object_unref (object);
1340 g_object_connect (gpointer _object,
1341 const gchar *signal_spec,
1344 GObject *object = _object;
1347 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1348 g_return_val_if_fail (object->ref_count > 0, object);
1350 va_start (var_args, signal_spec);
1353 GCallback callback = va_arg (var_args, GCallback);
1354 gpointer data = va_arg (var_args, gpointer);
1357 if (strncmp (signal_spec, "signal::", 8) == 0)
1358 sid = g_signal_connect_data (object, signal_spec + 8,
1359 callback, data, NULL,
1361 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1362 strncmp (signal_spec, "object-signal::", 15) == 0)
1363 sid = g_signal_connect_object (object, signal_spec + 15,
1366 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1367 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1368 sid = g_signal_connect_data (object, signal_spec + 16,
1369 callback, data, NULL,
1371 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1372 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1373 sid = g_signal_connect_object (object, signal_spec + 23,
1376 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1377 strncmp (signal_spec, "signal-after::", 14) == 0)
1378 sid = g_signal_connect_data (object, signal_spec + 14,
1379 callback, data, NULL,
1381 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1382 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1383 sid = g_signal_connect_object (object, signal_spec + 21,
1386 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1387 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1388 sid = g_signal_connect_data (object, signal_spec + 22,
1389 callback, data, NULL,
1390 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1391 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1392 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1393 sid = g_signal_connect_object (object, signal_spec + 29,
1395 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1398 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1401 signal_spec = va_arg (var_args, gchar*);
1409 g_object_disconnect (gpointer _object,
1410 const gchar *signal_spec,
1413 GObject *object = _object;
1416 g_return_if_fail (G_IS_OBJECT (object));
1417 g_return_if_fail (object->ref_count > 0);
1419 va_start (var_args, signal_spec);
1422 GCallback callback = va_arg (var_args, GCallback);
1423 gpointer data = va_arg (var_args, gpointer);
1424 guint sid = 0, detail = 0, mask = 0;
1426 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1427 strncmp (signal_spec, "any-signal::", 12) == 0)
1430 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1432 else if (strcmp (signal_spec, "any_signal") == 0 ||
1433 strcmp (signal_spec, "any-signal") == 0)
1436 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1440 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1444 if ((mask & G_SIGNAL_MATCH_ID) &&
1445 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1446 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1447 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1449 NULL, (gpointer)callback, data))
1450 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1451 signal_spec = va_arg (var_args, gchar*);
1462 } weak_refs[1]; /* flexible array */
1466 weak_refs_notify (gpointer data)
1468 WeakRefStack *wstack = data;
1471 for (i = 0; i < wstack->n_weak_refs; i++)
1472 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1477 g_object_weak_ref (GObject *object,
1481 WeakRefStack *wstack;
1484 g_return_if_fail (G_IS_OBJECT (object));
1485 g_return_if_fail (notify != NULL);
1486 g_return_if_fail (object->ref_count >= 1);
1488 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1491 i = wstack->n_weak_refs++;
1492 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1496 wstack = g_renew (WeakRefStack, NULL, 1);
1497 wstack->object = object;
1498 wstack->n_weak_refs = 1;
1501 wstack->weak_refs[i].notify = notify;
1502 wstack->weak_refs[i].data = data;
1503 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1507 g_object_weak_unref (GObject *object,
1511 WeakRefStack *wstack;
1512 gboolean found_one = FALSE;
1514 g_return_if_fail (G_IS_OBJECT (object));
1515 g_return_if_fail (notify != NULL);
1517 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1522 for (i = 0; i < wstack->n_weak_refs; i++)
1523 if (wstack->weak_refs[i].notify == notify &&
1524 wstack->weak_refs[i].data == data)
1527 wstack->n_weak_refs -= 1;
1528 if (i != wstack->n_weak_refs)
1529 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
1535 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
1539 g_object_add_weak_pointer (GObject *object,
1540 gpointer *weak_pointer_location)
1542 g_return_if_fail (G_IS_OBJECT (object));
1543 g_return_if_fail (weak_pointer_location != NULL);
1545 g_object_weak_ref (object,
1546 (GWeakNotify) g_nullify_pointer,
1547 weak_pointer_location);
1551 g_object_remove_weak_pointer (GObject *object,
1552 gpointer *weak_pointer_location)
1554 g_return_if_fail (G_IS_OBJECT (object));
1555 g_return_if_fail (weak_pointer_location != NULL);
1557 g_object_weak_unref (object,
1558 (GWeakNotify) g_nullify_pointer,
1559 weak_pointer_location);
1564 guint n_toggle_refs;
1566 GToggleNotify notify;
1568 } toggle_refs[1]; /* flexible array */
1572 toggle_refs_notify (GObject *object,
1573 gboolean is_last_ref)
1575 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
1577 /* Reentrancy here is not as tricky as it seems, because a toggle reference
1578 * will only be notified when there is exactly one of them.
1580 g_assert (tstack->n_toggle_refs == 1);
1581 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
1585 g_object_add_toggle_ref (GObject *object,
1586 GToggleNotify notify,
1589 ToggleRefStack *tstack;
1592 g_return_if_fail (G_IS_OBJECT (object));
1593 g_return_if_fail (notify != NULL);
1594 g_return_if_fail (object->ref_count >= 1);
1596 g_object_ref (object);
1598 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
1601 i = tstack->n_toggle_refs++;
1602 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
1603 * in tstate->toggle_refs */
1604 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
1608 tstack = g_renew (ToggleRefStack, NULL, 1);
1609 tstack->object = object;
1610 tstack->n_toggle_refs = 1;
1614 /* Set a flag for fast lookup after adding the first toggle reference */
1615 if (tstack->n_toggle_refs == 1)
1616 G_DATALIST_SET_FLAGS (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
1618 tstack->toggle_refs[i].notify = notify;
1619 tstack->toggle_refs[i].data = data;
1620 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
1621 (GDestroyNotify)g_free);
1625 g_object_remove_toggle_ref (GObject *object,
1626 GToggleNotify notify,
1629 ToggleRefStack *tstack;
1630 gboolean found_one = FALSE;
1632 g_return_if_fail (G_IS_OBJECT (object));
1633 g_return_if_fail (notify != NULL);
1635 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
1640 for (i = 0; i < tstack->n_toggle_refs; i++)
1641 if (tstack->toggle_refs[i].notify == notify &&
1642 tstack->toggle_refs[i].data == data)
1645 tstack->n_toggle_refs -= 1;
1646 if (i != tstack->n_toggle_refs)
1647 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
1649 if (tstack->n_toggle_refs == 0)
1650 G_DATALIST_UNSET_FLAGS (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
1652 g_object_unref (object);
1659 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
1663 g_object_ref (gpointer _object)
1665 GObject *object = _object;
1667 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1668 g_return_val_if_fail (object->ref_count > 0, NULL);
1670 #ifdef G_ENABLE_DEBUG
1671 if (g_trap_object_ref == object)
1673 #endif /* G_ENABLE_DEBUG */
1675 object->ref_count += 1;
1676 if (object->ref_count == 2 && OBJECT_HAS_TOGGLE_REF (object))
1677 toggle_refs_notify (object, FALSE);
1683 g_object_unref (gpointer _object)
1685 GObject *object = _object;
1687 g_return_if_fail (G_IS_OBJECT (object));
1688 g_return_if_fail (object->ref_count > 0);
1690 #ifdef G_ENABLE_DEBUG
1691 if (g_trap_object_ref == object)
1693 #endif /* G_ENABLE_DEBUG */
1695 if (object->ref_count > 1)
1697 object->ref_count -= 1;
1698 if (object->ref_count == 1 && OBJECT_HAS_TOGGLE_REF (object))
1699 toggle_refs_notify (object, TRUE);
1702 g_object_last_unref (object);
1706 g_object_get_qdata (GObject *object,
1709 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1711 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1715 g_object_set_qdata (GObject *object,
1719 g_return_if_fail (G_IS_OBJECT (object));
1720 g_return_if_fail (quark > 0);
1722 g_datalist_id_set_data (&object->qdata, quark, data);
1726 g_object_set_qdata_full (GObject *object,
1729 GDestroyNotify destroy)
1731 g_return_if_fail (G_IS_OBJECT (object));
1732 g_return_if_fail (quark > 0);
1734 g_datalist_id_set_data_full (&object->qdata, quark, data,
1735 data ? destroy : (GDestroyNotify) NULL);
1739 g_object_steal_qdata (GObject *object,
1742 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1743 g_return_val_if_fail (quark > 0, NULL);
1745 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1749 g_object_get_data (GObject *object,
1754 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1755 g_return_val_if_fail (key != NULL, NULL);
1757 quark = g_quark_try_string (key);
1759 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1763 g_object_set_data (GObject *object,
1767 g_return_if_fail (G_IS_OBJECT (object));
1768 g_return_if_fail (key != NULL);
1770 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1774 g_object_set_data_full (GObject *object,
1777 GDestroyNotify destroy)
1779 g_return_if_fail (G_IS_OBJECT (object));
1780 g_return_if_fail (key != NULL);
1782 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1783 data ? destroy : (GDestroyNotify) NULL);
1787 g_object_steal_data (GObject *object,
1792 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1793 g_return_val_if_fail (key != NULL, NULL);
1795 quark = g_quark_try_string (key);
1797 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1801 g_value_object_init (GValue *value)
1803 value->data[0].v_pointer = NULL;
1807 g_value_object_free_value (GValue *value)
1809 if (value->data[0].v_pointer)
1810 g_object_unref (value->data[0].v_pointer);
1814 g_value_object_copy_value (const GValue *src_value,
1817 if (src_value->data[0].v_pointer)
1818 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1820 dest_value->data[0].v_pointer = NULL;
1824 g_value_object_transform_value (const GValue *src_value,
1827 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)))
1828 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1830 dest_value->data[0].v_pointer = NULL;
1834 g_value_object_peek_pointer (const GValue *value)
1836 return value->data[0].v_pointer;
1840 g_value_object_collect_value (GValue *value,
1841 guint n_collect_values,
1842 GTypeCValue *collect_values,
1843 guint collect_flags)
1845 if (collect_values[0].v_pointer)
1847 GObject *object = collect_values[0].v_pointer;
1849 if (object->g_type_instance.g_class == NULL)
1850 return g_strconcat ("invalid unclassed object pointer for value type `",
1851 G_VALUE_TYPE_NAME (value),
1854 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1855 return g_strconcat ("invalid object type `",
1856 G_OBJECT_TYPE_NAME (object),
1857 "' for value type `",
1858 G_VALUE_TYPE_NAME (value),
1861 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1862 value->data[0].v_pointer = g_object_ref (object);
1865 value->data[0].v_pointer = NULL;
1871 g_value_object_lcopy_value (const GValue *value,
1872 guint n_collect_values,
1873 GTypeCValue *collect_values,
1874 guint collect_flags)
1876 GObject **object_p = collect_values[0].v_pointer;
1879 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1881 if (!value->data[0].v_pointer)
1883 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1884 *object_p = value->data[0].v_pointer;
1886 *object_p = g_object_ref (value->data[0].v_pointer);
1892 g_value_set_object (GValue *value,
1897 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1899 old = value->data[0].v_pointer;
1903 g_return_if_fail (G_IS_OBJECT (v_object));
1904 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1906 value->data[0].v_pointer = v_object;
1907 g_object_ref (value->data[0].v_pointer);
1910 value->data[0].v_pointer = NULL;
1913 g_object_unref (old);
1917 g_value_set_object_take_ownership (GValue *value,
1920 g_value_take_object (value, v_object);
1924 g_value_take_object (GValue *value,
1927 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1929 if (value->data[0].v_pointer)
1931 g_object_unref (value->data[0].v_pointer);
1932 value->data[0].v_pointer = NULL;
1937 g_return_if_fail (G_IS_OBJECT (v_object));
1938 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1940 value->data[0].v_pointer = v_object; /* we take over the reference count */
1945 g_value_get_object (const GValue *value)
1947 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1949 return value->data[0].v_pointer;
1953 g_value_dup_object (const GValue *value)
1955 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1957 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1961 g_signal_connect_object (gpointer instance,
1962 const gchar *detailed_signal,
1963 GCallback c_handler,
1965 GConnectFlags connect_flags)
1967 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1968 g_return_val_if_fail (detailed_signal != NULL, 0);
1969 g_return_val_if_fail (c_handler != NULL, 0);
1975 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1977 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1979 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1982 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1988 GClosure *closures[1]; /* flexible array */
1990 /* don't change this structure without supplying an accessor for
1991 * watched closures, e.g.:
1992 * GSList* g_object_list_watched_closures (GObject *object)
1995 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1996 * carray = g_object_get_data (object, "GObject-closure-array");
1999 * GSList *slist = NULL;
2001 * for (i = 0; i < carray->n_closures; i++)
2002 * slist = g_slist_prepend (slist, carray->closures[i]);
2010 object_remove_closure (gpointer data,
2013 GObject *object = data;
2014 CArray *carray = g_object_get_qdata (object, quark_closure_array);
2017 for (i = 0; i < carray->n_closures; i++)
2018 if (carray->closures[i] == closure)
2020 carray->n_closures--;
2021 if (i < carray->n_closures)
2022 carray->closures[i] = carray->closures[carray->n_closures];
2025 g_assert_not_reached ();
2029 destroy_closure_array (gpointer data)
2031 CArray *carray = data;
2032 GObject *object = carray->object;
2033 guint i, n = carray->n_closures;
2035 for (i = 0; i < n; i++)
2037 GClosure *closure = carray->closures[i];
2039 /* removing object_remove_closure() upfront is probably faster than
2040 * letting it fiddle with quark_closure_array which is empty anyways
2042 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
2043 g_closure_invalidate (closure);
2049 g_object_watch_closure (GObject *object,
2055 g_return_if_fail (G_IS_OBJECT (object));
2056 g_return_if_fail (closure != NULL);
2057 g_return_if_fail (closure->is_invalid == FALSE);
2058 g_return_if_fail (closure->in_marshal == FALSE);
2059 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
2061 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
2062 g_closure_add_marshal_guards (closure,
2063 object, (GClosureNotify) g_object_ref,
2064 object, (GClosureNotify) g_object_unref);
2065 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
2068 carray = g_renew (CArray, NULL, 1);
2069 carray->object = object;
2070 carray->n_closures = 1;
2075 i = carray->n_closures++;
2076 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
2078 carray->closures[i] = closure;
2079 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
2083 g_closure_new_object (guint sizeof_closure,
2088 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2089 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2091 closure = g_closure_new_simple (sizeof_closure, object);
2092 g_object_watch_closure (object, closure);
2098 g_cclosure_new_object (GCallback callback_func,
2103 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2104 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2105 g_return_val_if_fail (callback_func != NULL, NULL);
2107 closure = g_cclosure_new (callback_func, object, NULL);
2108 g_object_watch_closure (object, closure);
2114 g_cclosure_new_object_swap (GCallback callback_func,
2119 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2120 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2121 g_return_val_if_fail (callback_func != NULL, NULL);
2123 closure = g_cclosure_new_swap (callback_func, object, NULL);
2124 g_object_watch_closure (object, closure);
2129 #define __G_OBJECT_C__
2130 #include "gobjectaliasdef.c"