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 <glib/gdatasetprivate.h>
23 * MT safe with regards to reference counting.
26 #include "gvaluecollector.h"
28 #include "gparamspecs.h"
29 #include "gvaluetypes.h"
33 #include "gobjectalias.h"
35 /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */
36 #include "gobjectnotifyqueue.c"
39 #define PREALLOC_CPARAMS (8)
43 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
44 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
46 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
47 #define OBJECT_HAS_TOGGLE_REF(object) \
48 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
49 #define OBJECT_FLOATING_FLAG 0x2
59 /* --- properties --- */
65 /* --- prototypes --- */
66 static void g_object_base_class_init (GObjectClass *class);
67 static void g_object_base_class_finalize (GObjectClass *class);
68 static void g_object_do_class_init (GObjectClass *class);
69 static void g_object_init (GObject *object);
70 static GObject* g_object_constructor (GType type,
71 guint n_construct_properties,
72 GObjectConstructParam *construct_params);
73 static void g_object_real_dispose (GObject *object);
74 static void g_object_finalize (GObject *object);
75 static void g_object_do_set_property (GObject *object,
79 static void g_object_do_get_property (GObject *object,
83 static void g_value_object_init (GValue *value);
84 static void g_value_object_free_value (GValue *value);
85 static void g_value_object_copy_value (const GValue *src_value,
87 static void g_value_object_transform_value (const GValue *src_value,
89 static gpointer g_value_object_peek_pointer (const GValue *value);
90 static gchar* g_value_object_collect_value (GValue *value,
91 guint n_collect_values,
92 GTypeCValue *collect_values,
94 static gchar* g_value_object_lcopy_value (const GValue *value,
95 guint n_collect_values,
96 GTypeCValue *collect_values,
98 static void g_object_dispatch_properties_changed (GObject *object,
100 GParamSpec **pspecs);
101 static inline void object_get_property (GObject *object,
104 static inline void object_set_property (GObject *object,
107 GObjectNotifyQueue *nqueue);
108 static guint object_floating_flag_handler (GObject *object,
111 static void object_interface_check_properties (gpointer func_data,
115 /* --- variables --- */
116 static GQuark quark_closure_array = 0;
117 static GQuark quark_weak_refs = 0;
118 static GQuark quark_toggle_refs = 0;
119 static GParamSpecPool *pspec_pool = NULL;
120 static GObjectNotifyContext property_notify_context = { 0, };
121 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
122 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
123 G_LOCK_DEFINE_STATIC (construction_mutex);
124 static GSList *construction_objects = NULL;
126 /* --- functions --- */
127 #ifdef G_ENABLE_DEBUG
128 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
129 G_LOCK_DEFINE_STATIC (debug_objects);
130 static volatile GObject *g_trap_object_ref = NULL;
131 static guint debug_objects_count = 0;
132 static GHashTable *debug_objects_ht = NULL;
134 debug_objects_foreach (gpointer key,
138 GObject *object = value;
140 g_message ("[%p] stale %s\tref_count=%u",
142 G_OBJECT_TYPE_NAME (object),
146 debug_objects_atexit (void)
150 G_LOCK (debug_objects);
151 g_message ("stale GObjects: %u", debug_objects_count);
152 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
153 G_UNLOCK (debug_objects);
156 #endif /* G_ENABLE_DEBUG */
159 g_object_type_init (void)
161 static gboolean initialized = FALSE;
162 static const GTypeFundamentalInfo finfo = {
163 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
165 static GTypeInfo info = {
166 sizeof (GObjectClass),
167 (GBaseInitFunc) g_object_base_class_init,
168 (GBaseFinalizeFunc) g_object_base_class_finalize,
169 (GClassInitFunc) g_object_do_class_init,
170 NULL /* class_destroy */,
171 NULL /* class_data */,
174 (GInstanceInitFunc) g_object_init,
175 NULL, /* value_table */
177 static const GTypeValueTable value_table = {
178 g_value_object_init, /* value_init */
179 g_value_object_free_value, /* value_free */
180 g_value_object_copy_value, /* value_copy */
181 g_value_object_peek_pointer, /* value_peek_pointer */
182 "p", /* collect_format */
183 g_value_object_collect_value, /* collect_value */
184 "p", /* lcopy_format */
185 g_value_object_lcopy_value, /* lcopy_value */
189 g_return_if_fail (initialized == FALSE);
194 info.value_table = &value_table;
195 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
196 g_assert (type == G_TYPE_OBJECT);
197 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
199 #ifdef G_ENABLE_DEBUG
202 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
203 g_atexit (debug_objects_atexit);
205 #endif /* G_ENABLE_DEBUG */
209 g_object_base_class_init (GObjectClass *class)
211 GObjectClass *pclass = g_type_class_peek_parent (class);
213 /* reset instance specific fields and methods that don't get inherited */
214 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
215 class->get_property = NULL;
216 class->set_property = NULL;
220 g_object_base_class_finalize (GObjectClass *class)
224 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
226 g_slist_free (class->construct_properties);
227 class->construct_properties = NULL;
228 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
229 for (node = list; node; node = node->next)
231 GParamSpec *pspec = node->data;
233 g_param_spec_pool_remove (pspec_pool, pspec);
234 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
235 g_param_spec_unref (pspec);
241 g_object_notify_dispatcher (GObject *object,
245 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
249 g_object_do_class_init (GObjectClass *class)
251 /* read the comment about typedef struct CArray; on why not to change this quark */
252 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
254 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
255 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
256 pspec_pool = g_param_spec_pool_new (TRUE);
257 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
258 property_notify_context.dispatcher = g_object_notify_dispatcher;
260 class->constructor = g_object_constructor;
261 class->set_property = g_object_do_set_property;
262 class->get_property = g_object_do_get_property;
263 class->dispose = g_object_real_dispose;
264 class->finalize = g_object_finalize;
265 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
266 class->notify = NULL;
268 gobject_signals[NOTIFY] =
269 g_signal_new (g_intern_static_string ("notify"),
270 G_TYPE_FROM_CLASS (class),
271 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
272 G_STRUCT_OFFSET (GObjectClass, notify),
274 g_cclosure_marshal_VOID__PARAM,
278 /* Install a check function that we'll use to verify that classes that
279 * implement an interface implement all properties for that interface
281 g_type_add_interface_check (NULL, object_interface_check_properties);
285 install_property_internal (GType g_type,
289 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
291 g_warning ("When installing property: type `%s' already has a property named `%s'",
292 g_type_name (g_type),
297 g_param_spec_ref (pspec);
298 g_param_spec_sink (pspec);
299 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
300 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
304 g_object_class_install_property (GObjectClass *class,
308 g_return_if_fail (G_IS_OBJECT_CLASS (class));
309 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
310 if (pspec->flags & G_PARAM_WRITABLE)
311 g_return_if_fail (class->set_property != NULL);
312 if (pspec->flags & G_PARAM_READABLE)
313 g_return_if_fail (class->get_property != NULL);
314 g_return_if_fail (property_id > 0);
315 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
316 if (pspec->flags & G_PARAM_CONSTRUCT)
317 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
318 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
319 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
321 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
323 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
324 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
326 /* for property overrides of construct poperties, we have to get rid
327 * of the overidden inherited construct property
329 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
330 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
331 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
335 g_object_interface_install_property (gpointer g_iface,
338 GTypeInterface *iface_class = g_iface;
340 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
341 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
342 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
343 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
345 install_property_internal (iface_class->g_type, 0, pspec);
349 g_object_class_find_property (GObjectClass *class,
350 const gchar *property_name)
353 GParamSpec *redirect;
355 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
356 g_return_val_if_fail (property_name != NULL, NULL);
358 pspec = g_param_spec_pool_lookup (pspec_pool,
360 G_OBJECT_CLASS_TYPE (class),
364 redirect = g_param_spec_get_redirect_target (pspec);
375 g_object_interface_find_property (gpointer g_iface,
376 const gchar *property_name)
378 GTypeInterface *iface_class = g_iface;
380 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
381 g_return_val_if_fail (property_name != NULL, NULL);
383 return g_param_spec_pool_lookup (pspec_pool,
390 g_object_class_override_property (GObjectClass *oclass,
394 GParamSpec *overridden = NULL;
398 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
399 g_return_if_fail (property_id > 0);
400 g_return_if_fail (name != NULL);
402 /* Find the overridden property; first check parent types
404 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
405 if (parent_type != G_TYPE_NONE)
406 overridden = g_param_spec_pool_lookup (pspec_pool,
415 /* Now check interfaces
417 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
418 while (n_ifaces-- && !overridden)
420 overridden = g_param_spec_pool_lookup (pspec_pool,
431 g_warning ("%s: Can't find property to override for '%s::%s'",
432 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
436 new = g_param_spec_override (name, overridden);
437 g_object_class_install_property (oclass, property_id, new);
440 GParamSpec** /* free result */
441 g_object_class_list_properties (GObjectClass *class,
442 guint *n_properties_p)
447 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
449 pspecs = g_param_spec_pool_list (pspec_pool,
450 G_OBJECT_CLASS_TYPE (class),
458 GParamSpec** /* free result */
459 g_object_interface_list_properties (gpointer g_iface,
460 guint *n_properties_p)
462 GTypeInterface *iface_class = g_iface;
466 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
468 pspecs = g_param_spec_pool_list (pspec_pool,
478 g_object_init (GObject *object)
480 object->ref_count = 1;
481 g_datalist_init (&object->qdata);
483 /* freeze object's notification queue, g_object_newv() preserves pairedness */
484 g_object_notify_queue_freeze (object, &property_notify_context);
485 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
486 G_LOCK (construction_mutex);
487 construction_objects = g_slist_prepend (construction_objects, object);
488 G_UNLOCK (construction_mutex);
490 #ifdef G_ENABLE_DEBUG
493 G_LOCK (debug_objects);
494 debug_objects_count++;
495 g_hash_table_insert (debug_objects_ht, object, object);
496 G_UNLOCK (debug_objects);
498 #endif /* G_ENABLE_DEBUG */
502 g_object_do_set_property (GObject *object,
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
516 g_object_do_get_property (GObject *object,
524 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
530 g_object_real_dispose (GObject *object)
532 g_signal_handlers_destroy (object);
533 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
534 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
538 g_object_finalize (GObject *object)
540 g_datalist_clear (&object->qdata);
542 #ifdef G_ENABLE_DEBUG
545 G_LOCK (debug_objects);
546 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
547 g_hash_table_remove (debug_objects_ht, object);
548 debug_objects_count--;
549 G_UNLOCK (debug_objects);
551 #endif /* G_ENABLE_DEBUG */
556 g_object_dispatch_properties_changed (GObject *object,
562 for (i = 0; i < n_pspecs; i++)
563 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
567 g_object_run_dispose (GObject *object)
569 g_return_if_fail (G_IS_OBJECT (object));
570 g_return_if_fail (object->ref_count > 0);
572 g_object_ref (object);
573 G_OBJECT_GET_CLASS (object)->dispose (object);
574 g_object_unref (object);
578 g_object_freeze_notify (GObject *object)
580 g_return_if_fail (G_IS_OBJECT (object));
582 if (g_atomic_int_get (&object->ref_count) == 0)
585 g_object_ref (object);
586 g_object_notify_queue_freeze (object, &property_notify_context);
587 g_object_unref (object);
591 g_object_notify (GObject *object,
592 const gchar *property_name)
596 g_return_if_fail (G_IS_OBJECT (object));
597 g_return_if_fail (property_name != NULL);
598 if (g_atomic_int_get (&object->ref_count) == 0)
601 g_object_ref (object);
602 /* We don't need to get the redirect target
603 * (by, e.g. calling g_object_class_find_property())
604 * because g_object_notify_queue_add() does that
606 pspec = g_param_spec_pool_lookup (pspec_pool,
608 G_OBJECT_TYPE (object),
612 g_warning ("%s: object class `%s' has no property named `%s'",
614 G_OBJECT_TYPE_NAME (object),
618 GObjectNotifyQueue *nqueue;
620 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
621 g_object_notify_queue_add (object, nqueue, pspec);
622 g_object_notify_queue_thaw (object, nqueue);
624 g_object_unref (object);
628 g_object_thaw_notify (GObject *object)
630 GObjectNotifyQueue *nqueue;
632 g_return_if_fail (G_IS_OBJECT (object));
633 if (g_atomic_int_get (&object->ref_count) == 0)
636 g_object_ref (object);
637 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
638 if (!nqueue || !nqueue->freeze_count)
639 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
640 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
642 g_object_notify_queue_thaw (object, nqueue);
643 g_object_unref (object);
647 object_get_property (GObject *object,
651 GObjectClass *class = g_type_class_peek (pspec->owner_type);
652 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
653 GParamSpec *redirect;
655 redirect = g_param_spec_get_redirect_target (pspec);
659 class->get_property (object, param_id, value, pspec);
663 object_set_property (GObject *object,
666 GObjectNotifyQueue *nqueue)
668 GValue tmp_value = { 0, };
669 GObjectClass *class = g_type_class_peek (pspec->owner_type);
670 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
671 GParamSpec *redirect;
673 redirect = g_param_spec_get_redirect_target (pspec);
677 /* provide a copy to work from, convert (if necessary) and validate */
678 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
679 if (!g_value_transform (value, &tmp_value))
680 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
682 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
683 G_VALUE_TYPE_NAME (value));
684 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
686 gchar *contents = g_strdup_value_contents (value);
688 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
690 G_VALUE_TYPE_NAME (value),
692 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
697 class->set_property (object, param_id, &tmp_value, pspec);
698 g_object_notify_queue_add (object, nqueue, pspec);
700 g_value_unset (&tmp_value);
704 object_interface_check_properties (gpointer func_data,
707 GTypeInterface *iface_class = g_iface;
708 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
709 GType iface_type = iface_class->g_type;
713 if (!G_IS_OBJECT_CLASS (class))
716 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
720 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
722 G_OBJECT_CLASS_TYPE (class),
727 g_critical ("Object class %s doesn't implement property "
728 "'%s' from interface '%s'",
729 g_type_name (G_OBJECT_CLASS_TYPE (class)),
731 g_type_name (iface_type));
736 /* The implementation paramspec must have a less restrictive
737 * type than the interface parameter spec for set() and a
738 * more restrictive type for get(). We just require equality,
739 * rather than doing something more complicated checking
740 * the READABLE and WRITABLE flags. We also simplify here
741 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
744 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
745 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
747 g_critical ("Property '%s' on class '%s' has type '%s' "
748 "which is different from the type '%s', "
749 "of the property on interface '%s'\n",
751 g_type_name (G_OBJECT_CLASS_TYPE (class)),
752 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
753 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
754 g_type_name (iface_type));
757 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
759 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
760 * READABLE and WRITABLE remove restrictions. The implementation
761 * paramspec must have less restrictive flags.
764 (!SUBSET (class_pspec->flags,
766 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
767 !SUBSET (pspecs[n]->flags,
769 G_PARAM_READABLE | G_PARAM_WRITABLE)))
771 g_critical ("Flags for property '%s' on class '%s' "
772 "are not compatible with the property on"
775 g_type_name (G_OBJECT_CLASS_TYPE (class)),
776 g_type_name (iface_type));
785 g_object_new (GType object_type,
786 const gchar *first_property_name,
792 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
794 va_start (var_args, first_property_name);
795 object = g_object_new_valist (object_type, first_property_name, var_args);
802 slist_maybe_remove (GSList **slist,
805 GSList *last = NULL, *node = *slist;
808 if (node->data == data)
811 last->next = node->next;
814 g_slist_free_1 (node);
823 static inline gboolean
824 object_in_construction_list (GObject *object)
826 gboolean in_construction;
827 G_LOCK (construction_mutex);
828 in_construction = g_slist_find (construction_objects, object) != NULL;
829 G_UNLOCK (construction_mutex);
830 return in_construction;
834 g_object_newv (GType object_type,
836 GParameter *parameters)
838 GObjectConstructParam *cparams, *oparams;
839 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
841 GObjectClass *class, *unref_class = NULL;
843 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
846 gboolean newly_constructed;
849 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
851 class = g_type_class_peek_static (object_type);
853 class = unref_class = g_type_class_ref (object_type);
854 for (slist = class->construct_properties; slist; slist = slist->next)
856 clist = g_list_prepend (clist, slist->data);
857 n_total_cparams += 1;
860 /* collect parameters, sort into construction and normal ones */
861 oparams = g_new (GObjectConstructParam, n_parameters);
862 cparams = g_new (GObjectConstructParam, n_total_cparams);
863 for (i = 0; i < n_parameters; i++)
865 GValue *value = ¶meters[i].value;
866 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
872 g_warning ("%s: object class `%s' has no property named `%s'",
874 g_type_name (object_type),
878 if (!(pspec->flags & G_PARAM_WRITABLE))
880 g_warning ("%s: property `%s' of object class `%s' is not writable",
883 g_type_name (object_type));
886 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
888 GList *list = g_list_find (clist, pspec);
892 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
893 G_STRFUNC, pspec->name, g_type_name (object_type));
896 cparams[n_cparams].pspec = pspec;
897 cparams[n_cparams].value = value;
902 list->prev->next = list->next;
904 list->next->prev = list->prev;
905 g_list_free_1 (list);
909 oparams[n_oparams].pspec = pspec;
910 oparams[n_oparams].value = value;
915 /* set remaining construction properties to default values */
916 n_cvalues = n_total_cparams - n_cparams;
917 cvalues = g_new (GValue, n_cvalues);
920 GList *tmp = clist->next;
921 GParamSpec *pspec = clist->data;
922 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
925 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
926 g_param_value_set_default (pspec, value);
928 cparams[n_cparams].pspec = pspec;
929 cparams[n_cparams].value = value;
932 g_list_free_1 (clist);
936 /* construct object from construction parameters */
937 object = class->constructor (object_type, n_total_cparams, cparams);
938 /* free construction values */
941 g_value_unset (cvalues + n_cvalues);
944 /* adjust freeze_count according to g_object_init() and remaining properties */
945 G_LOCK (construction_mutex);
946 newly_constructed = slist_maybe_remove (&construction_objects, object);
947 G_UNLOCK (construction_mutex);
948 if (newly_constructed || n_oparams)
949 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
950 if (newly_constructed)
951 g_object_notify_queue_thaw (object, nqueue);
953 /* set remaining properties */
954 for (i = 0; i < n_oparams; i++)
955 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
958 /* release our own freeze count and handle notifications */
959 if (newly_constructed || n_oparams)
960 g_object_notify_queue_thaw (object, nqueue);
962 g_type_class_unref (unref_class);
968 g_object_new_valist (GType object_type,
969 const gchar *first_property_name,
976 guint n_params = 0, n_alloced_params = 16;
978 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
980 if (!first_property_name)
981 return g_object_newv (object_type, 0, NULL);
983 class = g_type_class_ref (object_type);
985 params = g_new (GParameter, n_alloced_params);
986 name = first_property_name;
990 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
996 g_warning ("%s: object class `%s' has no property named `%s'",
998 g_type_name (object_type),
1002 if (n_params >= n_alloced_params)
1004 n_alloced_params += 16;
1005 params = g_renew (GParameter, params, n_alloced_params);
1007 params[n_params].name = name;
1008 params[n_params].value.g_type = 0;
1009 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1010 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1013 g_warning ("%s: %s", G_STRFUNC, error);
1015 g_value_unset (¶ms[n_params].value);
1019 name = va_arg (var_args, gchar*);
1022 object = g_object_newv (object_type, n_params, params);
1025 g_value_unset (¶ms[n_params].value);
1028 g_type_class_unref (class);
1034 g_object_constructor (GType type,
1035 guint n_construct_properties,
1036 GObjectConstructParam *construct_params)
1041 object = (GObject*) g_type_create_instance (type);
1043 /* set construction parameters */
1044 if (n_construct_properties)
1046 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1048 /* set construct properties */
1049 while (n_construct_properties--)
1051 GValue *value = construct_params->value;
1052 GParamSpec *pspec = construct_params->pspec;
1055 object_set_property (object, pspec, value, nqueue);
1057 g_object_notify_queue_thaw (object, nqueue);
1058 /* the notification queue is still frozen from g_object_init(), so
1059 * we don't need to handle it here, g_object_newv() takes
1068 g_object_set_valist (GObject *object,
1069 const gchar *first_property_name,
1072 GObjectNotifyQueue *nqueue;
1075 g_return_if_fail (G_IS_OBJECT (object));
1077 g_object_ref (object);
1078 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1080 name = first_property_name;
1083 GValue value = { 0, };
1085 gchar *error = NULL;
1087 pspec = g_param_spec_pool_lookup (pspec_pool,
1089 G_OBJECT_TYPE (object),
1093 g_warning ("%s: object class `%s' has no property named `%s'",
1095 G_OBJECT_TYPE_NAME (object),
1099 if (!(pspec->flags & G_PARAM_WRITABLE))
1101 g_warning ("%s: property `%s' of object class `%s' is not writable",
1104 G_OBJECT_TYPE_NAME (object));
1107 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1109 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1110 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1114 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1116 G_VALUE_COLLECT (&value, var_args, 0, &error);
1119 g_warning ("%s: %s", G_STRFUNC, error);
1121 g_value_unset (&value);
1125 object_set_property (object, pspec, &value, nqueue);
1126 g_value_unset (&value);
1128 name = va_arg (var_args, gchar*);
1131 g_object_notify_queue_thaw (object, nqueue);
1132 g_object_unref (object);
1136 g_object_get_valist (GObject *object,
1137 const gchar *first_property_name,
1142 g_return_if_fail (G_IS_OBJECT (object));
1144 g_object_ref (object);
1146 name = first_property_name;
1150 GValue value = { 0, };
1154 pspec = g_param_spec_pool_lookup (pspec_pool,
1156 G_OBJECT_TYPE (object),
1160 g_warning ("%s: object class `%s' has no property named `%s'",
1162 G_OBJECT_TYPE_NAME (object),
1166 if (!(pspec->flags & G_PARAM_READABLE))
1168 g_warning ("%s: property `%s' of object class `%s' is not readable",
1171 G_OBJECT_TYPE_NAME (object));
1175 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1177 object_get_property (object, pspec, &value);
1179 G_VALUE_LCOPY (&value, var_args, 0, &error);
1182 g_warning ("%s: %s", G_STRFUNC, error);
1184 g_value_unset (&value);
1188 g_value_unset (&value);
1190 name = va_arg (var_args, gchar*);
1193 g_object_unref (object);
1197 g_object_set (gpointer _object,
1198 const gchar *first_property_name,
1201 GObject *object = _object;
1204 g_return_if_fail (G_IS_OBJECT (object));
1206 va_start (var_args, first_property_name);
1207 g_object_set_valist (object, first_property_name, var_args);
1212 g_object_get (gpointer _object,
1213 const gchar *first_property_name,
1216 GObject *object = _object;
1219 g_return_if_fail (G_IS_OBJECT (object));
1221 va_start (var_args, first_property_name);
1222 g_object_get_valist (object, first_property_name, var_args);
1227 g_object_set_property (GObject *object,
1228 const gchar *property_name,
1229 const GValue *value)
1231 GObjectNotifyQueue *nqueue;
1234 g_return_if_fail (G_IS_OBJECT (object));
1235 g_return_if_fail (property_name != NULL);
1236 g_return_if_fail (G_IS_VALUE (value));
1238 g_object_ref (object);
1239 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1241 pspec = g_param_spec_pool_lookup (pspec_pool,
1243 G_OBJECT_TYPE (object),
1246 g_warning ("%s: object class `%s' has no property named `%s'",
1248 G_OBJECT_TYPE_NAME (object),
1250 else if (!(pspec->flags & G_PARAM_WRITABLE))
1251 g_warning ("%s: property `%s' of object class `%s' is not writable",
1254 G_OBJECT_TYPE_NAME (object));
1255 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1256 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1257 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1259 object_set_property (object, pspec, value, nqueue);
1261 g_object_notify_queue_thaw (object, nqueue);
1262 g_object_unref (object);
1266 g_object_get_property (GObject *object,
1267 const gchar *property_name,
1272 g_return_if_fail (G_IS_OBJECT (object));
1273 g_return_if_fail (property_name != NULL);
1274 g_return_if_fail (G_IS_VALUE (value));
1276 g_object_ref (object);
1278 pspec = g_param_spec_pool_lookup (pspec_pool,
1280 G_OBJECT_TYPE (object),
1283 g_warning ("%s: object class `%s' has no property named `%s'",
1285 G_OBJECT_TYPE_NAME (object),
1287 else if (!(pspec->flags & G_PARAM_READABLE))
1288 g_warning ("%s: property `%s' of object class `%s' is not readable",
1291 G_OBJECT_TYPE_NAME (object));
1294 GValue *prop_value, tmp_value = { 0, };
1296 /* auto-conversion of the callers value type
1298 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1300 g_value_reset (value);
1303 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1305 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1306 G_STRFUNC, pspec->name,
1307 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1308 G_VALUE_TYPE_NAME (value));
1309 g_object_unref (object);
1314 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1315 prop_value = &tmp_value;
1317 object_get_property (object, pspec, prop_value);
1318 if (prop_value != value)
1320 g_value_transform (prop_value, value);
1321 g_value_unset (&tmp_value);
1325 g_object_unref (object);
1329 g_object_connect (gpointer _object,
1330 const gchar *signal_spec,
1333 GObject *object = _object;
1336 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1337 g_return_val_if_fail (object->ref_count > 0, object);
1339 va_start (var_args, signal_spec);
1342 GCallback callback = va_arg (var_args, GCallback);
1343 gpointer data = va_arg (var_args, gpointer);
1346 if (strncmp (signal_spec, "signal::", 8) == 0)
1347 sid = g_signal_connect_data (object, signal_spec + 8,
1348 callback, data, NULL,
1350 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1351 strncmp (signal_spec, "object-signal::", 15) == 0)
1352 sid = g_signal_connect_object (object, signal_spec + 15,
1355 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1356 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1357 sid = g_signal_connect_data (object, signal_spec + 16,
1358 callback, data, NULL,
1360 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1361 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1362 sid = g_signal_connect_object (object, signal_spec + 23,
1365 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1366 strncmp (signal_spec, "signal-after::", 14) == 0)
1367 sid = g_signal_connect_data (object, signal_spec + 14,
1368 callback, data, NULL,
1370 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1371 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1372 sid = g_signal_connect_object (object, signal_spec + 21,
1375 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1376 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1377 sid = g_signal_connect_data (object, signal_spec + 22,
1378 callback, data, NULL,
1379 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1380 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1381 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1382 sid = g_signal_connect_object (object, signal_spec + 29,
1384 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1387 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1390 signal_spec = va_arg (var_args, gchar*);
1398 g_object_disconnect (gpointer _object,
1399 const gchar *signal_spec,
1402 GObject *object = _object;
1405 g_return_if_fail (G_IS_OBJECT (object));
1406 g_return_if_fail (object->ref_count > 0);
1408 va_start (var_args, signal_spec);
1411 GCallback callback = va_arg (var_args, GCallback);
1412 gpointer data = va_arg (var_args, gpointer);
1413 guint sid = 0, detail = 0, mask = 0;
1415 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1416 strncmp (signal_spec, "any-signal::", 12) == 0)
1419 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1421 else if (strcmp (signal_spec, "any_signal") == 0 ||
1422 strcmp (signal_spec, "any-signal") == 0)
1425 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1429 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1433 if ((mask & G_SIGNAL_MATCH_ID) &&
1434 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1435 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1436 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1438 NULL, (gpointer)callback, data))
1439 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1440 signal_spec = va_arg (var_args, gchar*);
1451 } weak_refs[1]; /* flexible array */
1455 weak_refs_notify (gpointer data)
1457 WeakRefStack *wstack = data;
1460 for (i = 0; i < wstack->n_weak_refs; i++)
1461 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1466 g_object_weak_ref (GObject *object,
1470 WeakRefStack *wstack;
1473 g_return_if_fail (G_IS_OBJECT (object));
1474 g_return_if_fail (notify != NULL);
1475 g_return_if_fail (object->ref_count >= 1);
1477 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1480 i = wstack->n_weak_refs++;
1481 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1485 wstack = g_renew (WeakRefStack, NULL, 1);
1486 wstack->object = object;
1487 wstack->n_weak_refs = 1;
1490 wstack->weak_refs[i].notify = notify;
1491 wstack->weak_refs[i].data = data;
1492 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1496 g_object_weak_unref (GObject *object,
1500 WeakRefStack *wstack;
1501 gboolean found_one = FALSE;
1503 g_return_if_fail (G_IS_OBJECT (object));
1504 g_return_if_fail (notify != NULL);
1506 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1511 for (i = 0; i < wstack->n_weak_refs; i++)
1512 if (wstack->weak_refs[i].notify == notify &&
1513 wstack->weak_refs[i].data == data)
1516 wstack->n_weak_refs -= 1;
1517 if (i != wstack->n_weak_refs)
1518 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
1524 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
1528 g_object_add_weak_pointer (GObject *object,
1529 gpointer *weak_pointer_location)
1531 g_return_if_fail (G_IS_OBJECT (object));
1532 g_return_if_fail (weak_pointer_location != NULL);
1534 g_object_weak_ref (object,
1535 (GWeakNotify) g_nullify_pointer,
1536 weak_pointer_location);
1540 g_object_remove_weak_pointer (GObject *object,
1541 gpointer *weak_pointer_location)
1543 g_return_if_fail (G_IS_OBJECT (object));
1544 g_return_if_fail (weak_pointer_location != NULL);
1546 g_object_weak_unref (object,
1547 (GWeakNotify) g_nullify_pointer,
1548 weak_pointer_location);
1552 object_floating_flag_handler (GObject *object,
1558 case +1: /* force floating if possible */
1560 oldvalue = g_atomic_pointer_get (&object->qdata);
1561 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
1562 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
1563 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
1564 case -1: /* sink if possible */
1566 oldvalue = g_atomic_pointer_get (&object->qdata);
1567 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
1568 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
1569 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
1570 default: /* check floating */
1571 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
1576 g_object_is_floating (gpointer _object)
1578 GObject *object = _object;
1579 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
1580 return floating_flag_handler (object, 0);
1584 g_object_ref_sink (gpointer _object)
1586 GObject *object = _object;
1587 gboolean was_floating;
1588 g_return_val_if_fail (G_IS_OBJECT (object), object);
1589 g_return_val_if_fail (object->ref_count >= 1, object);
1590 g_object_ref (object);
1591 was_floating = floating_flag_handler (object, -1);
1593 g_object_unref (object);
1598 g_object_force_floating (GObject *object)
1600 gboolean was_floating;
1601 g_return_if_fail (G_IS_OBJECT (object));
1602 g_return_if_fail (object->ref_count >= 1);
1604 was_floating = floating_flag_handler (object, +1);
1609 guint n_toggle_refs;
1611 GToggleNotify notify;
1613 } toggle_refs[1]; /* flexible array */
1617 toggle_refs_notify (GObject *object,
1618 gboolean is_last_ref)
1620 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
1622 /* Reentrancy here is not as tricky as it seems, because a toggle reference
1623 * will only be notified when there is exactly one of them.
1625 g_assert (tstack->n_toggle_refs == 1);
1626 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
1630 g_object_add_toggle_ref (GObject *object,
1631 GToggleNotify notify,
1634 ToggleRefStack *tstack;
1637 g_return_if_fail (G_IS_OBJECT (object));
1638 g_return_if_fail (notify != NULL);
1639 g_return_if_fail (object->ref_count >= 1);
1641 g_object_ref (object);
1643 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
1646 i = tstack->n_toggle_refs++;
1647 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
1648 * in tstate->toggle_refs */
1649 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
1653 tstack = g_renew (ToggleRefStack, NULL, 1);
1654 tstack->object = object;
1655 tstack->n_toggle_refs = 1;
1659 /* Set a flag for fast lookup after adding the first toggle reference */
1660 if (tstack->n_toggle_refs == 1)
1661 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
1663 tstack->toggle_refs[i].notify = notify;
1664 tstack->toggle_refs[i].data = data;
1665 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
1666 (GDestroyNotify)g_free);
1670 g_object_remove_toggle_ref (GObject *object,
1671 GToggleNotify notify,
1674 ToggleRefStack *tstack;
1675 gboolean found_one = FALSE;
1677 g_return_if_fail (G_IS_OBJECT (object));
1678 g_return_if_fail (notify != NULL);
1680 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
1685 for (i = 0; i < tstack->n_toggle_refs; i++)
1686 if (tstack->toggle_refs[i].notify == notify &&
1687 tstack->toggle_refs[i].data == data)
1690 tstack->n_toggle_refs -= 1;
1691 if (i != tstack->n_toggle_refs)
1692 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
1694 if (tstack->n_toggle_refs == 0)
1695 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
1697 g_object_unref (object);
1704 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
1708 g_object_ref (gpointer _object)
1710 GObject *object = _object;
1713 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1714 g_return_val_if_fail (object->ref_count > 0, NULL);
1716 #ifdef G_ENABLE_DEBUG
1717 if (g_trap_object_ref == object)
1719 #endif /* G_ENABLE_DEBUG */
1722 old_val = g_atomic_int_exchange_and_add (&object->ref_count, 1);
1724 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
1725 toggle_refs_notify (object, FALSE);
1731 g_object_unref (gpointer _object)
1733 GObject *object = _object;
1737 g_return_if_fail (G_IS_OBJECT (object));
1738 g_return_if_fail (object->ref_count > 0);
1740 #ifdef G_ENABLE_DEBUG
1741 if (g_trap_object_ref == object)
1743 #endif /* G_ENABLE_DEBUG */
1745 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
1746 retry_atomic_decrement1:
1747 old_ref = g_atomic_int_get (&object->ref_count);
1750 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
1751 goto retry_atomic_decrement1;
1753 /* if we went from 2->1 we need to notify toggle refs if any */
1754 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
1755 toggle_refs_notify (object, TRUE);
1759 /* we are about tp remove the last reference */
1760 G_OBJECT_GET_CLASS (object)->dispose (object);
1762 /* may have been re-referenced meanwhile */
1763 retry_atomic_decrement2:
1764 old_ref = g_atomic_int_get (&object->ref_count);
1767 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
1768 goto retry_atomic_decrement2;
1770 /* if we went from 2->1 we need to notify toggle refs if any */
1771 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
1772 toggle_refs_notify (object, TRUE);
1777 /* we are still in the process of taking away the last ref */
1778 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1779 g_signal_handlers_destroy (object);
1780 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1782 /* decrement the last reference */
1783 is_zero = g_atomic_int_dec_and_test (&object->ref_count);
1785 /* may have been re-referenced meanwhile */
1786 if (G_LIKELY (is_zero))
1788 G_OBJECT_GET_CLASS (object)->finalize (object);
1789 #ifdef G_ENABLE_DEBUG
1792 /* catch objects not chaining finalize handlers */
1793 G_LOCK (debug_objects);
1794 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
1795 G_UNLOCK (debug_objects);
1797 #endif /* G_ENABLE_DEBUG */
1798 g_type_free_instance ((GTypeInstance*) object);
1804 g_object_get_qdata (GObject *object,
1807 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1809 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1813 g_object_set_qdata (GObject *object,
1817 g_return_if_fail (G_IS_OBJECT (object));
1818 g_return_if_fail (quark > 0);
1820 g_datalist_id_set_data (&object->qdata, quark, data);
1824 g_object_set_qdata_full (GObject *object,
1827 GDestroyNotify destroy)
1829 g_return_if_fail (G_IS_OBJECT (object));
1830 g_return_if_fail (quark > 0);
1832 g_datalist_id_set_data_full (&object->qdata, quark, data,
1833 data ? destroy : (GDestroyNotify) NULL);
1837 g_object_steal_qdata (GObject *object,
1840 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1841 g_return_val_if_fail (quark > 0, NULL);
1843 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1847 g_object_get_data (GObject *object,
1852 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1853 g_return_val_if_fail (key != NULL, NULL);
1855 quark = g_quark_try_string (key);
1857 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1861 g_object_set_data (GObject *object,
1865 g_return_if_fail (G_IS_OBJECT (object));
1866 g_return_if_fail (key != NULL);
1868 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1872 g_object_set_data_full (GObject *object,
1875 GDestroyNotify destroy)
1877 g_return_if_fail (G_IS_OBJECT (object));
1878 g_return_if_fail (key != NULL);
1880 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1881 data ? destroy : (GDestroyNotify) NULL);
1885 g_object_steal_data (GObject *object,
1890 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1891 g_return_val_if_fail (key != NULL, NULL);
1893 quark = g_quark_try_string (key);
1895 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1899 g_value_object_init (GValue *value)
1901 value->data[0].v_pointer = NULL;
1905 g_value_object_free_value (GValue *value)
1907 if (value->data[0].v_pointer)
1908 g_object_unref (value->data[0].v_pointer);
1912 g_value_object_copy_value (const GValue *src_value,
1915 if (src_value->data[0].v_pointer)
1916 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1918 dest_value->data[0].v_pointer = NULL;
1922 g_value_object_transform_value (const GValue *src_value,
1925 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)))
1926 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1928 dest_value->data[0].v_pointer = NULL;
1932 g_value_object_peek_pointer (const GValue *value)
1934 return value->data[0].v_pointer;
1938 g_value_object_collect_value (GValue *value,
1939 guint n_collect_values,
1940 GTypeCValue *collect_values,
1941 guint collect_flags)
1943 if (collect_values[0].v_pointer)
1945 GObject *object = collect_values[0].v_pointer;
1947 if (object->g_type_instance.g_class == NULL)
1948 return g_strconcat ("invalid unclassed object pointer for value type `",
1949 G_VALUE_TYPE_NAME (value),
1952 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1953 return g_strconcat ("invalid object type `",
1954 G_OBJECT_TYPE_NAME (object),
1955 "' for value type `",
1956 G_VALUE_TYPE_NAME (value),
1959 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1960 value->data[0].v_pointer = g_object_ref (object);
1963 value->data[0].v_pointer = NULL;
1969 g_value_object_lcopy_value (const GValue *value,
1970 guint n_collect_values,
1971 GTypeCValue *collect_values,
1972 guint collect_flags)
1974 GObject **object_p = collect_values[0].v_pointer;
1977 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1979 if (!value->data[0].v_pointer)
1981 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1982 *object_p = value->data[0].v_pointer;
1984 *object_p = g_object_ref (value->data[0].v_pointer);
1990 g_value_set_object (GValue *value,
1995 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1997 old = value->data[0].v_pointer;
2001 g_return_if_fail (G_IS_OBJECT (v_object));
2002 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2004 value->data[0].v_pointer = v_object;
2005 g_object_ref (value->data[0].v_pointer);
2008 value->data[0].v_pointer = NULL;
2011 g_object_unref (old);
2015 g_value_set_object_take_ownership (GValue *value,
2018 g_value_take_object (value, v_object);
2022 g_value_take_object (GValue *value,
2025 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2027 if (value->data[0].v_pointer)
2029 g_object_unref (value->data[0].v_pointer);
2030 value->data[0].v_pointer = NULL;
2035 g_return_if_fail (G_IS_OBJECT (v_object));
2036 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2038 value->data[0].v_pointer = v_object; /* we take over the reference count */
2043 g_value_get_object (const GValue *value)
2045 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2047 return value->data[0].v_pointer;
2051 g_value_dup_object (const GValue *value)
2053 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2055 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2059 g_signal_connect_object (gpointer instance,
2060 const gchar *detailed_signal,
2061 GCallback c_handler,
2063 GConnectFlags connect_flags)
2065 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2066 g_return_val_if_fail (detailed_signal != NULL, 0);
2067 g_return_val_if_fail (c_handler != NULL, 0);
2073 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2075 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2077 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2080 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
2086 GClosure *closures[1]; /* flexible array */
2088 /* don't change this structure without supplying an accessor for
2089 * watched closures, e.g.:
2090 * GSList* g_object_list_watched_closures (GObject *object)
2093 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2094 * carray = g_object_get_data (object, "GObject-closure-array");
2097 * GSList *slist = NULL;
2099 * for (i = 0; i < carray->n_closures; i++)
2100 * slist = g_slist_prepend (slist, carray->closures[i]);
2108 object_remove_closure (gpointer data,
2111 GObject *object = data;
2112 CArray *carray = g_object_get_qdata (object, quark_closure_array);
2115 for (i = 0; i < carray->n_closures; i++)
2116 if (carray->closures[i] == closure)
2118 carray->n_closures--;
2119 if (i < carray->n_closures)
2120 carray->closures[i] = carray->closures[carray->n_closures];
2123 g_assert_not_reached ();
2127 destroy_closure_array (gpointer data)
2129 CArray *carray = data;
2130 GObject *object = carray->object;
2131 guint i, n = carray->n_closures;
2133 for (i = 0; i < n; i++)
2135 GClosure *closure = carray->closures[i];
2137 /* removing object_remove_closure() upfront is probably faster than
2138 * letting it fiddle with quark_closure_array which is empty anyways
2140 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
2141 g_closure_invalidate (closure);
2147 g_object_watch_closure (GObject *object,
2153 g_return_if_fail (G_IS_OBJECT (object));
2154 g_return_if_fail (closure != NULL);
2155 g_return_if_fail (closure->is_invalid == FALSE);
2156 g_return_if_fail (closure->in_marshal == FALSE);
2157 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
2159 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
2160 g_closure_add_marshal_guards (closure,
2161 object, (GClosureNotify) g_object_ref,
2162 object, (GClosureNotify) g_object_unref);
2163 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
2166 carray = g_renew (CArray, NULL, 1);
2167 carray->object = object;
2168 carray->n_closures = 1;
2173 i = carray->n_closures++;
2174 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
2176 carray->closures[i] = closure;
2177 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
2181 g_closure_new_object (guint sizeof_closure,
2186 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2187 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2189 closure = g_closure_new_simple (sizeof_closure, object);
2190 g_object_watch_closure (object, closure);
2196 g_cclosure_new_object (GCallback callback_func,
2201 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2202 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2203 g_return_val_if_fail (callback_func != NULL, NULL);
2205 closure = g_cclosure_new (callback_func, object, NULL);
2206 g_object_watch_closure (object, closure);
2212 g_cclosure_new_object_swap (GCallback callback_func,
2217 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2218 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2219 g_return_val_if_fail (callback_func != NULL, NULL);
2221 closure = g_cclosure_new_swap (callback_func, object, NULL);
2222 g_object_watch_closure (object, closure);
2228 g_object_compat_control (gsize what,
2234 case 1: /* floating base type */
2235 return G_TYPE_INITIALLY_UNOWNED;
2236 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
2237 floating_flag_handler = (guint(*)(GObject*,gint)) data;
2239 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
2241 *pp = floating_flag_handler;
2248 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
2251 g_initially_unowned_init (GInitiallyUnowned *object)
2253 g_object_force_floating (object);
2257 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
2261 #define __G_OBJECT_C__
2262 #include "gobjectaliasdef.c"