Migrating docs.
[platform/upstream/glib.git] / gobject / gobject.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
20 #error "Only <glib-object.h> can be included directly."
21 #endif
22
23 #ifndef __G_OBJECT_H__
24 #define __G_OBJECT_H__
25
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>
31
32 G_BEGIN_DECLS
33
34 /* --- type macros --- */
35 /**
36  * G_TYPE_IS_OBJECT:
37  * @type: Type id to check
38  * 
39  * Check if the passed in type id is a %G_TYPE_OBJECT or derived from it.
40  * 
41  * Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT.
42  */
43 #define G_TYPE_IS_OBJECT(type)      (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
44 /**
45  * G_OBJECT:
46  * @object: Object which is subject to casting.
47  * 
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.
51  */
52 #define G_OBJECT(object)            (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))
53 /**
54  * G_OBJECT_CLASS:
55  * @class: a valid #GObjectClass
56  * 
57  * Casts a derived #GObjectClass structure into a #GObjectClass structure.
58  */
59 #define G_OBJECT_CLASS(class)       (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass))
60 /**
61  * G_IS_OBJECT:
62  * @object: Instance to check for being a %G_TYPE_OBJECT.
63  * 
64  * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT.
65  */
66 #define G_IS_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_OBJECT))
67 /**
68  * G_IS_OBJECT_CLASS:
69  * @class: a #GObjectClass
70  * 
71  * Checks whether @class "is a" valid #GObjectClass structure of type
72  * %G_TYPE_OBJECT or derived.
73  */
74 #define G_IS_OBJECT_CLASS(class)    (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT))
75 /**
76  * G_OBJECT_GET_CLASS:
77  * @object: a #GObject instance.
78  * 
79  * Get the class structure associated to a #GObject instance.
80  *
81  * Returns: pointer to object class structure.
82  */
83 #define G_OBJECT_GET_CLASS(object)  (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass))
84 /**
85  * G_OBJECT_TYPE:
86  * @object: Object to return the type id for.
87  * 
88  * Get the type id of an object.
89  * 
90  * Returns: Type id of @object.
91  */
92 #define G_OBJECT_TYPE(object)       (G_TYPE_FROM_INSTANCE (object))
93 /**
94  * G_OBJECT_TYPE_NAME:
95  * @object: Object to return the type name for.
96  * 
97  * Get the name of an object's type.
98  * 
99  * Returns: Type name of @object. The string is owned by the type system and 
100  *  should not be freed.
101  */
102 #define G_OBJECT_TYPE_NAME(object)  (g_type_name (G_OBJECT_TYPE (object)))
103 /**
104  * G_OBJECT_CLASS_TYPE:
105  * @class: a valid #GObjectClass
106  * 
107  * Get the type id of a class structure.
108  * 
109  * Returns: Type id of @class.
110  */
111 #define G_OBJECT_CLASS_TYPE(class)  (G_TYPE_FROM_CLASS (class))
112 /**
113  * G_OBJECT_CLASS_NAME:
114  * @class: a valid #GObjectClass
115  * 
116  * Return the name of a class structure's type.
117  * 
118  * Returns: Type name of @class. The string is owned by the type system and 
119  *  should not be freed.
120  */
121 #define G_OBJECT_CLASS_NAME(class)  (g_type_name (G_OBJECT_CLASS_TYPE (class)))
122 /**
123  * G_VALUE_HOLDS_OBJECT:
124  * @value: a valid #GValue structure
125  * 
126  * Checks whether the given #GValue can hold values derived from type %G_TYPE_OBJECT.
127  * 
128  * Returns: %TRUE on success.
129  */
130 #define G_VALUE_HOLDS_OBJECT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_OBJECT))
131
132 /* --- type macros --- */
133 /**
134  * G_TYPE_INITIALLY_UNOWNED:
135  * 
136  * The type for #GInitiallyUnowned.
137  */
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 */
145
146
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;
153 /**
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
160  * 
161  * The type of the @get_property function of #GObjectClass. 
162  */
163 typedef void (*GObjectGetPropertyFunc)  (GObject      *object,
164                                          guint         property_id,
165                                          GValue       *value,
166                                          GParamSpec   *pspec);
167 /**
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
174  * 
175  * The type of the @set_property function of #GObjectClass. 
176  */
177 typedef void (*GObjectSetPropertyFunc)  (GObject      *object,
178                                          guint         property_id,
179                                          const GValue *value,
180                                          GParamSpec   *pspec);
181 /**
182  * GObjectFinalizeFunc:
183  * @object: the #GObject being finalized
184  * 
185  * The type of the @finalize function of #GObjectClass.
186  */
187 typedef void (*GObjectFinalizeFunc)     (GObject      *object);
188 /**
189  * GWeakNotify:
190  * @data: data that was provided when the weak reference was established
191  * @where_the_object_was: the object being finalized
192  * 
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. 
197  */
198 typedef void (*GWeakNotify)             (gpointer      data,
199                                          GObject      *where_the_object_was);
200 /**
201  * GObject:
202  * 
203  * All the fields in the <structname>GObject</structname> structure are private 
204  * to the #GObject implementation and should never be accessed directly.
205  */
206 struct  _GObject
207 {
208   GTypeInstance  g_type_instance;
209   
210   /*< private >*/
211   volatile guint ref_count;
212   GData         *qdata;
213 };
214 /**
215  * GObjectClass:
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 
235  *  parent class.
236  * @dispatch_properties_changed: emits property change notification for a bunch
237  *  of properties. Overriding @dispatch_properties_changed should be rarely 
238  *  needed.
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.
247  * 
248  * The class structure for the <structname>GObject</structname> type.
249  * 
250  * <example>
251  * <title>Implementing singletons using a constructor</title>
252  * <programlisting>
253  * static MySingleton *the_singleton = NULL;
254  * 
255  * static GObject*
256  * my_singleton_constructor (GType                  type,
257  *                           guint                  n_construct_params,
258  *                           GObjectConstructParam *construct_params)
259  * {
260  *   GObject *object;
261  *   
262  *   if (!the_singleton)
263  *     {
264  *       object = G_OBJECT_CLASS (parent_class)->constructor (type,
265  *                                                            n_construct_params,
266  *                                                            construct_params);
267  *       the_singleton = MY_SINGLETON (object);
268  *     }
269  *   else
270  *     object = g_object_ref (G_OBJECT (the_singleton));
271  * 
272  *   return object;
273  * }
274  * </programlisting></example>
275  */
276 struct  _GObjectClass
277 {
278   GTypeClass   g_type_class;
279
280   /*< private >*/
281   GSList      *construct_properties;
282
283   /*< public >*/
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,
290                                          guint           property_id,
291                                          const GValue   *value,
292                                          GParamSpec     *pspec);
293   void       (*get_property)            (GObject        *object,
294                                          guint           property_id,
295                                          GValue         *value,
296                                          GParamSpec     *pspec);
297   void       (*dispose)                 (GObject        *object);
298   void       (*finalize)                (GObject        *object);
299   /* seldomly overidden */
300   void       (*dispatch_properties_changed) (GObject      *object,
301                                              guint         n_pspecs,
302                                              GParamSpec  **pspecs);
303   /* signals */
304   void       (*notify)                  (GObject        *object,
305                                          GParamSpec     *pspec);
306
307   /* called when done constructing */
308   void       (*constructed)             (GObject        *object);
309
310   /*< private >*/
311   /* padding */
312   gpointer      pdummy[7];
313 };
314 /**
315  * GObjectConstructParam:
316  * @pspec: the #GParamSpec of the construct parameter
317  * @value: the value to set the parameter to
318  * 
319  * The <structname>GObjectConstructParam</structname> struct is an auxiliary 
320  * structure used to hand #GParamSpec/#GValue pairs to the @constructor of
321  * a #GObjectClass.
322  */
323 struct _GObjectConstructParam
324 {
325   GParamSpec *pspec;
326   GValue     *value;
327 };
328
329 /**
330  * GInitiallyUnowned:
331  * 
332  * All the fields in the <structname>GInitiallyUnowned</structname> structure 
333  * are private to the #GInitiallyUnowned implementation and should never be 
334  * accessed directly.
335  */
336 /**
337  * GInitiallyUnownedClass:
338  * 
339  * The class structure for the <structname>GInitiallyUnowned</structname> type.
340  */
341
342
343 /* --- prototypes --- */
344 GType       g_initially_unowned_get_type      (void);
345 void        g_object_class_install_property   (GObjectClass   *oclass,
346                                                guint           property_id,
347                                                GParamSpec     *pspec);
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,
353                                                guint           property_id,
354                                                const gchar    *name);
355
356 void        g_object_interface_install_property (gpointer     g_iface,
357                                                  GParamSpec  *pspec);
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);
362
363 gpointer    g_object_new                      (GType           object_type,
364                                                const gchar    *first_property_name,
365                                                ...);
366 gpointer    g_object_newv                     (GType           object_type,
367                                                guint           n_parameters,
368                                                GParameter     *parameters);
369 GObject*    g_object_new_valist               (GType           object_type,
370                                                const gchar    *first_property_name,
371                                                va_list         var_args);
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,
386                                                va_list         var_args);
387 void        g_object_get_valist               (GObject        *object,
388                                                const gchar    *first_property_name,
389                                                va_list         var_args);
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,
395                                                GValue         *value);
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,
405                                                GWeakNotify     notify,
406                                                gpointer        data);
407 void        g_object_weak_unref               (GObject        *object,
408                                                GWeakNotify     notify,
409                                                gpointer        data);
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);
414
415 /**
416  * GToggleNotify:
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
422  *  references.
423  * 
424  * A callback function used for notification when the state
425  * of a toggle reference changes. See g_object_add_toggle_ref().
426  */
427 typedef void (*GToggleNotify) (gpointer      data,
428                                GObject      *object,
429                                gboolean      is_last_ref);
430
431 void g_object_add_toggle_ref    (GObject       *object,
432                                  GToggleNotify  notify,
433                                  gpointer       data);
434 void g_object_remove_toggle_ref (GObject       *object,
435                                  GToggleNotify  notify,
436                                  gpointer       data);
437
438 gpointer    g_object_get_qdata                (GObject        *object,
439                                                GQuark          quark);
440 void        g_object_set_qdata                (GObject        *object,
441                                                GQuark          quark,
442                                                gpointer        data);
443 void        g_object_set_qdata_full           (GObject        *object,
444                                                GQuark          quark,
445                                                gpointer        data,
446                                                GDestroyNotify  destroy);
447 gpointer    g_object_steal_qdata              (GObject        *object,
448                                                GQuark          quark);
449 gpointer    g_object_get_data                 (GObject        *object,
450                                                const gchar    *key);
451 void        g_object_set_data                 (GObject        *object,
452                                                const gchar    *key,
453                                                gpointer        data);
454 void        g_object_set_data_full            (GObject        *object,
455                                                const gchar    *key,
456                                                gpointer        data,
457                                                GDestroyNotify  destroy);
458 gpointer    g_object_steal_data               (GObject        *object,
459                                                const gchar    *key);
460 void        g_object_watch_closure            (GObject        *object,
461                                                GClosure       *closure);
462 GClosure*   g_cclosure_new_object             (GCallback       callback_func,
463                                                GObject        *object);
464 GClosure*   g_cclosure_new_object_swap        (GCallback       callback_func,
465                                                GObject        *object);
466 GClosure*   g_closure_new_object              (guint           sizeof_closure,
467                                                GObject        *object);
468 void        g_value_set_object                (GValue         *value,
469                                                gpointer        v_object);
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,
474                                                GCallback       c_handler,
475                                                gpointer        gobject,
476                                                GConnectFlags   connect_flags);
477
478 /*< protected >*/
479 void        g_object_force_floating           (GObject        *object);
480 void        g_object_run_dispose              (GObject        *object);
481
482
483 void        g_value_take_object               (GValue         *value,
484                                                gpointer        v_object);
485 #ifndef G_DISABLE_DEPRECATED
486 void        g_value_set_object_take_ownership (GValue         *value,
487                                                gpointer        v_object);
488 #endif
489
490 #if !defined(G_DISABLE_DEPRECATED) || defined(GTK_COMPILATION)
491 gsize       g_object_compat_control           (gsize           what,
492                                                gpointer        data);
493 #endif
494
495 /* --- implementation macros --- */
496 #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
497 G_STMT_START { \
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'", \
502              G_STRLOC, \
503              (pname), \
504              _property_id, \
505              _pspec->name, \
506              g_type_name (G_PARAM_SPEC_TYPE (_pspec)), \
507              G_OBJECT_TYPE_NAME (_object)); \
508 } G_STMT_END
509 /**
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
514  * 
515  * This macro should be used to emit a standard warning about unexpected 
516  * properties in set_property() and get_property() implementations.
517  */
518 #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
519     G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
520
521 G_END_DECLS
522
523 #endif /* __G_OBJECT_H__ */