1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * MT safe with regards to reference counting.
29 #include "glib/gdatasetprivate.h"
32 #include "gvaluecollector.h"
34 #include "gparamspecs.h"
35 #include "gvaluetypes.h"
36 #include "gobjectalias.h"
38 /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */
39 #include "gobjectnotifyqueue.c"
44 * @short_description: The base object type
45 * @see_also: #GParamSpecObject, g_param_spec_object()
46 * @title: The Base Object Type
48 * GObject is the fundamental type providing the common attributes and
49 * methods for all object types in GTK+, Pango and other libraries
50 * based on GObject. The GObject class provides methods for object
51 * construction and destruction, property access methods, and signal
52 * support. Signals are described in detail in <xref
53 * linkend="gobject-Signals"/>.
55 * <para id="floating-ref">
56 * #GInitiallyUnowned is derived from #GObject. The only difference between
57 * the two is that the initial reference of a #GInitiallyUnowned is flagged
58 * as a <firstterm>floating</firstterm> reference.
59 * This means that it is not specifically claimed to be "owned" by
60 * any code portion. The main motivation for providing floating references is
61 * C convenience. In particular, it allows code to be written as:
63 * container = create_container();
64 * container_add_child (container, create_child());
66 * If <function>container_add_child()</function> will g_object_ref_sink() the
67 * passed in child, no reference of the newly created child is leaked.
68 * Without floating references, <function>container_add_child()</function>
69 * can only g_object_ref() the new child, so to implement this code without
70 * reference leaks, it would have to be written as:
73 * container = create_container();
74 * child = create_child();
75 * container_add_child (container, child);
76 * g_object_unref (child);
78 * The floating reference can be converted into
79 * an ordinary reference by calling g_object_ref_sink().
80 * For already sunken objects (objects that don't have a floating reference
81 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
83 * Since floating references are useful almost exclusively for C convenience,
84 * language bindings that provide automated reference and memory ownership
85 * maintenance (such as smart pointers or garbage collection) therefore don't
86 * need to expose floating references in their API.
89 * Some object implementations may need to save an objects floating state
90 * across certain code portions (an example is #GtkMenu), to achive this, the
91 * following sequence can be used:
94 * // save floating state
95 * gboolean was_floating = g_object_is_floating (object);
96 * g_object_ref_sink (object);
97 * // protected code portion
99 * // restore floating state
101 * g_object_force_floating (object);
102 * g_obejct_unref (object); // release previously acquired reference
108 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
109 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
111 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
112 #define OBJECT_HAS_TOGGLE_REF(object) \
113 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
114 #define OBJECT_FLOATING_FLAG 0x2
116 #define CLASS_HAS_PROPS_FLAG 0x1
117 #define CLASS_HAS_PROPS(class) \
118 ((class)->flags & CLASS_HAS_PROPS_FLAG)
119 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
120 ((class)->constructor != g_object_constructor)
122 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
123 #define CLASS_HAS_DERIVED_CLASS(class) \
124 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
126 /* --- signals --- */
133 /* --- properties --- */
139 /* --- prototypes --- */
140 static void g_object_base_class_init (GObjectClass
*class);
141 static void g_object_base_class_finalize (GObjectClass
*class);
142 static void g_object_do_class_init (GObjectClass
*class);
143 static void g_object_init (GObject
*object
,
144 GObjectClass
*class);
145 static GObject
* g_object_constructor (GType type
,
146 guint n_construct_properties
,
147 GObjectConstructParam
*construct_params
);
148 static void g_object_real_dispose (GObject
*object
);
149 static void g_object_finalize (GObject
*object
);
150 static void g_object_do_set_property (GObject
*object
,
154 static void g_object_do_get_property (GObject
*object
,
158 static void g_value_object_init (GValue
*value
);
159 static void g_value_object_free_value (GValue
*value
);
160 static void g_value_object_copy_value (const GValue
*src_value
,
162 static void g_value_object_transform_value (const GValue
*src_value
,
164 static gpointer
g_value_object_peek_pointer (const GValue
*value
);
165 static gchar
* g_value_object_collect_value (GValue
*value
,
166 guint n_collect_values
,
167 GTypeCValue
*collect_values
,
168 guint collect_flags
);
169 static gchar
* g_value_object_lcopy_value (const GValue
*value
,
170 guint n_collect_values
,
171 GTypeCValue
*collect_values
,
172 guint collect_flags
);
173 static void g_object_dispatch_properties_changed (GObject
*object
,
175 GParamSpec
**pspecs
);
176 static inline void object_get_property (GObject
*object
,
179 static inline void object_set_property (GObject
*object
,
182 GObjectNotifyQueue
*nqueue
);
183 static guint
object_floating_flag_handler (GObject
*object
,
186 static void object_interface_check_properties (gpointer func_data
,
190 /* --- variables --- */
191 static GQuark quark_closure_array
= 0;
192 static GQuark quark_weak_refs
= 0;
193 static GQuark quark_toggle_refs
= 0;
194 static GParamSpecPool
*pspec_pool
= NULL
;
195 static GObjectNotifyContext property_notify_context
= { 0, };
196 static gulong gobject_signals
[LAST_SIGNAL
] = { 0, };
197 static guint (*floating_flag_handler
) (GObject
*, gint
) = object_floating_flag_handler
;
198 G_LOCK_DEFINE_STATIC (construction_mutex
);
199 static GSList
*construction_objects
= NULL
;
201 /* --- functions --- */
202 #ifdef G_ENABLE_DEBUG
203 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
204 G_LOCK_DEFINE_STATIC (debug_objects
);
205 static volatile GObject
*g_trap_object_ref
= NULL
;
206 static guint debug_objects_count
= 0;
207 static GHashTable
*debug_objects_ht
= NULL
;
210 debug_objects_foreach (gpointer key
,
214 GObject
*object
= value
;
216 g_message ("[%p] stale %s\tref_count=%u",
218 G_OBJECT_TYPE_NAME (object
),
223 debug_objects_atexit (void)
227 G_LOCK (debug_objects
);
228 g_message ("stale GObjects: %u", debug_objects_count
);
229 g_hash_table_foreach (debug_objects_ht
, debug_objects_foreach
, NULL
);
230 G_UNLOCK (debug_objects
);
233 #endif /* G_ENABLE_DEBUG */
236 g_object_type_init (void)
238 static gboolean initialized
= FALSE
;
239 static const GTypeFundamentalInfo finfo
= {
240 G_TYPE_FLAG_CLASSED
| G_TYPE_FLAG_INSTANTIATABLE
| G_TYPE_FLAG_DERIVABLE
| G_TYPE_FLAG_DEEP_DERIVABLE
,
242 static GTypeInfo info
= {
243 sizeof (GObjectClass
),
244 (GBaseInitFunc
) g_object_base_class_init
,
245 (GBaseFinalizeFunc
) g_object_base_class_finalize
,
246 (GClassInitFunc
) g_object_do_class_init
,
247 NULL
/* class_destroy */,
248 NULL
/* class_data */,
251 (GInstanceInitFunc
) g_object_init
,
252 NULL
, /* value_table */
254 static const GTypeValueTable value_table
= {
255 g_value_object_init
, /* value_init */
256 g_value_object_free_value
, /* value_free */
257 g_value_object_copy_value
, /* value_copy */
258 g_value_object_peek_pointer
, /* value_peek_pointer */
259 "p", /* collect_format */
260 g_value_object_collect_value
, /* collect_value */
261 "p", /* lcopy_format */
262 g_value_object_lcopy_value
, /* lcopy_value */
266 g_return_if_fail (initialized
== FALSE
);
271 info
.value_table
= &value_table
;
272 type
= g_type_register_fundamental (G_TYPE_OBJECT
, g_intern_static_string ("GObject"), &info
, &finfo
, 0);
273 g_assert (type
== G_TYPE_OBJECT
);
274 g_value_register_transform_func (G_TYPE_OBJECT
, G_TYPE_OBJECT
, g_value_object_transform_value
);
276 #ifdef G_ENABLE_DEBUG
279 debug_objects_ht
= g_hash_table_new (g_direct_hash
, NULL
);
280 g_atexit (debug_objects_atexit
);
282 #endif /* G_ENABLE_DEBUG */
286 g_object_base_class_init (GObjectClass
*class)
288 GObjectClass
*pclass
= g_type_class_peek_parent (class);
290 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
291 class->flags
&= ~CLASS_HAS_DERIVED_CLASS_FLAG
;
294 pclass
->flags
|= CLASS_HAS_DERIVED_CLASS_FLAG
;
296 /* reset instance specific fields and methods that don't get inherited */
297 class->construct_properties
= pclass
? g_slist_copy (pclass
->construct_properties
) : NULL
;
298 class->get_property
= NULL
;
299 class->set_property
= NULL
;
303 g_object_base_class_finalize (GObjectClass
*class)
307 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
309 g_slist_free (class->construct_properties
);
310 class->construct_properties
= NULL
;
311 list
= g_param_spec_pool_list_owned (pspec_pool
, G_OBJECT_CLASS_TYPE (class));
312 for (node
= list
; node
; node
= node
->next
)
314 GParamSpec
*pspec
= node
->data
;
316 g_param_spec_pool_remove (pspec_pool
, pspec
);
317 PARAM_SPEC_SET_PARAM_ID (pspec
, 0);
318 g_param_spec_unref (pspec
);
324 g_object_notify_dispatcher (GObject
*object
,
328 G_OBJECT_GET_CLASS (object
)->dispatch_properties_changed (object
, n_pspecs
, pspecs
);
332 g_object_do_class_init (GObjectClass
*class)
334 /* read the comment about typedef struct CArray; on why not to change this quark */
335 quark_closure_array
= g_quark_from_static_string ("GObject-closure-array");
337 quark_weak_refs
= g_quark_from_static_string ("GObject-weak-references");
338 quark_toggle_refs
= g_quark_from_static_string ("GObject-toggle-references");
339 pspec_pool
= g_param_spec_pool_new (TRUE
);
340 property_notify_context
.quark_notify_queue
= g_quark_from_static_string ("GObject-notify-queue");
341 property_notify_context
.dispatcher
= g_object_notify_dispatcher
;
343 class->constructor
= g_object_constructor
;
344 class->set_property
= g_object_do_set_property
;
345 class->get_property
= g_object_do_get_property
;
346 class->dispose
= g_object_real_dispose
;
347 class->finalize
= g_object_finalize
;
348 class->dispatch_properties_changed
= g_object_dispatch_properties_changed
;
349 class->notify
= NULL
;
353 * @gobject: the object which received the signal.
354 * @pspec: the #GParamSpec of the property which changed.
356 * The notify signal is emitted on an object when one of its
357 * properties has been changed. Note that getting this signal
358 * doesn't guarantee that the value of the property has actually
359 * changed, it may also be emitted when the setter for the property
360 * is called to reinstate the previous value.
362 * This signal is typically used to obtain change notification for a
363 * single property, by specifying the property name as a detail in the
364 * g_signal_connect() call, like this:
366 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
367 * G_CALLBACK (gtk_text_view_target_list_notify),
370 * It is important to note that you must use
371 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
372 * detail strings for the notify signal.
374 gobject_signals
[NOTIFY
] =
375 g_signal_new (g_intern_static_string ("notify"),
376 G_TYPE_FROM_CLASS (class),
377 G_SIGNAL_RUN_FIRST
| G_SIGNAL_NO_RECURSE
| G_SIGNAL_DETAILED
| G_SIGNAL_NO_HOOKS
| G_SIGNAL_ACTION
,
378 G_STRUCT_OFFSET (GObjectClass
, notify
),
380 g_cclosure_marshal_VOID__PARAM
,
384 /* Install a check function that we'll use to verify that classes that
385 * implement an interface implement all properties for that interface
387 g_type_add_interface_check (NULL
, object_interface_check_properties
);
391 install_property_internal (GType g_type
,
395 if (g_param_spec_pool_lookup (pspec_pool
, pspec
->name
, g_type
, FALSE
))
397 g_warning ("When installing property: type `%s' already has a property named `%s'",
398 g_type_name (g_type
),
403 g_param_spec_ref (pspec
);
404 g_param_spec_sink (pspec
);
405 PARAM_SPEC_SET_PARAM_ID (pspec
, property_id
);
406 g_param_spec_pool_insert (pspec_pool
, pspec
, g_type
);
410 * g_object_class_install_property:
411 * @oclass: a #GObjectClass
412 * @property_id: the id for the new property
413 * @pspec: the #GParamSpec for the new property
415 * Installs a new property. This is usually done in the class initializer.
417 * Note that it is possible to redefine a property in a derived class,
418 * by installing a property with the same name. This can be useful at times,
419 * e.g. to change the range of allowed values or the default value.
422 g_object_class_install_property (GObjectClass
*class,
426 g_return_if_fail (G_IS_OBJECT_CLASS (class));
427 g_return_if_fail (G_IS_PARAM_SPEC (pspec
));
429 if (CLASS_HAS_DERIVED_CLASS (class))
430 g_error ("Attempt to add property %s::%s to class after it was derived",
431 G_OBJECT_CLASS_NAME (class), pspec
->name
);
433 class->flags
|= CLASS_HAS_PROPS_FLAG
;
435 if (pspec
->flags
& G_PARAM_WRITABLE
)
436 g_return_if_fail (class->set_property
!= NULL
);
437 if (pspec
->flags
& G_PARAM_READABLE
)
438 g_return_if_fail (class->get_property
!= NULL
);
439 g_return_if_fail (property_id
> 0);
440 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec
) == 0); /* paranoid */
441 if (pspec
->flags
& G_PARAM_CONSTRUCT
)
442 g_return_if_fail ((pspec
->flags
& G_PARAM_CONSTRUCT_ONLY
) == 0);
443 if (pspec
->flags
& (G_PARAM_CONSTRUCT
| G_PARAM_CONSTRUCT_ONLY
))
444 g_return_if_fail (pspec
->flags
& G_PARAM_WRITABLE
);
446 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id
, pspec
);
448 if (pspec
->flags
& (G_PARAM_CONSTRUCT
| G_PARAM_CONSTRUCT_ONLY
))
449 class->construct_properties
= g_slist_prepend (class->construct_properties
, pspec
);
451 /* for property overrides of construct poperties, we have to get rid
452 * of the overidden inherited construct property
454 pspec
= g_param_spec_pool_lookup (pspec_pool
, pspec
->name
, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE
);
455 if (pspec
&& pspec
->flags
& (G_PARAM_CONSTRUCT
| G_PARAM_CONSTRUCT_ONLY
))
456 class->construct_properties
= g_slist_remove (class->construct_properties
, pspec
);
460 * g_object_interface_install_property:
461 * @g_iface: any interface vtable for the interface, or the default
462 * vtable for the interface.
463 * @pspec: the #GParamSpec for the new property
465 * Add a property to an interface; this is only useful for interfaces
466 * that are added to GObject-derived types. Adding a property to an
467 * interface forces all objects classes with that interface to have a
468 * compatible property. The compatible property could be a newly
469 * created #GParamSpec, but normally
470 * g_object_class_override_property() will be used so that the object
471 * class only needs to provide an implementation and inherits the
472 * property description, default value, bounds, and so forth from the
473 * interface property.
475 * This function is meant to be called from the interface's default
476 * vtable initialization function (the @class_init member of
477 * #GTypeInfo.) It must not be called after after @class_init has
478 * been called for any object types implementing this interface.
483 g_object_interface_install_property (gpointer g_iface
,
486 GTypeInterface
*iface_class
= g_iface
;
488 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class
->g_type
));
489 g_return_if_fail (G_IS_PARAM_SPEC (pspec
));
490 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec
)); /* paranoid */
491 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec
) == 0); /* paranoid */
493 install_property_internal (iface_class
->g_type
, 0, pspec
);
497 * g_object_class_find_property:
498 * @oclass: a #GObjectClass
499 * @property_name: the name of the property to look up
501 * Looks up the #GParamSpec for a property of a class.
503 * Returns: the #GParamSpec for the property, or %NULL if the class
504 * doesn't have a property of that name
507 g_object_class_find_property (GObjectClass
*class,
508 const gchar
*property_name
)
511 GParamSpec
*redirect
;
513 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL
);
514 g_return_val_if_fail (property_name
!= NULL
, NULL
);
516 pspec
= g_param_spec_pool_lookup (pspec_pool
,
518 G_OBJECT_CLASS_TYPE (class),
522 redirect
= g_param_spec_get_redirect_target (pspec
);
533 * g_object_interface_find_property:
534 * @g_iface: any interface vtable for the interface, or the default
535 * vtable for the interface
536 * @property_name: name of a property to lookup.
538 * Find the #GParamSpec with the given name for an
539 * interface. Generally, the interface vtable passed in as @g_iface
540 * will be the default vtable from g_type_default_interface_ref(), or,
541 * if you know the interface has already been loaded,
542 * g_type_default_interface_peek().
546 * Returns: the #GParamSpec for the property of the interface with the
547 * name @property_name, or %NULL if no such property exists.
550 g_object_interface_find_property (gpointer g_iface
,
551 const gchar
*property_name
)
553 GTypeInterface
*iface_class
= g_iface
;
555 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class
->g_type
), NULL
);
556 g_return_val_if_fail (property_name
!= NULL
, NULL
);
558 return g_param_spec_pool_lookup (pspec_pool
,
565 * g_object_class_override_property:
566 * @oclass: a #GObjectClass
567 * @property_id: the new property ID
568 * @name: the name of a property registered in a parent class or
569 * in an interface of this class.
571 * Registers @property_id as referring to a property with the
572 * name @name in a parent class or in an interface implemented
573 * by @oclass. This allows this class to <firstterm>override</firstterm>
574 * a property implementation in a parent class or to provide
575 * the implementation of a property from an interface.
578 * Internally, overriding is implemented by creating a property of type
579 * #GParamSpecOverride; generally operations that query the properties of
580 * the object class, such as g_object_class_find_property() or
581 * g_object_class_list_properties() will return the overridden
582 * property. However, in one case, the @construct_properties argument of
583 * the @constructor virtual function, the #GParamSpecOverride is passed
584 * instead, so that the @param_id field of the #GParamSpec will be
585 * correct. For virtually all uses, this makes no difference. If you
586 * need to get the overridden property, you can call
587 * g_param_spec_get_redirect_target().
593 g_object_class_override_property (GObjectClass
*oclass
,
597 GParamSpec
*overridden
= NULL
;
601 g_return_if_fail (G_IS_OBJECT_CLASS (oclass
));
602 g_return_if_fail (property_id
> 0);
603 g_return_if_fail (name
!= NULL
);
605 /* Find the overridden property; first check parent types
607 parent_type
= g_type_parent (G_OBJECT_CLASS_TYPE (oclass
));
608 if (parent_type
!= G_TYPE_NONE
)
609 overridden
= g_param_spec_pool_lookup (pspec_pool
,
618 /* Now check interfaces
620 ifaces
= g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass
), &n_ifaces
);
621 while (n_ifaces
-- && !overridden
)
623 overridden
= g_param_spec_pool_lookup (pspec_pool
,
634 g_warning ("%s: Can't find property to override for '%s::%s'",
635 G_STRFUNC
, G_OBJECT_CLASS_NAME (oclass
), name
);
639 new = g_param_spec_override (name
, overridden
);
640 g_object_class_install_property (oclass
, property_id
, new);
644 * g_object_class_list_properties:
645 * @oclass: a #GObjectClass
646 * @n_properties: return location for the length of the returned array
648 * Get an array of #GParamSpec* for all properties of a class.
650 * Returns: an array of #GParamSpec* which should be freed after use
652 GParamSpec
** /* free result */
653 g_object_class_list_properties (GObjectClass
*class,
654 guint
*n_properties_p
)
659 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL
);
661 pspecs
= g_param_spec_pool_list (pspec_pool
,
662 G_OBJECT_CLASS_TYPE (class),
671 * g_object_interface_list_properties:
672 * @g_iface: any interface vtable for the interface, or the default
673 * vtable for the interface
674 * @n_properties_p: location to store number of properties returned.
676 * Lists the properties of an interface.Generally, the interface
677 * vtable passed in as @g_iface will be the default vtable from
678 * g_type_default_interface_ref(), or, if you know the interface has
679 * already been loaded, g_type_default_interface_peek().
683 * Returns: a pointer to an array of pointers to #GParamSpec
684 * structures. The paramspecs are owned by GLib, but the
685 * array should be freed with g_free() when you are done with
689 g_object_interface_list_properties (gpointer g_iface
,
690 guint
*n_properties_p
)
692 GTypeInterface
*iface_class
= g_iface
;
696 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class
->g_type
), NULL
);
698 pspecs
= g_param_spec_pool_list (pspec_pool
,
708 g_object_init (GObject
*object
,
711 object
->ref_count
= 1;
712 g_datalist_init (&object
->qdata
);
714 if (CLASS_HAS_PROPS (class))
716 /* freeze object's notification queue, g_object_newv() preserves pairedness */
717 g_object_notify_queue_freeze (object
, &property_notify_context
);
720 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
722 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
723 G_LOCK (construction_mutex
);
724 construction_objects
= g_slist_prepend (construction_objects
, object
);
725 G_UNLOCK (construction_mutex
);
728 #ifdef G_ENABLE_DEBUG
731 G_LOCK (debug_objects
);
732 debug_objects_count
++;
733 g_hash_table_insert (debug_objects_ht
, object
, object
);
734 G_UNLOCK (debug_objects
);
736 #endif /* G_ENABLE_DEBUG */
740 g_object_do_set_property (GObject
*object
,
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
754 g_object_do_get_property (GObject
*object
,
762 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
768 g_object_real_dispose (GObject
*object
)
770 g_signal_handlers_destroy (object
);
771 g_datalist_id_set_data (&object
->qdata
, quark_closure_array
, NULL
);
772 g_datalist_id_set_data (&object
->qdata
, quark_weak_refs
, NULL
);
776 g_object_finalize (GObject
*object
)
778 g_datalist_clear (&object
->qdata
);
780 #ifdef G_ENABLE_DEBUG
783 G_LOCK (debug_objects
);
784 g_assert (g_hash_table_lookup (debug_objects_ht
, object
) == object
);
785 g_hash_table_remove (debug_objects_ht
, object
);
786 debug_objects_count
--;
787 G_UNLOCK (debug_objects
);
789 #endif /* G_ENABLE_DEBUG */
794 g_object_dispatch_properties_changed (GObject
*object
,
800 for (i
= 0; i
< n_pspecs
; i
++)
801 g_signal_emit (object
, gobject_signals
[NOTIFY
], g_quark_from_string (pspecs
[i
]->name
), pspecs
[i
]);
805 * g_object_run_dispose:
806 * @object: a #GObject
808 * Releases all references to other objects. This can be used to break
811 * This functions should only be called from object system implementations.
814 g_object_run_dispose (GObject
*object
)
816 g_return_if_fail (G_IS_OBJECT (object
));
817 g_return_if_fail (object
->ref_count
> 0);
819 g_object_ref (object
);
820 G_OBJECT_GET_CLASS (object
)->dispose (object
);
821 g_object_unref (object
);
825 * g_object_freeze_notify:
826 * @object: a #GObject
828 * Increases the freeze count on @object. If the freeze count is
829 * non-zero, the emission of "notify" signals on @object is
830 * stopped. The signals are queued until the freeze count is decreased
833 * This is necessary for accessors that modify multiple properties to prevent
834 * premature notification while the object is still being modified.
837 g_object_freeze_notify (GObject
*object
)
839 g_return_if_fail (G_IS_OBJECT (object
));
841 if (g_atomic_int_get (&object
->ref_count
) == 0)
844 g_object_ref (object
);
845 g_object_notify_queue_freeze (object
, &property_notify_context
);
846 g_object_unref (object
);
851 * @object: a #GObject
852 * @property_name: the name of a property installed on the class of @object.
854 * Emits a "notify" signal for the property @property_name on @object.
857 g_object_notify (GObject
*object
,
858 const gchar
*property_name
)
862 g_return_if_fail (G_IS_OBJECT (object
));
863 g_return_if_fail (property_name
!= NULL
);
864 if (g_atomic_int_get (&object
->ref_count
) == 0)
867 g_object_ref (object
);
868 /* We don't need to get the redirect target
869 * (by, e.g. calling g_object_class_find_property())
870 * because g_object_notify_queue_add() does that
872 pspec
= g_param_spec_pool_lookup (pspec_pool
,
874 G_OBJECT_TYPE (object
),
878 g_warning ("%s: object class `%s' has no property named `%s'",
880 G_OBJECT_TYPE_NAME (object
),
884 GObjectNotifyQueue
*nqueue
;
886 nqueue
= g_object_notify_queue_freeze (object
, &property_notify_context
);
887 g_object_notify_queue_add (object
, nqueue
, pspec
);
888 g_object_notify_queue_thaw (object
, nqueue
);
890 g_object_unref (object
);
894 * g_object_thaw_notify:
895 * @object: a #GObject
897 * Reverts the effect of a previous call to
898 * g_object_freeze_notify(). The freeze count is decreased on @object
899 * and when it reaches zero, all queued "notify" signals are emitted.
901 * It is an error to call this function when the freeze count is zero.
904 g_object_thaw_notify (GObject
*object
)
906 GObjectNotifyQueue
*nqueue
;
908 g_return_if_fail (G_IS_OBJECT (object
));
909 if (g_atomic_int_get (&object
->ref_count
) == 0)
912 g_object_ref (object
);
913 nqueue
= g_object_notify_queue_from_object (object
, &property_notify_context
);
914 if (!nqueue
|| !nqueue
->freeze_count
)
915 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
916 G_STRFUNC
, G_OBJECT_TYPE_NAME (object
), object
);
918 g_object_notify_queue_thaw (object
, nqueue
);
919 g_object_unref (object
);
923 object_get_property (GObject
*object
,
927 GObjectClass
*class = g_type_class_peek (pspec
->owner_type
);
928 guint param_id
= PARAM_SPEC_PARAM_ID (pspec
);
929 GParamSpec
*redirect
;
931 redirect
= g_param_spec_get_redirect_target (pspec
);
935 class->get_property (object
, param_id
, value
, pspec
);
939 object_set_property (GObject
*object
,
942 GObjectNotifyQueue
*nqueue
)
944 GValue tmp_value
= { 0, };
945 GObjectClass
*class = g_type_class_peek (pspec
->owner_type
);
946 guint param_id
= PARAM_SPEC_PARAM_ID (pspec
);
947 GParamSpec
*redirect
;
949 redirect
= g_param_spec_get_redirect_target (pspec
);
953 /* provide a copy to work from, convert (if necessary) and validate */
954 g_value_init (&tmp_value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
955 if (!g_value_transform (value
, &tmp_value
))
956 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
958 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec
)),
959 G_VALUE_TYPE_NAME (value
));
960 else if (g_param_value_validate (pspec
, &tmp_value
) && !(pspec
->flags
& G_PARAM_LAX_VALIDATION
))
962 gchar
*contents
= g_strdup_value_contents (value
);
964 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
966 G_VALUE_TYPE_NAME (value
),
968 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec
)));
973 class->set_property (object
, param_id
, &tmp_value
, pspec
);
974 g_object_notify_queue_add (object
, nqueue
, pspec
);
976 g_value_unset (&tmp_value
);
980 object_interface_check_properties (gpointer func_data
,
983 GTypeInterface
*iface_class
= g_iface
;
984 GObjectClass
*class = g_type_class_peek (iface_class
->g_instance_type
);
985 GType iface_type
= iface_class
->g_type
;
989 if (!G_IS_OBJECT_CLASS (class))
992 pspecs
= g_param_spec_pool_list (pspec_pool
, iface_type
, &n
);
996 GParamSpec
*class_pspec
= g_param_spec_pool_lookup (pspec_pool
,
998 G_OBJECT_CLASS_TYPE (class),
1003 g_critical ("Object class %s doesn't implement property "
1004 "'%s' from interface '%s'",
1005 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1007 g_type_name (iface_type
));
1012 /* The implementation paramspec must have a less restrictive
1013 * type than the interface parameter spec for set() and a
1014 * more restrictive type for get(). We just require equality,
1015 * rather than doing something more complicated checking
1016 * the READABLE and WRITABLE flags. We also simplify here
1017 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1020 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs
[n
]),
1021 G_PARAM_SPEC_VALUE_TYPE (class_pspec
)))
1023 g_critical ("Property '%s' on class '%s' has type '%s' "
1024 "which is different from the type '%s', "
1025 "of the property on interface '%s'\n",
1027 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1028 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec
)),
1029 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs
[n
])),
1030 g_type_name (iface_type
));
1033 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1035 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1036 * READABLE and WRITABLE remove restrictions. The implementation
1037 * paramspec must have less restrictive flags.
1040 (!SUBSET (class_pspec
->flags
,
1042 G_PARAM_CONSTRUCT
| G_PARAM_CONSTRUCT_ONLY
) ||
1043 !SUBSET (pspecs
[n
]->flags
,
1045 G_PARAM_READABLE
| G_PARAM_WRITABLE
)))
1047 g_critical ("Flags for property '%s' on class '%s' "
1048 "are not compatible with the property on"
1051 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1052 g_type_name (iface_type
));
1061 g_object_get_type (void)
1063 return G_TYPE_OBJECT
;
1068 * @object_type: the type id of the #GObject subtype to instantiate
1069 * @first_property_name: the name of the first property
1070 * @...: the value of the first property, followed optionally by more
1071 * name/value pairs, followed by %NULL
1073 * Creates a new instance of a #GObject subtype and sets its properties.
1075 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1076 * which are not explicitly specified are set to their default values.
1078 * Returns: a new instance of @object_type
1081 g_object_new (GType object_type
,
1082 const gchar
*first_property_name
,
1088 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type
), NULL
);
1090 va_start (var_args
, first_property_name
);
1091 object
= g_object_new_valist (object_type
, first_property_name
, var_args
);
1098 slist_maybe_remove (GSList
**slist
,
1101 GSList
*last
= NULL
, *node
= *slist
;
1104 if (node
->data
== data
)
1107 last
->next
= node
->next
;
1109 *slist
= node
->next
;
1110 g_slist_free_1 (node
);
1119 static inline gboolean
1120 object_in_construction_list (GObject
*object
)
1122 gboolean in_construction
;
1123 G_LOCK (construction_mutex
);
1124 in_construction
= g_slist_find (construction_objects
, object
) != NULL
;
1125 G_UNLOCK (construction_mutex
);
1126 return in_construction
;
1131 * @object_type: the type id of the #GObject subtype to instantiate
1132 * @n_parameters: the length of the @parameters array
1133 * @parameters: an array of #GParameter
1135 * Creates a new instance of a #GObject subtype and sets its properties.
1137 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1138 * which are not explicitly specified are set to their default values.
1140 * Returns: a new instance of @object_type
1143 g_object_newv (GType object_type
,
1145 GParameter
*parameters
)
1147 GObjectConstructParam
*cparams
= NULL
, *oparams
;
1148 GObjectNotifyQueue
*nqueue
= NULL
; /* shouldn't be initialized, just to silence compiler */
1150 GObjectClass
*class, *unref_class
= NULL
;
1152 guint n_total_cparams
= 0, n_cparams
= 0, n_oparams
= 0, n_cvalues
;
1154 GList
*clist
= NULL
;
1155 gboolean newly_constructed
;
1158 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type
), NULL
);
1160 class = g_type_class_peek_static (object_type
);
1162 class = unref_class
= g_type_class_ref (object_type
);
1163 for (slist
= class->construct_properties
; slist
; slist
= slist
->next
)
1165 clist
= g_list_prepend (clist
, slist
->data
);
1166 n_total_cparams
+= 1;
1169 if (n_parameters
== 0 && n_total_cparams
== 0)
1171 /* This is a simple object with no construct properties, and
1172 * no properties are being set, so short circuit the parameter
1173 * handling. This speeds up simple object construction.
1176 object
= class->constructor (object_type
, 0, NULL
);
1177 goto did_construction
;
1180 /* collect parameters, sort into construction and normal ones */
1181 oparams
= g_new (GObjectConstructParam
, n_parameters
);
1182 cparams
= g_new (GObjectConstructParam
, n_total_cparams
);
1183 for (i
= 0; i
< n_parameters
; i
++)
1185 GValue
*value
= ¶meters
[i
].value
;
1186 GParamSpec
*pspec
= g_param_spec_pool_lookup (pspec_pool
,
1192 g_warning ("%s: object class `%s' has no property named `%s'",
1194 g_type_name (object_type
),
1195 parameters
[i
].name
);
1198 if (!(pspec
->flags
& G_PARAM_WRITABLE
))
1200 g_warning ("%s: property `%s' of object class `%s' is not writable",
1203 g_type_name (object_type
));
1206 if (pspec
->flags
& (G_PARAM_CONSTRUCT
| G_PARAM_CONSTRUCT_ONLY
))
1208 GList
*list
= g_list_find (clist
, pspec
);
1212 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1213 G_STRFUNC
, pspec
->name
, g_type_name (object_type
));
1216 cparams
[n_cparams
].pspec
= pspec
;
1217 cparams
[n_cparams
].value
= value
;
1222 list
->prev
->next
= list
->next
;
1224 list
->next
->prev
= list
->prev
;
1225 g_list_free_1 (list
);
1229 oparams
[n_oparams
].pspec
= pspec
;
1230 oparams
[n_oparams
].value
= value
;
1235 /* set remaining construction properties to default values */
1236 n_cvalues
= n_total_cparams
- n_cparams
;
1237 cvalues
= g_new (GValue
, n_cvalues
);
1240 GList
*tmp
= clist
->next
;
1241 GParamSpec
*pspec
= clist
->data
;
1242 GValue
*value
= cvalues
+ n_total_cparams
- n_cparams
- 1;
1245 g_value_init (value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
1246 g_param_value_set_default (pspec
, value
);
1248 cparams
[n_cparams
].pspec
= pspec
;
1249 cparams
[n_cparams
].value
= value
;
1252 g_list_free_1 (clist
);
1256 /* construct object from construction parameters */
1257 object
= class->constructor (object_type
, n_total_cparams
, cparams
);
1258 /* free construction values */
1261 g_value_unset (cvalues
+ n_cvalues
);
1265 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1267 /* adjust freeze_count according to g_object_init() and remaining properties */
1268 G_LOCK (construction_mutex
);
1269 newly_constructed
= slist_maybe_remove (&construction_objects
, object
);
1270 G_UNLOCK (construction_mutex
);
1273 newly_constructed
= TRUE
;
1275 if (CLASS_HAS_PROPS (class))
1277 if (newly_constructed
|| n_oparams
)
1278 nqueue
= g_object_notify_queue_freeze (object
, &property_notify_context
);
1279 if (newly_constructed
)
1280 g_object_notify_queue_thaw (object
, nqueue
);
1283 /* run 'constructed' handler if there is one */
1284 if (newly_constructed
&& class->constructed
)
1285 class->constructed (object
);
1287 /* set remaining properties */
1288 for (i
= 0; i
< n_oparams
; i
++)
1289 object_set_property (object
, oparams
[i
].pspec
, oparams
[i
].value
, nqueue
);
1292 if (CLASS_HAS_PROPS (class))
1294 /* release our own freeze count and handle notifications */
1295 if (newly_constructed
|| n_oparams
)
1296 g_object_notify_queue_thaw (object
, nqueue
);
1300 g_type_class_unref (unref_class
);
1306 * g_object_new_valist:
1307 * @object_type: the type id of the #GObject subtype to instantiate
1308 * @first_property_name: the name of the first property
1309 * @var_args: the value of the first property, followed optionally by more
1310 * name/value pairs, followed by %NULL
1312 * Creates a new instance of a #GObject subtype and sets its properties.
1314 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1315 * which are not explicitly specified are set to their default values.
1317 * Returns: a new instance of @object_type
1320 g_object_new_valist (GType object_type
,
1321 const gchar
*first_property_name
,
1324 GObjectClass
*class;
1328 guint n_params
= 0, n_alloced_params
= 16;
1330 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type
), NULL
);
1332 if (!first_property_name
)
1333 return g_object_newv (object_type
, 0, NULL
);
1335 class = g_type_class_ref (object_type
);
1337 params
= g_new (GParameter
, n_alloced_params
);
1338 name
= first_property_name
;
1341 gchar
*error
= NULL
;
1342 GParamSpec
*pspec
= g_param_spec_pool_lookup (pspec_pool
,
1348 g_warning ("%s: object class `%s' has no property named `%s'",
1350 g_type_name (object_type
),
1354 if (n_params
>= n_alloced_params
)
1356 n_alloced_params
+= 16;
1357 params
= g_renew (GParameter
, params
, n_alloced_params
);
1359 params
[n_params
].name
= name
;
1360 params
[n_params
].value
.g_type
= 0;
1361 g_value_init (¶ms
[n_params
].value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
1362 G_VALUE_COLLECT (¶ms
[n_params
].value
, var_args
, 0, &error
);
1365 g_warning ("%s: %s", G_STRFUNC
, error
);
1367 g_value_unset (¶ms
[n_params
].value
);
1371 name
= va_arg (var_args
, gchar
*);
1374 object
= g_object_newv (object_type
, n_params
, params
);
1377 g_value_unset (¶ms
[n_params
].value
);
1380 g_type_class_unref (class);
1386 g_object_constructor (GType type
,
1387 guint n_construct_properties
,
1388 GObjectConstructParam
*construct_params
)
1393 object
= (GObject
*) g_type_create_instance (type
);
1395 /* set construction parameters */
1396 if (n_construct_properties
)
1398 GObjectNotifyQueue
*nqueue
= g_object_notify_queue_freeze (object
, &property_notify_context
);
1400 /* set construct properties */
1401 while (n_construct_properties
--)
1403 GValue
*value
= construct_params
->value
;
1404 GParamSpec
*pspec
= construct_params
->pspec
;
1407 object_set_property (object
, pspec
, value
, nqueue
);
1409 g_object_notify_queue_thaw (object
, nqueue
);
1410 /* the notification queue is still frozen from g_object_init(), so
1411 * we don't need to handle it here, g_object_newv() takes
1420 * g_object_set_valist:
1421 * @object: a #GObject
1422 * @first_property_name: name of the first property to set
1423 * @var_args: value for the first property, followed optionally by more
1424 * name/value pairs, followed by %NULL
1426 * Sets properties on an object.
1429 g_object_set_valist (GObject
*object
,
1430 const gchar
*first_property_name
,
1433 GObjectNotifyQueue
*nqueue
;
1436 g_return_if_fail (G_IS_OBJECT (object
));
1438 g_object_ref (object
);
1439 nqueue
= g_object_notify_queue_freeze (object
, &property_notify_context
);
1441 name
= first_property_name
;
1444 GValue value
= { 0, };
1446 gchar
*error
= NULL
;
1448 pspec
= g_param_spec_pool_lookup (pspec_pool
,
1450 G_OBJECT_TYPE (object
),
1454 g_warning ("%s: object class `%s' has no property named `%s'",
1456 G_OBJECT_TYPE_NAME (object
),
1460 if (!(pspec
->flags
& G_PARAM_WRITABLE
))
1462 g_warning ("%s: property `%s' of object class `%s' is not writable",
1465 G_OBJECT_TYPE_NAME (object
));
1468 if ((pspec
->flags
& G_PARAM_CONSTRUCT_ONLY
) && !object_in_construction_list (object
))
1470 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1471 G_STRFUNC
, pspec
->name
, G_OBJECT_TYPE_NAME (object
));
1475 g_value_init (&value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
1477 G_VALUE_COLLECT (&value
, var_args
, 0, &error
);
1480 g_warning ("%s: %s", G_STRFUNC
, error
);
1482 g_value_unset (&value
);
1486 object_set_property (object
, pspec
, &value
, nqueue
);
1487 g_value_unset (&value
);
1489 name
= va_arg (var_args
, gchar
*);
1492 g_object_notify_queue_thaw (object
, nqueue
);
1493 g_object_unref (object
);
1497 * g_object_get_valist:
1498 * @object: a #GObject
1499 * @first_property_name: name of the first property to get
1500 * @var_args: return location for the first property, followed optionally by more
1501 * name/return location pairs, followed by %NULL
1503 * Gets properties of an object.
1505 * In general, a copy is made of the property contents and the caller
1506 * is responsible for freeing the memory in the appropriate manner for
1507 * the type, for instance by calling g_free() or g_object_unref().
1509 * See g_object_get().
1512 g_object_get_valist (GObject
*object
,
1513 const gchar
*first_property_name
,
1518 g_return_if_fail (G_IS_OBJECT (object
));
1520 g_object_ref (object
);
1522 name
= first_property_name
;
1526 GValue value
= { 0, };
1530 pspec
= g_param_spec_pool_lookup (pspec_pool
,
1532 G_OBJECT_TYPE (object
),
1536 g_warning ("%s: object class `%s' has no property named `%s'",
1538 G_OBJECT_TYPE_NAME (object
),
1542 if (!(pspec
->flags
& G_PARAM_READABLE
))
1544 g_warning ("%s: property `%s' of object class `%s' is not readable",
1547 G_OBJECT_TYPE_NAME (object
));
1551 g_value_init (&value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
1553 object_get_property (object
, pspec
, &value
);
1555 G_VALUE_LCOPY (&value
, var_args
, 0, &error
);
1558 g_warning ("%s: %s", G_STRFUNC
, error
);
1560 g_value_unset (&value
);
1564 g_value_unset (&value
);
1566 name
= va_arg (var_args
, gchar
*);
1569 g_object_unref (object
);
1574 * @object: a #GObject
1575 * @first_property_name: name of the first property to set
1576 * @...: value for the first property, followed optionally by more
1577 * name/value pairs, followed by %NULL
1579 * Sets properties on an object.
1582 g_object_set (gpointer _object
,
1583 const gchar
*first_property_name
,
1586 GObject
*object
= _object
;
1589 g_return_if_fail (G_IS_OBJECT (object
));
1591 va_start (var_args
, first_property_name
);
1592 g_object_set_valist (object
, first_property_name
, var_args
);
1598 * @object: a #GObject
1599 * @first_property_name: name of the first property to get
1600 * @...: return location for the first property, followed optionally by more
1601 * name/return location pairs, followed by %NULL
1603 * Gets properties of an object.
1605 * In general, a copy is made of the property contents and the caller
1606 * is responsible for freeing the memory in the appropriate manner for
1607 * the type, for instance by calling g_free() or g_object_unref().
1610 * <title>Using g_object_get(<!-- -->)</title>
1611 * An example of using g_object_get() to get the contents
1612 * of three properties - one of type #G_TYPE_INT,
1613 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1619 * g_object_get (my_object,
1620 * "int-property", &intval,
1621 * "str-property", &strval,
1622 * "obj-property", &objval,
1625 * // Do something with intval, strval, objval
1628 * g_object_unref (objval);
1633 g_object_get (gpointer _object
,
1634 const gchar
*first_property_name
,
1637 GObject
*object
= _object
;
1640 g_return_if_fail (G_IS_OBJECT (object
));
1642 va_start (var_args
, first_property_name
);
1643 g_object_get_valist (object
, first_property_name
, var_args
);
1648 * g_object_set_property:
1649 * @object: a #GObject
1650 * @property_name: the name of the property to set
1653 * Sets a property on an object.
1656 g_object_set_property (GObject
*object
,
1657 const gchar
*property_name
,
1658 const GValue
*value
)
1660 GObjectNotifyQueue
*nqueue
;
1663 g_return_if_fail (G_IS_OBJECT (object
));
1664 g_return_if_fail (property_name
!= NULL
);
1665 g_return_if_fail (G_IS_VALUE (value
));
1667 g_object_ref (object
);
1668 nqueue
= g_object_notify_queue_freeze (object
, &property_notify_context
);
1670 pspec
= g_param_spec_pool_lookup (pspec_pool
,
1672 G_OBJECT_TYPE (object
),
1675 g_warning ("%s: object class `%s' has no property named `%s'",
1677 G_OBJECT_TYPE_NAME (object
),
1679 else if (!(pspec
->flags
& G_PARAM_WRITABLE
))
1680 g_warning ("%s: property `%s' of object class `%s' is not writable",
1683 G_OBJECT_TYPE_NAME (object
));
1684 else if ((pspec
->flags
& G_PARAM_CONSTRUCT_ONLY
) && !object_in_construction_list (object
))
1685 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1686 G_STRFUNC
, pspec
->name
, G_OBJECT_TYPE_NAME (object
));
1688 object_set_property (object
, pspec
, value
, nqueue
);
1690 g_object_notify_queue_thaw (object
, nqueue
);
1691 g_object_unref (object
);
1695 * g_object_get_property:
1696 * @object: a #GObject
1697 * @property_name: the name of the property to get
1698 * @value: return location for the property value
1700 * Gets a property of an object.
1702 * In general, a copy is made of the property contents and the caller is
1703 * responsible for freeing the memory by calling g_value_unset().
1705 * Note that g_object_get_property() is really intended for language
1706 * bindings, g_object_get() is much more convenient for C programming.
1709 g_object_get_property (GObject
*object
,
1710 const gchar
*property_name
,
1715 g_return_if_fail (G_IS_OBJECT (object
));
1716 g_return_if_fail (property_name
!= NULL
);
1717 g_return_if_fail (G_IS_VALUE (value
));
1719 g_object_ref (object
);
1721 pspec
= g_param_spec_pool_lookup (pspec_pool
,
1723 G_OBJECT_TYPE (object
),
1726 g_warning ("%s: object class `%s' has no property named `%s'",
1728 G_OBJECT_TYPE_NAME (object
),
1730 else if (!(pspec
->flags
& G_PARAM_READABLE
))
1731 g_warning ("%s: property `%s' of object class `%s' is not readable",
1734 G_OBJECT_TYPE_NAME (object
));
1737 GValue
*prop_value
, tmp_value
= { 0, };
1739 /* auto-conversion of the callers value type
1741 if (G_VALUE_TYPE (value
) == G_PARAM_SPEC_VALUE_TYPE (pspec
))
1743 g_value_reset (value
);
1746 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec
), G_VALUE_TYPE (value
)))
1748 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1749 G_STRFUNC
, pspec
->name
,
1750 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec
)),
1751 G_VALUE_TYPE_NAME (value
));
1752 g_object_unref (object
);
1757 g_value_init (&tmp_value
, G_PARAM_SPEC_VALUE_TYPE (pspec
));
1758 prop_value
= &tmp_value
;
1760 object_get_property (object
, pspec
, prop_value
);
1761 if (prop_value
!= value
)
1763 g_value_transform (prop_value
, value
);
1764 g_value_unset (&tmp_value
);
1768 g_object_unref (object
);
1773 * @object: a #GObject
1774 * @signal_spec: the spec for the first signal
1775 * @...: #GCallback for the first signal, followed by data for the
1776 * first signal, followed optionally by more signal
1777 * spec/callback/data triples, followed by %NULL
1779 * A convenience function to connect multiple signals at once.
1781 * The signal specs expected by this function have the form
1782 * "modifier::signal_name", where modifier can be one of the following:
1785 * <term>signal</term>
1787 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1788 * </para></listitem>
1791 * <term>object_signal</term>
1792 * <term>object-signal</term>
1794 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1795 * </para></listitem>
1798 * <term>swapped_signal</term>
1799 * <term>swapped-signal</term>
1801 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1802 * </para></listitem>
1805 * <term>swapped_object_signal</term>
1806 * <term>swapped-object-signal</term>
1808 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1809 * </para></listitem>
1812 * <term>signal_after</term>
1813 * <term>signal-after</term>
1815 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1816 * </para></listitem>
1819 * <term>object_signal_after</term>
1820 * <term>object-signal-after</term>
1822 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1823 * </para></listitem>
1826 * <term>swapped_signal_after</term>
1827 * <term>swapped-signal-after</term>
1829 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1830 * </para></listitem>
1833 * <term>swapped_object_signal_after</term>
1834 * <term>swapped-object-signal-after</term>
1836 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1837 * </para></listitem>
1842 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1843 * "type", GTK_WINDOW_POPUP,
1846 * "signal::event", gtk_menu_window_event, menu,
1847 * "signal::size_request", gtk_menu_window_size_request, menu,
1848 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1855 g_object_connect (gpointer _object
,
1856 const gchar
*signal_spec
,
1859 GObject
*object
= _object
;
1862 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
1863 g_return_val_if_fail (object
->ref_count
> 0, object
);
1865 va_start (var_args
, signal_spec
);
1868 GCallback callback
= va_arg (var_args
, GCallback
);
1869 gpointer data
= va_arg (var_args
, gpointer
);
1872 if (strncmp (signal_spec
, "signal::", 8) == 0)
1873 sid
= g_signal_connect_data (object
, signal_spec
+ 8,
1874 callback
, data
, NULL
,
1876 else if (strncmp (signal_spec
, "object_signal::", 15) == 0 ||
1877 strncmp (signal_spec
, "object-signal::", 15) == 0)
1878 sid
= g_signal_connect_object (object
, signal_spec
+ 15,
1881 else if (strncmp (signal_spec
, "swapped_signal::", 16) == 0 ||
1882 strncmp (signal_spec
, "swapped-signal::", 16) == 0)
1883 sid
= g_signal_connect_data (object
, signal_spec
+ 16,
1884 callback
, data
, NULL
,
1886 else if (strncmp (signal_spec
, "swapped_object_signal::", 23) == 0 ||
1887 strncmp (signal_spec
, "swapped-object-signal::", 23) == 0)
1888 sid
= g_signal_connect_object (object
, signal_spec
+ 23,
1891 else if (strncmp (signal_spec
, "signal_after::", 14) == 0 ||
1892 strncmp (signal_spec
, "signal-after::", 14) == 0)
1893 sid
= g_signal_connect_data (object
, signal_spec
+ 14,
1894 callback
, data
, NULL
,
1896 else if (strncmp (signal_spec
, "object_signal_after::", 21) == 0 ||
1897 strncmp (signal_spec
, "object-signal-after::", 21) == 0)
1898 sid
= g_signal_connect_object (object
, signal_spec
+ 21,
1901 else if (strncmp (signal_spec
, "swapped_signal_after::", 22) == 0 ||
1902 strncmp (signal_spec
, "swapped-signal-after::", 22) == 0)
1903 sid
= g_signal_connect_data (object
, signal_spec
+ 22,
1904 callback
, data
, NULL
,
1905 G_CONNECT_SWAPPED
| G_CONNECT_AFTER
);
1906 else if (strncmp (signal_spec
, "swapped_object_signal_after::", 29) == 0 ||
1907 strncmp (signal_spec
, "swapped-object-signal-after::", 29) == 0)
1908 sid
= g_signal_connect_object (object
, signal_spec
+ 29,
1910 G_CONNECT_SWAPPED
| G_CONNECT_AFTER
);
1913 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC
, signal_spec
);
1916 signal_spec
= va_arg (var_args
, gchar
*);
1924 * g_object_disconnect:
1925 * @object: a #GObject
1926 * @signal_spec: the spec for the first signal
1927 * @...: #GCallback for the first signal, followed by data for the first signal,
1928 * followed optionally by more signal spec/callback/data triples,
1931 * A convenience function to disconnect multiple signals at once.
1933 * The signal specs expected by this function have the form
1934 * "any_signal", which means to disconnect any signal with matching
1935 * callback and data, or "any_signal::signal_name", which only
1936 * disconnects the signal named "signal_name".
1939 g_object_disconnect (gpointer _object
,
1940 const gchar
*signal_spec
,
1943 GObject
*object
= _object
;
1946 g_return_if_fail (G_IS_OBJECT (object
));
1947 g_return_if_fail (object
->ref_count
> 0);
1949 va_start (var_args
, signal_spec
);
1952 GCallback callback
= va_arg (var_args
, GCallback
);
1953 gpointer data
= va_arg (var_args
, gpointer
);
1954 guint sid
= 0, detail
= 0, mask
= 0;
1956 if (strncmp (signal_spec
, "any_signal::", 12) == 0 ||
1957 strncmp (signal_spec
, "any-signal::", 12) == 0)
1960 mask
= G_SIGNAL_MATCH_ID
| G_SIGNAL_MATCH_FUNC
| G_SIGNAL_MATCH_DATA
;
1962 else if (strcmp (signal_spec
, "any_signal") == 0 ||
1963 strcmp (signal_spec
, "any-signal") == 0)
1966 mask
= G_SIGNAL_MATCH_FUNC
| G_SIGNAL_MATCH_DATA
;
1970 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC
, signal_spec
);
1974 if ((mask
& G_SIGNAL_MATCH_ID
) &&
1975 !g_signal_parse_name (signal_spec
, G_OBJECT_TYPE (object
), &sid
, &detail
, FALSE
))
1976 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC
, signal_spec
);
1977 else if (!g_signal_handlers_disconnect_matched (object
, mask
| (detail
? G_SIGNAL_MATCH_DETAIL
: 0),
1979 NULL
, (gpointer
)callback
, data
))
1980 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC
, callback
, data
);
1981 signal_spec
= va_arg (var_args
, gchar
*);
1992 } weak_refs
[1]; /* flexible array */
1996 weak_refs_notify (gpointer data
)
1998 WeakRefStack
*wstack
= data
;
2001 for (i
= 0; i
< wstack
->n_weak_refs
; i
++)
2002 wstack
->weak_refs
[i
].notify (wstack
->weak_refs
[i
].data
, wstack
->object
);
2007 * g_object_weak_ref:
2008 * @object: #GObject to reference weakly
2009 * @notify: callback to invoke before the object is freed
2010 * @data: extra data to pass to notify
2012 * Adds a weak reference callback to an object. Weak references are
2013 * used for notification when an object is finalized. They are called
2014 * "weak references" because they allow you to safely hold a pointer
2015 * to an object without calling g_object_ref() (g_object_ref() adds a
2016 * strong reference, that is, forces the object to stay alive).
2019 g_object_weak_ref (GObject
*object
,
2023 WeakRefStack
*wstack
;
2026 g_return_if_fail (G_IS_OBJECT (object
));
2027 g_return_if_fail (notify
!= NULL
);
2028 g_return_if_fail (object
->ref_count
>= 1);
2030 wstack
= g_datalist_id_remove_no_notify (&object
->qdata
, quark_weak_refs
);
2033 i
= wstack
->n_weak_refs
++;
2034 wstack
= g_realloc (wstack
, sizeof (*wstack
) + sizeof (wstack
->weak_refs
[0]) * i
);
2038 wstack
= g_renew (WeakRefStack
, NULL
, 1);
2039 wstack
->object
= object
;
2040 wstack
->n_weak_refs
= 1;
2043 wstack
->weak_refs
[i
].notify
= notify
;
2044 wstack
->weak_refs
[i
].data
= data
;
2045 g_datalist_id_set_data_full (&object
->qdata
, quark_weak_refs
, wstack
, weak_refs_notify
);
2049 * g_object_weak_unref:
2050 * @object: #GObject to remove a weak reference from
2051 * @notify: callback to search for
2052 * @data: data to search for
2054 * Removes a weak reference callback to an object.
2057 g_object_weak_unref (GObject
*object
,
2061 WeakRefStack
*wstack
;
2062 gboolean found_one
= FALSE
;
2064 g_return_if_fail (G_IS_OBJECT (object
));
2065 g_return_if_fail (notify
!= NULL
);
2067 wstack
= g_datalist_id_get_data (&object
->qdata
, quark_weak_refs
);
2072 for (i
= 0; i
< wstack
->n_weak_refs
; i
++)
2073 if (wstack
->weak_refs
[i
].notify
== notify
&&
2074 wstack
->weak_refs
[i
].data
== data
)
2077 wstack
->n_weak_refs
-= 1;
2078 if (i
!= wstack
->n_weak_refs
)
2079 wstack
->weak_refs
[i
] = wstack
->weak_refs
[wstack
->n_weak_refs
];
2085 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC
, notify
, data
);
2089 * g_object_add_weak_pointer:
2090 * @object: The object that should be weak referenced.
2091 * @weak_pointer_location: The memory address of a pointer.
2093 * Adds a weak reference from weak_pointer to @object to indicate that
2094 * the pointer located at @weak_pointer_location is only valid during
2095 * the lifetime of @object. When the @object is finalized,
2096 * @weak_pointer will be set to %NULL.
2099 g_object_add_weak_pointer (GObject
*object
,
2100 gpointer
*weak_pointer_location
)
2102 g_return_if_fail (G_IS_OBJECT (object
));
2103 g_return_if_fail (weak_pointer_location
!= NULL
);
2105 g_object_weak_ref (object
,
2106 (GWeakNotify
) g_nullify_pointer
,
2107 weak_pointer_location
);
2111 * g_object_remove_weak_pointer:
2112 * @object: The object that is weak referenced.
2113 * @weak_pointer_location: The memory address of a pointer.
2115 * Removes a weak reference from @object that was previously added
2116 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2117 * to match the one used with g_object_add_weak_pointer().
2120 g_object_remove_weak_pointer (GObject
*object
,
2121 gpointer
*weak_pointer_location
)
2123 g_return_if_fail (G_IS_OBJECT (object
));
2124 g_return_if_fail (weak_pointer_location
!= NULL
);
2126 g_object_weak_unref (object
,
2127 (GWeakNotify
) g_nullify_pointer
,
2128 weak_pointer_location
);
2132 object_floating_flag_handler (GObject
*object
,
2138 case +1: /* force floating if possible */
2140 oldvalue
= g_atomic_pointer_get (&object
->qdata
);
2141 while (!g_atomic_pointer_compare_and_exchange ((void**) &object
->qdata
, oldvalue
,
2142 (gpointer
) ((gsize
) oldvalue
| OBJECT_FLOATING_FLAG
)));
2143 return (gsize
) oldvalue
& OBJECT_FLOATING_FLAG
;
2144 case -1: /* sink if possible */
2146 oldvalue
= g_atomic_pointer_get (&object
->qdata
);
2147 while (!g_atomic_pointer_compare_and_exchange ((void**) &object
->qdata
, oldvalue
,
2148 (gpointer
) ((gsize
) oldvalue
& ~(gsize
) OBJECT_FLOATING_FLAG
)));
2149 return (gsize
) oldvalue
& OBJECT_FLOATING_FLAG
;
2150 default: /* check floating */
2151 return 0 != ((gsize
) g_atomic_pointer_get (&object
->qdata
) & OBJECT_FLOATING_FLAG
);
2156 * g_object_is_floating:
2157 * @object: a #GObject
2159 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2164 * Returns: %TRUE if @object has a floating reference
2167 g_object_is_floating (gpointer _object
)
2169 GObject
*object
= _object
;
2170 g_return_val_if_fail (G_IS_OBJECT (object
), FALSE
);
2171 return floating_flag_handler (object
, 0);
2175 * g_object_ref_sink:
2176 * @object: a #GObject
2178 * Increase the reference count of @object, and possibly remove the
2179 * <link linkend="floating-ref">floating</link> reference, if @object
2180 * has a floating reference.
2182 * In other words, if the object is floating, then this call "assumes
2183 * ownership" of the floating reference, converting it to a normal
2184 * reference by clearing the floating flag while leaving the reference
2185 * count unchanged. If the object is not floating, then this call
2186 * adds a new normal reference increasing the reference count by one.
2193 g_object_ref_sink (gpointer _object
)
2195 GObject
*object
= _object
;
2196 gboolean was_floating
;
2197 g_return_val_if_fail (G_IS_OBJECT (object
), object
);
2198 g_return_val_if_fail (object
->ref_count
>= 1, object
);
2199 g_object_ref (object
);
2200 was_floating
= floating_flag_handler (object
, -1);
2202 g_object_unref (object
);
2207 * g_object_force_floating:
2208 * @object: a #GObject
2210 * This function is intended for #GObject implementations to re-enforce a
2211 * <link linkend="floating-ref">floating</link> object reference.
2212 * Doing this is seldomly required, all
2213 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2214 * usually just needs to be sunken by calling g_object_ref_sink().
2219 g_object_force_floating (GObject
*object
)
2221 gboolean was_floating
;
2222 g_return_if_fail (G_IS_OBJECT (object
));
2223 g_return_if_fail (object
->ref_count
>= 1);
2225 was_floating
= floating_flag_handler (object
, +1);
2230 guint n_toggle_refs
;
2232 GToggleNotify notify
;
2234 } toggle_refs
[1]; /* flexible array */
2238 toggle_refs_notify (GObject
*object
,
2239 gboolean is_last_ref
)
2241 ToggleRefStack
*tstack
= g_datalist_id_get_data (&object
->qdata
, quark_toggle_refs
);
2243 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2244 * will only be notified when there is exactly one of them.
2246 g_assert (tstack
->n_toggle_refs
== 1);
2247 tstack
->toggle_refs
[0].notify (tstack
->toggle_refs
[0].data
, tstack
->object
, is_last_ref
);
2251 * g_object_add_toggle_ref:
2252 * @object: a #GObject
2253 * @notify: a function to call when this reference is the
2254 * last reference to the object, or is no longer
2255 * the last reference.
2256 * @data: data to pass to @notify
2258 * Increases the reference count of the object by one and sets a
2259 * callback to be called when all other references to the object are
2260 * dropped, or when this is already the last reference to the object
2261 * and another reference is established.
2263 * This functionality is intended for binding @object to a proxy
2264 * object managed by another memory manager. This is done with two
2265 * paired references: the strong reference added by
2266 * g_object_add_toggle_ref() and a reverse reference to the proxy
2267 * object which is either a strong reference or weak reference.
2269 * The setup is that when there are no other references to @object,
2270 * only a weak reference is held in the reverse direction from @object
2271 * to the proxy object, but when there are other references held to
2272 * @object, a strong reference is held. The @notify callback is called
2273 * when the reference from @object to the proxy object should be
2274 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2275 * true) or weak to strong (@is_last_ref false).
2277 * Since a (normal) reference must be held to the object before
2278 * calling g_object_toggle_ref(), the initial state of the reverse
2279 * link is always strong.
2281 * Multiple toggle references may be added to the same gobject,
2282 * however if there are multiple toggle references to an object, none
2283 * of them will ever be notified until all but one are removed. For
2284 * this reason, you should only ever use a toggle reference if there
2285 * is important state in the proxy object.
2290 g_object_add_toggle_ref (GObject
*object
,
2291 GToggleNotify notify
,
2294 ToggleRefStack
*tstack
;
2297 g_return_if_fail (G_IS_OBJECT (object
));
2298 g_return_if_fail (notify
!= NULL
);
2299 g_return_if_fail (object
->ref_count
>= 1);
2301 g_object_ref (object
);
2303 tstack
= g_datalist_id_remove_no_notify (&object
->qdata
, quark_toggle_refs
);
2306 i
= tstack
->n_toggle_refs
++;
2307 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2308 * in tstate->toggle_refs */
2309 tstack
= g_realloc (tstack
, sizeof (*tstack
) + sizeof (tstack
->toggle_refs
[0]) * i
);
2313 tstack
= g_renew (ToggleRefStack
, NULL
, 1);
2314 tstack
->object
= object
;
2315 tstack
->n_toggle_refs
= 1;
2319 /* Set a flag for fast lookup after adding the first toggle reference */
2320 if (tstack
->n_toggle_refs
== 1)
2321 g_datalist_set_flags (&object
->qdata
, OBJECT_HAS_TOGGLE_REF_FLAG
);
2323 tstack
->toggle_refs
[i
].notify
= notify
;
2324 tstack
->toggle_refs
[i
].data
= data
;
2325 g_datalist_id_set_data_full (&object
->qdata
, quark_toggle_refs
, tstack
,
2326 (GDestroyNotify
)g_free
);
2330 * g_object_remove_toggle_ref:
2331 * @object: a #GObject
2332 * @notify: a function to call when this reference is the
2333 * last reference to the object, or is no longer
2334 * the last reference.
2335 * @data: data to pass to @notify
2337 * Removes a reference added with g_object_add_toggle_ref(). The
2338 * reference count of the object is decreased by one.
2343 g_object_remove_toggle_ref (GObject
*object
,
2344 GToggleNotify notify
,
2347 ToggleRefStack
*tstack
;
2348 gboolean found_one
= FALSE
;
2350 g_return_if_fail (G_IS_OBJECT (object
));
2351 g_return_if_fail (notify
!= NULL
);
2353 tstack
= g_datalist_id_get_data (&object
->qdata
, quark_toggle_refs
);
2358 for (i
= 0; i
< tstack
->n_toggle_refs
; i
++)
2359 if (tstack
->toggle_refs
[i
].notify
== notify
&&
2360 tstack
->toggle_refs
[i
].data
== data
)
2363 tstack
->n_toggle_refs
-= 1;
2364 if (i
!= tstack
->n_toggle_refs
)
2365 tstack
->toggle_refs
[i
] = tstack
->toggle_refs
[tstack
->n_toggle_refs
];
2367 if (tstack
->n_toggle_refs
== 0)
2368 g_datalist_unset_flags (&object
->qdata
, OBJECT_HAS_TOGGLE_REF_FLAG
);
2370 g_object_unref (object
);
2377 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC
, notify
, data
);
2382 * @object: a #GObject
2384 * Increases the reference count of @object.
2386 * Returns: the same @object
2389 g_object_ref (gpointer _object
)
2391 GObject
*object
= _object
;
2394 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
2395 g_return_val_if_fail (object
->ref_count
> 0, NULL
);
2397 #ifdef G_ENABLE_DEBUG
2398 if (g_trap_object_ref
== object
)
2400 #endif /* G_ENABLE_DEBUG */
2403 old_val
= g_atomic_int_exchange_and_add ((int *)&object
->ref_count
, 1);
2405 if (old_val
== 1 && OBJECT_HAS_TOGGLE_REF (object
))
2406 toggle_refs_notify (object
, FALSE
);
2413 * @object: a #GObject
2415 * Decreases the reference count of @object. When its reference count
2416 * drops to 0, the object is finalized (i.e. its memory is freed).
2419 g_object_unref (gpointer _object
)
2421 GObject
*object
= _object
;
2425 g_return_if_fail (G_IS_OBJECT (object
));
2426 g_return_if_fail (object
->ref_count
> 0);
2428 #ifdef G_ENABLE_DEBUG
2429 if (g_trap_object_ref
== object
)
2431 #endif /* G_ENABLE_DEBUG */
2433 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2434 retry_atomic_decrement1
:
2435 old_ref
= g_atomic_int_get (&object
->ref_count
);
2438 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2439 gboolean has_toggle_ref
= OBJECT_HAS_TOGGLE_REF (object
);
2441 if (!g_atomic_int_compare_and_exchange ((int *)&object
->ref_count
, old_ref
, old_ref
- 1))
2442 goto retry_atomic_decrement1
;
2444 /* if we went from 2->1 we need to notify toggle refs if any */
2445 if (old_ref
== 2 && has_toggle_ref
) /* The last ref being held in this case is owned by the toggle_ref */
2446 toggle_refs_notify (object
, TRUE
);
2450 /* we are about tp remove the last reference */
2451 G_OBJECT_GET_CLASS (object
)->dispose (object
);
2453 /* may have been re-referenced meanwhile */
2454 retry_atomic_decrement2
:
2455 old_ref
= g_atomic_int_get ((int *)&object
->ref_count
);
2458 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2459 gboolean has_toggle_ref
= OBJECT_HAS_TOGGLE_REF (object
);
2461 if (!g_atomic_int_compare_and_exchange ((int *)&object
->ref_count
, old_ref
, old_ref
- 1))
2462 goto retry_atomic_decrement2
;
2464 /* if we went from 2->1 we need to notify toggle refs if any */
2465 if (old_ref
== 2 && has_toggle_ref
) /* The last ref being held in this case is owned by the toggle_ref */
2466 toggle_refs_notify (object
, TRUE
);
2471 /* we are still in the process of taking away the last ref */
2472 g_datalist_id_set_data (&object
->qdata
, quark_closure_array
, NULL
);
2473 g_signal_handlers_destroy (object
);
2474 g_datalist_id_set_data (&object
->qdata
, quark_weak_refs
, NULL
);
2476 /* decrement the last reference */
2477 is_zero
= g_atomic_int_dec_and_test ((int *)&object
->ref_count
);
2479 /* may have been re-referenced meanwhile */
2480 if (G_LIKELY (is_zero
))
2482 G_OBJECT_GET_CLASS (object
)->finalize (object
);
2483 #ifdef G_ENABLE_DEBUG
2486 /* catch objects not chaining finalize handlers */
2487 G_LOCK (debug_objects
);
2488 g_assert (g_hash_table_lookup (debug_objects_ht
, object
) == NULL
);
2489 G_UNLOCK (debug_objects
);
2491 #endif /* G_ENABLE_DEBUG */
2492 g_type_free_instance ((GTypeInstance
*) object
);
2498 * g_object_get_qdata:
2499 * @object: The GObject to get a stored user data pointer from
2500 * @quark: A #GQuark, naming the user data pointer
2502 * This function gets back user data pointers stored via
2503 * g_object_set_qdata().
2505 * Returns: The user data pointer set, or %NULL
2508 g_object_get_qdata (GObject
*object
,
2511 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
2513 return quark
? g_datalist_id_get_data (&object
->qdata
, quark
) : NULL
;
2517 * g_object_set_qdata:
2518 * @object: The GObject to set store a user data pointer
2519 * @quark: A #GQuark, naming the user data pointer
2520 * @data: An opaque user data pointer
2522 * This sets an opaque, named pointer on an object.
2523 * The name is specified through a #GQuark (retrived e.g. via
2524 * g_quark_from_static_string()), and the pointer
2525 * can be gotten back from the @object with g_object_get_qdata()
2526 * until the @object is finalized.
2527 * Setting a previously set user data pointer, overrides (frees)
2528 * the old pointer set, using #NULL as pointer essentially
2529 * removes the data stored.
2532 g_object_set_qdata (GObject
*object
,
2536 g_return_if_fail (G_IS_OBJECT (object
));
2537 g_return_if_fail (quark
> 0);
2539 g_datalist_id_set_data (&object
->qdata
, quark
, data
);
2543 * g_object_set_qdata_full:
2544 * @object: The GObject to set store a user data pointer
2545 * @quark: A #GQuark, naming the user data pointer
2546 * @data: An opaque user data pointer
2547 * @destroy: Function to invoke with @data as argument, when @data
2550 * This function works like g_object_set_qdata(), but in addition,
2551 * a void (*destroy) (gpointer) function may be specified which is
2552 * called with @data as argument when the @object is finalized, or
2553 * the data is being overwritten by a call to g_object_set_qdata()
2554 * with the same @quark.
2557 g_object_set_qdata_full (GObject
*object
,
2560 GDestroyNotify destroy
)
2562 g_return_if_fail (G_IS_OBJECT (object
));
2563 g_return_if_fail (quark
> 0);
2565 g_datalist_id_set_data_full (&object
->qdata
, quark
, data
,
2566 data
? destroy
: (GDestroyNotify
) NULL
);
2570 * g_object_steal_qdata:
2571 * @object: The GObject to get a stored user data pointer from
2572 * @quark: A #GQuark, naming the user data pointer
2574 * This function gets back user data pointers stored via
2575 * g_object_set_qdata() and removes the @data from object
2576 * without invoking its destroy() function (if any was
2578 * Usually, calling this function is only required to update
2579 * user data pointers with a destroy notifier, for example:
2582 * object_add_to_user_list (GObject *object,
2583 * const gchar *new_string)
2585 * // the quark, naming the object data
2586 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2587 * // retrive the old string list
2588 * GList *list = g_object_steal_qdata (object, quark_string_list);
2590 * // prepend new string
2591 * list = g_list_prepend (list, g_strdup (new_string));
2592 * // this changed 'list', so we need to set it again
2593 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2596 * free_string_list (gpointer data)
2598 * GList *node, *list = data;
2600 * for (node = list; node; node = node->next)
2601 * g_free (node->data);
2602 * g_list_free (list);
2605 * Using g_object_get_qdata() in the above example, instead of
2606 * g_object_steal_qdata() would have left the destroy function set,
2607 * and thus the partial string list would have been freed upon
2608 * g_object_set_qdata_full().
2610 * Returns: The user data pointer set, or %NULL
2613 g_object_steal_qdata (GObject
*object
,
2616 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
2617 g_return_val_if_fail (quark
> 0, NULL
);
2619 return g_datalist_id_remove_no_notify (&object
->qdata
, quark
);
2623 * g_object_get_data:
2624 * @object: #GObject containing the associations
2625 * @key: name of the key for that association
2627 * Gets a named field from the objects table of associations (see g_object_set_data()).
2629 * Returns: the data if found, or %NULL if no such data exists.
2632 g_object_get_data (GObject
*object
,
2637 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
2638 g_return_val_if_fail (key
!= NULL
, NULL
);
2640 quark
= g_quark_try_string (key
);
2642 return quark
? g_datalist_id_get_data (&object
->qdata
, quark
) : NULL
;
2646 * g_object_set_data:
2647 * @object: #GObject containing the associations.
2648 * @key: name of the key
2649 * @data: data to associate with that key
2651 * Each object carries around a table of associations from
2652 * strings to pointers. This function lets you set an association.
2654 * If the object already had an association with that name,
2655 * the old association will be destroyed.
2658 g_object_set_data (GObject
*object
,
2662 g_return_if_fail (G_IS_OBJECT (object
));
2663 g_return_if_fail (key
!= NULL
);
2665 g_datalist_id_set_data (&object
->qdata
, g_quark_from_string (key
), data
);
2669 * g_object_set_data_full:
2670 * @object: #GObject containing the associations
2671 * @key: name of the key
2672 * @data: data to associate with that key
2673 * @destroy: function to call when the association is destroyed
2675 * Like g_object_set_data() except it adds notification
2676 * for when the association is destroyed, either by setting it
2677 * to a different value or when the object is destroyed.
2679 * Note that the @destroy callback is not called if @data is %NULL.
2682 g_object_set_data_full (GObject
*object
,
2685 GDestroyNotify destroy
)
2687 g_return_if_fail (G_IS_OBJECT (object
));
2688 g_return_if_fail (key
!= NULL
);
2690 g_datalist_id_set_data_full (&object
->qdata
, g_quark_from_string (key
), data
,
2691 data
? destroy
: (GDestroyNotify
) NULL
);
2695 * g_object_steal_data:
2696 * @object: #GObject containing the associations
2697 * @key: name of the key
2699 * Remove a specified datum from the object's data associations,
2700 * without invoking the association's destroy handler.
2702 * Returns: the data if found, or %NULL if no such data exists.
2705 g_object_steal_data (GObject
*object
,
2710 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
2711 g_return_val_if_fail (key
!= NULL
, NULL
);
2713 quark
= g_quark_try_string (key
);
2715 return quark
? g_datalist_id_remove_no_notify (&object
->qdata
, quark
) : NULL
;
2719 g_value_object_init (GValue
*value
)
2721 value
->data
[0].v_pointer
= NULL
;
2725 g_value_object_free_value (GValue
*value
)
2727 if (value
->data
[0].v_pointer
)
2728 g_object_unref (value
->data
[0].v_pointer
);
2732 g_value_object_copy_value (const GValue
*src_value
,
2735 if (src_value
->data
[0].v_pointer
)
2736 dest_value
->data
[0].v_pointer
= g_object_ref (src_value
->data
[0].v_pointer
);
2738 dest_value
->data
[0].v_pointer
= NULL
;
2742 g_value_object_transform_value (const GValue
*src_value
,
2745 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
)))
2746 dest_value
->data
[0].v_pointer
= g_object_ref (src_value
->data
[0].v_pointer
);
2748 dest_value
->data
[0].v_pointer
= NULL
;
2752 g_value_object_peek_pointer (const GValue
*value
)
2754 return value
->data
[0].v_pointer
;
2758 g_value_object_collect_value (GValue
*value
,
2759 guint n_collect_values
,
2760 GTypeCValue
*collect_values
,
2761 guint collect_flags
)
2763 if (collect_values
[0].v_pointer
)
2765 GObject
*object
= collect_values
[0].v_pointer
;
2767 if (object
->g_type_instance
.g_class
== NULL
)
2768 return g_strconcat ("invalid unclassed object pointer for value type `",
2769 G_VALUE_TYPE_NAME (value
),
2772 else if (!g_value_type_compatible (G_OBJECT_TYPE (object
), G_VALUE_TYPE (value
)))
2773 return g_strconcat ("invalid object type `",
2774 G_OBJECT_TYPE_NAME (object
),
2775 "' for value type `",
2776 G_VALUE_TYPE_NAME (value
),
2779 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2780 value
->data
[0].v_pointer
= g_object_ref (object
);
2783 value
->data
[0].v_pointer
= NULL
;
2789 g_value_object_lcopy_value (const GValue
*value
,
2790 guint n_collect_values
,
2791 GTypeCValue
*collect_values
,
2792 guint collect_flags
)
2794 GObject
**object_p
= collect_values
[0].v_pointer
;
2797 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value
));
2799 if (!value
->data
[0].v_pointer
)
2801 else if (collect_flags
& G_VALUE_NOCOPY_CONTENTS
)
2802 *object_p
= value
->data
[0].v_pointer
;
2804 *object_p
= g_object_ref (value
->data
[0].v_pointer
);
2810 * g_value_set_object:
2811 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2812 * @v_object: object value to be set
2814 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2816 * g_value_set_object() increases the reference count of @v_object
2817 * (the #GValue holds a reference to @v_object). If you do not wish
2818 * to increase the reference count of the object (i.e. you wish to
2819 * pass your current reference to the #GValue because you no longer
2820 * need it), use g_value_take_object() instead.
2822 * It is important that your #GValue holds a reference to @v_object (either its
2823 * own, or one it has taken) to ensure that the object won't be destroyed while
2824 * the #GValue still exists).
2827 g_value_set_object (GValue
*value
,
2832 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value
));
2834 old
= value
->data
[0].v_pointer
;
2838 g_return_if_fail (G_IS_OBJECT (v_object
));
2839 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object
), G_VALUE_TYPE (value
)));
2841 value
->data
[0].v_pointer
= v_object
;
2842 g_object_ref (value
->data
[0].v_pointer
);
2845 value
->data
[0].v_pointer
= NULL
;
2848 g_object_unref (old
);
2852 * g_value_set_object_take_ownership:
2853 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2854 * @v_object: object value to be set
2856 * This is an internal function introduced mainly for C marshallers.
2858 * Deprecated: 2.4: Use g_value_take_object() instead.
2861 g_value_set_object_take_ownership (GValue
*value
,
2864 g_value_take_object (value
, v_object
);
2868 * g_value_take_object:
2869 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2870 * @v_object: object value to be set
2872 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2873 * and takes over the ownership of the callers reference to @v_object;
2874 * the caller doesn't have to unref it any more (i.e. the reference
2875 * count of the object is not increased).
2877 * If you want the #GValue to hold its own reference to @v_object, use
2878 * g_value_set_object() instead.
2883 g_value_take_object (GValue
*value
,
2886 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value
));
2888 if (value
->data
[0].v_pointer
)
2890 g_object_unref (value
->data
[0].v_pointer
);
2891 value
->data
[0].v_pointer
= NULL
;
2896 g_return_if_fail (G_IS_OBJECT (v_object
));
2897 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object
), G_VALUE_TYPE (value
)));
2899 value
->data
[0].v_pointer
= v_object
; /* we take over the reference count */
2904 * g_value_get_object:
2905 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2907 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2909 * Returns: object contents of @value
2912 g_value_get_object (const GValue
*value
)
2914 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value
), NULL
);
2916 return value
->data
[0].v_pointer
;
2920 * g_value_dup_object:
2921 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2923 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
2924 * its reference count.
2926 * Returns: object content of @value, should be unreferenced when no
2930 g_value_dup_object (const GValue
*value
)
2932 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value
), NULL
);
2934 return value
->data
[0].v_pointer
? g_object_ref (value
->data
[0].v_pointer
) : NULL
;
2938 * g_signal_connect_object:
2939 * @instance: the instance to connect to.
2940 * @detailed_signal: a string of the form "signal-name::detail".
2941 * @c_handler: the #GCallback to connect.
2942 * @gobject: the object to pass as data to @c_handler.
2943 * @connect_flags: a combination of #GConnnectFlags.
2945 * This is similar to g_signal_connect_data(), but uses a closure which
2946 * ensures that the @gobject stays alive during the call to @c_handler
2947 * by temporarily adding a reference count to @gobject.
2949 * Note that there is a bug in GObject that makes this function
2950 * much less useful than it might seem otherwise. Once @gobject is
2951 * disposed, the callback will no longer be called, but, the signal
2952 * handler is <emphasis>not</emphasis> currently disconnected. If the
2953 * @instance is itself being freed at the same time than this doesn't
2954 * matter, since the signal will automatically be removed, but
2955 * if @instance persists, then the signal handler will leak. You
2956 * should not remove the signal yourself because in a future versions of
2957 * GObject, the handler <emphasis>will</emphasis> automatically
2960 * It's possible to work around this problem in a way that will
2961 * continue to work with future versions of GObject by checking
2962 * that the signal handler is still connected before disconnected it:
2963 * <informalexample><programlisting>
2964 * if (g_signal_handler_is_connected (instance, id))
2965 * g_signal_handler_disconnect (instance, id);
2966 * </programlisting></informalexample>
2968 * Returns: the handler id.
2971 g_signal_connect_object (gpointer instance
,
2972 const gchar
*detailed_signal
,
2973 GCallback c_handler
,
2975 GConnectFlags connect_flags
)
2977 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2978 g_return_val_if_fail (detailed_signal
!= NULL
, 0);
2979 g_return_val_if_fail (c_handler
!= NULL
, 0);
2985 g_return_val_if_fail (G_IS_OBJECT (gobject
), 0);
2987 closure
= ((connect_flags
& G_CONNECT_SWAPPED
) ? g_cclosure_new_object_swap
: g_cclosure_new_object
) (c_handler
, gobject
);
2989 return g_signal_connect_closure (instance
, detailed_signal
, closure
, connect_flags
& G_CONNECT_AFTER
);
2992 return g_signal_connect_data (instance
, detailed_signal
, c_handler
, NULL
, NULL
, connect_flags
);
2998 GClosure
*closures
[1]; /* flexible array */
3000 /* don't change this structure without supplying an accessor for
3001 * watched closures, e.g.:
3002 * GSList* g_object_list_watched_closures (GObject *object)
3005 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3006 * carray = g_object_get_data (object, "GObject-closure-array");
3009 * GSList *slist = NULL;
3011 * for (i = 0; i < carray->n_closures; i++)
3012 * slist = g_slist_prepend (slist, carray->closures[i]);
3020 object_remove_closure (gpointer data
,
3023 GObject
*object
= data
;
3024 CArray
*carray
= g_object_get_qdata (object
, quark_closure_array
);
3027 for (i
= 0; i
< carray
->n_closures
; i
++)
3028 if (carray
->closures
[i
] == closure
)
3030 carray
->n_closures
--;
3031 if (i
< carray
->n_closures
)
3032 carray
->closures
[i
] = carray
->closures
[carray
->n_closures
];
3035 g_assert_not_reached ();
3039 destroy_closure_array (gpointer data
)
3041 CArray
*carray
= data
;
3042 GObject
*object
= carray
->object
;
3043 guint i
, n
= carray
->n_closures
;
3045 for (i
= 0; i
< n
; i
++)
3047 GClosure
*closure
= carray
->closures
[i
];
3049 /* removing object_remove_closure() upfront is probably faster than
3050 * letting it fiddle with quark_closure_array which is empty anyways
3052 g_closure_remove_invalidate_notifier (closure
, object
, object_remove_closure
);
3053 g_closure_invalidate (closure
);
3059 * g_object_watch_closure:
3060 * @object: GObject restricting lifetime of @closure
3061 * @closure: GClosure to watch
3063 * This function essentially limits the life time of the @closure to
3064 * the life time of the object. That is, when the object is finalized,
3065 * the @closure is invalidated by calling g_closure_invalidate() on
3066 * it, in order to prevent invocations of the closure with a finalized
3067 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3068 * added as marshal guards to the @closure, to ensure that an extra
3069 * reference count is held on @object during invocation of the
3070 * @closure. Usually, this function will be called on closures that
3071 * use this @object as closure data.
3074 g_object_watch_closure (GObject
*object
,
3080 g_return_if_fail (G_IS_OBJECT (object
));
3081 g_return_if_fail (closure
!= NULL
);
3082 g_return_if_fail (closure
->is_invalid
== FALSE
);
3083 g_return_if_fail (closure
->in_marshal
== FALSE
);
3084 g_return_if_fail (object
->ref_count
> 0); /* this doesn't work on finalizing objects */
3086 g_closure_add_invalidate_notifier (closure
, object
, object_remove_closure
);
3087 g_closure_add_marshal_guards (closure
,
3088 object
, (GClosureNotify
) g_object_ref
,
3089 object
, (GClosureNotify
) g_object_unref
);
3090 carray
= g_datalist_id_remove_no_notify (&object
->qdata
, quark_closure_array
);
3093 carray
= g_renew (CArray
, NULL
, 1);
3094 carray
->object
= object
;
3095 carray
->n_closures
= 1;
3100 i
= carray
->n_closures
++;
3101 carray
= g_realloc (carray
, sizeof (*carray
) + sizeof (carray
->closures
[0]) * i
);
3103 carray
->closures
[i
] = closure
;
3104 g_datalist_id_set_data_full (&object
->qdata
, quark_closure_array
, carray
, destroy_closure_array
);
3108 * g_closure_new_object:
3109 * @sizeof_closure: the size of the structure to allocate, must be at least
3110 * <literal>sizeof (GClosure)</literal>
3111 * @object: a #GObject pointer to store in the @data field of the newly
3112 * allocated #GClosure
3114 * A variant of g_closure_new_simple() which stores @object in the
3115 * @data field of the closure and calls g_object_watch_closure() on
3116 * @object and the created closure. This function is mainly useful
3117 * when implementing new types of closures.
3119 * Returns: a newly allocated #GClosure
3122 g_closure_new_object (guint sizeof_closure
,
3127 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
3128 g_return_val_if_fail (object
->ref_count
> 0, NULL
); /* this doesn't work on finalizing objects */
3130 closure
= g_closure_new_simple (sizeof_closure
, object
);
3131 g_object_watch_closure (object
, closure
);
3137 * g_cclosure_new_object:
3138 * @callback_func: the function to invoke
3139 * @object: a #GObject pointer to pass to @callback_func
3141 * A variant of g_cclosure_new() which uses @object as @user_data and
3142 * calls g_object_watch_closure() on @object and the created
3143 * closure. This function is useful when you have a callback closely
3144 * associated with a #GObject, and want the callback to no longer run
3145 * after the object is is freed.
3147 * Returns: a new #GCClosure
3150 g_cclosure_new_object (GCallback callback_func
,
3155 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
3156 g_return_val_if_fail (object
->ref_count
> 0, NULL
); /* this doesn't work on finalizing objects */
3157 g_return_val_if_fail (callback_func
!= NULL
, NULL
);
3159 closure
= g_cclosure_new (callback_func
, object
, NULL
);
3160 g_object_watch_closure (object
, closure
);
3166 * g_cclosure_new_object_swap:
3167 * @callback_func: the function to invoke
3168 * @object: a #GObject pointer to pass to @callback_func
3170 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3171 * and calls g_object_watch_closure() on @object and the created
3172 * closure. This function is useful when you have a callback closely
3173 * associated with a #GObject, and want the callback to no longer run
3174 * after the object is is freed.
3176 * Returns: a new #GCClosure
3179 g_cclosure_new_object_swap (GCallback callback_func
,
3184 g_return_val_if_fail (G_IS_OBJECT (object
), NULL
);
3185 g_return_val_if_fail (object
->ref_count
> 0, NULL
); /* this doesn't work on finalizing objects */
3186 g_return_val_if_fail (callback_func
!= NULL
, NULL
);
3188 closure
= g_cclosure_new_swap (callback_func
, object
, NULL
);
3189 g_object_watch_closure (object
, closure
);
3195 g_object_compat_control (gsize what
,
3201 case 1: /* floating base type */
3202 return G_TYPE_INITIALLY_UNOWNED
;
3203 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3204 floating_flag_handler
= (guint(*)(GObject
*,gint
)) data
;
3206 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3208 *pp
= floating_flag_handler
;
3215 G_DEFINE_TYPE (GInitiallyUnowned
, g_initially_unowned
, G_TYPE_OBJECT
);
3218 g_initially_unowned_init (GInitiallyUnowned
*object
)
3220 g_object_force_floating (object
);
3224 g_initially_unowned_class_init (GInitiallyUnownedClass
*klass
)
3228 #define __G_OBJECT_C__
3229 #include "gobjectaliasdef.c"