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, see <http://www.gnu.org/licenses/>.
17 #ifndef __G_OBJECT_H__
18 #define __G_OBJECT_H__
20 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
21 #error "Only <glib-object.h> can be included directly."
24 #include <gobject/gtype.h>
25 #include <gobject/gvalue.h>
26 #include <gobject/gparam.h>
27 #include <gobject/gclosure.h>
28 #include <gobject/gsignal.h>
29 #include <gobject/gboxed.h>
33 /* --- type macros --- */
36 * @type: Type id to check
38 * Check if the passed in type id is a %G_TYPE_OBJECT or derived from it.
40 * Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT.
42 #define G_TYPE_IS_OBJECT(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
45 * @object: Object which is subject to casting.
47 * Casts a #GObject or derived pointer into a (GObject*) pointer.
48 * Depending on the current debugging level, this function may invoke
49 * certain runtime checks to identify invalid casts.
51 #define G_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))
54 * @class: a valid #GObjectClass
56 * Casts a derived #GObjectClass structure into a #GObjectClass structure.
58 #define G_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass))
61 * @object: Instance to check for being a %G_TYPE_OBJECT.
63 * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT.
65 #define G_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_OBJECT))
68 * @class: a #GObjectClass
70 * Checks whether @class "is a" valid #GObjectClass structure of type
71 * %G_TYPE_OBJECT or derived.
73 #define G_IS_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT))
76 * @object: a #GObject instance.
78 * Get the class structure associated to a #GObject instance.
80 * Returns: pointer to object class structure.
82 #define G_OBJECT_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass))
85 * @object: Object to return the type id for.
87 * Get the type id of an object.
89 * Returns: Type id of @object.
91 #define G_OBJECT_TYPE(object) (G_TYPE_FROM_INSTANCE (object))
94 * @object: Object to return the type name for.
96 * Get the name of an object's type.
98 * Returns: Type name of @object. The string is owned by the type system and
99 * should not be freed.
101 #define G_OBJECT_TYPE_NAME(object) (g_type_name (G_OBJECT_TYPE (object)))
103 * G_OBJECT_CLASS_TYPE:
104 * @class: a valid #GObjectClass
106 * Get the type id of a class structure.
108 * Returns: Type id of @class.
110 #define G_OBJECT_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
112 * G_OBJECT_CLASS_NAME:
113 * @class: a valid #GObjectClass
115 * Return the name of a class structure's type.
117 * Returns: Type name of @class. The string is owned by the type system and
118 * should not be freed.
120 #define G_OBJECT_CLASS_NAME(class) (g_type_name (G_OBJECT_CLASS_TYPE (class)))
122 * G_VALUE_HOLDS_OBJECT:
123 * @value: a valid #GValue structure
125 * Checks whether the given #GValue can hold values derived from type %G_TYPE_OBJECT.
127 * Returns: %TRUE on success.
129 #define G_VALUE_HOLDS_OBJECT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_OBJECT))
131 /* --- type macros --- */
133 * G_TYPE_INITIALLY_UNOWNED:
135 * The type for #GInitiallyUnowned.
137 #define G_TYPE_INITIALLY_UNOWNED (g_initially_unowned_get_type())
139 * G_INITIALLY_UNOWNED:
140 * @object: Object which is subject to casting.
142 * Casts a #GInitiallyUnowned or derived pointer into a (GInitiallyUnowned*)
143 * pointer. Depending on the current debugging level, this function may invoke
144 * certain runtime checks to identify invalid casts.
146 #define G_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnowned))
148 * G_INITIALLY_UNOWNED_CLASS:
149 * @class: a valid #GInitiallyUnownedClass
151 * Casts a derived #GInitiallyUnownedClass structure into a
152 * #GInitiallyUnownedClass structure.
154 #define G_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
156 * G_IS_INITIALLY_UNOWNED:
157 * @object: Instance to check for being a %G_TYPE_INITIALLY_UNOWNED.
159 * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_INITIALLY_UNOWNED.
161 #define G_IS_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_INITIALLY_UNOWNED))
163 * G_IS_INITIALLY_UNOWNED_CLASS:
164 * @class: a #GInitiallyUnownedClass
166 * Checks whether @class "is a" valid #GInitiallyUnownedClass structure of type
167 * %G_TYPE_INITIALLY_UNOWNED or derived.
169 #define G_IS_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_INITIALLY_UNOWNED))
171 * G_INITIALLY_UNOWNED_GET_CLASS:
172 * @object: a #GInitiallyUnowned instance.
174 * Get the class structure associated to a #GInitiallyUnowned instance.
176 * Returns: pointer to object class structure.
178 #define G_INITIALLY_UNOWNED_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
179 /* GInitiallyUnowned ia a GObject with initially floating reference count */
182 /* --- typedefs & structures --- */
183 typedef struct _GObject GObject
;
184 typedef struct _GObjectClass GObjectClass
;
185 typedef struct _GObject GInitiallyUnowned
;
186 typedef struct _GObjectClass GInitiallyUnownedClass
;
187 typedef struct _GObjectConstructParam GObjectConstructParam
;
189 * GObjectGetPropertyFunc:
190 * @object: a #GObject
191 * @property_id: the numeric id under which the property was registered with
192 * g_object_class_install_property().
193 * @value: a #GValue to return the property value in
194 * @pspec: the #GParamSpec describing the property
196 * The type of the @get_property function of #GObjectClass.
198 typedef void (*GObjectGetPropertyFunc
) (GObject
*object
,
203 * GObjectSetPropertyFunc:
204 * @object: a #GObject
205 * @property_id: the numeric id under which the property was registered with
206 * g_object_class_install_property().
207 * @value: the new value for the property
208 * @pspec: the #GParamSpec describing the property
210 * The type of the @set_property function of #GObjectClass.
212 typedef void (*GObjectSetPropertyFunc
) (GObject
*object
,
217 * GObjectFinalizeFunc:
218 * @object: the #GObject being finalized
220 * The type of the @finalize function of #GObjectClass.
222 typedef void (*GObjectFinalizeFunc
) (GObject
*object
);
225 * @data: data that was provided when the weak reference was established
226 * @where_the_object_was: the object being finalized
228 * A #GWeakNotify function can be added to an object as a callback that gets
229 * triggered when the object is finalized. Since the object is already being
230 * finalized when the #GWeakNotify is called, there's not much you could do
231 * with the object, apart from e.g. using its address as hash-index or the like.
233 typedef void (*GWeakNotify
) (gpointer data
,
234 GObject
*where_the_object_was
);
238 * All the fields in the <structname>GObject</structname> structure are private
239 * to the #GObject implementation and should never be accessed directly.
243 GTypeInstance g_type_instance
;
246 volatile guint ref_count
;
251 * @g_type_class: the parent class
252 * @constructor: the @constructor function is called by g_object_new () to
253 * complete the object initialization after all the construction properties are
254 * set. The first thing a @constructor implementation must do is chain up to the
255 * @constructor of the parent class. Overriding @constructor should be rarely
256 * needed, e.g. to handle construct properties, or to implement singletons.
257 * @set_property: the generic setter for all properties of this type. Should be
258 * overridden for every type with properties. If implementations of
259 * @set_property don't emit property change notification explicitly, this will
260 * be done implicitly by the type system. However, if the notify signal is
261 * emitted explicitly, the type system will not emit it a second time.
262 * @get_property: the generic getter for all properties of this type. Should be
263 * overridden for every type with properties.
264 * @dispose: the @dispose function is supposed to drop all references to other
265 * objects, but keep the instance otherwise intact, so that client method
266 * invocations still work. It may be run multiple times (due to reference
267 * loops). Before returning, @dispose should chain up to the @dispose method
268 * of the parent class.
269 * @finalize: instance finalization function, should finish the finalization of
270 * the instance begun in @dispose and chain up to the @finalize method of the
272 * @dispatch_properties_changed: emits property change notification for a bunch
273 * of properties. Overriding @dispatch_properties_changed should be rarely
275 * @notify: the class closure for the notify signal
276 * @constructed: the @constructed function is called by g_object_new() as the
277 * final step of the object creation process. At the point of the call, all
278 * construction properties have been set on the object. The purpose of this
279 * call is to allow for object initialisation steps that can only be performed
280 * after construction properties have been set. @constructed implementors
281 * should chain up to the @constructed call of their parent class to allow it
282 * to complete its initialisation.
284 * The class structure for the <structname>GObject</structname> type.
287 * <title>Implementing singletons using a constructor</title>
289 * static MySingleton *the_singleton = NULL;
292 * my_singleton_constructor (GType type,
293 * guint n_construct_params,
294 * GObjectConstructParam *construct_params)
298 * if (!the_singleton)
300 * object = G_OBJECT_CLASS (parent_class)->constructor (type,
301 * n_construct_params,
303 * the_singleton = MY_SINGLETON (object);
306 * object = g_object_ref (G_OBJECT (the_singleton));
310 * </programlisting></example>
314 GTypeClass g_type_class
;
317 GSList
*construct_properties
;
320 /* seldom overidden */
321 GObject
* (*constructor
) (GType type
,
322 guint n_construct_properties
,
323 GObjectConstructParam
*construct_properties
);
324 /* overridable methods */
325 void (*set_property
) (GObject
*object
,
329 void (*get_property
) (GObject
*object
,
333 void (*dispose
) (GObject
*object
);
334 void (*finalize
) (GObject
*object
);
335 /* seldom overidden */
336 void (*dispatch_properties_changed
) (GObject
*object
,
338 GParamSpec
**pspecs
);
340 void (*notify
) (GObject
*object
,
343 /* called when done constructing */
344 void (*constructed
) (GObject
*object
);
353 * GObjectConstructParam:
354 * @pspec: the #GParamSpec of the construct parameter
355 * @value: the value to set the parameter to
357 * The <structname>GObjectConstructParam</structname> struct is an auxiliary
358 * structure used to hand #GParamSpec/#GValue pairs to the @constructor of
361 struct _GObjectConstructParam
370 * All the fields in the <structname>GInitiallyUnowned</structname> structure
371 * are private to the #GInitiallyUnowned implementation and should never be
375 * GInitiallyUnownedClass:
377 * The class structure for the <structname>GInitiallyUnowned</structname> type.
381 /* --- prototypes --- */
382 GLIB_AVAILABLE_IN_ALL
383 GType
g_initially_unowned_get_type (void);
384 GLIB_AVAILABLE_IN_ALL
385 void g_object_class_install_property (GObjectClass
*oclass
,
388 GLIB_AVAILABLE_IN_ALL
389 GParamSpec
* g_object_class_find_property (GObjectClass
*oclass
,
390 const gchar
*property_name
);
391 GLIB_AVAILABLE_IN_ALL
392 GParamSpec
**g_object_class_list_properties (GObjectClass
*oclass
,
393 guint
*n_properties
);
394 GLIB_AVAILABLE_IN_ALL
395 void g_object_class_override_property (GObjectClass
*oclass
,
398 GLIB_AVAILABLE_IN_ALL
399 void g_object_class_install_properties (GObjectClass
*oclass
,
401 GParamSpec
**pspecs
);
403 GLIB_AVAILABLE_IN_ALL
404 void g_object_interface_install_property (gpointer g_iface
,
406 GLIB_AVAILABLE_IN_ALL
407 GParamSpec
* g_object_interface_find_property (gpointer g_iface
,
408 const gchar
*property_name
);
409 GLIB_AVAILABLE_IN_ALL
410 GParamSpec
**g_object_interface_list_properties (gpointer g_iface
,
411 guint
*n_properties_p
);
413 GLIB_AVAILABLE_IN_ALL
414 GType
g_object_get_type (void) G_GNUC_CONST
;
415 GLIB_AVAILABLE_IN_ALL
416 gpointer
g_object_new (GType object_type
,
417 const gchar
*first_property_name
,
419 GLIB_AVAILABLE_IN_ALL
420 gpointer
g_object_newv (GType object_type
,
422 GParameter
*parameters
);
423 GLIB_AVAILABLE_IN_ALL
424 GObject
* g_object_new_valist (GType object_type
,
425 const gchar
*first_property_name
,
427 GLIB_AVAILABLE_IN_ALL
428 void g_object_set (gpointer object
,
429 const gchar
*first_property_name
,
430 ...) G_GNUC_NULL_TERMINATED
;
431 GLIB_AVAILABLE_IN_ALL
432 void g_object_get (gpointer object
,
433 const gchar
*first_property_name
,
434 ...) G_GNUC_NULL_TERMINATED
;
435 GLIB_AVAILABLE_IN_ALL
436 gpointer
g_object_connect (gpointer object
,
437 const gchar
*signal_spec
,
438 ...) G_GNUC_NULL_TERMINATED
;
439 GLIB_AVAILABLE_IN_ALL
440 void g_object_disconnect (gpointer object
,
441 const gchar
*signal_spec
,
442 ...) G_GNUC_NULL_TERMINATED
;
443 GLIB_AVAILABLE_IN_ALL
444 void g_object_set_valist (GObject
*object
,
445 const gchar
*first_property_name
,
447 GLIB_AVAILABLE_IN_ALL
448 void g_object_get_valist (GObject
*object
,
449 const gchar
*first_property_name
,
451 GLIB_AVAILABLE_IN_ALL
452 void g_object_set_property (GObject
*object
,
453 const gchar
*property_name
,
454 const GValue
*value
);
455 GLIB_AVAILABLE_IN_ALL
456 void g_object_get_property (GObject
*object
,
457 const gchar
*property_name
,
459 GLIB_AVAILABLE_IN_ALL
460 void g_object_freeze_notify (GObject
*object
);
461 GLIB_AVAILABLE_IN_ALL
462 void g_object_notify (GObject
*object
,
463 const gchar
*property_name
);
464 GLIB_AVAILABLE_IN_ALL
465 void g_object_notify_by_pspec (GObject
*object
,
467 GLIB_AVAILABLE_IN_ALL
468 void g_object_thaw_notify (GObject
*object
);
469 GLIB_AVAILABLE_IN_ALL
470 gboolean
g_object_is_floating (gpointer object
);
471 GLIB_AVAILABLE_IN_ALL
472 gpointer
g_object_ref_sink (gpointer object
);
473 GLIB_AVAILABLE_IN_ALL
474 gpointer
g_object_ref (gpointer object
);
475 GLIB_AVAILABLE_IN_ALL
476 void g_object_unref (gpointer object
);
477 GLIB_AVAILABLE_IN_ALL
478 void g_object_weak_ref (GObject
*object
,
481 GLIB_AVAILABLE_IN_ALL
482 void g_object_weak_unref (GObject
*object
,
485 GLIB_AVAILABLE_IN_ALL
486 void g_object_add_weak_pointer (GObject
*object
,
487 gpointer
*weak_pointer_location
);
488 GLIB_AVAILABLE_IN_ALL
489 void g_object_remove_weak_pointer (GObject
*object
,
490 gpointer
*weak_pointer_location
);
494 * @data: Callback data passed to g_object_add_toggle_ref()
495 * @object: The object on which g_object_add_toggle_ref() was called.
496 * @is_last_ref: %TRUE if the toggle reference is now the
497 * last reference to the object. %FALSE if the toggle
498 * reference was the last reference and there are now other
501 * A callback function used for notification when the state
502 * of a toggle reference changes. See g_object_add_toggle_ref().
504 typedef void (*GToggleNotify
) (gpointer data
,
506 gboolean is_last_ref
);
508 GLIB_AVAILABLE_IN_ALL
509 void g_object_add_toggle_ref (GObject
*object
,
510 GToggleNotify notify
,
512 GLIB_AVAILABLE_IN_ALL
513 void g_object_remove_toggle_ref (GObject
*object
,
514 GToggleNotify notify
,
517 GLIB_AVAILABLE_IN_ALL
518 gpointer
g_object_get_qdata (GObject
*object
,
520 GLIB_AVAILABLE_IN_ALL
521 void g_object_set_qdata (GObject
*object
,
524 GLIB_AVAILABLE_IN_ALL
525 void g_object_set_qdata_full (GObject
*object
,
528 GDestroyNotify destroy
);
529 GLIB_AVAILABLE_IN_ALL
530 gpointer
g_object_steal_qdata (GObject
*object
,
533 GLIB_AVAILABLE_IN_2_34
534 gpointer
g_object_dup_qdata (GObject
*object
,
536 GDuplicateFunc dup_func
,
538 GLIB_AVAILABLE_IN_2_34
539 gboolean
g_object_replace_qdata (GObject
*object
,
543 GDestroyNotify destroy
,
544 GDestroyNotify
*old_destroy
);
546 GLIB_AVAILABLE_IN_ALL
547 gpointer
g_object_get_data (GObject
*object
,
549 GLIB_AVAILABLE_IN_ALL
550 void g_object_set_data (GObject
*object
,
553 GLIB_AVAILABLE_IN_ALL
554 void g_object_set_data_full (GObject
*object
,
557 GDestroyNotify destroy
);
558 GLIB_AVAILABLE_IN_ALL
559 gpointer
g_object_steal_data (GObject
*object
,
562 GLIB_AVAILABLE_IN_2_34
563 gpointer
g_object_dup_data (GObject
*object
,
565 GDuplicateFunc dup_func
,
567 GLIB_AVAILABLE_IN_2_34
568 gboolean
g_object_replace_data (GObject
*object
,
572 GDestroyNotify destroy
,
573 GDestroyNotify
*old_destroy
);
576 GLIB_AVAILABLE_IN_ALL
577 void g_object_watch_closure (GObject
*object
,
579 GLIB_AVAILABLE_IN_ALL
580 GClosure
* g_cclosure_new_object (GCallback callback_func
,
582 GLIB_AVAILABLE_IN_ALL
583 GClosure
* g_cclosure_new_object_swap (GCallback callback_func
,
585 GLIB_AVAILABLE_IN_ALL
586 GClosure
* g_closure_new_object (guint sizeof_closure
,
588 GLIB_AVAILABLE_IN_ALL
589 void g_value_set_object (GValue
*value
,
591 GLIB_AVAILABLE_IN_ALL
592 gpointer
g_value_get_object (const GValue
*value
);
593 GLIB_AVAILABLE_IN_ALL
594 gpointer
g_value_dup_object (const GValue
*value
);
595 GLIB_AVAILABLE_IN_ALL
596 gulong
g_signal_connect_object (gpointer instance
,
597 const gchar
*detailed_signal
,
600 GConnectFlags connect_flags
);
603 GLIB_AVAILABLE_IN_ALL
604 void g_object_force_floating (GObject
*object
);
605 GLIB_AVAILABLE_IN_ALL
606 void g_object_run_dispose (GObject
*object
);
609 GLIB_AVAILABLE_IN_ALL
610 void g_value_take_object (GValue
*value
,
612 GLIB_DEPRECATED_FOR(g_value_take_object
)
613 void g_value_set_object_take_ownership (GValue
*value
,
617 gsize
g_object_compat_control (gsize what
,
620 /* --- implementation macros --- */
621 #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
623 GObject *_glib__object = (GObject*) (object); \
624 GParamSpec *_glib__pspec = (GParamSpec*) (pspec); \
625 guint _glib__property_id = (property_id); \
626 g_warning ("%s: invalid %s id %u for \"%s\" of type '%s' in '%s'", \
629 _glib__property_id, \
630 _glib__pspec->name, \
631 g_type_name (G_PARAM_SPEC_TYPE (_glib__pspec)), \
632 G_OBJECT_TYPE_NAME (_glib__object)); \
635 * G_OBJECT_WARN_INVALID_PROPERTY_ID:
636 * @object: the #GObject on which set_property() or get_property() was called
637 * @property_id: the numeric id of the property
638 * @pspec: the #GParamSpec of the property
640 * This macro should be used to emit a standard warning about unexpected
641 * properties in set_property() and get_property() implementations.
643 #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
644 G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
646 GLIB_AVAILABLE_IN_ALL
647 void g_clear_object (volatile GObject
**object_ptr
);
648 #define g_clear_object(object_ptr) g_clear_pointer ((object_ptr), g_object_unref)
652 union { gpointer p
; } priv
;
655 GLIB_AVAILABLE_IN_ALL
656 void g_weak_ref_init (GWeakRef
*weak_ref
,
658 GLIB_AVAILABLE_IN_ALL
659 void g_weak_ref_clear (GWeakRef
*weak_ref
);
660 GLIB_AVAILABLE_IN_ALL
661 gpointer
g_weak_ref_get (GWeakRef
*weak_ref
);
662 GLIB_AVAILABLE_IN_ALL
663 void g_weak_ref_set (GWeakRef
*weak_ref
,
668 #endif /* __G_OBJECT_H__ */