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.
19 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
20 #error "Only <glib-object.h> can be included directly."
23 #ifndef __G_OBJECT_H__
24 #define __G_OBJECT_H__
26 #include <gobject/gtype.h>
27 #include <gobject/gvalue.h>
28 #include <gobject/gparam.h>
29 #include <gobject/gclosure.h>
30 #include <gobject/gsignal.h>
34 /* --- type macros --- */
37 * @type: Type id to check
39 * Check if the passed in type id is a %G_TYPE_OBJECT or derived from it.
41 * Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT.
43 #define G_TYPE_IS_OBJECT(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
46 * @object: Object which is subject to casting.
48 * Casts a #GObject or derived pointer into a (GObject*) pointer.
49 * Depending on the current debugging level, this function may invoke
50 * certain runtime checks to identify invalid casts.
52 #define G_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))
55 * @class: a valid #GObjectClass
57 * Casts a derived #GObjectClass structure into a #GObjectClass structure.
59 #define G_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass))
62 * @object: Instance to check for being a %G_TYPE_OBJECT.
64 * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT.
66 #define G_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_OBJECT))
69 * @class: a #GObjectClass
71 * Checks whether @class "is a" valid #GObjectClass structure of type
72 * %G_TYPE_OBJECT or derived.
74 #define G_IS_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT))
77 * @object: a #GObject instance.
79 * Get the class structure associated to a #GObject instance.
81 * Returns: pointer to object class structure.
83 #define G_OBJECT_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass))
86 * @object: Object to return the type id for.
88 * Get the type id of an object.
90 * Returns: Type id of @object.
92 #define G_OBJECT_TYPE(object) (G_TYPE_FROM_INSTANCE (object))
95 * @object: Object to return the type name for.
97 * Get the name of an object's type.
99 * Returns: Type name of @object. The string is owned by the type system and
100 * should not be freed.
102 #define G_OBJECT_TYPE_NAME(object) (g_type_name (G_OBJECT_TYPE (object)))
104 * G_OBJECT_CLASS_TYPE:
105 * @class: a valid #GObjectClass
107 * Get the type id of a class structure.
109 * Returns: Type id of @class.
111 #define G_OBJECT_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
113 * G_OBJECT_CLASS_NAME:
114 * @class: a valid #GObjectClass
116 * Return the name of a class structure's type.
118 * Returns: Type name of @class. The string is owned by the type system and
119 * should not be freed.
121 #define G_OBJECT_CLASS_NAME(class) (g_type_name (G_OBJECT_CLASS_TYPE (class)))
123 * G_VALUE_HOLDS_OBJECT:
124 * @value: a valid #GValue structure
126 * Checks whether the given #GValue can hold values derived from type %G_TYPE_OBJECT.
128 * Returns: %TRUE on success.
130 #define G_VALUE_HOLDS_OBJECT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_OBJECT))
132 /* --- type macros --- */
134 * G_TYPE_INITIALLY_UNOWNED:
136 * The type for #GInitiallyUnowned.
138 #define G_TYPE_INITIALLY_UNOWNED (g_initially_unowned_get_type())
139 #define G_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnowned))
140 #define G_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
141 #define G_IS_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_INITIALLY_UNOWNED))
142 #define G_IS_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_INITIALLY_UNOWNED))
143 #define G_INITIALLY_UNOWNED_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
144 /* GInitiallyUnowned ia a GObject with initially floating reference count */
147 /* --- typedefs & structures --- */
148 typedef struct _GObject GObject;
149 typedef struct _GObjectClass GObjectClass;
150 typedef struct _GObject GInitiallyUnowned;
151 typedef struct _GObjectClass GInitiallyUnownedClass;
152 typedef struct _GObjectConstructParam GObjectConstructParam;
154 * GObjectGetPropertyFunc:
155 * @object: a #GObject
156 * @property_id: the numeric id under which the property was registered with
157 * g_object_class_install_property().
158 * @value: a #GValue to return the property value in
159 * @pspec: the #GParamSpec describing the property
161 * The type of the @get_property function of #GObjectClass.
163 typedef void (*GObjectGetPropertyFunc) (GObject *object,
168 * GObjectSetPropertyFunc:
169 * @object: a #GObject
170 * @property_id: the numeric id under which the property was registered with
171 * g_object_class_install_property().
172 * @value: the new value for the property
173 * @pspec: the #GParamSpec describing the property
175 * The type of the @set_property function of #GObjectClass.
177 typedef void (*GObjectSetPropertyFunc) (GObject *object,
182 * GObjectFinalizeFunc:
183 * @object: the #GObject being finalized
185 * The type of the @finalize function of #GObjectClass.
187 typedef void (*GObjectFinalizeFunc) (GObject *object);
190 * @data: data that was provided when the weak reference was established
191 * @where_the_object_was: the object being finalized
193 * A #GWeakNotify function can be added to an object as a callback that gets
194 * triggered when the object is finalized. Since the object is already being
195 * finalized when the #GWeakNotify is called, there's not much you could do
196 * with the object, apart from e.g. using its adress as hash-index or the like.
198 typedef void (*GWeakNotify) (gpointer data,
199 GObject *where_the_object_was);
203 * All the fields in the <structname>GObject</structname> structure are private
204 * to the #GObject implementation and should never be accessed directly.
208 GTypeInstance g_type_instance;
211 volatile guint ref_count;
216 * @g_type_class: the parent class
217 * @constructor: the @constructor function is called by g_object_new () to
218 * complete the object initialization after all the construction properties are
219 * set. The first thing a @constructor implementation must do is chain up to the
220 * @constructor of the parent class. Overriding @constructor should be rarely
221 * needed, e.g. to handle construct properties, or to implement singletons.
222 * @set_property: the generic setter for all properties of this type. Should be
223 * overridden for every type with properties. Implementations of @set_property
224 * don't need to emit property change notification explicitly, this is handled
225 * by the type system.
226 * @get_property: the generic getter for all properties of this type. Should be
227 * overridden for every type with properties.
228 * @dispose: the @dispose function is supposed to drop all references to other
229 * objects, but keep the instance otherwise intact, so that client method
230 * invocations still work. It may be run multiple times (due to reference
231 * loops). Before returning, @dispose should chain up to the @dispose method
232 * of the parent class.
233 * @finalize: instance finalization function, should finish the finalization of
234 * the instance begun in @dispose and chain up to the @finalize method of the
236 * @dispatch_properties_changed: emits property change notification for a bunch
237 * of properties. Overriding @dispatch_properties_changed should be rarely
239 * @notify: the class closure for the notify signal
240 * @constructed: the @constructed function is called by g_object_new() as the
241 * final step of the object creation process. At the point of the call, all
242 * construction properties have been set on the object. The purpose of this
243 * call is to allow for object initialisation steps that can only be performed
244 * after construction properties have been set. @constructed implementors
245 * should chain up to the @constructed call of their parent class to allow it
246 * to complete its initialisation.
248 * The class structure for the <structname>GObject</structname> type.
251 * <title>Implementing singletons using a constructor</title>
253 * static MySingleton *the_singleton = NULL;
256 * my_singleton_constructor (GType type,
257 * guint n_construct_params,
258 * GObjectConstructParam *construct_params)
262 * if (!the_singleton)
264 * object = G_OBJECT_CLASS (parent_class)->constructor (type,
265 * n_construct_params,
267 * the_singleton = MY_SINGLETON (object);
270 * object = g_object_ref (G_OBJECT (the_singleton));
274 * </programlisting></example>
278 GTypeClass g_type_class;
281 GSList *construct_properties;
284 /* seldomly overidden */
285 GObject* (*constructor) (GType type,
286 guint n_construct_properties,
287 GObjectConstructParam *construct_properties);
288 /* overridable methods */
289 void (*set_property) (GObject *object,
293 void (*get_property) (GObject *object,
297 void (*dispose) (GObject *object);
298 void (*finalize) (GObject *object);
299 /* seldomly overidden */
300 void (*dispatch_properties_changed) (GObject *object,
302 GParamSpec **pspecs);
304 void (*notify) (GObject *object,
307 /* called when done constructing */
308 void (*constructed) (GObject *object);
315 * GObjectConstructParam:
316 * @pspec: the #GParamSpec of the construct parameter
317 * @value: the value to set the parameter to
319 * The <structname>GObjectConstructParam</structname> struct is an auxiliary
320 * structure used to hand #GParamSpec/#GValue pairs to the @constructor of
323 struct _GObjectConstructParam
332 * All the fields in the <structname>GInitiallyUnowned</structname> structure
333 * are private to the #GInitiallyUnowned implementation and should never be
337 * GInitiallyUnownedClass:
339 * The class structure for the <structname>GInitiallyUnowned</structname> type.
343 /* --- prototypes --- */
344 GType g_initially_unowned_get_type (void);
345 void g_object_class_install_property (GObjectClass *oclass,
348 GParamSpec* g_object_class_find_property (GObjectClass *oclass,
349 const gchar *property_name);
350 GParamSpec**g_object_class_list_properties (GObjectClass *oclass,
351 guint *n_properties);
352 void g_object_class_override_property (GObjectClass *oclass,
356 void g_object_interface_install_property (gpointer g_iface,
358 GParamSpec* g_object_interface_find_property (gpointer g_iface,
359 const gchar *property_name);
360 GParamSpec**g_object_interface_list_properties (gpointer g_iface,
361 guint *n_properties_p);
363 gpointer g_object_new (GType object_type,
364 const gchar *first_property_name,
366 gpointer g_object_newv (GType object_type,
368 GParameter *parameters);
369 GObject* g_object_new_valist (GType object_type,
370 const gchar *first_property_name,
372 void g_object_set (gpointer object,
373 const gchar *first_property_name,
374 ...) G_GNUC_NULL_TERMINATED;
375 void g_object_get (gpointer object,
376 const gchar *first_property_name,
377 ...) G_GNUC_NULL_TERMINATED;
378 gpointer g_object_connect (gpointer object,
379 const gchar *signal_spec,
380 ...) G_GNUC_NULL_TERMINATED;
381 void g_object_disconnect (gpointer object,
382 const gchar *signal_spec,
383 ...) G_GNUC_NULL_TERMINATED;
384 void g_object_set_valist (GObject *object,
385 const gchar *first_property_name,
387 void g_object_get_valist (GObject *object,
388 const gchar *first_property_name,
390 void g_object_set_property (GObject *object,
391 const gchar *property_name,
392 const GValue *value);
393 void g_object_get_property (GObject *object,
394 const gchar *property_name,
396 void g_object_freeze_notify (GObject *object);
397 void g_object_notify (GObject *object,
398 const gchar *property_name);
399 void g_object_thaw_notify (GObject *object);
400 gboolean g_object_is_floating (gpointer object);
401 gpointer g_object_ref_sink (gpointer object);
402 gpointer g_object_ref (gpointer object);
403 void g_object_unref (gpointer object);
404 void g_object_weak_ref (GObject *object,
407 void g_object_weak_unref (GObject *object,
410 void g_object_add_weak_pointer (GObject *object,
411 gpointer *weak_pointer_location);
412 void g_object_remove_weak_pointer (GObject *object,
413 gpointer *weak_pointer_location);
417 * @data: Callback data passed to g_object_add_toggle_ref()
418 * @object: The object on which g_object_add_toggle_ref() was called.
419 * @is_last_ref: %TRUE if the toggle reference is now the
420 * last reference to the object. %FALSE if the toggle
421 * reference was the last reference and there are now other
424 * A callback function used for notification when the state
425 * of a toggle reference changes. See g_object_add_toggle_ref().
427 typedef void (*GToggleNotify) (gpointer data,
429 gboolean is_last_ref);
431 void g_object_add_toggle_ref (GObject *object,
432 GToggleNotify notify,
434 void g_object_remove_toggle_ref (GObject *object,
435 GToggleNotify notify,
438 gpointer g_object_get_qdata (GObject *object,
440 void g_object_set_qdata (GObject *object,
443 void g_object_set_qdata_full (GObject *object,
446 GDestroyNotify destroy);
447 gpointer g_object_steal_qdata (GObject *object,
449 gpointer g_object_get_data (GObject *object,
451 void g_object_set_data (GObject *object,
454 void g_object_set_data_full (GObject *object,
457 GDestroyNotify destroy);
458 gpointer g_object_steal_data (GObject *object,
460 void g_object_watch_closure (GObject *object,
462 GClosure* g_cclosure_new_object (GCallback callback_func,
464 GClosure* g_cclosure_new_object_swap (GCallback callback_func,
466 GClosure* g_closure_new_object (guint sizeof_closure,
468 void g_value_set_object (GValue *value,
470 gpointer g_value_get_object (const GValue *value);
471 gpointer g_value_dup_object (const GValue *value);
472 gulong g_signal_connect_object (gpointer instance,
473 const gchar *detailed_signal,
476 GConnectFlags connect_flags);
479 void g_object_force_floating (GObject *object);
480 void g_object_run_dispose (GObject *object);
483 void g_value_take_object (GValue *value,
485 #ifndef G_DISABLE_DEPRECATED
486 void g_value_set_object_take_ownership (GValue *value,
490 #if !defined(G_DISABLE_DEPRECATED) || defined(GTK_COMPILATION)
491 gsize g_object_compat_control (gsize what,
495 /* --- implementation macros --- */
496 #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
498 GObject *_object = (GObject*) (object); \
499 GParamSpec *_pspec = (GParamSpec*) (pspec); \
500 guint _property_id = (property_id); \
501 g_warning ("%s: invalid %s id %u for \"%s\" of type `%s' in `%s'", \
506 g_type_name (G_PARAM_SPEC_TYPE (_pspec)), \
507 G_OBJECT_TYPE_NAME (_object)); \
510 * G_OBJECT_WARN_INVALID_PROPERTY_ID:
511 * @object: the #GObject on which set_property() or get_property() was called
512 * @property_id: the numeric id of the property
513 * @pspec: the #GParamSpec of the property
515 * This macro should be used to emit a standard warning about unexpected
516 * properties in set_property() and get_property() implementations.
518 #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
519 G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
523 #endif /* __G_OBJECT_H__ */