sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. sl@0: * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General sl@0: * Public License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place, Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * MT safe with regards to reference counting. sl@0: */ sl@0: sl@0: #include "config.h" sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "gdatasetprivate.h" sl@0: sl@0: #include "gobject.h" sl@0: #include "gvaluecollector.h" sl@0: #include "gsignal.h" sl@0: #include "gparamspecs.h" sl@0: #include "gvaluetypes.h" sl@0: #include "gobjectalias.h" sl@0: #ifdef __SYMBIAN32__ sl@0: #include sl@0: #include "gobject_wsd.h" sl@0: #include sl@0: #endif /* __SYMBIAN32__ */ sl@0: /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */ sl@0: #include "gobjectnotifyqueue.c" sl@0: sl@0: sl@0: sl@0: /** sl@0: * SECTION:objects sl@0: * @short_description: The base object type sl@0: * @see_also: #GParamSpecObject, g_param_spec_object() sl@0: * @title: The Base Object Type sl@0: * sl@0: * GObject is the fundamental type providing the common attributes and sl@0: * methods for all object types in GTK+, Pango and other libraries sl@0: * based on GObject. The GObject class provides methods for object sl@0: * construction and destruction, property access methods, and signal sl@0: * support. Signals are described in detail in . sl@0: * sl@0: * sl@0: * #GInitiallyUnowned is derived from #GObject. The only difference between sl@0: * the two is that the initial reference of a #GInitiallyUnowned is flagged sl@0: * as a floating reference. sl@0: * This means that it is not specifically claimed to be "owned" by sl@0: * any code portion. The main motivation for providing floating references is sl@0: * C convenience. In particular, it allows code to be written as: sl@0: * |[ sl@0: * container = create_container(); sl@0: * container_add_child (container, create_child()); sl@0: * ]| sl@0: * If container_add_child() will g_object_ref_sink() the sl@0: * passed in child, no reference of the newly created child is leaked. sl@0: * Without floating references, container_add_child() sl@0: * can only g_object_ref() the new child, so to implement this code without sl@0: * reference leaks, it would have to be written as: sl@0: * |[ sl@0: * Child *child; sl@0: * container = create_container(); sl@0: * child = create_child(); sl@0: * container_add_child (container, child); sl@0: * g_object_unref (child); sl@0: * ]| sl@0: * The floating reference can be converted into sl@0: * an ordinary reference by calling g_object_ref_sink(). sl@0: * For already sunken objects (objects that don't have a floating reference sl@0: * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns sl@0: * a new reference. sl@0: * Since floating references are useful almost exclusively for C convenience, sl@0: * language bindings that provide automated reference and memory ownership sl@0: * maintenance (such as smart pointers or garbage collection) therefore don't sl@0: * need to expose floating references in their API. sl@0: * sl@0: * sl@0: * Some object implementations may need to save an objects floating state sl@0: * across certain code portions (an example is #GtkMenu), to achive this, the sl@0: * following sequence can be used: sl@0: * sl@0: * |[ sl@0: * // save floating state sl@0: * gboolean was_floating = g_object_is_floating (object); sl@0: * g_object_ref_sink (object); sl@0: * // protected code portion sl@0: * ...; sl@0: * // restore floating state sl@0: * if (was_floating) sl@0: * g_object_force_floating (object); sl@0: * g_obejct_unref (object); // release previously acquired reference sl@0: * ]| sl@0: */ sl@0: sl@0: sl@0: /* --- macros --- */ sl@0: #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id) sl@0: #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id)) sl@0: sl@0: #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1 sl@0: #define OBJECT_HAS_TOGGLE_REF(object) \ sl@0: ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0) sl@0: #define OBJECT_FLOATING_FLAG 0x2 sl@0: sl@0: sl@0: /* --- signals --- */ sl@0: enum { sl@0: NOTIFY, sl@0: LAST_SIGNAL sl@0: }; sl@0: sl@0: sl@0: /* --- properties --- */ sl@0: enum { sl@0: PROP_NONE sl@0: }; sl@0: sl@0: sl@0: /* --- prototypes --- */ sl@0: static void g_object_base_class_init (GObjectClass *class); sl@0: static void g_object_base_class_finalize (GObjectClass *class); sl@0: static void g_object_do_class_init (GObjectClass *class); sl@0: static void g_object_init (GObject *object); sl@0: static GObject* g_object_constructor (GType type, sl@0: guint n_construct_properties, sl@0: GObjectConstructParam *construct_params); sl@0: static void g_object_real_dispose (GObject *object); sl@0: static void g_object_finalize (GObject *object); sl@0: static void g_object_do_set_property (GObject *object, sl@0: guint property_id, sl@0: const GValue *value, sl@0: GParamSpec *pspec); sl@0: static void g_object_do_get_property (GObject *object, sl@0: guint property_id, sl@0: GValue *value, sl@0: GParamSpec *pspec); sl@0: static void g_value_object_init (GValue *value); sl@0: static void g_value_object_free_value (GValue *value); sl@0: static void g_value_object_copy_value (const GValue *src_value, sl@0: GValue *dest_value); sl@0: static void g_value_object_transform_value (const GValue *src_value, sl@0: GValue *dest_value); sl@0: static gpointer g_value_object_peek_pointer (const GValue *value); sl@0: static gchar* g_value_object_collect_value (GValue *value, sl@0: guint n_collect_values, sl@0: GTypeCValue *collect_values, sl@0: guint collect_flags); sl@0: static gchar* g_value_object_lcopy_value (const GValue *value, sl@0: guint n_collect_values, sl@0: GTypeCValue *collect_values, sl@0: guint collect_flags); sl@0: static void g_object_dispatch_properties_changed (GObject *object, sl@0: guint n_pspecs, sl@0: GParamSpec **pspecs); sl@0: static inline void object_get_property (GObject *object, sl@0: GParamSpec *pspec, sl@0: GValue *value); sl@0: static inline void object_set_property (GObject *object, sl@0: GParamSpec *pspec, sl@0: const GValue *value, sl@0: GObjectNotifyQueue *nqueue); sl@0: #if (EMULATOR) sl@0: guint object_floating_flag_handler (GObject *object, sl@0: gint job); sl@0: #else sl@0: static guint object_floating_flag_handler (GObject *object, sl@0: gint job); sl@0: #endif /* EMULATOR */ sl@0: static void object_interface_check_properties (gpointer func_data, sl@0: gpointer g_iface); sl@0: sl@0: sl@0: /* --- variables --- */ sl@0: #if EMULATOR sl@0: sl@0: PLS(quark_closure_array,gobject,GQuark) sl@0: PLS(quark_weak_refs,gobject,GQuark) sl@0: PLS(quark_toggle_refs,gobject,GQuark) sl@0: PLS(pspec_pool,gobject,GParamSpecPool *) sl@0: PLS(property_notify_context,gobject,GObjectNotifyContext) sl@0: PLS_ARRAY(gobject_signals,gobject,gulong) sl@0: PLS_MACRO(construction_mutex,gobject,GStaticMutex) sl@0: PLS(construction_objects,gobject,GSList *) sl@0: PLS(floating_flag_handler,gobject,function_type) sl@0: sl@0: #define quark_closure_array (*FUNCTION_NAME(quark_closure_array,gobject)()) sl@0: #define quark_weak_refs (*FUNCTION_NAME(quark_weak_refs,gobject)()) sl@0: #define quark_toggle_refs (*FUNCTION_NAME(quark_toggle_refs,gobject)()) sl@0: #define pspec_pool (*FUNCTION_NAME(pspec_pool,gobject)()) sl@0: #define property_notify_context (*FUNCTION_NAME(property_notify_context,gobject)()) sl@0: #define gobject_signals (FUNCTION_NAME(gobject_signals,gobject)()) sl@0: #define g__construction_mutex_lock (*FUNCTION_NAME_MACRO(construction_mutex,gobject)()) sl@0: #define construction_objects (*FUNCTION_NAME(construction_objects,gobject)()) sl@0: #define floating_flag_handler (*FUNCTION_NAME(floating_flag_handler,gobject)()) sl@0: sl@0: #else sl@0: sl@0: static GQuark quark_closure_array = 0; sl@0: static GQuark quark_weak_refs = 0; sl@0: static GQuark quark_toggle_refs = 0; sl@0: static GParamSpecPool *pspec_pool = NULL; sl@0: static GObjectNotifyContext property_notify_context = { 0, }; sl@0: static gulong gobject_signals[LAST_SIGNAL] = { 0, }; sl@0: static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler; sl@0: G_LOCK_DEFINE_STATIC (construction_mutex); sl@0: static GSList *construction_objects = NULL; sl@0: sl@0: #endif /* EMULATOR */ sl@0: sl@0: /* --- functions --- */ sl@0: #ifdef G_ENABLE_DEBUG sl@0: #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) sl@0: #if EMULATOR sl@0: sl@0: PLS_MACRO(debug_objects,gobject,GStaticMutex) sl@0: PLS(g_trap_object_ref,gobject,volatile GObject *) sl@0: PLS(debug_objects_count,gobject,guint) sl@0: PLS(debug_objects_ht,gobject,GHashTable *) sl@0: sl@0: #define g__debug_objects_lock (*FUNCTION_NAME_MACRO(debug_objects,gobject)()) sl@0: #define g_trap_object_ref (*FUNCTION_NAME(g_trap_object_ref,gobject)()) sl@0: #define debug_objects_count (*FUNCTION_NAME(debug_objects_count,gobject)()) sl@0: #define debug_objects_ht (*FUNCTION_NAME(debug_objects_ht,gobject)()) sl@0: sl@0: sl@0: #else sl@0: sl@0: G_LOCK_DEFINE_STATIC (debug_objects); sl@0: static volatile GObject *g_trap_object_ref = NULL; sl@0: static guint debug_objects_count = 0; sl@0: static GHashTable *debug_objects_ht = NULL; sl@0: #endif /* EMULATOR */ sl@0: static void sl@0: debug_objects_foreach (gpointer key, sl@0: gpointer value, sl@0: gpointer user_data) sl@0: { sl@0: GObject *object = value; sl@0: sl@0: g_message ("[%p] stale %s\tref_count=%u", sl@0: object, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: object->ref_count); sl@0: } sl@0: sl@0: static void sl@0: debug_objects_atexit (void) sl@0: { sl@0: IF_DEBUG (OBJECTS) sl@0: { sl@0: G_LOCK (debug_objects); sl@0: g_message ("stale GObjects: %u", debug_objects_count); sl@0: g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL); sl@0: G_UNLOCK (debug_objects); sl@0: } sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: #if EMULATOR sl@0: sl@0: PLS(initialized ,g_object_type_init,gboolean) sl@0: PLS(info ,g_object_type_init,GTypeInfo) sl@0: sl@0: #define initialized (*FUNCTION_NAME(initialized ,g_object_type_init)()) sl@0: #define info (*FUNCTION_NAME(info ,g_object_type_init)()) sl@0: sl@0: const GTypeInfo gobject_info = { sl@0: sizeof (GObjectClass), sl@0: (GBaseInitFunc) g_object_base_class_init, sl@0: (GBaseFinalizeFunc) g_object_base_class_finalize, sl@0: (GClassInitFunc) g_object_do_class_init, sl@0: NULL /* class_destroy */, sl@0: NULL /* class_data */, sl@0: sizeof (GObject), sl@0: 0 /* n_preallocs */, sl@0: (GInstanceInitFunc) g_object_init, sl@0: NULL, /* value_table */ sl@0: }; sl@0: sl@0: sl@0: #endif /* EMULATOR */ sl@0: sl@0: void sl@0: g_object_type_init (void) sl@0: { sl@0: #if !(EMULATOR) sl@0: static gboolean initialized = FALSE; sl@0: #endif /* EMULATOR */ sl@0: static const GTypeFundamentalInfo finfo = { sl@0: G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE, sl@0: }; sl@0: #if !(EMULATOR) sl@0: static GTypeInfo info = { sl@0: sizeof (GObjectClass), sl@0: (GBaseInitFunc) g_object_base_class_init, sl@0: (GBaseFinalizeFunc) g_object_base_class_finalize, sl@0: (GClassInitFunc) g_object_do_class_init, sl@0: NULL /* class_destroy */, sl@0: NULL /* class_data */, sl@0: sizeof (GObject), sl@0: 0 /* n_preallocs */, sl@0: (GInstanceInitFunc) g_object_init, sl@0: NULL, /* value_table */ sl@0: }; sl@0: #endif /* EMULATOR */ sl@0: static const GTypeValueTable value_table = { sl@0: g_value_object_init, /* value_init */ sl@0: g_value_object_free_value, /* value_free */ sl@0: g_value_object_copy_value, /* value_copy */ sl@0: g_value_object_peek_pointer, /* value_peek_pointer */ sl@0: "p", /* collect_format */ sl@0: g_value_object_collect_value, /* collect_value */ sl@0: "p", /* lcopy_format */ sl@0: g_value_object_lcopy_value, /* lcopy_value */ sl@0: }; sl@0: GType type; sl@0: sl@0: g_return_if_fail (initialized == FALSE); sl@0: initialized = TRUE; sl@0: sl@0: /* G_TYPE_OBJECT sl@0: */ sl@0: info.value_table = &value_table; sl@0: type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0); sl@0: g_assert (type == G_TYPE_OBJECT); sl@0: g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (OBJECTS) sl@0: { sl@0: debug_objects_ht = g_hash_table_new (g_direct_hash, NULL); sl@0: g_atexit (debug_objects_atexit); sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: } sl@0: sl@0: #if EMULATOR sl@0: #undef initialized sl@0: #undef info sl@0: #endif /* EMULATOR */ sl@0: sl@0: static void sl@0: g_object_base_class_init (GObjectClass *class) sl@0: { sl@0: GObjectClass *pclass = g_type_class_peek_parent (class); sl@0: sl@0: /* reset instance specific fields and methods that don't get inherited */ sl@0: class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL; sl@0: class->get_property = NULL; sl@0: class->set_property = NULL; sl@0: } sl@0: sl@0: static void sl@0: g_object_base_class_finalize (GObjectClass *class) sl@0: { sl@0: GList *list, *node; sl@0: sl@0: _g_signals_destroy (G_OBJECT_CLASS_TYPE (class)); sl@0: sl@0: g_slist_free (class->construct_properties); sl@0: class->construct_properties = NULL; sl@0: list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class)); sl@0: for (node = list; node; node = node->next) sl@0: { sl@0: GParamSpec *pspec = node->data; sl@0: sl@0: g_param_spec_pool_remove (pspec_pool, pspec); sl@0: PARAM_SPEC_SET_PARAM_ID (pspec, 0); sl@0: g_param_spec_unref (pspec); sl@0: } sl@0: g_list_free (list); sl@0: } sl@0: sl@0: static void sl@0: g_object_notify_dispatcher (GObject *object, sl@0: guint n_pspecs, sl@0: GParamSpec **pspecs) sl@0: { sl@0: G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs); sl@0: } sl@0: sl@0: static void sl@0: g_object_do_class_init (GObjectClass *class) sl@0: { sl@0: /* read the comment about typedef struct CArray; on why not to change this quark */ sl@0: quark_closure_array = g_quark_from_static_string ("GObject-closure-array"); sl@0: sl@0: quark_weak_refs = g_quark_from_static_string ("GObject-weak-references"); sl@0: quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references"); sl@0: pspec_pool = g_param_spec_pool_new (TRUE); sl@0: property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue"); sl@0: property_notify_context.dispatcher = g_object_notify_dispatcher; sl@0: sl@0: class->constructor = g_object_constructor; sl@0: class->set_property = g_object_do_set_property; sl@0: class->get_property = g_object_do_get_property; sl@0: class->dispose = g_object_real_dispose; sl@0: class->finalize = g_object_finalize; sl@0: class->dispatch_properties_changed = g_object_dispatch_properties_changed; sl@0: class->notify = NULL; sl@0: sl@0: /** sl@0: * GObject::notify: sl@0: * @gobject: the object which received the signal. sl@0: * @pspec: the #GParamSpec of the property which changed. sl@0: * sl@0: * The notify signal is emitted on an object when one of its sl@0: * properties has been changed. Note that getting this signal sl@0: * doesn't guarantee that the value of the property has actually sl@0: * changed, it may also be emitted when the setter for the property sl@0: * is called to reinstate the previous value. sl@0: * sl@0: * This signal is typically used to obtain change notification for a sl@0: * single property, by specifying the property name as a detail in the sl@0: * g_signal_connect() call, like this: sl@0: * |[ sl@0: * g_signal_connect (text_view->buffer, "notify::paste-target-list", sl@0: * G_CALLBACK (gtk_text_view_target_list_notify), sl@0: * text_view) sl@0: * ]| sl@0: * It is important to note that you must use sl@0: * canonical parameter names as sl@0: * detail strings for the notify signal. sl@0: */ sl@0: gobject_signals[NOTIFY] = sl@0: g_signal_new (g_intern_static_string ("notify"), sl@0: G_TYPE_FROM_CLASS (class), sl@0: G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION, sl@0: G_STRUCT_OFFSET (GObjectClass, notify), sl@0: NULL, NULL, sl@0: g_cclosure_marshal_VOID__PARAM, sl@0: G_TYPE_NONE, sl@0: 1, G_TYPE_PARAM); sl@0: sl@0: /* Install a check function that we'll use to verify that classes that sl@0: * implement an interface implement all properties for that interface sl@0: */ sl@0: g_type_add_interface_check (NULL, object_interface_check_properties); sl@0: } sl@0: sl@0: static void sl@0: install_property_internal (GType g_type, sl@0: guint property_id, sl@0: GParamSpec *pspec) sl@0: { sl@0: if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE)) sl@0: { sl@0: g_warning ("When installing property: type `%s' already has a property named `%s'", sl@0: g_type_name (g_type), sl@0: pspec->name); sl@0: return; sl@0: } sl@0: sl@0: g_param_spec_ref (pspec); sl@0: g_param_spec_sink (pspec); sl@0: PARAM_SPEC_SET_PARAM_ID (pspec, property_id); sl@0: g_param_spec_pool_insert (pspec_pool, pspec, g_type); sl@0: } sl@0: sl@0: /** sl@0: * g_object_class_install_property: sl@0: * @oclass: a #GObjectClass sl@0: * @property_id: the id for the new property sl@0: * @pspec: the #GParamSpec for the new property sl@0: * sl@0: * Installs a new property. This is usually done in the class initializer. sl@0: * sl@0: * Note that it is possible to redefine a property in a derived class, sl@0: * by installing a property with the same name. This can be useful at times, sl@0: * e.g. to change the range of allowed values or the default value. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_class_install_property (GObjectClass *class, sl@0: guint property_id, sl@0: GParamSpec *pspec) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT_CLASS (class)); sl@0: g_return_if_fail (G_IS_PARAM_SPEC (pspec)); sl@0: if (pspec->flags & G_PARAM_WRITABLE) sl@0: g_return_if_fail (class->set_property != NULL); sl@0: if (pspec->flags & G_PARAM_READABLE) sl@0: g_return_if_fail (class->get_property != NULL); sl@0: g_return_if_fail (property_id > 0); sl@0: g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */ sl@0: if (pspec->flags & G_PARAM_CONSTRUCT) sl@0: g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0); sl@0: if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) sl@0: g_return_if_fail (pspec->flags & G_PARAM_WRITABLE); sl@0: sl@0: install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec); sl@0: sl@0: if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) sl@0: class->construct_properties = g_slist_prepend (class->construct_properties, pspec); sl@0: sl@0: /* for property overrides of construct poperties, we have to get rid sl@0: * of the overidden inherited construct property sl@0: */ sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE); sl@0: if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) sl@0: class->construct_properties = g_slist_remove (class->construct_properties, pspec); sl@0: } sl@0: sl@0: /** sl@0: * g_object_interface_install_property: sl@0: * @g_iface: any interface vtable for the interface, or the default sl@0: * vtable for the interface. sl@0: * @pspec: the #GParamSpec for the new property sl@0: * sl@0: * Add a property to an interface; this is only useful for interfaces sl@0: * that are added to GObject-derived types. Adding a property to an sl@0: * interface forces all objects classes with that interface to have a sl@0: * compatible property. The compatible property could be a newly sl@0: * created #GParamSpec, but normally sl@0: * g_object_class_override_property() will be used so that the object sl@0: * class only needs to provide an implementation and inherits the sl@0: * property description, default value, bounds, and so forth from the sl@0: * interface property. sl@0: * sl@0: * This function is meant to be called from the interface's default sl@0: * vtable initialization function (the @class_init member of sl@0: * #GTypeInfo.) It must not be called after after @class_init has sl@0: * been called for any object types implementing this interface. sl@0: * sl@0: * Since: 2.4 sl@0: */ sl@0: EXPORT_C void sl@0: g_object_interface_install_property (gpointer g_iface, sl@0: GParamSpec *pspec) sl@0: { sl@0: GTypeInterface *iface_class = g_iface; sl@0: sl@0: g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type)); sl@0: g_return_if_fail (G_IS_PARAM_SPEC (pspec)); sl@0: g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */ sl@0: g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */ sl@0: sl@0: install_property_internal (iface_class->g_type, 0, pspec); sl@0: } sl@0: sl@0: /** sl@0: * g_object_class_find_property: sl@0: * @oclass: a #GObjectClass sl@0: * @property_name: the name of the property to look up sl@0: * sl@0: * Looks up the #GParamSpec for a property of a class. sl@0: * sl@0: * Returns: the #GParamSpec for the property, or %NULL if the class sl@0: * doesn't have a property of that name sl@0: */ sl@0: EXPORT_C GParamSpec* sl@0: g_object_class_find_property (GObjectClass *class, sl@0: const gchar *property_name) sl@0: { sl@0: GParamSpec *pspec; sl@0: GParamSpec *redirect; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); sl@0: g_return_val_if_fail (property_name != NULL, NULL); sl@0: sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: property_name, sl@0: G_OBJECT_CLASS_TYPE (class), sl@0: TRUE); sl@0: if (pspec) sl@0: { sl@0: redirect = g_param_spec_get_redirect_target (pspec); sl@0: if (redirect) sl@0: return redirect; sl@0: else sl@0: return pspec; sl@0: } sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_object_interface_find_property: sl@0: * @g_iface: any interface vtable for the interface, or the default sl@0: * vtable for the interface sl@0: * @property_name: name of a property to lookup. sl@0: * sl@0: * Find the #GParamSpec with the given name for an sl@0: * interface. Generally, the interface vtable passed in as @g_iface sl@0: * will be the default vtable from g_type_default_interface_ref(), or, sl@0: * if you know the interface has already been loaded, sl@0: * g_type_default_interface_peek(). sl@0: * sl@0: * Since: 2.4 sl@0: * sl@0: * Returns: the #GParamSpec for the property of the interface with the sl@0: * name @property_name, or %NULL if no such property exists. sl@0: */ sl@0: EXPORT_C GParamSpec* sl@0: g_object_interface_find_property (gpointer g_iface, sl@0: const gchar *property_name) sl@0: { sl@0: GTypeInterface *iface_class = g_iface; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); sl@0: g_return_val_if_fail (property_name != NULL, NULL); sl@0: sl@0: return g_param_spec_pool_lookup (pspec_pool, sl@0: property_name, sl@0: iface_class->g_type, sl@0: FALSE); sl@0: } sl@0: sl@0: /** sl@0: * g_object_class_override_property: sl@0: * @oclass: a #GObjectClass sl@0: * @property_id: the new property ID sl@0: * @name: the name of a property registered in a parent class or sl@0: * in an interface of this class. sl@0: * sl@0: * Registers @property_id as referring to a property with the sl@0: * name @name in a parent class or in an interface implemented sl@0: * by @oclass. This allows this class to override sl@0: * a property implementation in a parent class or to provide sl@0: * the implementation of a property from an interface. sl@0: * sl@0: * sl@0: * Internally, overriding is implemented by creating a property of type sl@0: * #GParamSpecOverride; generally operations that query the properties of sl@0: * the object class, such as g_object_class_find_property() or sl@0: * g_object_class_list_properties() will return the overridden sl@0: * property. However, in one case, the @construct_properties argument of sl@0: * the @constructor virtual function, the #GParamSpecOverride is passed sl@0: * instead, so that the @param_id field of the #GParamSpec will be sl@0: * correct. For virtually all uses, this makes no difference. If you sl@0: * need to get the overridden property, you can call sl@0: * g_param_spec_get_redirect_target(). sl@0: * sl@0: * sl@0: * Since: 2.4 sl@0: */ sl@0: EXPORT_C void sl@0: g_object_class_override_property (GObjectClass *oclass, sl@0: guint property_id, sl@0: const gchar *name) sl@0: { sl@0: GParamSpec *overridden = NULL; sl@0: GParamSpec *new; sl@0: GType parent_type; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT_CLASS (oclass)); sl@0: g_return_if_fail (property_id > 0); sl@0: g_return_if_fail (name != NULL); sl@0: sl@0: /* Find the overridden property; first check parent types sl@0: */ sl@0: parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass)); sl@0: if (parent_type != G_TYPE_NONE) sl@0: overridden = g_param_spec_pool_lookup (pspec_pool, sl@0: name, sl@0: parent_type, sl@0: TRUE); sl@0: if (!overridden) sl@0: { sl@0: GType *ifaces; sl@0: guint n_ifaces; sl@0: sl@0: /* Now check interfaces sl@0: */ sl@0: ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces); sl@0: while (n_ifaces-- && !overridden) sl@0: { sl@0: overridden = g_param_spec_pool_lookup (pspec_pool, sl@0: name, sl@0: ifaces[n_ifaces], sl@0: FALSE); sl@0: } sl@0: sl@0: g_free (ifaces); sl@0: } sl@0: sl@0: if (!overridden) sl@0: { sl@0: g_warning ("%s: Can't find property to override for '%s::%s'", sl@0: G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name); sl@0: return; sl@0: } sl@0: sl@0: new = g_param_spec_override (name, overridden); sl@0: g_object_class_install_property (oclass, property_id, new); sl@0: } sl@0: sl@0: /** sl@0: * g_object_class_list_properties: sl@0: * @oclass: a #GObjectClass sl@0: * @n_properties: return location for the length of the returned array sl@0: * sl@0: * Get an array of #GParamSpec* for all properties of a class. sl@0: * sl@0: * Returns: an array of #GParamSpec* which should be freed after use sl@0: */ sl@0: EXPORT_C GParamSpec** /* free result */ sl@0: g_object_class_list_properties (GObjectClass *class, sl@0: guint *n_properties_p) sl@0: { sl@0: GParamSpec **pspecs; sl@0: guint n; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); sl@0: sl@0: pspecs = g_param_spec_pool_list (pspec_pool, sl@0: G_OBJECT_CLASS_TYPE (class), sl@0: &n); sl@0: if (n_properties_p) sl@0: *n_properties_p = n; sl@0: sl@0: return pspecs; sl@0: } sl@0: sl@0: /** sl@0: * g_object_interface_list_properties: sl@0: * @g_iface: any interface vtable for the interface, or the default sl@0: * vtable for the interface sl@0: * @n_properties_p: location to store number of properties returned. sl@0: * sl@0: * Lists the properties of an interface.Generally, the interface sl@0: * vtable passed in as @g_iface will be the default vtable from sl@0: * g_type_default_interface_ref(), or, if you know the interface has sl@0: * already been loaded, g_type_default_interface_peek(). sl@0: * sl@0: * Since: 2.4 sl@0: * sl@0: * Returns: a pointer to an array of pointers to #GParamSpec sl@0: * structures. The paramspecs are owned by GLib, but the sl@0: * array should be freed with g_free() when you are done with sl@0: * it. sl@0: */ sl@0: EXPORT_C GParamSpec** /* free result */ sl@0: g_object_interface_list_properties (gpointer g_iface, sl@0: guint *n_properties_p) sl@0: { sl@0: GTypeInterface *iface_class = g_iface; sl@0: GParamSpec **pspecs; sl@0: guint n; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); sl@0: sl@0: pspecs = g_param_spec_pool_list (pspec_pool, sl@0: iface_class->g_type, sl@0: &n); sl@0: if (n_properties_p) sl@0: *n_properties_p = n; sl@0: sl@0: return pspecs; sl@0: } sl@0: sl@0: static void sl@0: g_object_init (GObject *object) sl@0: { sl@0: object->ref_count = 1; sl@0: g_datalist_init (&object->qdata); sl@0: sl@0: /* freeze object's notification queue, g_object_newv() preserves pairedness */ sl@0: g_object_notify_queue_freeze (object, &property_notify_context); sl@0: /* enter construction list for notify_queue_thaw() and to allow construct-only properties */ sl@0: G_LOCK (construction_mutex); sl@0: construction_objects = g_slist_prepend (construction_objects, object); sl@0: G_UNLOCK (construction_mutex); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (OBJECTS) sl@0: { sl@0: G_LOCK (debug_objects); sl@0: debug_objects_count++; sl@0: g_hash_table_insert (debug_objects_ht, object, object); sl@0: G_UNLOCK (debug_objects); sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: } sl@0: sl@0: static void sl@0: g_object_do_set_property (GObject *object, sl@0: guint property_id, sl@0: const GValue *value, sl@0: GParamSpec *pspec) sl@0: { sl@0: switch (property_id) sl@0: { sl@0: default: sl@0: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: static void sl@0: g_object_do_get_property (GObject *object, sl@0: guint property_id, sl@0: GValue *value, sl@0: GParamSpec *pspec) sl@0: { sl@0: switch (property_id) sl@0: { sl@0: default: sl@0: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: static void sl@0: g_object_real_dispose (GObject *object) sl@0: { sl@0: g_signal_handlers_destroy (object); sl@0: g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); sl@0: g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); sl@0: } sl@0: sl@0: static void sl@0: g_object_finalize (GObject *object) sl@0: { sl@0: g_datalist_clear (&object->qdata); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (OBJECTS) sl@0: { sl@0: G_LOCK (debug_objects); sl@0: g_assert (g_hash_table_lookup (debug_objects_ht, object) == object); sl@0: g_hash_table_remove (debug_objects_ht, object); sl@0: debug_objects_count--; sl@0: G_UNLOCK (debug_objects); sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: } sl@0: sl@0: sl@0: static void sl@0: g_object_dispatch_properties_changed (GObject *object, sl@0: guint n_pspecs, sl@0: GParamSpec **pspecs) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < n_pspecs; i++) sl@0: g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]); sl@0: } sl@0: sl@0: /** sl@0: * g_object_run_dispose: sl@0: * @object: a #GObject sl@0: * sl@0: * Releases all references to other objects. This can be used to break sl@0: * reference cycles. sl@0: * sl@0: * This functions should only be called from object system implementations. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_run_dispose (GObject *object) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (object->ref_count > 0); sl@0: sl@0: g_object_ref (object); sl@0: G_OBJECT_GET_CLASS (object)->dispose (object); sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_freeze_notify: sl@0: * @object: a #GObject sl@0: * sl@0: * Increases the freeze count on @object. If the freeze count is sl@0: * non-zero, the emission of "notify" signals on @object is sl@0: * stopped. The signals are queued until the freeze count is decreased sl@0: * to zero. sl@0: * sl@0: * This is necessary for accessors that modify multiple properties to prevent sl@0: * premature notification while the object is still being modified. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_freeze_notify (GObject *object) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: sl@0: if (g_atomic_int_get (&object->ref_count) == 0) sl@0: return; sl@0: sl@0: g_object_ref (object); sl@0: g_object_notify_queue_freeze (object, &property_notify_context); sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_notify: sl@0: * @object: a #GObject sl@0: * @property_name: the name of a property installed on the class of @object. sl@0: * sl@0: * Emits a "notify" signal for the property @property_name on @object. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_notify (GObject *object, sl@0: const gchar *property_name) sl@0: { sl@0: GParamSpec *pspec; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (property_name != NULL); sl@0: if (g_atomic_int_get (&object->ref_count) == 0) sl@0: return; sl@0: sl@0: g_object_ref (object); sl@0: /* We don't need to get the redirect target sl@0: * (by, e.g. calling g_object_class_find_property()) sl@0: * because g_object_notify_queue_add() does that sl@0: */ sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: property_name, sl@0: G_OBJECT_TYPE (object), sl@0: TRUE); sl@0: sl@0: if (!pspec) sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: property_name); sl@0: else sl@0: { sl@0: GObjectNotifyQueue *nqueue; sl@0: sl@0: nqueue = g_object_notify_queue_freeze (object, &property_notify_context); sl@0: g_object_notify_queue_add (object, nqueue, pspec); sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: } sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_thaw_notify: sl@0: * @object: a #GObject sl@0: * sl@0: * Reverts the effect of a previous call to sl@0: * g_object_freeze_notify(). The freeze count is decreased on @object sl@0: * and when it reaches zero, all queued "notify" signals are emitted. sl@0: * sl@0: * It is an error to call this function when the freeze count is zero. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_thaw_notify (GObject *object) sl@0: { sl@0: GObjectNotifyQueue *nqueue; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: if (g_atomic_int_get (&object->ref_count) == 0) sl@0: return; sl@0: sl@0: g_object_ref (object); sl@0: nqueue = g_object_notify_queue_from_object (object, &property_notify_context); sl@0: if (!nqueue || !nqueue->freeze_count) sl@0: g_warning ("%s: property-changed notification for %s(%p) is not frozen", sl@0: G_STRFUNC, G_OBJECT_TYPE_NAME (object), object); sl@0: else sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: static inline void sl@0: object_get_property (GObject *object, sl@0: GParamSpec *pspec, sl@0: GValue *value) sl@0: { sl@0: GObjectClass *class = g_type_class_peek (pspec->owner_type); sl@0: guint param_id = PARAM_SPEC_PARAM_ID (pspec); sl@0: GParamSpec *redirect; sl@0: sl@0: redirect = g_param_spec_get_redirect_target (pspec); sl@0: if (redirect) sl@0: pspec = redirect; sl@0: sl@0: class->get_property (object, param_id, value, pspec); sl@0: } sl@0: sl@0: static inline void sl@0: object_set_property (GObject *object, sl@0: GParamSpec *pspec, sl@0: const GValue *value, sl@0: GObjectNotifyQueue *nqueue) sl@0: { sl@0: GValue tmp_value = { 0, }; sl@0: GObjectClass *class = g_type_class_peek (pspec->owner_type); sl@0: guint param_id = PARAM_SPEC_PARAM_ID (pspec); sl@0: GParamSpec *redirect; sl@0: sl@0: redirect = g_param_spec_get_redirect_target (pspec); sl@0: if (redirect) sl@0: pspec = redirect; sl@0: sl@0: /* provide a copy to work from, convert (if necessary) and validate */ sl@0: g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: if (!g_value_transform (value, &tmp_value)) sl@0: g_warning ("unable to set property `%s' of type `%s' from value of type `%s'", sl@0: pspec->name, sl@0: g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)), sl@0: G_VALUE_TYPE_NAME (value)); sl@0: else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION)) sl@0: { sl@0: gchar *contents = g_strdup_value_contents (value); sl@0: sl@0: g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'", sl@0: contents, sl@0: G_VALUE_TYPE_NAME (value), sl@0: pspec->name, sl@0: g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec))); sl@0: g_free (contents); sl@0: } sl@0: else sl@0: { sl@0: class->set_property (object, param_id, &tmp_value, pspec); sl@0: g_object_notify_queue_add (object, nqueue, pspec); sl@0: } sl@0: g_value_unset (&tmp_value); sl@0: } sl@0: sl@0: static void sl@0: object_interface_check_properties (gpointer func_data, sl@0: gpointer g_iface) sl@0: { sl@0: GTypeInterface *iface_class = g_iface; sl@0: GObjectClass *class = g_type_class_peek (iface_class->g_instance_type); sl@0: GType iface_type = iface_class->g_type; sl@0: GParamSpec **pspecs; sl@0: guint n; sl@0: sl@0: if (!G_IS_OBJECT_CLASS (class)) sl@0: return; sl@0: sl@0: pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n); sl@0: sl@0: while (n--) sl@0: { sl@0: GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: pspecs[n]->name, sl@0: G_OBJECT_CLASS_TYPE (class), sl@0: TRUE); sl@0: sl@0: if (!class_pspec) sl@0: { sl@0: g_critical ("Object class %s doesn't implement property " sl@0: "'%s' from interface '%s'", sl@0: g_type_name (G_OBJECT_CLASS_TYPE (class)), sl@0: pspecs[n]->name, sl@0: g_type_name (iface_type)); sl@0: sl@0: continue; sl@0: } sl@0: sl@0: /* The implementation paramspec must have a less restrictive sl@0: * type than the interface parameter spec for set() and a sl@0: * more restrictive type for get(). We just require equality, sl@0: * rather than doing something more complicated checking sl@0: * the READABLE and WRITABLE flags. We also simplify here sl@0: * by only checking the value type, not the G_PARAM_SPEC_TYPE. sl@0: */ sl@0: if (class_pspec && sl@0: !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]), sl@0: G_PARAM_SPEC_VALUE_TYPE (class_pspec))) sl@0: { sl@0: g_critical ("Property '%s' on class '%s' has type '%s' " sl@0: "which is different from the type '%s', " sl@0: "of the property on interface '%s'\n", sl@0: pspecs[n]->name, sl@0: g_type_name (G_OBJECT_CLASS_TYPE (class)), sl@0: g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)), sl@0: g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), sl@0: g_type_name (iface_type)); sl@0: } sl@0: sl@0: #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0) sl@0: sl@0: /* CONSTRUCT and CONSTRUCT_ONLY add restrictions. sl@0: * READABLE and WRITABLE remove restrictions. The implementation sl@0: * paramspec must have less restrictive flags. sl@0: */ sl@0: if (class_pspec && sl@0: (!SUBSET (class_pspec->flags, sl@0: pspecs[n]->flags, sl@0: G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) || sl@0: !SUBSET (pspecs[n]->flags, sl@0: class_pspec->flags, sl@0: G_PARAM_READABLE | G_PARAM_WRITABLE))) sl@0: { sl@0: g_critical ("Flags for property '%s' on class '%s' " sl@0: "are not compatible with the property on" sl@0: "interface '%s'\n", sl@0: pspecs[n]->name, sl@0: g_type_name (G_OBJECT_CLASS_TYPE (class)), sl@0: g_type_name (iface_type)); sl@0: } sl@0: #undef SUBSET sl@0: } sl@0: sl@0: g_free (pspecs); sl@0: } sl@0: sl@0: EXPORT_C GType sl@0: g_object_get_type (void) sl@0: { sl@0: return G_TYPE_OBJECT; sl@0: } sl@0: sl@0: /** sl@0: * g_object_new: sl@0: * @object_type: the type id of the #GObject subtype to instantiate sl@0: * @first_property_name: the name of the first property sl@0: * @...: the value of the first property, followed optionally by more sl@0: * name/value pairs, followed by %NULL sl@0: * sl@0: * Creates a new instance of a #GObject subtype and sets its properties. sl@0: * sl@0: * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) sl@0: * which are not explicitly specified are set to their default values. sl@0: * sl@0: * Returns: a new instance of @object_type sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_new (GType object_type, sl@0: const gchar *first_property_name, sl@0: ...) sl@0: { sl@0: GObject *object; sl@0: va_list var_args; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); sl@0: sl@0: va_start (var_args, first_property_name); sl@0: object = g_object_new_valist (object_type, first_property_name, var_args); sl@0: va_end (var_args); sl@0: sl@0: return object; sl@0: } sl@0: sl@0: static gboolean sl@0: slist_maybe_remove (GSList **slist, sl@0: gconstpointer data) sl@0: { sl@0: GSList *last = NULL, *node = *slist; sl@0: while (node) sl@0: { sl@0: if (node->data == data) sl@0: { sl@0: if (last) sl@0: last->next = node->next; sl@0: else sl@0: *slist = node->next; sl@0: g_slist_free_1 (node); sl@0: return TRUE; sl@0: } sl@0: last = node; sl@0: node = last->next; sl@0: } sl@0: return FALSE; sl@0: } sl@0: sl@0: static inline gboolean sl@0: object_in_construction_list (GObject *object) sl@0: { sl@0: gboolean in_construction; sl@0: G_LOCK (construction_mutex); sl@0: in_construction = g_slist_find (construction_objects, object) != NULL; sl@0: G_UNLOCK (construction_mutex); sl@0: return in_construction; sl@0: } sl@0: sl@0: /** sl@0: * g_object_newv: sl@0: * @object_type: the type id of the #GObject subtype to instantiate sl@0: * @n_parameters: the length of the @parameters array sl@0: * @parameters: an array of #GParameter sl@0: * sl@0: * Creates a new instance of a #GObject subtype and sets its properties. sl@0: * sl@0: * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) sl@0: * which are not explicitly specified are set to their default values. sl@0: * sl@0: * Returns: a new instance of @object_type sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_newv (GType object_type, sl@0: guint n_parameters, sl@0: GParameter *parameters) sl@0: { sl@0: GObjectConstructParam *cparams, *oparams; sl@0: GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */ sl@0: GObject *object; sl@0: GObjectClass *class, *unref_class = NULL; sl@0: GSList *slist; sl@0: guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues; sl@0: GValue *cvalues; sl@0: GList *clist = NULL; sl@0: gboolean newly_constructed; sl@0: guint i; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); sl@0: sl@0: class = g_type_class_peek_static (object_type); sl@0: if (!class) sl@0: class = unref_class = g_type_class_ref (object_type); sl@0: for (slist = class->construct_properties; slist; slist = slist->next) sl@0: { sl@0: clist = g_list_prepend (clist, slist->data); sl@0: n_total_cparams += 1; sl@0: } sl@0: sl@0: /* collect parameters, sort into construction and normal ones */ sl@0: oparams = g_new (GObjectConstructParam, n_parameters); sl@0: cparams = g_new (GObjectConstructParam, n_total_cparams); sl@0: for (i = 0; i < n_parameters; i++) sl@0: { sl@0: GValue *value = ¶meters[i].value; sl@0: GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: parameters[i].name, sl@0: object_type, sl@0: TRUE); sl@0: if (!pspec) sl@0: { sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: g_type_name (object_type), sl@0: parameters[i].name); sl@0: continue; sl@0: } sl@0: if (!(pspec->flags & G_PARAM_WRITABLE)) sl@0: { sl@0: g_warning ("%s: property `%s' of object class `%s' is not writable", sl@0: G_STRFUNC, sl@0: pspec->name, sl@0: g_type_name (object_type)); sl@0: continue; sl@0: } sl@0: if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) sl@0: { sl@0: GList *list = g_list_find (clist, pspec); sl@0: sl@0: if (!list) sl@0: { sl@0: g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice", sl@0: G_STRFUNC, pspec->name, g_type_name (object_type)); sl@0: continue; sl@0: } sl@0: cparams[n_cparams].pspec = pspec; sl@0: cparams[n_cparams].value = value; sl@0: n_cparams++; sl@0: if (!list->prev) sl@0: clist = list->next; sl@0: else sl@0: list->prev->next = list->next; sl@0: if (list->next) sl@0: list->next->prev = list->prev; sl@0: g_list_free_1 (list); sl@0: } sl@0: else sl@0: { sl@0: oparams[n_oparams].pspec = pspec; sl@0: oparams[n_oparams].value = value; sl@0: n_oparams++; sl@0: } sl@0: } sl@0: sl@0: /* set remaining construction properties to default values */ sl@0: n_cvalues = n_total_cparams - n_cparams; sl@0: cvalues = g_new (GValue, n_cvalues); sl@0: while (clist) sl@0: { sl@0: GList *tmp = clist->next; sl@0: GParamSpec *pspec = clist->data; sl@0: GValue *value = cvalues + n_total_cparams - n_cparams - 1; sl@0: sl@0: value->g_type = 0; sl@0: g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: g_param_value_set_default (pspec, value); sl@0: sl@0: cparams[n_cparams].pspec = pspec; sl@0: cparams[n_cparams].value = value; sl@0: n_cparams++; sl@0: sl@0: g_list_free_1 (clist); sl@0: clist = tmp; sl@0: } sl@0: sl@0: /* construct object from construction parameters */ sl@0: object = class->constructor (object_type, n_total_cparams, cparams); sl@0: /* free construction values */ sl@0: g_free (cparams); sl@0: while (n_cvalues--) sl@0: g_value_unset (cvalues + n_cvalues); sl@0: g_free (cvalues); sl@0: sl@0: /* adjust freeze_count according to g_object_init() and remaining properties */ sl@0: G_LOCK (construction_mutex); sl@0: newly_constructed = slist_maybe_remove (&construction_objects, object); sl@0: G_UNLOCK (construction_mutex); sl@0: if (newly_constructed || n_oparams) sl@0: nqueue = g_object_notify_queue_freeze (object, &property_notify_context); sl@0: if (newly_constructed) sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: sl@0: /* run 'constructed' handler if there is one */ sl@0: if (newly_constructed && class->constructed) sl@0: class->constructed (object); sl@0: sl@0: /* set remaining properties */ sl@0: for (i = 0; i < n_oparams; i++) sl@0: object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue); sl@0: g_free (oparams); sl@0: sl@0: /* release our own freeze count and handle notifications */ sl@0: if (newly_constructed || n_oparams) sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: sl@0: if (unref_class) sl@0: g_type_class_unref (unref_class); sl@0: sl@0: return object; sl@0: } sl@0: sl@0: /** sl@0: * g_object_new_valist: sl@0: * @object_type: the type id of the #GObject subtype to instantiate sl@0: * @first_property_name: the name of the first property sl@0: * @var_args: the value of the first property, followed optionally by more sl@0: * name/value pairs, followed by %NULL sl@0: * sl@0: * Creates a new instance of a #GObject subtype and sets its properties. sl@0: * sl@0: * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) sl@0: * which are not explicitly specified are set to their default values. sl@0: * sl@0: * Returns: a new instance of @object_type sl@0: */ sl@0: EXPORT_C GObject* sl@0: g_object_new_valist (GType object_type, sl@0: const gchar *first_property_name, sl@0: va_list var_args) sl@0: { sl@0: GObjectClass *class; sl@0: GParameter *params; sl@0: const gchar *name; sl@0: GObject *object; sl@0: guint n_params = 0, n_alloced_params = 16; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); sl@0: sl@0: if (!first_property_name) sl@0: return g_object_newv (object_type, 0, NULL); sl@0: sl@0: class = g_type_class_ref (object_type); sl@0: sl@0: params = g_new (GParameter, n_alloced_params); sl@0: name = first_property_name; sl@0: while (name) sl@0: { sl@0: gchar *error = NULL; sl@0: GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: name, sl@0: object_type, sl@0: TRUE); sl@0: if (!pspec) sl@0: { sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: g_type_name (object_type), sl@0: name); sl@0: break; sl@0: } sl@0: if (n_params >= n_alloced_params) sl@0: { sl@0: n_alloced_params += 16; sl@0: params = g_renew (GParameter, params, n_alloced_params); sl@0: } sl@0: params[n_params].name = name; sl@0: params[n_params].value.g_type = 0; sl@0: g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error); sl@0: if (error) sl@0: { sl@0: g_warning ("%s: %s", G_STRFUNC, error); sl@0: g_free (error); sl@0: g_value_unset (¶ms[n_params].value); sl@0: break; sl@0: } sl@0: n_params++; sl@0: name = va_arg (var_args, gchar*); sl@0: } sl@0: sl@0: object = g_object_newv (object_type, n_params, params); sl@0: sl@0: while (n_params--) sl@0: g_value_unset (¶ms[n_params].value); sl@0: g_free (params); sl@0: sl@0: g_type_class_unref (class); sl@0: sl@0: return object; sl@0: } sl@0: sl@0: static GObject* sl@0: g_object_constructor (GType type, sl@0: guint n_construct_properties, sl@0: GObjectConstructParam *construct_params) sl@0: { sl@0: GObject *object; sl@0: sl@0: /* create object */ sl@0: object = (GObject*) g_type_create_instance (type); sl@0: sl@0: /* set construction parameters */ sl@0: if (n_construct_properties) sl@0: { sl@0: GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context); sl@0: sl@0: /* set construct properties */ sl@0: while (n_construct_properties--) sl@0: { sl@0: GValue *value = construct_params->value; sl@0: GParamSpec *pspec = construct_params->pspec; sl@0: sl@0: construct_params++; sl@0: object_set_property (object, pspec, value, nqueue); sl@0: } sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: /* the notification queue is still frozen from g_object_init(), so sl@0: * we don't need to handle it here, g_object_newv() takes sl@0: * care of that sl@0: */ sl@0: } sl@0: sl@0: return object; sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_valist: sl@0: * @object: a #GObject sl@0: * @first_property_name: name of the first property to set sl@0: * @var_args: value for the first property, followed optionally by more sl@0: * name/value pairs, followed by %NULL sl@0: * sl@0: * Sets properties on an object. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_valist (GObject *object, sl@0: const gchar *first_property_name, sl@0: va_list var_args) sl@0: { sl@0: GObjectNotifyQueue *nqueue; sl@0: const gchar *name; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: sl@0: g_object_ref (object); sl@0: nqueue = g_object_notify_queue_freeze (object, &property_notify_context); sl@0: sl@0: name = first_property_name; sl@0: while (name) sl@0: { sl@0: GValue value = { 0, }; sl@0: GParamSpec *pspec; sl@0: gchar *error = NULL; sl@0: sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: name, sl@0: G_OBJECT_TYPE (object), sl@0: TRUE); sl@0: if (!pspec) sl@0: { sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: name); sl@0: break; sl@0: } sl@0: if (!(pspec->flags & G_PARAM_WRITABLE)) sl@0: { sl@0: g_warning ("%s: property `%s' of object class `%s' is not writable", sl@0: G_STRFUNC, sl@0: pspec->name, sl@0: G_OBJECT_TYPE_NAME (object)); sl@0: break; sl@0: } sl@0: if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object)) sl@0: { sl@0: g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction", sl@0: G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); sl@0: break; sl@0: } sl@0: sl@0: g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: sl@0: G_VALUE_COLLECT (&value, var_args, 0, &error); sl@0: if (error) sl@0: { sl@0: g_warning ("%s: %s", G_STRFUNC, error); sl@0: g_free (error); sl@0: g_value_unset (&value); sl@0: break; sl@0: } sl@0: sl@0: object_set_property (object, pspec, &value, nqueue); sl@0: g_value_unset (&value); sl@0: sl@0: name = va_arg (var_args, gchar*); sl@0: } sl@0: sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_get_valist: sl@0: * @object: a #GObject sl@0: * @first_property_name: name of the first property to get sl@0: * @var_args: return location for the first property, followed optionally by more sl@0: * name/return location pairs, followed by %NULL sl@0: * sl@0: * Gets properties of an object. sl@0: * sl@0: * In general, a copy is made of the property contents and the caller sl@0: * is responsible for freeing the memory in the appropriate manner for sl@0: * the type, for instance by calling g_free() or g_object_unref(). sl@0: * sl@0: * See g_object_get(). sl@0: */ sl@0: EXPORT_C void sl@0: g_object_get_valist (GObject *object, sl@0: const gchar *first_property_name, sl@0: va_list var_args) sl@0: { sl@0: const gchar *name; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: sl@0: g_object_ref (object); sl@0: sl@0: name = first_property_name; sl@0: sl@0: while (name) sl@0: { sl@0: GValue value = { 0, }; sl@0: GParamSpec *pspec; sl@0: gchar *error; sl@0: sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: name, sl@0: G_OBJECT_TYPE (object), sl@0: TRUE); sl@0: if (!pspec) sl@0: { sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: name); sl@0: break; sl@0: } sl@0: if (!(pspec->flags & G_PARAM_READABLE)) sl@0: { sl@0: g_warning ("%s: property `%s' of object class `%s' is not readable", sl@0: G_STRFUNC, sl@0: pspec->name, sl@0: G_OBJECT_TYPE_NAME (object)); sl@0: break; sl@0: } sl@0: sl@0: g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: sl@0: object_get_property (object, pspec, &value); sl@0: sl@0: G_VALUE_LCOPY (&value, var_args, 0, &error); sl@0: if (error) sl@0: { sl@0: g_warning ("%s: %s", G_STRFUNC, error); sl@0: g_free (error); sl@0: g_value_unset (&value); sl@0: break; sl@0: } sl@0: sl@0: g_value_unset (&value); sl@0: sl@0: name = va_arg (var_args, gchar*); sl@0: } sl@0: sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_set: sl@0: * @object: a #GObject sl@0: * @first_property_name: name of the first property to set sl@0: * @...: value for the first property, followed optionally by more sl@0: * name/value pairs, followed by %NULL sl@0: * sl@0: * Sets properties on an object. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set (gpointer _object, sl@0: const gchar *first_property_name, sl@0: ...) sl@0: { sl@0: GObject *object = _object; sl@0: va_list var_args; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: sl@0: va_start (var_args, first_property_name); sl@0: g_object_set_valist (object, first_property_name, var_args); sl@0: va_end (var_args); sl@0: } sl@0: sl@0: /** sl@0: * g_object_get: sl@0: * @object: a #GObject sl@0: * @first_property_name: name of the first property to get sl@0: * @...: return location for the first property, followed optionally by more sl@0: * name/return location pairs, followed by %NULL sl@0: * sl@0: * Gets properties of an object. sl@0: * sl@0: * In general, a copy is made of the property contents and the caller sl@0: * is responsible for freeing the memory in the appropriate manner for sl@0: * the type, for instance by calling g_free() or g_object_unref(). sl@0: * sl@0: * sl@0: * Using g_object_get(<!-- -->) sl@0: * An example of using g_object_get() to get the contents sl@0: * of three properties - one of type #G_TYPE_INT, sl@0: * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT: sl@0: * sl@0: * gint intval; sl@0: * gchar *strval; sl@0: * GObject *objval; sl@0: * sl@0: * g_object_get (my_object, sl@0: * "int-property", &intval, sl@0: * "str-property", &strval, sl@0: * "obj-property", &objval, sl@0: * NULL); sl@0: * sl@0: * // Do something with intval, strval, objval sl@0: * sl@0: * g_free (strval); sl@0: * g_object_unref (objval); sl@0: * sl@0: * sl@0: */ sl@0: EXPORT_C void sl@0: g_object_get (gpointer _object, sl@0: const gchar *first_property_name, sl@0: ...) sl@0: { sl@0: GObject *object = _object; sl@0: va_list var_args; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: sl@0: va_start (var_args, first_property_name); sl@0: g_object_get_valist (object, first_property_name, var_args); sl@0: va_end (var_args); sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_property: sl@0: * @object: a #GObject sl@0: * @property_name: the name of the property to set sl@0: * @value: the value sl@0: * sl@0: * Sets a property on an object. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_property (GObject *object, sl@0: const gchar *property_name, sl@0: const GValue *value) sl@0: { sl@0: GObjectNotifyQueue *nqueue; sl@0: GParamSpec *pspec; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (property_name != NULL); sl@0: g_return_if_fail (G_IS_VALUE (value)); sl@0: sl@0: g_object_ref (object); sl@0: nqueue = g_object_notify_queue_freeze (object, &property_notify_context); sl@0: sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: property_name, sl@0: G_OBJECT_TYPE (object), sl@0: TRUE); sl@0: if (!pspec) sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: property_name); sl@0: else if (!(pspec->flags & G_PARAM_WRITABLE)) sl@0: g_warning ("%s: property `%s' of object class `%s' is not writable", sl@0: G_STRFUNC, sl@0: pspec->name, sl@0: G_OBJECT_TYPE_NAME (object)); sl@0: else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object)) sl@0: g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction", sl@0: G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); sl@0: else sl@0: object_set_property (object, pspec, value, nqueue); sl@0: sl@0: g_object_notify_queue_thaw (object, nqueue); sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_get_property: sl@0: * @object: a #GObject sl@0: * @property_name: the name of the property to get sl@0: * @value: return location for the property value sl@0: * sl@0: * Gets a property of an object. sl@0: * sl@0: * In general, a copy is made of the property contents and the caller is sl@0: * responsible for freeing the memory by calling g_value_unset(). sl@0: * sl@0: * Note that g_object_get_property() is really intended for language sl@0: * bindings, g_object_get() is much more convenient for C programming. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_get_property (GObject *object, sl@0: const gchar *property_name, sl@0: GValue *value) sl@0: { sl@0: GParamSpec *pspec; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (property_name != NULL); sl@0: g_return_if_fail (G_IS_VALUE (value)); sl@0: sl@0: g_object_ref (object); sl@0: sl@0: pspec = g_param_spec_pool_lookup (pspec_pool, sl@0: property_name, sl@0: G_OBJECT_TYPE (object), sl@0: TRUE); sl@0: if (!pspec) sl@0: g_warning ("%s: object class `%s' has no property named `%s'", sl@0: G_STRFUNC, sl@0: G_OBJECT_TYPE_NAME (object), sl@0: property_name); sl@0: else if (!(pspec->flags & G_PARAM_READABLE)) sl@0: g_warning ("%s: property `%s' of object class `%s' is not readable", sl@0: G_STRFUNC, sl@0: pspec->name, sl@0: G_OBJECT_TYPE_NAME (object)); sl@0: else sl@0: { sl@0: GValue *prop_value, tmp_value = { 0, }; sl@0: sl@0: /* auto-conversion of the callers value type sl@0: */ sl@0: if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec)) sl@0: { sl@0: g_value_reset (value); sl@0: prop_value = value; sl@0: } sl@0: else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value))) sl@0: { sl@0: g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'", sl@0: G_STRFUNC, pspec->name, sl@0: g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)), sl@0: G_VALUE_TYPE_NAME (value)); sl@0: g_object_unref (object); sl@0: return; sl@0: } sl@0: else sl@0: { sl@0: g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); sl@0: prop_value = &tmp_value; sl@0: } sl@0: object_get_property (object, pspec, prop_value); sl@0: if (prop_value != value) sl@0: { sl@0: g_value_transform (prop_value, value); sl@0: g_value_unset (&tmp_value); sl@0: } sl@0: } sl@0: sl@0: g_object_unref (object); sl@0: } sl@0: sl@0: /** sl@0: * g_object_connect: sl@0: * @object: a #GObject sl@0: * @signal_spec: the spec for the first signal sl@0: * @...: #GCallback for the first signal, followed by data for the sl@0: * first signal, followed optionally by more signal sl@0: * spec/callback/data triples, followed by %NULL sl@0: * sl@0: * A convenience function to connect multiple signals at once. sl@0: * sl@0: * The signal specs expected by this function have the form sl@0: * "modifier::signal_name", where modifier can be one of the following: sl@0: * sl@0: * sl@0: * signal sl@0: * sl@0: * equivalent to g_signal_connect_data (..., NULL, 0) sl@0: * sl@0: * sl@0: * sl@0: * object_signal sl@0: * object-signal sl@0: * sl@0: * equivalent to g_signal_connect_object (..., 0) sl@0: * sl@0: * sl@0: * sl@0: * swapped_signal sl@0: * swapped-signal sl@0: * sl@0: * equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED) sl@0: * sl@0: * sl@0: * sl@0: * swapped_object_signal sl@0: * swapped-object-signal sl@0: * sl@0: * equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED) sl@0: * sl@0: * sl@0: * sl@0: * signal_after sl@0: * signal-after sl@0: * sl@0: * equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER) sl@0: * sl@0: * sl@0: * sl@0: * object_signal_after sl@0: * object-signal-after sl@0: * sl@0: * equivalent to g_signal_connect_object (..., G_CONNECT_AFTER) sl@0: * sl@0: * sl@0: * sl@0: * swapped_signal_after sl@0: * swapped-signal-after sl@0: * sl@0: * equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER) sl@0: * sl@0: * sl@0: * sl@0: * swapped_object_signal_after sl@0: * swapped-object-signal-after sl@0: * sl@0: * equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER) sl@0: * sl@0: * sl@0: * sl@0: * sl@0: * |[ sl@0: * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW, sl@0: * "type", GTK_WINDOW_POPUP, sl@0: * "child", menu, sl@0: * NULL), sl@0: * "signal::event", gtk_menu_window_event, menu, sl@0: * "signal::size_request", gtk_menu_window_size_request, menu, sl@0: * "signal::destroy", gtk_widget_destroyed, &menu->toplevel, sl@0: * NULL); sl@0: * ]| sl@0: * sl@0: * Returns: @object sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_connect (gpointer _object, sl@0: const gchar *signal_spec, sl@0: ...) sl@0: { sl@0: GObject *object = _object; sl@0: va_list var_args; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (object->ref_count > 0, object); sl@0: sl@0: va_start (var_args, signal_spec); sl@0: while (signal_spec) sl@0: { sl@0: GCallback callback = va_arg (var_args, GCallback); sl@0: gpointer data = va_arg (var_args, gpointer); sl@0: gulong sid; sl@0: sl@0: if (strncmp (signal_spec, "signal::", 8) == 0) sl@0: sid = g_signal_connect_data (object, signal_spec + 8, sl@0: callback, data, NULL, sl@0: 0); sl@0: else if (strncmp (signal_spec, "object_signal::", 15) == 0 || sl@0: strncmp (signal_spec, "object-signal::", 15) == 0) sl@0: sid = g_signal_connect_object (object, signal_spec + 15, sl@0: callback, data, sl@0: 0); sl@0: else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 || sl@0: strncmp (signal_spec, "swapped-signal::", 16) == 0) sl@0: sid = g_signal_connect_data (object, signal_spec + 16, sl@0: callback, data, NULL, sl@0: G_CONNECT_SWAPPED); sl@0: else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 || sl@0: strncmp (signal_spec, "swapped-object-signal::", 23) == 0) sl@0: sid = g_signal_connect_object (object, signal_spec + 23, sl@0: callback, data, sl@0: G_CONNECT_SWAPPED); sl@0: else if (strncmp (signal_spec, "signal_after::", 14) == 0 || sl@0: strncmp (signal_spec, "signal-after::", 14) == 0) sl@0: sid = g_signal_connect_data (object, signal_spec + 14, sl@0: callback, data, NULL, sl@0: G_CONNECT_AFTER); sl@0: else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 || sl@0: strncmp (signal_spec, "object-signal-after::", 21) == 0) sl@0: sid = g_signal_connect_object (object, signal_spec + 21, sl@0: callback, data, sl@0: G_CONNECT_AFTER); sl@0: else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 || sl@0: strncmp (signal_spec, "swapped-signal-after::", 22) == 0) sl@0: sid = g_signal_connect_data (object, signal_spec + 22, sl@0: callback, data, NULL, sl@0: G_CONNECT_SWAPPED | G_CONNECT_AFTER); sl@0: else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 || sl@0: strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0) sl@0: sid = g_signal_connect_object (object, signal_spec + 29, sl@0: callback, data, sl@0: G_CONNECT_SWAPPED | G_CONNECT_AFTER); sl@0: else sl@0: { sl@0: g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); sl@0: break; sl@0: } sl@0: signal_spec = va_arg (var_args, gchar*); sl@0: } sl@0: va_end (var_args); sl@0: sl@0: return object; sl@0: } sl@0: sl@0: /** sl@0: * g_object_disconnect: sl@0: * @object: a #GObject sl@0: * @signal_spec: the spec for the first signal sl@0: * @...: #GCallback for the first signal, followed by data for the first signal, sl@0: * followed optionally by more signal spec/callback/data triples, sl@0: * followed by %NULL sl@0: * sl@0: * A convenience function to disconnect multiple signals at once. sl@0: * sl@0: * The signal specs expected by this function have the form sl@0: * "any_signal", which means to disconnect any signal with matching sl@0: * callback and data, or "any_signal::signal_name", which only sl@0: * disconnects the signal named "signal_name". sl@0: */ sl@0: EXPORT_C void sl@0: g_object_disconnect (gpointer _object, sl@0: const gchar *signal_spec, sl@0: ...) sl@0: { sl@0: GObject *object = _object; sl@0: va_list var_args; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (object->ref_count > 0); sl@0: sl@0: va_start (var_args, signal_spec); sl@0: while (signal_spec) sl@0: { sl@0: GCallback callback = va_arg (var_args, GCallback); sl@0: gpointer data = va_arg (var_args, gpointer); sl@0: guint sid = 0, detail = 0, mask = 0; sl@0: sl@0: if (strncmp (signal_spec, "any_signal::", 12) == 0 || sl@0: strncmp (signal_spec, "any-signal::", 12) == 0) sl@0: { sl@0: signal_spec += 12; sl@0: mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; sl@0: } sl@0: else if (strcmp (signal_spec, "any_signal") == 0 || sl@0: strcmp (signal_spec, "any-signal") == 0) sl@0: { sl@0: signal_spec += 10; sl@0: mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; sl@0: } sl@0: else sl@0: { sl@0: g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); sl@0: break; sl@0: } sl@0: sl@0: if ((mask & G_SIGNAL_MATCH_ID) && sl@0: !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE)) sl@0: g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec); sl@0: else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0), sl@0: sid, detail, sl@0: NULL, (gpointer)callback, data)) sl@0: g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data); sl@0: signal_spec = va_arg (var_args, gchar*); sl@0: } sl@0: va_end (var_args); sl@0: } sl@0: sl@0: typedef struct { sl@0: GObject *object; sl@0: guint n_weak_refs; sl@0: struct { sl@0: GWeakNotify notify; sl@0: gpointer data; sl@0: } weak_refs[1]; /* flexible array */ sl@0: } WeakRefStack; sl@0: sl@0: static void sl@0: weak_refs_notify (gpointer data) sl@0: { sl@0: WeakRefStack *wstack = data; sl@0: guint i; sl@0: sl@0: for (i = 0; i < wstack->n_weak_refs; i++) sl@0: wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object); sl@0: g_free (wstack); sl@0: } sl@0: sl@0: /** sl@0: * g_object_weak_ref: sl@0: * @object: #GObject to reference weakly sl@0: * @notify: callback to invoke before the object is freed sl@0: * @data: extra data to pass to notify sl@0: * sl@0: * Adds a weak reference callback to an object. Weak references are sl@0: * used for notification when an object is finalized. They are called sl@0: * "weak references" because they allow you to safely hold a pointer sl@0: * to an object without calling g_object_ref() (g_object_ref() adds a sl@0: * strong reference, that is, forces the object to stay alive). sl@0: */ sl@0: EXPORT_C void sl@0: g_object_weak_ref (GObject *object, sl@0: GWeakNotify notify, sl@0: gpointer data) sl@0: { sl@0: WeakRefStack *wstack; sl@0: guint i; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (notify != NULL); sl@0: g_return_if_fail (object->ref_count >= 1); sl@0: sl@0: wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs); sl@0: if (wstack) sl@0: { sl@0: i = wstack->n_weak_refs++; sl@0: wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i); sl@0: } sl@0: else sl@0: { sl@0: wstack = g_renew (WeakRefStack, NULL, 1); sl@0: wstack->object = object; sl@0: wstack->n_weak_refs = 1; sl@0: i = 0; sl@0: } sl@0: wstack->weak_refs[i].notify = notify; sl@0: wstack->weak_refs[i].data = data; sl@0: g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify); sl@0: } sl@0: sl@0: /** sl@0: * g_object_weak_unref: sl@0: * @object: #GObject to remove a weak reference from sl@0: * @notify: callback to search for sl@0: * @data: data to search for sl@0: * sl@0: * Removes a weak reference callback to an object. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_weak_unref (GObject *object, sl@0: GWeakNotify notify, sl@0: gpointer data) sl@0: { sl@0: WeakRefStack *wstack; sl@0: gboolean found_one = FALSE; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (notify != NULL); sl@0: sl@0: wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs); sl@0: if (wstack) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < wstack->n_weak_refs; i++) sl@0: if (wstack->weak_refs[i].notify == notify && sl@0: wstack->weak_refs[i].data == data) sl@0: { sl@0: found_one = TRUE; sl@0: wstack->n_weak_refs -= 1; sl@0: if (i != wstack->n_weak_refs) sl@0: wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs]; sl@0: sl@0: break; sl@0: } sl@0: } sl@0: if (!found_one) sl@0: g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data); sl@0: } sl@0: sl@0: /** sl@0: * g_object_add_weak_pointer: sl@0: * @object: The object that should be weak referenced. sl@0: * @weak_pointer_location: The memory address of a pointer. sl@0: * sl@0: * Adds a weak reference from weak_pointer to @object to indicate that sl@0: * the pointer located at @weak_pointer_location is only valid during sl@0: * the lifetime of @object. When the @object is finalized, sl@0: * @weak_pointer will be set to %NULL. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_add_weak_pointer (GObject *object, sl@0: gpointer *weak_pointer_location) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (weak_pointer_location != NULL); sl@0: sl@0: g_object_weak_ref (object, sl@0: (GWeakNotify) g_nullify_pointer, sl@0: weak_pointer_location); sl@0: } sl@0: sl@0: /** sl@0: * g_object_remove_weak_pointer: sl@0: * @object: The object that is weak referenced. sl@0: * @weak_pointer_location: The memory address of a pointer. sl@0: * sl@0: * Removes a weak reference from @object that was previously added sl@0: * using g_object_add_weak_pointer(). The @weak_pointer_location has sl@0: * to match the one used with g_object_add_weak_pointer(). sl@0: */ sl@0: EXPORT_C void sl@0: g_object_remove_weak_pointer (GObject *object, sl@0: gpointer *weak_pointer_location) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (weak_pointer_location != NULL); sl@0: sl@0: g_object_weak_unref (object, sl@0: (GWeakNotify) g_nullify_pointer, sl@0: weak_pointer_location); sl@0: } sl@0: sl@0: #if EMULATOR sl@0: guint sl@0: object_floating_flag_handler (GObject *object, sl@0: gint job) sl@0: #else sl@0: static guint sl@0: object_floating_flag_handler (GObject *object, sl@0: gint job) sl@0: #endif /* EMULATOR */ sl@0: { sl@0: switch (job) sl@0: { sl@0: gpointer oldvalue; sl@0: case +1: /* force floating if possible */ sl@0: do sl@0: oldvalue = g_atomic_pointer_get (&object->qdata); sl@0: while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, sl@0: (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG))); sl@0: return (gsize) oldvalue & OBJECT_FLOATING_FLAG; sl@0: case -1: /* sink if possible */ sl@0: do sl@0: oldvalue = g_atomic_pointer_get (&object->qdata); sl@0: while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, sl@0: (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG))); sl@0: return (gsize) oldvalue & OBJECT_FLOATING_FLAG; sl@0: default: /* check floating */ sl@0: return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * g_object_is_floating: sl@0: * @object: a #GObject sl@0: * sl@0: * Checks wether @object has a floating sl@0: * reference. sl@0: * sl@0: * Since: 2.10 sl@0: * sl@0: * Returns: %TRUE if @object has a floating reference sl@0: */ sl@0: EXPORT_C gboolean sl@0: g_object_is_floating (gpointer _object) sl@0: { sl@0: GObject *object = _object; sl@0: g_return_val_if_fail (G_IS_OBJECT (object), FALSE); sl@0: return floating_flag_handler (object, 0); sl@0: } sl@0: sl@0: /** sl@0: * g_object_ref_sink: sl@0: * @object: a #GObject sl@0: * sl@0: * Increase the reference count of @object, and possibly remove the sl@0: * floating reference, if @object sl@0: * has a floating reference. sl@0: * sl@0: * In other words, if the object is floating, then this call "assumes sl@0: * ownership" of the floating reference, converting it to a normal sl@0: * reference by clearing the floating flag while leaving the reference sl@0: * count unchanged. If the object is not floating, then this call sl@0: * adds a new normal reference increasing the reference count by one. sl@0: * sl@0: * Since: 2.10 sl@0: * sl@0: * Returns: @object sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_ref_sink (gpointer _object) sl@0: { sl@0: GObject *object = _object; sl@0: gboolean was_floating; sl@0: g_return_val_if_fail (G_IS_OBJECT (object), object); sl@0: g_return_val_if_fail (object->ref_count >= 1, object); sl@0: g_object_ref (object); sl@0: was_floating = floating_flag_handler (object, -1); sl@0: if (was_floating) sl@0: g_object_unref (object); sl@0: return object; sl@0: } sl@0: sl@0: /** sl@0: * g_object_force_floating: sl@0: * @object: a #GObject sl@0: * sl@0: * This function is intended for #GObject implementations to re-enforce a sl@0: * floating object reference. sl@0: * Doing this is seldomly required, all sl@0: * #GInitiallyUnowneds are created with a floating reference which sl@0: * usually just needs to be sunken by calling g_object_ref_sink(). sl@0: * sl@0: * Since: 2.10 sl@0: */ sl@0: EXPORT_C void sl@0: g_object_force_floating (GObject *object) sl@0: { sl@0: gboolean was_floating; sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (object->ref_count >= 1); sl@0: sl@0: was_floating = floating_flag_handler (object, +1); sl@0: } sl@0: sl@0: typedef struct { sl@0: GObject *object; sl@0: guint n_toggle_refs; sl@0: struct { sl@0: GToggleNotify notify; sl@0: gpointer data; sl@0: } toggle_refs[1]; /* flexible array */ sl@0: } ToggleRefStack; sl@0: sl@0: static void sl@0: toggle_refs_notify (GObject *object, sl@0: gboolean is_last_ref) sl@0: { sl@0: ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); sl@0: sl@0: /* Reentrancy here is not as tricky as it seems, because a toggle reference sl@0: * will only be notified when there is exactly one of them. sl@0: */ sl@0: g_assert (tstack->n_toggle_refs == 1); sl@0: tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref); sl@0: } sl@0: sl@0: /** sl@0: * g_object_add_toggle_ref: sl@0: * @object: a #GObject sl@0: * @notify: a function to call when this reference is the sl@0: * last reference to the object, or is no longer sl@0: * the last reference. sl@0: * @data: data to pass to @notify sl@0: * sl@0: * Increases the reference count of the object by one and sets a sl@0: * callback to be called when all other references to the object are sl@0: * dropped, or when this is already the last reference to the object sl@0: * and another reference is established. sl@0: * sl@0: * This functionality is intended for binding @object to a proxy sl@0: * object managed by another memory manager. This is done with two sl@0: * paired references: the strong reference added by sl@0: * g_object_add_toggle_ref() and a reverse reference to the proxy sl@0: * object which is either a strong reference or weak reference. sl@0: * sl@0: * The setup is that when there are no other references to @object, sl@0: * only a weak reference is held in the reverse direction from @object sl@0: * to the proxy object, but when there are other references held to sl@0: * @object, a strong reference is held. The @notify callback is called sl@0: * when the reference from @object to the proxy object should be sl@0: * toggled from strong to weak (@is_last_ref sl@0: * true) or weak to strong (@is_last_ref false). sl@0: * sl@0: * Since a (normal) reference must be held to the object before sl@0: * calling g_object_toggle_ref(), the initial state of the reverse sl@0: * link is always strong. sl@0: * sl@0: * Multiple toggle references may be added to the same gobject, sl@0: * however if there are multiple toggle references to an object, none sl@0: * of them will ever be notified until all but one are removed. For sl@0: * this reason, you should only ever use a toggle reference if there sl@0: * is important state in the proxy object. sl@0: * sl@0: * Since: 2.8 sl@0: */ sl@0: EXPORT_C void sl@0: g_object_add_toggle_ref (GObject *object, sl@0: GToggleNotify notify, sl@0: gpointer data) sl@0: { sl@0: ToggleRefStack *tstack; sl@0: guint i; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (notify != NULL); sl@0: g_return_if_fail (object->ref_count >= 1); sl@0: sl@0: g_object_ref (object); sl@0: sl@0: tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs); sl@0: if (tstack) sl@0: { sl@0: i = tstack->n_toggle_refs++; sl@0: /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared sl@0: * in tstate->toggle_refs */ sl@0: tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i); sl@0: } sl@0: else sl@0: { sl@0: tstack = g_renew (ToggleRefStack, NULL, 1); sl@0: tstack->object = object; sl@0: tstack->n_toggle_refs = 1; sl@0: i = 0; sl@0: } sl@0: sl@0: /* Set a flag for fast lookup after adding the first toggle reference */ sl@0: if (tstack->n_toggle_refs == 1) sl@0: g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); sl@0: sl@0: tstack->toggle_refs[i].notify = notify; sl@0: tstack->toggle_refs[i].data = data; sl@0: g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack, sl@0: (GDestroyNotify)g_free); sl@0: } sl@0: sl@0: /** sl@0: * g_object_remove_toggle_ref: sl@0: * @object: a #GObject sl@0: * @notify: a function to call when this reference is the sl@0: * last reference to the object, or is no longer sl@0: * the last reference. sl@0: * @data: data to pass to @notify sl@0: * sl@0: * Removes a reference added with g_object_add_toggle_ref(). The sl@0: * reference count of the object is decreased by one. sl@0: * sl@0: * Since: 2.8 sl@0: */ sl@0: EXPORT_C void sl@0: g_object_remove_toggle_ref (GObject *object, sl@0: GToggleNotify notify, sl@0: gpointer data) sl@0: { sl@0: ToggleRefStack *tstack; sl@0: gboolean found_one = FALSE; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (notify != NULL); sl@0: sl@0: tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); sl@0: if (tstack) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < tstack->n_toggle_refs; i++) sl@0: if (tstack->toggle_refs[i].notify == notify && sl@0: tstack->toggle_refs[i].data == data) sl@0: { sl@0: found_one = TRUE; sl@0: tstack->n_toggle_refs -= 1; sl@0: if (i != tstack->n_toggle_refs) sl@0: tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs]; sl@0: sl@0: if (tstack->n_toggle_refs == 0) sl@0: g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); sl@0: sl@0: g_object_unref (object); sl@0: sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (!found_one) sl@0: g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data); sl@0: } sl@0: sl@0: /** sl@0: * g_object_ref: sl@0: * @object: a #GObject sl@0: * sl@0: * Increases the reference count of @object. sl@0: * sl@0: * Returns: the same @object sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_ref (gpointer _object) sl@0: { sl@0: GObject *object = _object; sl@0: gint old_val; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (object->ref_count > 0, NULL); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: if (g_trap_object_ref == object) sl@0: G_BREAKPOINT (); sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: sl@0: old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1); sl@0: sl@0: if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object)) sl@0: toggle_refs_notify (object, FALSE); sl@0: sl@0: return object; sl@0: } sl@0: sl@0: /** sl@0: * g_object_unref: sl@0: * @object: a #GObject sl@0: * sl@0: * Decreases the reference count of @object. When its reference count sl@0: * drops to 0, the object is finalized (i.e. its memory is freed). sl@0: */ sl@0: EXPORT_C void sl@0: g_object_unref (gpointer _object) sl@0: { sl@0: GObject *object = _object; sl@0: gint old_ref; sl@0: gboolean is_zero; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (object->ref_count > 0); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: if (g_trap_object_ref == object) sl@0: G_BREAKPOINT (); sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */ sl@0: retry_atomic_decrement1: sl@0: old_ref = g_atomic_int_get (&object->ref_count); sl@0: if (old_ref > 1) sl@0: { sl@0: if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1)) sl@0: goto retry_atomic_decrement1; sl@0: sl@0: /* if we went from 2->1 we need to notify toggle refs if any */ sl@0: if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object)) sl@0: toggle_refs_notify (object, TRUE); sl@0: } sl@0: else sl@0: { sl@0: /* we are about tp remove the last reference */ sl@0: G_OBJECT_GET_CLASS (object)->dispose (object); sl@0: sl@0: /* may have been re-referenced meanwhile */ sl@0: retry_atomic_decrement2: sl@0: old_ref = g_atomic_int_get ((int *)&object->ref_count); sl@0: if (old_ref > 1) sl@0: { sl@0: if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1)) sl@0: goto retry_atomic_decrement2; sl@0: sl@0: /* if we went from 2->1 we need to notify toggle refs if any */ sl@0: if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object)) sl@0: toggle_refs_notify (object, TRUE); sl@0: sl@0: return; sl@0: } sl@0: sl@0: /* we are still in the process of taking away the last ref */ sl@0: g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); sl@0: g_signal_handlers_destroy (object); sl@0: g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); sl@0: sl@0: /* decrement the last reference */ sl@0: is_zero = g_atomic_int_dec_and_test ((int *)&object->ref_count); sl@0: sl@0: /* may have been re-referenced meanwhile */ sl@0: if (G_LIKELY (is_zero)) sl@0: { sl@0: G_OBJECT_GET_CLASS (object)->finalize (object); sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (OBJECTS) sl@0: { sl@0: /* catch objects not chaining finalize handlers */ sl@0: G_LOCK (debug_objects); sl@0: g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL); sl@0: G_UNLOCK (debug_objects); sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: g_type_free_instance ((GTypeInstance*) object); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * g_object_get_qdata: sl@0: * @object: The GObject to get a stored user data pointer from sl@0: * @quark: A #GQuark, naming the user data pointer sl@0: * sl@0: * This function gets back user data pointers stored via sl@0: * g_object_set_qdata(). sl@0: * sl@0: * Returns: The user data pointer set, or %NULL sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_get_qdata (GObject *object, sl@0: GQuark quark) sl@0: { sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: sl@0: return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_qdata: sl@0: * @object: The GObject to set store a user data pointer sl@0: * @quark: A #GQuark, naming the user data pointer sl@0: * @data: An opaque user data pointer sl@0: * sl@0: * This sets an opaque, named pointer on an object. sl@0: * The name is specified through a #GQuark (retrived e.g. via sl@0: * g_quark_from_static_string()), and the pointer sl@0: * can be gotten back from the @object with g_object_get_qdata() sl@0: * until the @object is finalized. sl@0: * Setting a previously set user data pointer, overrides (frees) sl@0: * the old pointer set, using #NULL as pointer essentially sl@0: * removes the data stored. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_qdata (GObject *object, sl@0: GQuark quark, sl@0: gpointer data) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (quark > 0); sl@0: sl@0: g_datalist_id_set_data (&object->qdata, quark, data); sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_qdata_full: sl@0: * @object: The GObject to set store a user data pointer sl@0: * @quark: A #GQuark, naming the user data pointer sl@0: * @data: An opaque user data pointer sl@0: * @destroy: Function to invoke with @data as argument, when @data sl@0: * needs to be freed sl@0: * sl@0: * This function works like g_object_set_qdata(), but in addition, sl@0: * a void (*destroy) (gpointer) function may be specified which is sl@0: * called with @data as argument when the @object is finalized, or sl@0: * the data is being overwritten by a call to g_object_set_qdata() sl@0: * with the same @quark. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_qdata_full (GObject *object, sl@0: GQuark quark, sl@0: gpointer data, sl@0: GDestroyNotify destroy) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (quark > 0); sl@0: sl@0: g_datalist_id_set_data_full (&object->qdata, quark, data, sl@0: data ? destroy : (GDestroyNotify) NULL); sl@0: } sl@0: sl@0: /** sl@0: * g_object_steal_qdata: sl@0: * @object: The GObject to get a stored user data pointer from sl@0: * @quark: A #GQuark, naming the user data pointer sl@0: * sl@0: * This function gets back user data pointers stored via sl@0: * g_object_set_qdata() and removes the @data from object sl@0: * without invoking its destroy() function (if any was sl@0: * set). sl@0: * Usually, calling this function is only required to update sl@0: * user data pointers with a destroy notifier, for example: sl@0: * |[ sl@0: * void sl@0: * object_add_to_user_list (GObject *object, sl@0: * const gchar *new_string) sl@0: * { sl@0: * // the quark, naming the object data sl@0: * GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); sl@0: * // retrive the old string list sl@0: * GList *list = g_object_steal_qdata (object, quark_string_list); sl@0: * sl@0: * // prepend new string sl@0: * list = g_list_prepend (list, g_strdup (new_string)); sl@0: * // this changed 'list', so we need to set it again sl@0: * g_object_set_qdata_full (object, quark_string_list, list, free_string_list); sl@0: * } sl@0: * static void sl@0: * free_string_list (gpointer data) sl@0: * { sl@0: * GList *node, *list = data; sl@0: * sl@0: * for (node = list; node; node = node->next) sl@0: * g_free (node->data); sl@0: * g_list_free (list); sl@0: * } sl@0: * ]| sl@0: * Using g_object_get_qdata() in the above example, instead of sl@0: * g_object_steal_qdata() would have left the destroy function set, sl@0: * and thus the partial string list would have been freed upon sl@0: * g_object_set_qdata_full(). sl@0: * sl@0: * Returns: The user data pointer set, or %NULL sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_steal_qdata (GObject *object, sl@0: GQuark quark) sl@0: { sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (quark > 0, NULL); sl@0: sl@0: return g_datalist_id_remove_no_notify (&object->qdata, quark); sl@0: } sl@0: sl@0: /** sl@0: * g_object_get_data: sl@0: * @object: #GObject containing the associations sl@0: * @key: name of the key for that association sl@0: * sl@0: * Gets a named field from the objects table of associations (see g_object_set_data()). sl@0: * sl@0: * Returns: the data if found, or %NULL if no such data exists. sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_get_data (GObject *object, sl@0: const gchar *key) sl@0: { sl@0: GQuark quark; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (key != NULL, NULL); sl@0: sl@0: quark = g_quark_try_string (key); sl@0: sl@0: return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_data: sl@0: * @object: #GObject containing the associations. sl@0: * @key: name of the key sl@0: * @data: data to associate with that key sl@0: * sl@0: * Each object carries around a table of associations from sl@0: * strings to pointers. This function lets you set an association. sl@0: * sl@0: * If the object already had an association with that name, sl@0: * the old association will be destroyed. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_data (GObject *object, sl@0: const gchar *key, sl@0: gpointer data) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (key != NULL); sl@0: sl@0: g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data); sl@0: } sl@0: sl@0: /** sl@0: * g_object_set_data_full: sl@0: * @object: #GObject containing the associations sl@0: * @key: name of the key sl@0: * @data: data to associate with that key sl@0: * @destroy: function to call when the association is destroyed sl@0: * sl@0: * Like g_object_set_data() except it adds notification sl@0: * for when the association is destroyed, either by setting it sl@0: * to a different value or when the object is destroyed. sl@0: * sl@0: * Note that the @destroy callback is not called if @data is %NULL. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_set_data_full (GObject *object, sl@0: const gchar *key, sl@0: gpointer data, sl@0: GDestroyNotify destroy) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (key != NULL); sl@0: sl@0: g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data, sl@0: data ? destroy : (GDestroyNotify) NULL); sl@0: } sl@0: sl@0: /** sl@0: * g_object_steal_data: sl@0: * @object: #GObject containing the associations sl@0: * @key: name of the key sl@0: * sl@0: * Remove a specified datum from the object's data associations, sl@0: * without invoking the association's destroy handler. sl@0: * sl@0: * Returns: the data if found, or %NULL if no such data exists. sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_object_steal_data (GObject *object, sl@0: const gchar *key) sl@0: { sl@0: GQuark quark; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (key != NULL, NULL); sl@0: sl@0: quark = g_quark_try_string (key); sl@0: sl@0: return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL; sl@0: } sl@0: sl@0: static void sl@0: g_value_object_init (GValue *value) sl@0: { sl@0: value->data[0].v_pointer = NULL; sl@0: } sl@0: sl@0: static void sl@0: g_value_object_free_value (GValue *value) sl@0: { sl@0: if (value->data[0].v_pointer) sl@0: g_object_unref (value->data[0].v_pointer); sl@0: } sl@0: sl@0: static void sl@0: g_value_object_copy_value (const GValue *src_value, sl@0: GValue *dest_value) sl@0: { sl@0: if (src_value->data[0].v_pointer) sl@0: dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer); sl@0: else sl@0: dest_value->data[0].v_pointer = NULL; sl@0: } sl@0: sl@0: static void sl@0: g_value_object_transform_value (const GValue *src_value, sl@0: GValue *dest_value) sl@0: { sl@0: 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))) sl@0: dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer); sl@0: else sl@0: dest_value->data[0].v_pointer = NULL; sl@0: } sl@0: sl@0: static gpointer sl@0: g_value_object_peek_pointer (const GValue *value) sl@0: { sl@0: return value->data[0].v_pointer; sl@0: } sl@0: sl@0: static gchar* sl@0: g_value_object_collect_value (GValue *value, sl@0: guint n_collect_values, sl@0: GTypeCValue *collect_values, sl@0: guint collect_flags) sl@0: { sl@0: if (collect_values[0].v_pointer) sl@0: { sl@0: GObject *object = collect_values[0].v_pointer; sl@0: sl@0: if (object->g_type_instance.g_class == NULL) sl@0: return g_strconcat ("invalid unclassed object pointer for value type `", sl@0: G_VALUE_TYPE_NAME (value), sl@0: "'", sl@0: NULL); sl@0: else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value))) sl@0: return g_strconcat ("invalid object type `", sl@0: G_OBJECT_TYPE_NAME (object), sl@0: "' for value type `", sl@0: G_VALUE_TYPE_NAME (value), sl@0: "'", sl@0: NULL); sl@0: /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */ sl@0: value->data[0].v_pointer = g_object_ref (object); sl@0: } sl@0: else sl@0: value->data[0].v_pointer = NULL; sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: static gchar* sl@0: g_value_object_lcopy_value (const GValue *value, sl@0: guint n_collect_values, sl@0: GTypeCValue *collect_values, sl@0: guint collect_flags) sl@0: { sl@0: GObject **object_p = collect_values[0].v_pointer; sl@0: sl@0: if (!object_p) sl@0: return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value)); sl@0: sl@0: if (!value->data[0].v_pointer) sl@0: *object_p = NULL; sl@0: else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) sl@0: *object_p = value->data[0].v_pointer; sl@0: else sl@0: *object_p = g_object_ref (value->data[0].v_pointer); sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_value_set_object: sl@0: * @value: a valid #GValue of %G_TYPE_OBJECT derived type sl@0: * @v_object: object value to be set sl@0: * sl@0: * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object. sl@0: * sl@0: * g_value_set_object() increases the reference count of @v_object sl@0: * (the #GValue holds a reference to @v_object). If you do not wish sl@0: * to increase the reference count of the object (i.e. you wish to sl@0: * pass your current reference to the #GValue because you no longer sl@0: * need it), use g_value_take_object() instead. sl@0: * sl@0: * It is important that your #GValue holds a reference to @v_object (either its sl@0: * own, or one it has taken) to ensure that the object won't be destroyed while sl@0: * the #GValue still exists). sl@0: */ sl@0: EXPORT_C void sl@0: g_value_set_object (GValue *value, sl@0: gpointer v_object) sl@0: { sl@0: GObject *old; sl@0: sl@0: g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); sl@0: sl@0: old = value->data[0].v_pointer; sl@0: sl@0: if (v_object) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (v_object)); sl@0: g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); sl@0: sl@0: value->data[0].v_pointer = v_object; sl@0: g_object_ref (value->data[0].v_pointer); sl@0: } sl@0: else sl@0: value->data[0].v_pointer = NULL; sl@0: sl@0: if (old) sl@0: g_object_unref (old); sl@0: } sl@0: sl@0: /** sl@0: * g_value_set_object_take_ownership: sl@0: * @value: a valid #GValue of %G_TYPE_OBJECT derived type sl@0: * @v_object: object value to be set sl@0: * sl@0: * This is an internal function introduced mainly for C marshallers. sl@0: * sl@0: * Deprecated: 2.4: Use g_value_take_object() instead. sl@0: */ sl@0: EXPORT_C void sl@0: g_value_set_object_take_ownership (GValue *value, sl@0: gpointer v_object) sl@0: { sl@0: g_value_take_object (value, v_object); sl@0: } sl@0: sl@0: /** sl@0: * g_value_take_object: sl@0: * @value: a valid #GValue of %G_TYPE_OBJECT derived type sl@0: * @v_object: object value to be set sl@0: * sl@0: * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object sl@0: * and takes over the ownership of the callers reference to @v_object; sl@0: * the caller doesn't have to unref it any more (i.e. the reference sl@0: * count of the object is not increased). sl@0: * sl@0: * If you want the #GValue to hold its own reference to @v_object, use sl@0: * g_value_set_object() instead. sl@0: * sl@0: * Since: 2.4 sl@0: */ sl@0: EXPORT_C void sl@0: g_value_take_object (GValue *value, sl@0: gpointer v_object) sl@0: { sl@0: g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); sl@0: sl@0: if (value->data[0].v_pointer) sl@0: { sl@0: g_object_unref (value->data[0].v_pointer); sl@0: value->data[0].v_pointer = NULL; sl@0: } sl@0: sl@0: if (v_object) sl@0: { sl@0: g_return_if_fail (G_IS_OBJECT (v_object)); sl@0: g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); sl@0: sl@0: value->data[0].v_pointer = v_object; /* we take over the reference count */ sl@0: } sl@0: } sl@0: sl@0: /** sl@0: * g_value_get_object: sl@0: * @value: a valid #GValue of %G_TYPE_OBJECT derived type sl@0: * sl@0: * Get the contents of a %G_TYPE_OBJECT derived #GValue. sl@0: * sl@0: * Returns: object contents of @value sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_value_get_object (const GValue *value) sl@0: { sl@0: g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); sl@0: sl@0: return value->data[0].v_pointer; sl@0: } sl@0: sl@0: /** sl@0: * g_value_dup_object: sl@0: * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT sl@0: * sl@0: * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing sl@0: * its reference count. sl@0: * sl@0: * Returns: object content of @value, should be unreferenced when no sl@0: * longer needed. sl@0: */ sl@0: EXPORT_C gpointer sl@0: g_value_dup_object (const GValue *value) sl@0: { sl@0: g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); sl@0: sl@0: return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_connect_object: sl@0: * @instance: the instance to connect to. sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * @c_handler: the #GCallback to connect. sl@0: * @gobject: the object to pass as data to @c_handler. sl@0: * @connect_flags: a combination of #GConnnectFlags. sl@0: * sl@0: * This is similar to g_signal_connect_data(), but uses a closure which sl@0: * ensures that the @gobject stays alive during the call to @c_handler sl@0: * by temporarily adding a reference count to @gobject. sl@0: * sl@0: * Note that there is a bug in GObject that makes this function sl@0: * much less useful than it might seem otherwise. Once @gobject is sl@0: * disposed, the callback will no longer be called, but, the signal sl@0: * handler is not currently disconnected. If the sl@0: * @instance is itself being freed at the same time than this doesn't sl@0: * matter, since the signal will automatically be removed, but sl@0: * if @instance persists, then the signal handler will leak. You sl@0: * should not remove the signal yourself because in a future versions of sl@0: * GObject, the handler will automatically sl@0: * be disconnected. sl@0: * sl@0: * It's possible to work around this problem in a way that will sl@0: * continue to work with future versions of GObject by checking sl@0: * that the signal handler is still connected before disconnected it: sl@0: * sl@0: * if (g_signal_handler_is_connected (instance, id)) sl@0: * g_signal_handler_disconnect (instance, id); sl@0: * sl@0: * sl@0: * Returns: the handler id. sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_connect_object (gpointer instance, sl@0: const gchar *detailed_signal, sl@0: GCallback c_handler, sl@0: gpointer gobject, sl@0: GConnectFlags connect_flags) sl@0: { sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail (detailed_signal != NULL, 0); sl@0: g_return_val_if_fail (c_handler != NULL, 0); sl@0: sl@0: if (gobject) sl@0: { sl@0: GClosure *closure; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (gobject), 0); sl@0: sl@0: closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject); sl@0: sl@0: return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER); sl@0: } sl@0: else sl@0: return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags); sl@0: } sl@0: sl@0: typedef struct { sl@0: GObject *object; sl@0: guint n_closures; sl@0: GClosure *closures[1]; /* flexible array */ sl@0: } CArray; sl@0: /* don't change this structure without supplying an accessor for sl@0: * watched closures, e.g.: sl@0: * GSList* g_object_list_watched_closures (GObject *object) sl@0: * { sl@0: * CArray *carray; sl@0: * g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: * carray = g_object_get_data (object, "GObject-closure-array"); sl@0: * if (carray) sl@0: * { sl@0: * GSList *slist = NULL; sl@0: * guint i; sl@0: * for (i = 0; i < carray->n_closures; i++) sl@0: * slist = g_slist_prepend (slist, carray->closures[i]); sl@0: * return slist; sl@0: * } sl@0: * return NULL; sl@0: * } sl@0: */ sl@0: sl@0: static void sl@0: object_remove_closure (gpointer data, sl@0: GClosure *closure) sl@0: { sl@0: GObject *object = data; sl@0: CArray *carray = g_object_get_qdata (object, quark_closure_array); sl@0: guint i; sl@0: sl@0: for (i = 0; i < carray->n_closures; i++) sl@0: if (carray->closures[i] == closure) sl@0: { sl@0: carray->n_closures--; sl@0: if (i < carray->n_closures) sl@0: carray->closures[i] = carray->closures[carray->n_closures]; sl@0: return; sl@0: } sl@0: g_assert_not_reached (); sl@0: } sl@0: sl@0: static void sl@0: destroy_closure_array (gpointer data) sl@0: { sl@0: CArray *carray = data; sl@0: GObject *object = carray->object; sl@0: guint i, n = carray->n_closures; sl@0: sl@0: for (i = 0; i < n; i++) sl@0: { sl@0: GClosure *closure = carray->closures[i]; sl@0: sl@0: /* removing object_remove_closure() upfront is probably faster than sl@0: * letting it fiddle with quark_closure_array which is empty anyways sl@0: */ sl@0: g_closure_remove_invalidate_notifier (closure, object, object_remove_closure); sl@0: g_closure_invalidate (closure); sl@0: } sl@0: g_free (carray); sl@0: } sl@0: sl@0: /** sl@0: * g_object_watch_closure: sl@0: * @object: GObject restricting lifetime of @closure sl@0: * @closure: GClosure to watch sl@0: * sl@0: * This function essentially limits the life time of the @closure to sl@0: * the life time of the object. That is, when the object is finalized, sl@0: * the @closure is invalidated by calling g_closure_invalidate() on sl@0: * it, in order to prevent invocations of the closure with a finalized sl@0: * (nonexisting) object. Also, g_object_ref() and g_object_unref() are sl@0: * added as marshal guards to the @closure, to ensure that an extra sl@0: * reference count is held on @object during invocation of the sl@0: * @closure. Usually, this function will be called on closures that sl@0: * use this @object as closure data. sl@0: */ sl@0: EXPORT_C void sl@0: g_object_watch_closure (GObject *object, sl@0: GClosure *closure) sl@0: { sl@0: CArray *carray; sl@0: guint i; sl@0: sl@0: g_return_if_fail (G_IS_OBJECT (object)); sl@0: g_return_if_fail (closure != NULL); sl@0: g_return_if_fail (closure->is_invalid == FALSE); sl@0: g_return_if_fail (closure->in_marshal == FALSE); sl@0: g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */ sl@0: sl@0: g_closure_add_invalidate_notifier (closure, object, object_remove_closure); sl@0: g_closure_add_marshal_guards (closure, sl@0: object, (GClosureNotify) g_object_ref, sl@0: object, (GClosureNotify) g_object_unref); sl@0: carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array); sl@0: if (!carray) sl@0: { sl@0: carray = g_renew (CArray, NULL, 1); sl@0: carray->object = object; sl@0: carray->n_closures = 1; sl@0: i = 0; sl@0: } sl@0: else sl@0: { sl@0: i = carray->n_closures++; sl@0: carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i); sl@0: } sl@0: carray->closures[i] = closure; sl@0: g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array); sl@0: } sl@0: sl@0: /** sl@0: * g_closure_new_object: sl@0: * @sizeof_closure: the size of the structure to allocate, must be at least sl@0: * sizeof (GClosure) sl@0: * @object: a #GObject pointer to store in the @data field of the newly sl@0: * allocated #GClosure sl@0: * sl@0: * A variant of g_closure_new_simple() which stores @object in the sl@0: * @data field of the closure and calls g_object_watch_closure() on sl@0: * @object and the created closure. This function is mainly useful sl@0: * when implementing new types of closures. sl@0: * sl@0: * Returns: a newly allocated #GClosure sl@0: */ sl@0: EXPORT_C GClosure* sl@0: g_closure_new_object (guint sizeof_closure, sl@0: GObject *object) sl@0: { sl@0: GClosure *closure; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ sl@0: sl@0: closure = g_closure_new_simple (sizeof_closure, object); sl@0: g_object_watch_closure (object, closure); sl@0: sl@0: return closure; sl@0: } sl@0: sl@0: /** sl@0: * g_cclosure_new_object: sl@0: * @callback_func: the function to invoke sl@0: * @object: a #GObject pointer to pass to @callback_func sl@0: * sl@0: * A variant of g_cclosure_new() which uses @object as @user_data and sl@0: * calls g_object_watch_closure() on @object and the created sl@0: * closure. This function is useful when you have a callback closely sl@0: * associated with a #GObject, and want the callback to no longer run sl@0: * after the object is is freed. sl@0: * sl@0: * Returns: a new #GCClosure sl@0: */ sl@0: EXPORT_C GClosure* sl@0: g_cclosure_new_object (GCallback callback_func, sl@0: GObject *object) sl@0: { sl@0: GClosure *closure; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ sl@0: g_return_val_if_fail (callback_func != NULL, NULL); sl@0: sl@0: closure = g_cclosure_new (callback_func, object, NULL); sl@0: g_object_watch_closure (object, closure); sl@0: sl@0: return closure; sl@0: } sl@0: sl@0: /** sl@0: * g_cclosure_new_object_swap: sl@0: * @callback_func: the function to invoke sl@0: * @object: a #GObject pointer to pass to @callback_func sl@0: * sl@0: * A variant of g_cclosure_new_swap() which uses @object as @user_data sl@0: * and calls g_object_watch_closure() on @object and the created sl@0: * closure. This function is useful when you have a callback closely sl@0: * associated with a #GObject, and want the callback to no longer run sl@0: * after the object is is freed. sl@0: * sl@0: * Returns: a new #GCClosure sl@0: */ sl@0: EXPORT_C GClosure* sl@0: g_cclosure_new_object_swap (GCallback callback_func, sl@0: GObject *object) sl@0: { sl@0: GClosure *closure; sl@0: sl@0: g_return_val_if_fail (G_IS_OBJECT (object), NULL); sl@0: g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ sl@0: g_return_val_if_fail (callback_func != NULL, NULL); sl@0: sl@0: closure = g_cclosure_new_swap (callback_func, object, NULL); sl@0: g_object_watch_closure (object, closure); sl@0: sl@0: return closure; sl@0: } sl@0: sl@0: EXPORT_C gsize sl@0: g_object_compat_control (gsize what, sl@0: gpointer data) sl@0: { sl@0: switch (what) sl@0: { sl@0: gpointer *pp; sl@0: case 1: /* floating base type */ sl@0: return G_TYPE_INITIALLY_UNOWNED; sl@0: case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */ sl@0: floating_flag_handler = (guint(*)(GObject*,gint)) data; sl@0: return 1; sl@0: case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */ sl@0: pp = data; sl@0: *pp = (gpointer)floating_flag_handler; sl@0: return 1; sl@0: default: sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT); sl@0: sl@0: static void sl@0: g_initially_unowned_init (GInitiallyUnowned *object) sl@0: { sl@0: g_object_force_floating (object); sl@0: } sl@0: sl@0: static void sl@0: g_initially_unowned_class_init (GInitiallyUnownedClass *klass) sl@0: { sl@0: } sl@0: sl@0: #define __G_OBJECT_C__ sl@0: #include "gobjectaliasdef.c"