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"
26 #include "gvaluecollector.h"
28 #include "gparamspecs.h"
29 #include "gvaluetypes.h"
30 #include "gobjectnotifyqueue.c"
35 #define PREALLOC_CPARAMS (8)
39 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
40 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
50 /* --- properties --- */
56 /* --- prototypes --- */
57 static void g_object_base_class_init (GObjectClass *class);
58 static void g_object_base_class_finalize (GObjectClass *class);
59 static void g_object_do_class_init (GObjectClass *class);
60 static void g_object_init (GObject *object);
61 static GObject* g_object_constructor (GType type,
62 guint n_construct_properties,
63 GObjectConstructParam *construct_params);
64 static void g_object_last_unref (GObject *object);
65 static void g_object_real_dispose (GObject *object);
66 static void g_object_finalize (GObject *object);
67 static void g_object_do_set_property (GObject *object,
71 static void g_object_do_get_property (GObject *object,
75 static void g_value_object_init (GValue *value);
76 static void g_value_object_free_value (GValue *value);
77 static void g_value_object_copy_value (const GValue *src_value,
79 static void g_value_object_transform_value (const GValue *src_value,
81 static gpointer g_value_object_peek_pointer (const GValue *value);
82 static gchar* g_value_object_collect_value (GValue *value,
83 guint n_collect_values,
84 GTypeCValue *collect_values,
86 static gchar* g_value_object_lcopy_value (const GValue *value,
87 guint n_collect_values,
88 GTypeCValue *collect_values,
90 static void g_object_dispatch_properties_changed (GObject *object,
93 static inline void object_get_property (GObject *object,
96 static inline void object_set_property (GObject *object,
99 GObjectNotifyQueue *nqueue);
101 static void object_interface_check_properties (gpointer func_data,
105 /* --- variables --- */
106 static GQuark quark_closure_array = 0;
107 static GQuark quark_weak_refs = 0;
108 static GParamSpecPool *pspec_pool = NULL;
109 static GObjectNotifyContext property_notify_context = { 0, };
110 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
111 G_LOCK_DEFINE_STATIC (construct_objects_lock);
112 static GSList *construct_objects = NULL;
115 /* --- functions --- */
116 #ifdef G_ENABLE_DEBUG
117 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
118 G_LOCK_DEFINE_STATIC (debug_objects);
119 static volatile GObject *g_trap_object_ref = NULL;
120 static guint debug_objects_count = 0;
121 static GHashTable *debug_objects_ht = NULL;
123 debug_objects_foreach (gpointer key,
127 GObject *object = value;
129 g_message ("[%p] stale %s\tref_count=%u",
131 G_OBJECT_TYPE_NAME (object),
135 debug_objects_atexit (void)
139 G_LOCK (debug_objects);
140 g_message ("stale GObjects: %u", debug_objects_count);
141 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
142 G_UNLOCK (debug_objects);
145 #endif /* G_ENABLE_DEBUG */
148 g_object_type_init (void)
150 static gboolean initialized = FALSE;
151 static const GTypeFundamentalInfo finfo = {
152 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
154 static GTypeInfo info = {
155 sizeof (GObjectClass),
156 (GBaseInitFunc) g_object_base_class_init,
157 (GBaseFinalizeFunc) g_object_base_class_finalize,
158 (GClassInitFunc) g_object_do_class_init,
159 NULL /* class_destroy */,
160 NULL /* class_data */,
163 (GInstanceInitFunc) g_object_init,
164 NULL, /* value_table */
166 static const GTypeValueTable value_table = {
167 g_value_object_init, /* value_init */
168 g_value_object_free_value, /* value_free */
169 g_value_object_copy_value, /* value_copy */
170 g_value_object_peek_pointer, /* value_peek_pointer */
171 "p", /* collect_format */
172 g_value_object_collect_value, /* collect_value */
173 "p", /* lcopy_format */
174 g_value_object_lcopy_value, /* lcopy_value */
178 g_return_if_fail (initialized == FALSE);
183 info.value_table = &value_table;
184 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo, 0);
185 g_assert (type == G_TYPE_OBJECT);
186 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
188 #ifdef G_ENABLE_DEBUG
191 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
192 g_atexit (debug_objects_atexit);
194 #endif /* G_ENABLE_DEBUG */
198 g_object_base_class_init (GObjectClass *class)
200 GObjectClass *pclass = g_type_class_peek_parent (class);
202 /* reset instance specific fields and methods that don't get inherited */
203 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
204 class->get_property = NULL;
205 class->set_property = NULL;
209 g_object_base_class_finalize (GObjectClass *class)
213 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
215 g_slist_free (class->construct_properties);
216 class->construct_properties = NULL;
217 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
218 for (node = list; node; node = node->next)
220 GParamSpec *pspec = node->data;
222 g_param_spec_pool_remove (pspec_pool, pspec);
223 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
224 g_param_spec_unref (pspec);
230 g_object_notify_dispatcher (GObject *object,
234 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
238 g_object_do_class_init (GObjectClass *class)
240 /* read the comment about typedef struct CArray; on why not to change this quark */
241 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
243 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
244 pspec_pool = g_param_spec_pool_new (TRUE);
245 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
246 property_notify_context.dispatcher = g_object_notify_dispatcher;
248 class->constructor = g_object_constructor;
249 class->set_property = g_object_do_set_property;
250 class->get_property = g_object_do_get_property;
251 class->dispose = g_object_real_dispose;
252 class->finalize = g_object_finalize;
253 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
254 class->notify = NULL;
256 gobject_signals[NOTIFY] =
257 g_signal_new ("notify",
258 G_TYPE_FROM_CLASS (class),
259 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
260 G_STRUCT_OFFSET (GObjectClass, notify),
262 g_cclosure_marshal_VOID__PARAM,
266 /* Install a check function that we'll use to verify that classes that
267 * implement an interface implement all properties for that interface
269 g_type_add_interface_check (NULL, object_interface_check_properties);
273 install_property_internal (GType g_type,
277 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
279 g_warning ("When installing property: type `%s' already has a property named `%s'",
280 g_type_name (g_type),
285 g_param_spec_ref (pspec);
286 g_param_spec_sink (pspec);
287 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
288 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
292 g_object_class_install_property (GObjectClass *class,
296 g_return_if_fail (G_IS_OBJECT_CLASS (class));
297 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
298 if (pspec->flags & G_PARAM_WRITABLE)
299 g_return_if_fail (class->set_property != NULL);
300 if (pspec->flags & G_PARAM_READABLE)
301 g_return_if_fail (class->get_property != NULL);
302 g_return_if_fail (property_id > 0);
303 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
304 if (pspec->flags & G_PARAM_CONSTRUCT)
305 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
306 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
307 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
309 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
311 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
312 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
314 /* for property overrides of construct poperties, we have to get rid
315 * of the overidden inherited construct property
317 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
318 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
319 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
323 g_object_interface_install_property (gpointer g_iface,
326 GTypeInterface *iface_class = g_iface;
328 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
329 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
330 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
331 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
333 install_property_internal (iface_class->g_type, 0, pspec);
337 g_object_class_find_property (GObjectClass *class,
338 const gchar *property_name)
341 GParamSpec *redirect;
343 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
344 g_return_val_if_fail (property_name != NULL, NULL);
346 pspec = g_param_spec_pool_lookup (pspec_pool,
348 G_OBJECT_CLASS_TYPE (class),
352 redirect = g_param_spec_get_redirect_target (pspec);
363 g_object_interface_find_property (gpointer g_iface,
364 const gchar *property_name)
366 GTypeInterface *iface_class = g_iface;
368 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
369 g_return_val_if_fail (property_name != NULL, NULL);
371 return g_param_spec_pool_lookup (pspec_pool,
378 g_object_class_override_property (GObjectClass *oclass,
382 GParamSpec *overridden = NULL;
386 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
387 g_return_if_fail (property_id > 0);
388 g_return_if_fail (name != NULL);
390 /* Find the overridden property; first check parent types
392 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
393 if (parent_type != G_TYPE_NONE)
394 overridden = g_param_spec_pool_lookup (pspec_pool,
403 /* Now check interfaces
405 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
406 while (n_ifaces-- && !overridden)
408 overridden = g_param_spec_pool_lookup (pspec_pool,
419 g_warning ("%s: Can't find property to override for '%s::%s'",
420 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
424 new = g_param_spec_override (name, overridden);
425 g_object_class_install_property (oclass, property_id, new);
428 GParamSpec** /* free result */
429 g_object_class_list_properties (GObjectClass *class,
430 guint *n_properties_p)
435 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
437 pspecs = g_param_spec_pool_list (pspec_pool,
438 G_OBJECT_CLASS_TYPE (class),
446 GParamSpec** /* free result */
447 g_object_interface_list_properties (gpointer g_iface,
448 guint *n_properties_p)
450 GTypeInterface *iface_class = g_iface;
454 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
456 pspecs = g_param_spec_pool_list (pspec_pool,
466 g_object_init (GObject *object)
468 object->ref_count = 1;
469 g_datalist_init (&object->qdata);
471 /* freeze object's notification queue, g_object_newv() preserves pairedness */
472 g_object_notify_queue_freeze (object, &property_notify_context);
474 /* allow construct-only properties to be set */
475 G_LOCK (construct_objects_lock);
476 construct_objects = g_slist_prepend (construct_objects, object);
477 G_UNLOCK (construct_objects_lock);
479 #ifdef G_ENABLE_DEBUG
482 G_LOCK (debug_objects);
483 debug_objects_count++;
484 g_hash_table_insert (debug_objects_ht, object, object);
485 G_UNLOCK (debug_objects);
487 #endif /* G_ENABLE_DEBUG */
491 g_object_do_set_property (GObject *object,
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
505 g_object_do_get_property (GObject *object,
513 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
519 g_object_real_dispose (GObject *object)
523 g_signal_handlers_destroy (object);
524 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
526 /* yes, temporarily altering the ref_count is hackish, but that
527 * enforces people not jerking around with weak_ref notifiers
529 ref_count = object->ref_count;
530 object->ref_count = 0;
531 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
532 object->ref_count = ref_count;
536 g_object_finalize (GObject *object)
538 g_datalist_clear (&object->qdata);
540 #ifdef G_ENABLE_DEBUG
543 G_LOCK (debug_objects);
544 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
545 g_hash_table_remove (debug_objects_ht, object);
546 debug_objects_count--;
547 G_UNLOCK (debug_objects);
549 #endif /* G_ENABLE_DEBUG */
553 g_object_last_unref (GObject *object)
555 g_return_if_fail (object->ref_count > 0);
557 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
558 G_OBJECT_GET_CLASS (object)->dispose (object);
560 #ifdef G_ENABLE_DEBUG
561 if (g_trap_object_ref == object)
563 #endif /* G_ENABLE_DEBUG */
565 object->ref_count -= 1;
567 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
569 g_signal_handlers_destroy (object);
570 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
571 G_OBJECT_GET_CLASS (object)->finalize (object);
572 #ifdef G_ENABLE_DEBUG
575 /* catch objects not chaining finalize handlers */
576 G_LOCK (debug_objects);
577 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
578 G_UNLOCK (debug_objects);
580 #endif /* G_ENABLE_DEBUG */
581 g_type_free_instance ((GTypeInstance*) object);
586 g_object_dispatch_properties_changed (GObject *object,
592 for (i = 0; i < n_pspecs; i++)
593 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
597 g_object_run_dispose (GObject *object)
599 g_return_if_fail (G_IS_OBJECT (object));
600 g_return_if_fail (object->ref_count > 0);
602 g_object_ref (object);
603 G_OBJECT_GET_CLASS (object)->dispose (object);
604 g_object_unref (object);
608 g_object_freeze_notify (GObject *object)
610 g_return_if_fail (G_IS_OBJECT (object));
611 if (!object->ref_count)
614 g_object_ref (object);
615 g_object_notify_queue_freeze (object, &property_notify_context);
616 g_object_unref (object);
620 g_object_notify (GObject *object,
621 const gchar *property_name)
625 g_return_if_fail (G_IS_OBJECT (object));
626 g_return_if_fail (property_name != NULL);
627 if (!object->ref_count)
630 g_object_ref (object);
631 /* We don't need to get the redirect target
632 * (by, e.g. calling g_object_class_find_property())
633 * because g_object_notify_queue_add() does that
635 pspec = g_param_spec_pool_lookup (pspec_pool,
637 G_OBJECT_TYPE (object),
641 g_warning ("%s: object class `%s' has no property named `%s'",
643 G_OBJECT_TYPE_NAME (object),
647 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
649 g_object_notify_queue_add (object, nqueue, pspec);
650 g_object_notify_queue_thaw (object, nqueue);
652 g_object_unref (object);
656 g_object_thaw_notify (GObject *object)
658 GObjectNotifyQueue *nqueue;
660 g_return_if_fail (G_IS_OBJECT (object));
661 if (!object->ref_count)
664 g_object_ref (object);
665 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
666 if (!nqueue || !nqueue->freeze_count)
667 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
668 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
670 g_object_notify_queue_thaw (object, nqueue);
671 g_object_unref (object);
675 object_get_property (GObject *object,
679 GObjectClass *class = g_type_class_peek (pspec->owner_type);
680 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
681 GParamSpec *redirect;
683 redirect = g_param_spec_get_redirect_target (pspec);
687 class->get_property (object, param_id, value, pspec);
691 object_set_property (GObject *object,
694 GObjectNotifyQueue *nqueue)
696 GValue tmp_value = { 0, };
697 GObjectClass *class = g_type_class_peek (pspec->owner_type);
698 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
699 GParamSpec *redirect;
701 redirect = g_param_spec_get_redirect_target (pspec);
705 /* provide a copy to work from, convert (if necessary) and validate */
706 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
707 if (!g_value_transform (value, &tmp_value))
708 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
710 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
711 G_VALUE_TYPE_NAME (value));
712 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
714 gchar *contents = g_strdup_value_contents (value);
716 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
718 G_VALUE_TYPE_NAME (value),
720 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
725 class->set_property (object, param_id, &tmp_value, pspec);
726 g_object_notify_queue_add (object, nqueue, pspec);
728 g_value_unset (&tmp_value);
732 object_interface_check_properties (gpointer func_data,
735 GTypeInterface *iface_class = g_iface;
736 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
737 GType iface_type = iface_class->g_type;
741 if (!G_IS_OBJECT_CLASS (class))
744 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
748 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
750 G_OBJECT_CLASS_TYPE (class),
755 g_critical ("Object class %s doesn't implement property "
756 "'%s' from interface '%s'",
757 g_type_name (G_OBJECT_CLASS_TYPE (class)),
759 g_type_name (iface_type));
764 /* The implementation paramspec must have a less restrictive
765 * type than the interface parameter spec for set() and a
766 * more restrictive type for get(). We just require equality,
767 * rather than doing something more complicated checking
768 * the READABLE and WRITABLE flags. We also simplify here
769 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
772 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
773 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
775 g_critical ("Property '%s' on class '%s' has type '%s' "
776 "which is different from the type '%s', "
777 "of the property on interface '%s'\n",
779 g_type_name (G_OBJECT_CLASS_TYPE (class)),
780 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
781 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
782 g_type_name (iface_type));
785 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
787 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
788 * READABLE and WRITABLE remove restrictions. The implementation
789 * paramspec must have less restrictive flags.
792 (!SUBSET (class_pspec->flags,
794 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
795 !SUBSET (pspecs[n]->flags,
797 G_PARAM_READABLE | G_PARAM_WRITABLE)))
799 g_critical ("Flags for property '%s' on class '%s' "
800 "are not compatible with the property on"
803 g_type_name (G_OBJECT_CLASS_TYPE (class)),
804 g_type_name (iface_type));
813 g_object_new (GType object_type,
814 const gchar *first_property_name,
820 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
822 va_start (var_args, first_property_name);
823 object = g_object_new_valist (object_type, first_property_name, var_args);
830 object_in_construction (GObject *object)
832 gboolean in_construction;
833 G_LOCK (construct_objects_lock);
834 in_construction = g_slist_find (construct_objects, object) != NULL;
835 G_UNLOCK (construct_objects_lock);
836 return in_construction;
840 g_object_newv (GType object_type,
842 GParameter *parameters)
844 GObjectConstructParam *cparams, *oparams;
845 GObjectNotifyQueue *nqueue;
847 GObjectClass *class, *unref_class = NULL;
849 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
854 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
856 class = g_type_class_peek_static (object_type);
858 class = unref_class = g_type_class_ref (object_type);
859 for (slist = class->construct_properties; slist; slist = slist->next)
861 clist = g_list_prepend (clist, slist->data);
862 n_total_cparams += 1;
865 /* collect parameters, sort into construction and normal ones */
866 oparams = g_new (GObjectConstructParam, n_parameters);
867 cparams = g_new (GObjectConstructParam, n_total_cparams);
868 for (i = 0; i < n_parameters; i++)
870 GValue *value = ¶meters[i].value;
871 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
877 g_warning ("%s: object class `%s' has no property named `%s'",
879 g_type_name (object_type),
883 if (!(pspec->flags & G_PARAM_WRITABLE))
885 g_warning ("%s: property `%s' of object class `%s' is not writable",
888 g_type_name (object_type));
891 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
893 GList *list = g_list_find (clist, pspec);
897 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
898 G_STRFUNC, pspec->name, g_type_name (object_type));
901 cparams[n_cparams].pspec = pspec;
902 cparams[n_cparams].value = value;
907 list->prev->next = list->next;
909 list->next->prev = list->prev;
910 g_list_free_1 (list);
914 oparams[n_oparams].pspec = pspec;
915 oparams[n_oparams].value = value;
920 /* set remaining construction properties to default values */
921 n_cvalues = n_total_cparams - n_cparams;
922 cvalues = g_new (GValue, n_cvalues);
925 GList *tmp = clist->next;
926 GParamSpec *pspec = clist->data;
927 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
930 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
931 g_param_value_set_default (pspec, value);
933 cparams[n_cparams].pspec = pspec;
934 cparams[n_cparams].value = value;
937 g_list_free_1 (clist);
941 /* construct object from construction parameters */
942 object = class->constructor (object_type, n_total_cparams, cparams);
943 G_LOCK (construct_objects_lock);
944 construct_objects = g_slist_remove (construct_objects, object);
945 G_UNLOCK (construct_objects_lock);
947 /* free construction values */
950 g_value_unset (cvalues + n_cvalues);
953 /* release g_object_init() notification queue freeze_count */
954 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
955 g_object_notify_queue_thaw (object, nqueue);
957 /* set remaining properties */
958 for (i = 0; i < n_oparams; i++)
959 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
963 g_type_class_unref (unref_class);
965 /* release our own freeze count and handle notifications */
966 g_object_notify_queue_thaw (object, nqueue);
972 g_object_new_valist (GType object_type,
973 const gchar *first_property_name,
980 guint n_params = 0, n_alloced_params = 16;
982 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
984 if (!first_property_name)
985 return g_object_newv (object_type, 0, NULL);
987 class = g_type_class_ref (object_type);
989 params = g_new (GParameter, n_alloced_params);
990 name = first_property_name;
994 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1000 g_warning ("%s: object class `%s' has no property named `%s'",
1002 g_type_name (object_type),
1006 if (n_params >= n_alloced_params)
1008 n_alloced_params += 16;
1009 params = g_renew (GParameter, params, n_alloced_params);
1011 params[n_params].name = name;
1012 params[n_params].value.g_type = 0;
1013 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1014 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1017 g_warning ("%s: %s", G_STRFUNC, error);
1019 g_value_unset (¶ms[n_params].value);
1023 name = va_arg (var_args, gchar*);
1026 object = g_object_newv (object_type, n_params, params);
1029 g_value_unset (¶ms[n_params].value);
1032 g_type_class_unref (class);
1038 g_object_constructor (GType type,
1039 guint n_construct_properties,
1040 GObjectConstructParam *construct_params)
1045 object = (GObject*) g_type_create_instance (type);
1047 /* set construction parameters */
1048 if (n_construct_properties)
1050 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1052 /* set construct properties */
1053 while (n_construct_properties--)
1055 GValue *value = construct_params->value;
1056 GParamSpec *pspec = construct_params->pspec;
1059 object_set_property (object, pspec, value, nqueue);
1061 g_object_notify_queue_thaw (object, nqueue);
1062 /* the notification queue is still frozen from g_object_init(), so
1063 * we don't need to handle it here, g_object_newv() takes
1072 g_object_set_valist (GObject *object,
1073 const gchar *first_property_name,
1076 GObjectNotifyQueue *nqueue;
1079 g_return_if_fail (G_IS_OBJECT (object));
1081 g_object_ref (object);
1082 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1084 name = first_property_name;
1087 GValue value = { 0, };
1089 gchar *error = NULL;
1091 pspec = g_param_spec_pool_lookup (pspec_pool,
1093 G_OBJECT_TYPE (object),
1097 g_warning ("%s: object class `%s' has no property named `%s'",
1099 G_OBJECT_TYPE_NAME (object),
1103 if (!(pspec->flags & G_PARAM_WRITABLE))
1105 g_warning ("%s: property `%s' of object class `%s' is not writable",
1108 G_OBJECT_TYPE_NAME (object));
1111 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1113 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1114 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1118 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1120 G_VALUE_COLLECT (&value, var_args, 0, &error);
1123 g_warning ("%s: %s", G_STRFUNC, error);
1125 g_value_unset (&value);
1129 object_set_property (object, pspec, &value, nqueue);
1130 g_value_unset (&value);
1132 name = va_arg (var_args, gchar*);
1135 g_object_notify_queue_thaw (object, nqueue);
1136 g_object_unref (object);
1140 g_object_get_valist (GObject *object,
1141 const gchar *first_property_name,
1146 g_return_if_fail (G_IS_OBJECT (object));
1148 g_object_ref (object);
1150 name = first_property_name;
1154 GValue value = { 0, };
1158 pspec = g_param_spec_pool_lookup (pspec_pool,
1160 G_OBJECT_TYPE (object),
1164 g_warning ("%s: object class `%s' has no property named `%s'",
1166 G_OBJECT_TYPE_NAME (object),
1170 if (!(pspec->flags & G_PARAM_READABLE))
1172 g_warning ("%s: property `%s' of object class `%s' is not readable",
1175 G_OBJECT_TYPE_NAME (object));
1179 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1181 object_get_property (object, pspec, &value);
1183 G_VALUE_LCOPY (&value, var_args, 0, &error);
1186 g_warning ("%s: %s", G_STRFUNC, error);
1188 g_value_unset (&value);
1192 g_value_unset (&value);
1194 name = va_arg (var_args, gchar*);
1197 g_object_unref (object);
1201 g_object_set (gpointer _object,
1202 const gchar *first_property_name,
1205 GObject *object = _object;
1208 g_return_if_fail (G_IS_OBJECT (object));
1210 va_start (var_args, first_property_name);
1211 g_object_set_valist (object, first_property_name, var_args);
1216 g_object_get (gpointer _object,
1217 const gchar *first_property_name,
1220 GObject *object = _object;
1223 g_return_if_fail (G_IS_OBJECT (object));
1225 va_start (var_args, first_property_name);
1226 g_object_get_valist (object, first_property_name, var_args);
1231 g_object_set_property (GObject *object,
1232 const gchar *property_name,
1233 const GValue *value)
1235 GObjectNotifyQueue *nqueue;
1238 g_return_if_fail (G_IS_OBJECT (object));
1239 g_return_if_fail (property_name != NULL);
1240 g_return_if_fail (G_IS_VALUE (value));
1242 g_object_ref (object);
1243 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1245 pspec = g_param_spec_pool_lookup (pspec_pool,
1247 G_OBJECT_TYPE (object),
1250 g_warning ("%s: object class `%s' has no property named `%s'",
1252 G_OBJECT_TYPE_NAME (object),
1254 else if (!(pspec->flags & G_PARAM_WRITABLE))
1255 g_warning ("%s: property `%s' of object class `%s' is not writable",
1258 G_OBJECT_TYPE_NAME (object));
1259 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
1260 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1261 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1263 object_set_property (object, pspec, value, nqueue);
1265 g_object_notify_queue_thaw (object, nqueue);
1266 g_object_unref (object);
1270 g_object_get_property (GObject *object,
1271 const gchar *property_name,
1276 g_return_if_fail (G_IS_OBJECT (object));
1277 g_return_if_fail (property_name != NULL);
1278 g_return_if_fail (G_IS_VALUE (value));
1280 g_object_ref (object);
1282 pspec = g_param_spec_pool_lookup (pspec_pool,
1284 G_OBJECT_TYPE (object),
1287 g_warning ("%s: object class `%s' has no property named `%s'",
1289 G_OBJECT_TYPE_NAME (object),
1291 else if (!(pspec->flags & G_PARAM_READABLE))
1292 g_warning ("%s: property `%s' of object class `%s' is not readable",
1295 G_OBJECT_TYPE_NAME (object));
1298 GValue *prop_value, tmp_value = { 0, };
1300 /* auto-conversion of the callers value type
1302 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1304 g_value_reset (value);
1307 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1309 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1310 G_STRFUNC, pspec->name,
1311 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1312 G_VALUE_TYPE_NAME (value));
1313 g_object_unref (object);
1318 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1319 prop_value = &tmp_value;
1321 object_get_property (object, pspec, prop_value);
1322 if (prop_value != value)
1324 g_value_transform (prop_value, value);
1325 g_value_unset (&tmp_value);
1329 g_object_unref (object);
1333 g_object_connect (gpointer _object,
1334 const gchar *signal_spec,
1337 GObject *object = _object;
1340 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1341 g_return_val_if_fail (object->ref_count > 0, object);
1343 va_start (var_args, signal_spec);
1346 GCallback callback = va_arg (var_args, GCallback);
1347 gpointer data = va_arg (var_args, gpointer);
1350 if (strncmp (signal_spec, "signal::", 8) == 0)
1351 sid = g_signal_connect_data (object, signal_spec + 8,
1352 callback, data, NULL,
1354 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1355 strncmp (signal_spec, "object-signal::", 15) == 0)
1356 sid = g_signal_connect_object (object, signal_spec + 15,
1359 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1360 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1361 sid = g_signal_connect_data (object, signal_spec + 16,
1362 callback, data, NULL,
1364 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1365 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1366 sid = g_signal_connect_object (object, signal_spec + 23,
1369 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1370 strncmp (signal_spec, "signal-after::", 14) == 0)
1371 sid = g_signal_connect_data (object, signal_spec + 14,
1372 callback, data, NULL,
1374 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1375 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1376 sid = g_signal_connect_object (object, signal_spec + 21,
1379 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1380 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1381 sid = g_signal_connect_data (object, signal_spec + 22,
1382 callback, data, NULL,
1383 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1384 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1385 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1386 sid = g_signal_connect_object (object, signal_spec + 29,
1388 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1391 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1394 signal_spec = va_arg (var_args, gchar*);
1402 g_object_disconnect (gpointer _object,
1403 const gchar *signal_spec,
1406 GObject *object = _object;
1409 g_return_if_fail (G_IS_OBJECT (object));
1410 g_return_if_fail (object->ref_count > 0);
1412 va_start (var_args, signal_spec);
1415 GCallback callback = va_arg (var_args, GCallback);
1416 gpointer data = va_arg (var_args, gpointer);
1417 guint sid = 0, detail = 0, mask = 0;
1419 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1420 strncmp (signal_spec, "any-signal::", 12) == 0)
1423 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1425 else if (strcmp (signal_spec, "any_signal") == 0 ||
1426 strcmp (signal_spec, "any-signal") == 0)
1429 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1433 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1437 if ((mask & G_SIGNAL_MATCH_ID) &&
1438 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1439 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1440 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1442 NULL, (gpointer)callback, data))
1443 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1444 signal_spec = va_arg (var_args, gchar*);
1455 } weak_refs[1]; /* flexible array */
1459 weak_refs_notify (gpointer data)
1461 WeakRefStack *wstack = data;
1464 for (i = 0; i < wstack->n_weak_refs; i++)
1465 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1470 g_object_weak_ref (GObject *object,
1474 WeakRefStack *wstack;
1477 g_return_if_fail (G_IS_OBJECT (object));
1478 g_return_if_fail (notify != NULL);
1479 g_return_if_fail (object->ref_count >= 1);
1481 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1484 i = wstack->n_weak_refs++;
1485 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1489 wstack = g_renew (WeakRefStack, NULL, 1);
1490 wstack->object = object;
1491 wstack->n_weak_refs = 1;
1494 wstack->weak_refs[i].notify = notify;
1495 wstack->weak_refs[i].data = data;
1496 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1500 g_object_weak_unref (GObject *object,
1504 WeakRefStack *wstack;
1505 gboolean found_one = FALSE;
1507 g_return_if_fail (G_IS_OBJECT (object));
1508 g_return_if_fail (notify != NULL);
1510 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1515 for (i = 0; i < wstack->n_weak_refs; i++)
1516 if (wstack->weak_refs[i].notify == notify &&
1517 wstack->weak_refs[i].data == data)
1520 wstack->n_weak_refs -= 1;
1521 if (i != wstack->n_weak_refs)
1523 wstack->weak_refs[i].notify = wstack->weak_refs[wstack->n_weak_refs].notify;
1524 wstack->weak_refs[i].data = wstack->weak_refs[wstack->n_weak_refs].data;
1530 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
1534 g_object_add_weak_pointer (GObject *object,
1535 gpointer *weak_pointer_location)
1537 g_return_if_fail (G_IS_OBJECT (object));
1538 g_return_if_fail (weak_pointer_location != NULL);
1540 g_object_weak_ref (object,
1541 (GWeakNotify) g_nullify_pointer,
1542 weak_pointer_location);
1546 g_object_remove_weak_pointer (GObject *object,
1547 gpointer *weak_pointer_location)
1549 g_return_if_fail (G_IS_OBJECT (object));
1550 g_return_if_fail (weak_pointer_location != NULL);
1552 g_object_weak_unref (object,
1553 (GWeakNotify) g_nullify_pointer,
1554 weak_pointer_location);
1558 g_object_ref (gpointer _object)
1560 GObject *object = _object;
1562 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1563 g_return_val_if_fail (object->ref_count > 0, NULL);
1565 #ifdef G_ENABLE_DEBUG
1566 if (g_trap_object_ref == object)
1568 #endif /* G_ENABLE_DEBUG */
1570 object->ref_count += 1;
1576 g_object_unref (gpointer _object)
1578 GObject *object = _object;
1580 g_return_if_fail (G_IS_OBJECT (object));
1581 g_return_if_fail (object->ref_count > 0);
1583 #ifdef G_ENABLE_DEBUG
1584 if (g_trap_object_ref == object)
1586 #endif /* G_ENABLE_DEBUG */
1588 if (object->ref_count > 1)
1589 object->ref_count -= 1;
1591 g_object_last_unref (object);
1595 g_object_get_qdata (GObject *object,
1598 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1600 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1604 g_object_set_qdata (GObject *object,
1608 g_return_if_fail (G_IS_OBJECT (object));
1609 g_return_if_fail (quark > 0);
1611 g_datalist_id_set_data (&object->qdata, quark, data);
1615 g_object_set_qdata_full (GObject *object,
1618 GDestroyNotify destroy)
1620 g_return_if_fail (G_IS_OBJECT (object));
1621 g_return_if_fail (quark > 0);
1623 g_datalist_id_set_data_full (&object->qdata, quark, data,
1624 data ? destroy : (GDestroyNotify) NULL);
1628 g_object_steal_qdata (GObject *object,
1631 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1632 g_return_val_if_fail (quark > 0, NULL);
1634 return g_datalist_id_remove_no_notify (&object->qdata, quark);
1638 g_object_get_data (GObject *object,
1643 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1644 g_return_val_if_fail (key != NULL, NULL);
1646 quark = g_quark_try_string (key);
1648 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
1652 g_object_set_data (GObject *object,
1656 g_return_if_fail (G_IS_OBJECT (object));
1657 g_return_if_fail (key != NULL);
1659 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
1663 g_object_set_data_full (GObject *object,
1666 GDestroyNotify destroy)
1668 g_return_if_fail (G_IS_OBJECT (object));
1669 g_return_if_fail (key != NULL);
1671 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
1672 data ? destroy : (GDestroyNotify) NULL);
1676 g_object_steal_data (GObject *object,
1681 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1682 g_return_val_if_fail (key != NULL, NULL);
1684 quark = g_quark_try_string (key);
1686 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
1690 g_value_object_init (GValue *value)
1692 value->data[0].v_pointer = NULL;
1696 g_value_object_free_value (GValue *value)
1698 if (value->data[0].v_pointer)
1699 g_object_unref (value->data[0].v_pointer);
1703 g_value_object_copy_value (const GValue *src_value,
1706 if (src_value->data[0].v_pointer)
1707 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1709 dest_value->data[0].v_pointer = NULL;
1713 g_value_object_transform_value (const GValue *src_value,
1716 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)))
1717 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
1719 dest_value->data[0].v_pointer = NULL;
1723 g_value_object_peek_pointer (const GValue *value)
1725 return value->data[0].v_pointer;
1729 g_value_object_collect_value (GValue *value,
1730 guint n_collect_values,
1731 GTypeCValue *collect_values,
1732 guint collect_flags)
1734 if (collect_values[0].v_pointer)
1736 GObject *object = collect_values[0].v_pointer;
1738 if (object->g_type_instance.g_class == NULL)
1739 return g_strconcat ("invalid unclassed object pointer for value type `",
1740 G_VALUE_TYPE_NAME (value),
1743 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
1744 return g_strconcat ("invalid object type `",
1745 G_OBJECT_TYPE_NAME (object),
1746 "' for value type `",
1747 G_VALUE_TYPE_NAME (value),
1750 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
1751 value->data[0].v_pointer = g_object_ref (object);
1754 value->data[0].v_pointer = NULL;
1760 g_value_object_lcopy_value (const GValue *value,
1761 guint n_collect_values,
1762 GTypeCValue *collect_values,
1763 guint collect_flags)
1765 GObject **object_p = collect_values[0].v_pointer;
1768 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
1770 if (!value->data[0].v_pointer)
1772 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
1773 *object_p = value->data[0].v_pointer;
1775 *object_p = g_object_ref (value->data[0].v_pointer);
1781 g_value_set_object (GValue *value,
1786 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1788 old = value->data[0].v_pointer;
1792 g_return_if_fail (G_IS_OBJECT (v_object));
1793 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1795 value->data[0].v_pointer = v_object;
1796 g_object_ref (value->data[0].v_pointer);
1799 value->data[0].v_pointer = NULL;
1802 g_object_unref (old);
1806 g_value_set_object_take_ownership (GValue *value,
1809 g_value_take_object (value, v_object);
1813 g_value_take_object (GValue *value,
1816 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
1818 if (value->data[0].v_pointer)
1820 g_object_unref (value->data[0].v_pointer);
1821 value->data[0].v_pointer = NULL;
1826 g_return_if_fail (G_IS_OBJECT (v_object));
1827 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
1829 value->data[0].v_pointer = v_object; /* we take over the reference count */
1834 g_value_get_object (const GValue *value)
1836 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1838 return value->data[0].v_pointer;
1842 g_value_dup_object (const GValue *value)
1844 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
1846 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
1850 g_signal_connect_object (gpointer instance,
1851 const gchar *detailed_signal,
1852 GCallback c_handler,
1854 GConnectFlags connect_flags)
1856 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1857 g_return_val_if_fail (detailed_signal != NULL, 0);
1858 g_return_val_if_fail (c_handler != NULL, 0);
1864 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
1866 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
1868 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
1871 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
1877 GClosure *closures[1]; /* flexible array */
1879 /* don't change this structure without supplying an accessor for
1880 * watched closures, e.g.:
1881 * GSList* g_object_list_watched_closures (GObject *object)
1884 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1885 * carray = g_object_get_data (object, "GObject-closure-array");
1888 * GSList *slist = NULL;
1890 * for (i = 0; i < carray->n_closures; i++)
1891 * slist = g_slist_prepend (slist, carray->closures[i]);
1899 object_remove_closure (gpointer data,
1902 GObject *object = data;
1903 CArray *carray = g_object_get_qdata (object, quark_closure_array);
1906 for (i = 0; i < carray->n_closures; i++)
1907 if (carray->closures[i] == closure)
1909 carray->n_closures--;
1910 if (i < carray->n_closures)
1911 carray->closures[i] = carray->closures[carray->n_closures];
1914 g_assert_not_reached ();
1918 destroy_closure_array (gpointer data)
1920 CArray *carray = data;
1921 GObject *object = carray->object;
1922 guint i, n = carray->n_closures;
1924 for (i = 0; i < n; i++)
1926 GClosure *closure = carray->closures[i];
1928 /* removing object_remove_closure() upfront is probably faster than
1929 * letting it fiddle with quark_closure_array which is empty anyways
1931 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
1932 g_closure_invalidate (closure);
1938 g_object_watch_closure (GObject *object,
1944 g_return_if_fail (G_IS_OBJECT (object));
1945 g_return_if_fail (closure != NULL);
1946 g_return_if_fail (closure->is_invalid == FALSE);
1947 g_return_if_fail (closure->in_marshal == FALSE);
1948 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
1950 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
1951 g_closure_add_marshal_guards (closure,
1952 object, (GClosureNotify) g_object_ref,
1953 object, (GClosureNotify) g_object_unref);
1954 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
1957 carray = g_renew (CArray, NULL, 1);
1958 carray->object = object;
1959 carray->n_closures = 1;
1964 i = carray->n_closures++;
1965 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
1967 carray->closures[i] = closure;
1968 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
1972 g_closure_new_object (guint sizeof_closure,
1977 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1978 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1980 closure = g_closure_new_simple (sizeof_closure, object);
1981 g_object_watch_closure (object, closure);
1987 g_cclosure_new_object (GCallback callback_func,
1992 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1993 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
1994 g_return_val_if_fail (callback_func != NULL, NULL);
1996 closure = g_cclosure_new (callback_func, object, NULL);
1997 g_object_watch_closure (object, closure);
2003 g_cclosure_new_object_swap (GCallback callback_func,
2008 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2009 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
2010 g_return_val_if_fail (callback_func != NULL, NULL);
2012 closure = g_cclosure_new_swap (callback_func, object, NULL);
2013 g_object_watch_closure (object, closure);
2018 #define __G_OBJECT_C__
2019 #include "gobjectaliasdef.c"