Spelling fixes
[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 /**
140  * G_INITIALLY_UNOWNED:
141  * @object: Object which is subject to casting.
142  * 
143  * Casts a #GInitiallyUnowned or derived pointer into a (GInitiallyUnowned*) 
144  * pointer. Depending on the current debugging level, this function may invoke
145  * certain runtime checks to identify invalid casts.
146  */
147 #define G_INITIALLY_UNOWNED(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnowned))
148 /**
149  * G_INITIALLY_UNOWNED_CLASS:
150  * @class: a valid #GInitiallyUnownedClass
151  * 
152  * Casts a derived #GInitiallyUnownedClass structure into a
153  * #GInitiallyUnownedClass structure.
154  */
155 #define G_INITIALLY_UNOWNED_CLASS(class)      (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
156 /**
157  * G_IS_INITIALLY_UNOWNED:
158  * @object: Instance to check for being a %G_TYPE_INITIALLY_UNOWNED.
159  * 
160  * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_INITIALLY_UNOWNED.
161  */
162 #define G_IS_INITIALLY_UNOWNED(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_INITIALLY_UNOWNED))
163 /**
164  * G_IS_INITIALLY_UNOWNED_CLASS:
165  * @class: a #GInitiallyUnownedClass
166  * 
167  * Checks whether @class "is a" valid #GInitiallyUnownedClass structure of type
168  * %G_TYPE_INITIALLY_UNOWNED or derived.
169  */
170 #define G_IS_INITIALLY_UNOWNED_CLASS(class)   (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_INITIALLY_UNOWNED))
171 /**
172  * G_INITIALLY_UNOWNED_GET_CLASS:
173  * @object: a #GInitiallyUnowned instance.
174  * 
175  * Get the class structure associated to a #GInitiallyUnowned instance.
176  *
177  * Returns: pointer to object class structure.
178  */
179 #define G_INITIALLY_UNOWNED_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
180 /* GInitiallyUnowned ia a GObject with initially floating reference count */
181
182
183 /* --- typedefs & structures --- */
184 typedef struct _GObject                  GObject;
185 typedef struct _GObjectClass             GObjectClass;
186 typedef struct _GObject                  GInitiallyUnowned;
187 typedef struct _GObjectClass             GInitiallyUnownedClass;
188 typedef struct _GObjectConstructParam    GObjectConstructParam;
189 /**
190  * GObjectGetPropertyFunc:
191  * @object: a #GObject
192  * @property_id: the numeric id under which the property was registered with
193  *  g_object_class_install_property().
194  * @value: a #GValue to return the property value in
195  * @pspec: the #GParamSpec describing the property
196  * 
197  * The type of the @get_property function of #GObjectClass. 
198  */
199 typedef void (*GObjectGetPropertyFunc)  (GObject      *object,
200                                          guint         property_id,
201                                          GValue       *value,
202                                          GParamSpec   *pspec);
203 /**
204  * GObjectSetPropertyFunc:
205  * @object: a #GObject
206  * @property_id: the numeric id under which the property was registered with
207  *  g_object_class_install_property().
208  * @value: the new value for the property
209  * @pspec: the #GParamSpec describing the property
210  * 
211  * The type of the @set_property function of #GObjectClass. 
212  */
213 typedef void (*GObjectSetPropertyFunc)  (GObject      *object,
214                                          guint         property_id,
215                                          const GValue *value,
216                                          GParamSpec   *pspec);
217 /**
218  * GObjectFinalizeFunc:
219  * @object: the #GObject being finalized
220  * 
221  * The type of the @finalize function of #GObjectClass.
222  */
223 typedef void (*GObjectFinalizeFunc)     (GObject      *object);
224 /**
225  * GWeakNotify:
226  * @data: data that was provided when the weak reference was established
227  * @where_the_object_was: the object being finalized
228  * 
229  * A #GWeakNotify function can be added to an object as a callback that gets
230  * triggered when the object is finalized. Since the object is already being
231  * finalized when the #GWeakNotify is called, there's not much you could do 
232  * with the object, apart from e.g. using its address as hash-index or the like. 
233  */
234 typedef void (*GWeakNotify)             (gpointer      data,
235                                          GObject      *where_the_object_was);
236 /**
237  * GObject:
238  * 
239  * All the fields in the <structname>GObject</structname> structure are private 
240  * to the #GObject implementation and should never be accessed directly.
241  */
242 struct  _GObject
243 {
244   GTypeInstance  g_type_instance;
245   
246   /*< private >*/
247   volatile guint ref_count;
248   GData         *qdata;
249 };
250 /**
251  * GObjectClass:
252  * @g_type_class: the parent class
253  * @constructor: the @constructor function is called by g_object_new () to 
254  *  complete the object initialization after all the construction properties are
255  *  set. The first thing a @constructor implementation must do is chain up to the
256  *  @constructor of the parent class. Overriding @constructor should be rarely 
257  *  needed, e.g. to handle construct properties, or to implement singletons.
258  * @set_property: the generic setter for all properties of this type. Should be
259  *  overridden for every type with properties. Implementations of @set_property
260  *  don't need to emit property change notification explicitly, this is handled
261  *  by the type system.
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 
271  *  parent class.
272  * @dispatch_properties_changed: emits property change notification for a bunch
273  *  of properties. Overriding @dispatch_properties_changed should be rarely 
274  *  needed.
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.
283  * 
284  * The class structure for the <structname>GObject</structname> type.
285  * 
286  * <example>
287  * <title>Implementing singletons using a constructor</title>
288  * <programlisting>
289  * static MySingleton *the_singleton = NULL;
290  * 
291  * static GObject*
292  * my_singleton_constructor (GType                  type,
293  *                           guint                  n_construct_params,
294  *                           GObjectConstructParam *construct_params)
295  * {
296  *   GObject *object;
297  *   
298  *   if (!the_singleton)
299  *     {
300  *       object = G_OBJECT_CLASS (parent_class)->constructor (type,
301  *                                                            n_construct_params,
302  *                                                            construct_params);
303  *       the_singleton = MY_SINGLETON (object);
304  *     }
305  *   else
306  *     object = g_object_ref (G_OBJECT (the_singleton));
307  * 
308  *   return object;
309  * }
310  * </programlisting></example>
311  */
312 struct  _GObjectClass
313 {
314   GTypeClass   g_type_class;
315
316   /*< private >*/
317   GSList      *construct_properties;
318
319   /*< public >*/
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,
326                                          guint           property_id,
327                                          const GValue   *value,
328                                          GParamSpec     *pspec);
329   void       (*get_property)            (GObject        *object,
330                                          guint           property_id,
331                                          GValue         *value,
332                                          GParamSpec     *pspec);
333   void       (*dispose)                 (GObject        *object);
334   void       (*finalize)                (GObject        *object);
335   /* seldom overidden */
336   void       (*dispatch_properties_changed) (GObject      *object,
337                                              guint         n_pspecs,
338                                              GParamSpec  **pspecs);
339   /* signals */
340   void       (*notify)                  (GObject        *object,
341                                          GParamSpec     *pspec);
342
343   /* called when done constructing */
344   void       (*constructed)             (GObject        *object);
345
346   /*< private >*/
347   gsize         flags;
348
349   /* padding */
350   gpointer      pdummy[6];
351 };
352 /**
353  * GObjectConstructParam:
354  * @pspec: the #GParamSpec of the construct parameter
355  * @value: the value to set the parameter to
356  * 
357  * The <structname>GObjectConstructParam</structname> struct is an auxiliary 
358  * structure used to hand #GParamSpec/#GValue pairs to the @constructor of
359  * a #GObjectClass.
360  */
361 struct _GObjectConstructParam
362 {
363   GParamSpec *pspec;
364   GValue     *value;
365 };
366
367 /**
368  * GInitiallyUnowned:
369  * 
370  * All the fields in the <structname>GInitiallyUnowned</structname> structure 
371  * are private to the #GInitiallyUnowned implementation and should never be 
372  * accessed directly.
373  */
374 /**
375  * GInitiallyUnownedClass:
376  * 
377  * The class structure for the <structname>GInitiallyUnowned</structname> type.
378  */
379
380
381 /* --- prototypes --- */
382 GType       g_initially_unowned_get_type      (void);
383 void        g_object_class_install_property   (GObjectClass   *oclass,
384                                                guint           property_id,
385                                                GParamSpec     *pspec);
386 GParamSpec* g_object_class_find_property      (GObjectClass   *oclass,
387                                                const gchar    *property_name);
388 GParamSpec**g_object_class_list_properties    (GObjectClass   *oclass,
389                                                guint          *n_properties);
390 void        g_object_class_override_property  (GObjectClass   *oclass,
391                                                guint           property_id,
392                                                const gchar    *name);
393 void        g_object_class_install_properties (GObjectClass   *oclass,
394                                                guint           n_pspecs,
395                                                GParamSpec    **pspecs);
396
397 void        g_object_interface_install_property (gpointer     g_iface,
398                                                  GParamSpec  *pspec);
399 GParamSpec* g_object_interface_find_property    (gpointer     g_iface,
400                                                  const gchar *property_name);
401 GParamSpec**g_object_interface_list_properties  (gpointer     g_iface,
402                                                  guint       *n_properties_p);
403
404 GType       g_object_get_type                 (void) G_GNUC_CONST;
405 gpointer    g_object_new                      (GType           object_type,
406                                                const gchar    *first_property_name,
407                                                ...);
408 gpointer    g_object_newv                     (GType           object_type,
409                                                guint           n_parameters,
410                                                GParameter     *parameters);
411 GObject*    g_object_new_valist               (GType           object_type,
412                                                const gchar    *first_property_name,
413                                                va_list         var_args);
414 void        g_object_set                      (gpointer        object,
415                                                const gchar    *first_property_name,
416                                                ...) G_GNUC_NULL_TERMINATED;
417 void        g_object_get                      (gpointer        object,
418                                                const gchar    *first_property_name,
419                                                ...) G_GNUC_NULL_TERMINATED;
420 gpointer    g_object_connect                  (gpointer        object,
421                                                const gchar    *signal_spec,
422                                                ...) G_GNUC_NULL_TERMINATED;
423 void        g_object_disconnect               (gpointer        object,
424                                                const gchar    *signal_spec,
425                                                ...) G_GNUC_NULL_TERMINATED;
426 void        g_object_set_valist               (GObject        *object,
427                                                const gchar    *first_property_name,
428                                                va_list         var_args);
429 void        g_object_get_valist               (GObject        *object,
430                                                const gchar    *first_property_name,
431                                                va_list         var_args);
432 void        g_object_set_property             (GObject        *object,
433                                                const gchar    *property_name,
434                                                const GValue   *value);
435 void        g_object_get_property             (GObject        *object,
436                                                const gchar    *property_name,
437                                                GValue         *value);
438 void        g_object_freeze_notify            (GObject        *object);
439 void        g_object_notify                   (GObject        *object,
440                                                const gchar    *property_name);
441 void        g_object_notify_by_pspec          (GObject        *object,
442                                                GParamSpec     *pspec);
443 void        g_object_thaw_notify              (GObject        *object);
444 gboolean    g_object_is_floating              (gpointer        object);
445 gpointer    g_object_ref_sink                 (gpointer        object);
446 gpointer    g_object_ref                      (gpointer        object);
447 void        g_object_unref                    (gpointer        object);
448 void        g_object_weak_ref                 (GObject        *object,
449                                                GWeakNotify     notify,
450                                                gpointer        data);
451 void        g_object_weak_unref               (GObject        *object,
452                                                GWeakNotify     notify,
453                                                gpointer        data);
454 void        g_object_add_weak_pointer         (GObject        *object, 
455                                                gpointer       *weak_pointer_location);
456 void        g_object_remove_weak_pointer      (GObject        *object, 
457                                                gpointer       *weak_pointer_location);
458
459 /**
460  * GToggleNotify:
461  * @data: Callback data passed to g_object_add_toggle_ref()
462  * @object: The object on which g_object_add_toggle_ref() was called.
463  * @is_last_ref: %TRUE if the toggle reference is now the
464  *  last reference to the object. %FALSE if the toggle
465  *  reference was the last reference and there are now other
466  *  references.
467  * 
468  * A callback function used for notification when the state
469  * of a toggle reference changes. See g_object_add_toggle_ref().
470  */
471 typedef void (*GToggleNotify) (gpointer      data,
472                                GObject      *object,
473                                gboolean      is_last_ref);
474
475 void g_object_add_toggle_ref    (GObject       *object,
476                                  GToggleNotify  notify,
477                                  gpointer       data);
478 void g_object_remove_toggle_ref (GObject       *object,
479                                  GToggleNotify  notify,
480                                  gpointer       data);
481
482 gpointer    g_object_get_qdata                (GObject        *object,
483                                                GQuark          quark);
484 void        g_object_set_qdata                (GObject        *object,
485                                                GQuark          quark,
486                                                gpointer        data);
487 void        g_object_set_qdata_full           (GObject        *object,
488                                                GQuark          quark,
489                                                gpointer        data,
490                                                GDestroyNotify  destroy);
491 gpointer    g_object_steal_qdata              (GObject        *object,
492                                                GQuark          quark);
493 gpointer    g_object_get_data                 (GObject        *object,
494                                                const gchar    *key);
495 void        g_object_set_data                 (GObject        *object,
496                                                const gchar    *key,
497                                                gpointer        data);
498 void        g_object_set_data_full            (GObject        *object,
499                                                const gchar    *key,
500                                                gpointer        data,
501                                                GDestroyNotify  destroy);
502 gpointer    g_object_steal_data               (GObject        *object,
503                                                const gchar    *key);
504 void        g_object_watch_closure            (GObject        *object,
505                                                GClosure       *closure);
506 GClosure*   g_cclosure_new_object             (GCallback       callback_func,
507                                                GObject        *object);
508 GClosure*   g_cclosure_new_object_swap        (GCallback       callback_func,
509                                                GObject        *object);
510 GClosure*   g_closure_new_object              (guint           sizeof_closure,
511                                                GObject        *object);
512 void        g_value_set_object                (GValue         *value,
513                                                gpointer        v_object);
514 gpointer    g_value_get_object                (const GValue   *value);
515 gpointer    g_value_dup_object                (const GValue   *value);
516 gulong      g_signal_connect_object           (gpointer        instance,
517                                                const gchar    *detailed_signal,
518                                                GCallback       c_handler,
519                                                gpointer        gobject,
520                                                GConnectFlags   connect_flags);
521
522 /*< protected >*/
523 void        g_object_force_floating           (GObject        *object);
524 void        g_object_run_dispose              (GObject        *object);
525
526
527 void        g_value_take_object               (GValue         *value,
528                                                gpointer        v_object);
529 #ifndef G_DISABLE_DEPRECATED
530 void        g_value_set_object_take_ownership (GValue         *value,
531                                                gpointer        v_object);
532 #endif
533
534 #if !defined(G_DISABLE_DEPRECATED) || defined(GTK_COMPILATION)
535 gsize       g_object_compat_control           (gsize           what,
536                                                gpointer        data);
537 #endif
538
539 /* --- implementation macros --- */
540 #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
541 G_STMT_START { \
542   GObject *_object = (GObject*) (object); \
543   GParamSpec *_pspec = (GParamSpec*) (pspec); \
544   guint _property_id = (property_id); \
545   g_warning ("%s: invalid %s id %u for \"%s\" of type `%s' in `%s'", \
546              G_STRLOC, \
547              (pname), \
548              _property_id, \
549              _pspec->name, \
550              g_type_name (G_PARAM_SPEC_TYPE (_pspec)), \
551              G_OBJECT_TYPE_NAME (_object)); \
552 } G_STMT_END
553 /**
554  * G_OBJECT_WARN_INVALID_PROPERTY_ID:
555  * @object: the #GObject on which set_property() or get_property() was called
556  * @property_id: the numeric id of the property
557  * @pspec: the #GParamSpec of the property
558  * 
559  * This macro should be used to emit a standard warning about unexpected 
560  * properties in set_property() and get_property() implementations.
561  */
562 #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
563     G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
564
565 void    g_clear_object (volatile GObject **object_ptr);
566 #define g_clear_object(object_ptr) \
567   G_STMT_START {                                                             \
568     /* Only one access, please */                                            \
569     gpointer *_p = (gpointer) (object_ptr);                                  \
570     gpointer _o;                                                             \
571                                                                              \
572     do                                                                       \
573       _o = g_atomic_pointer_get (_p);                                        \
574     while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_p, _o, NULL));\
575                                                                              \
576     if (_o)                                                                  \
577       g_object_unref (_o);                                                   \
578   } G_STMT_END
579
580 G_END_DECLS
581
582 #endif /* __G_OBJECT_H__ */