Make some changes to the way that GMainContext works:
[platform/upstream/glib.git] / gobject / gtype.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 #ifndef __G_TYPE_H__
20 #define __G_TYPE_H__
21
22 extern const char *g_log_domain_gruntime;
23 #include        <glib.h>
24
25 G_BEGIN_DECLS
26
27 /* Basic Type Macros
28  */
29 #define G_TYPE_FUNDAMENTAL(type)                ((type) & 0xff)
30 #define G_TYPE_FUNDAMENTAL_MAX                  (0xff)
31 #define G_TYPE_DERIVE_ID(ptype, branch_seqno)   (G_TYPE_FUNDAMENTAL (ptype) | ((branch_seqno) << 8))
32 #define G_TYPE_BRANCH_SEQNO(type)               ((type) >> 8)
33 #define G_TYPE_FUNDAMENTAL_LAST                 ((GType) g_type_fundamental_last ())
34
35
36 /* predefined fundamental and derived types
37  */
38 typedef enum    /*< skip >*/
39 {
40   /* standard types, introduced by g_type_init() */
41   G_TYPE_INVALID,
42   G_TYPE_NONE,
43   G_TYPE_INTERFACE,
44
45   /* GLib type ids */
46   G_TYPE_CHAR,
47   G_TYPE_UCHAR,
48   G_TYPE_BOOLEAN,
49   G_TYPE_INT,
50   G_TYPE_UINT,
51   G_TYPE_LONG,
52   G_TYPE_ULONG,
53   G_TYPE_ENUM,
54   G_TYPE_FLAGS,
55   G_TYPE_FLOAT,
56   G_TYPE_DOUBLE,
57   G_TYPE_STRING,
58   G_TYPE_POINTER,
59   G_TYPE_BOXED,
60   G_TYPE_PARAM,
61   G_TYPE_OBJECT,
62
63   /* reserved fundamental type ids,
64    * mail gtk-devel-list@redhat.com for reservations
65    */
66   G_TYPE_RESERVED_BSE_FIRST,
67   G_TYPE_RESERVED_BSE_LAST      = G_TYPE_RESERVED_BSE_FIRST + 15,
68   G_TYPE_RESERVED_LAST_FUNDAMENTAL,
69
70   /* derived type ids */
71   G_TYPE_CLOSURE                = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 1),
72   G_TYPE_VALUE                  = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 2),
73   G_TYPE_VALUE_ARRAY            = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 3),
74   G_TYPE_GSTRING                = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 4),
75   G_TYPE_PARAM_CHAR             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 1),
76   G_TYPE_PARAM_UCHAR            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 2),
77   G_TYPE_PARAM_BOOLEAN          = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 3),
78   G_TYPE_PARAM_INT              = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 4),
79   G_TYPE_PARAM_UINT             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 5),
80   G_TYPE_PARAM_LONG             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 6),
81   G_TYPE_PARAM_ULONG            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 7),
82   G_TYPE_PARAM_UNICHAR          = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 8),
83   G_TYPE_PARAM_ENUM             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 9),
84   G_TYPE_PARAM_FLAGS            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 10),
85   G_TYPE_PARAM_FLOAT            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 11),
86   G_TYPE_PARAM_DOUBLE           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 12),
87   G_TYPE_PARAM_STRING           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 13),
88   G_TYPE_PARAM_PARAM            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 14),
89   G_TYPE_PARAM_BOXED            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 15),
90   G_TYPE_PARAM_POINTER          = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 16),
91   G_TYPE_PARAM_VALUE_ARRAY      = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 17),
92   G_TYPE_PARAM_CLOSURE          = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 18),
93   G_TYPE_PARAM_OBJECT           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 19)
94 } GTypeFundamentals;
95
96
97 /* Type Checking Macros
98  */
99 #define G_TYPE_IS_FUNDAMENTAL(type)             (G_TYPE_BRANCH_SEQNO (type) == 0)
100 #define G_TYPE_IS_DERIVED(type)                 (G_TYPE_BRANCH_SEQNO (type) > 0)
101 #define G_TYPE_IS_INTERFACE(type)               (G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE)
102 #define G_TYPE_IS_CLASSED(type)                 (g_type_check_flags ((type), G_TYPE_FLAG_CLASSED))
103 #define G_TYPE_IS_INSTANTIATABLE(type)          (g_type_check_flags ((type), G_TYPE_FLAG_INSTANTIATABLE))
104 #define G_TYPE_IS_DERIVABLE(type)               (g_type_check_flags ((type), G_TYPE_FLAG_DERIVABLE))
105 #define G_TYPE_IS_DEEP_DERIVABLE(type)          (g_type_check_flags ((type), G_TYPE_FLAG_DEEP_DERIVABLE))
106 #define G_TYPE_IS_ABSTRACT(type)                (g_type_check_flags ((type), G_TYPE_FLAG_ABSTRACT))
107 #define G_TYPE_IS_VALUE_ABSTRACT(type)          (g_type_check_flags ((type), G_TYPE_FLAG_VALUE_ABSTRACT))
108 #define G_TYPE_IS_PARAM(type)                   (G_TYPE_FUNDAMENTAL (type) == G_TYPE_PARAM)
109 #define G_TYPE_IS_VALUE_TYPE(type)              (g_type_check_is_value_type (type))
110 #define G_TYPE_HAS_VALUE_TABLE(type)            (g_type_value_table_peek (type) != NULL)
111
112
113 /* Typedefs
114  */
115 typedef guint32                         GType;
116 typedef struct _GValue                  GValue;
117 typedef union  _GTypeCValue             GTypeCValue;
118 typedef struct _GTypePlugin             GTypePlugin;
119 typedef struct _GTypeClass              GTypeClass;
120 typedef struct _GTypeInterface          GTypeInterface;
121 typedef struct _GTypeInstance           GTypeInstance;
122 typedef struct _GTypeInfo               GTypeInfo;
123 typedef struct _GTypeFundamentalInfo    GTypeFundamentalInfo;
124 typedef struct _GInterfaceInfo          GInterfaceInfo;
125 typedef struct _GTypeValueTable         GTypeValueTable;
126 typedef struct _GTypeQuery              GTypeQuery;
127
128
129 /* Basic Type Structures
130  */
131 struct _GTypeClass
132 {
133   /*< private >*/
134   GType g_type;
135 };
136 struct _GTypeInstance
137 {
138   /*< private >*/
139   GTypeClass *g_class;
140 };
141 struct _GTypeInterface
142 {
143   /*< private >*/
144   GType g_type;         /* iface type */
145   GType g_instance_type;
146 };
147 struct _GTypeQuery
148 {
149   GType         type;
150   const gchar  *type_name;
151   guint         class_size;
152   guint         instance_size;
153 };
154
155
156 /* Casts, checks and accessors for structured types
157  * usage of these macros is reserved to type implementations only
158  */
159 /*< protected >*/
160 #define G_TYPE_CHECK_INSTANCE(instance)                         (_G_TYPE_CHI ((GTypeInstance*) (instance)))
161 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
162 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type)            (_G_TYPE_CIT ((instance), (g_type)))
163 #define G_TYPE_INSTANCE_GET_CLASS(instance, g_type, c_type)     (_G_TYPE_IGC ((instance), (g_type), c_type))
164 #define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
165 #define G_TYPE_CHECK_CLASS_CAST(g_class, g_type, c_type)        (_G_TYPE_CCC ((g_class), (g_type), c_type))
166 #define G_TYPE_CHECK_CLASS_TYPE(g_class, g_type)                (_G_TYPE_CCT ((g_class), (g_type)))
167 #define G_TYPE_CHECK_VALUE(value)                               (_G_TYPE_CHV ((value)))
168 #define G_TYPE_CHECK_VALUE_TYPE(value, g_type)                  (_G_TYPE_CVH ((value), (g_type)))
169 #define G_TYPE_FROM_INSTANCE(instance)                          (G_TYPE_FROM_CLASS (((GTypeInstance*) (instance))->g_class))
170 #define G_TYPE_FROM_CLASS(g_class)                              (((GTypeClass*) (g_class))->g_type)
171 #define G_TYPE_FROM_INTERFACE(g_iface)                          (((GTypeInterface*) (g_iface))->g_type)
172
173
174 /* debug flags for g_type_init() */
175 typedef enum    /*< skip >*/
176 {
177   G_TYPE_DEBUG_NONE     = 0,
178   G_TYPE_DEBUG_OBJECTS  = 1 << 0,
179   G_TYPE_DEBUG_SIGNALS  = 1 << 1,
180   G_TYPE_DEBUG_MASK     = 0x03
181 } GTypeDebugFlags;
182
183
184 /* --- prototypes --- */
185 void                  g_type_init                    (void);
186 void                  g_type_init_with_debug_flags   (GTypeDebugFlags  debug_flags);
187 G_CONST_RETURN gchar* g_type_name                    (GType            type);
188 GQuark                g_type_qname                   (GType            type);
189 GType                 g_type_from_name               (const gchar     *name);
190 GType                 g_type_parent                  (GType            type);
191 guint                 g_type_depth                   (GType            type);
192 GType                 g_type_next_base               (GType            leaf_type,
193                                                       GType            root_type);
194 gboolean              g_type_is_a                    (GType            type,
195                                                       GType            is_a_type);
196 guint                 g_type_fundamental_branch_last (GType            type);
197 gpointer              g_type_class_ref               (GType            type);
198 gpointer              g_type_class_peek              (GType            type);
199 void                  g_type_class_unref             (gpointer         g_class);
200 gpointer              g_type_class_peek_parent       (gpointer         g_class);
201 gpointer              g_type_interface_peek          (gpointer         instance_class,
202                                                       GType            iface_type);
203
204 /* g_free() the returned arrays */
205 GType*                g_type_children                (GType            type,
206                                                       guint           *n_children);
207 GType*                g_type_interfaces              (GType            type,
208                                                       guint           *n_interfaces);
209
210 /* per-type _static_ data */
211 void                  g_type_set_qdata               (GType            type,
212                                                       GQuark           quark,
213                                                       gpointer         data);
214 gpointer              g_type_get_qdata               (GType            type,
215                                                       GQuark           quark);
216 void                  g_type_query                   (GType            type,
217                                                       GTypeQuery      *query);
218
219
220 /* --- type registration --- */
221 typedef void   (*GBaseInitFunc)              (gpointer         g_class);
222 typedef void   (*GBaseFinalizeFunc)          (gpointer         g_class);
223 typedef void   (*GClassInitFunc)             (gpointer         g_class,
224                                               gpointer         class_data);
225 typedef void   (*GClassFinalizeFunc)         (gpointer         g_class,
226                                               gpointer         class_data);
227 typedef void   (*GInstanceInitFunc)          (GTypeInstance   *instance,
228                                               gpointer         g_class);
229 typedef void   (*GInterfaceInitFunc)         (gpointer         g_iface,
230                                               gpointer         iface_data);
231 typedef void   (*GInterfaceFinalizeFunc)     (gpointer         g_iface,
232                                               gpointer         iface_data);
233 typedef gboolean (*GTypeClassCacheFunc)      (gpointer         cache_data,
234                                               GTypeClass      *g_class);
235 typedef enum    /*< skip >*/
236 {
237   G_TYPE_FLAG_CLASSED           = (1 << 0),
238   G_TYPE_FLAG_INSTANTIATABLE    = (1 << 1),
239   G_TYPE_FLAG_DERIVABLE         = (1 << 2),
240   G_TYPE_FLAG_DEEP_DERIVABLE    = (1 << 3)
241 } GTypeFundamentalFlags;
242 typedef enum    /*< skip >*/
243 {
244   G_TYPE_FLAG_ABSTRACT          = (1 << 4),
245   G_TYPE_FLAG_VALUE_ABSTRACT    = (1 << 5)
246 } GTypeFlags;
247 struct _GTypeInfo
248 {
249   /* interface types, classed types, instantiated types */
250   guint16                class_size;
251
252   GBaseInitFunc          base_init;
253   GBaseFinalizeFunc      base_finalize;
254
255   /* classed types, instantiated types */
256   GClassInitFunc         class_init;
257   GClassFinalizeFunc     class_finalize;
258   gconstpointer          class_data;
259
260   /* instantiated types */
261   guint16                instance_size;
262   guint16                n_preallocs;
263   GInstanceInitFunc      instance_init;
264
265   /* value handling */
266   const GTypeValueTable *value_table;
267 };
268 struct _GTypeFundamentalInfo
269 {
270   GTypeFundamentalFlags  type_flags;
271 };
272 struct _GInterfaceInfo
273 {
274   GInterfaceInitFunc     interface_init;
275   GInterfaceFinalizeFunc interface_finalize;
276   gpointer               interface_data;
277 };
278 struct _GTypeValueTable
279 {
280   void     (*value_init)         (GValue       *value);
281   void     (*value_free)         (GValue       *value);
282   void     (*value_copy)         (const GValue *src_value,
283                                   GValue       *dest_value);
284   /* varargs functionality (optional) */
285   gpointer (*value_peek_pointer) (const GValue *value);
286   gchar     *collect_format;
287   gchar*   (*collect_value)      (GValue       *value,
288                                   guint         n_collect_values,
289                                   GTypeCValue  *collect_values,
290                                   guint         collect_flags);
291   gchar     *lcopy_format;
292   gchar*   (*lcopy_value)        (const GValue *value,
293                                   guint         n_collect_values,
294                                   GTypeCValue  *collect_values,
295                                   guint         collect_flags);
296 };
297 GType g_type_register_static            (GType                       parent_type,
298                                          const gchar                *type_name,
299                                          const GTypeInfo            *info,
300                                          GTypeFlags                  flags);
301 GType g_type_register_dynamic           (GType                       parent_type,
302                                          const gchar                *type_name,
303                                          GTypePlugin                *plugin,
304                                          GTypeFlags                  flags);
305 GType g_type_register_fundamental       (GType                       type_id,
306                                          const gchar                *type_name,
307                                          const GTypeInfo            *info,
308                                          const GTypeFundamentalInfo *finfo,
309                                          GTypeFlags                  flags);
310 void  g_type_add_interface_static       (GType                       instance_type,
311                                          GType                       interface_type,
312                                          const GInterfaceInfo       *info);
313 void  g_type_add_interface_dynamic      (GType                       instance_type,
314                                          GType                       interface_type,
315                                          GTypePlugin                *plugin);
316 void  g_type_interface_add_prerequisite (GType                       interface_type,
317                                          GType                       prerequisite_type);
318
319
320 /* --- protected (for fundamental type implementations) --- */
321 GTypePlugin*     g_type_get_plugin              (GType               type);
322 GTypePlugin*     g_type_interface_get_plugin    (GType               instance_type,
323                                                  GType               implementation_type);
324
325 GType            g_type_fundamental_last        (void);
326 gboolean         g_type_check_flags             (GType               type,
327                                                  guint               flags);
328 GTypeInstance*   g_type_create_instance         (GType               type);
329 void             g_type_free_instance           (GTypeInstance      *instance);
330 void             g_type_add_class_cache_func    (gpointer            cache_data,
331                                                  GTypeClassCacheFunc cache_func);
332 void             g_type_remove_class_cache_func (gpointer            cache_data,
333                                                  GTypeClassCacheFunc cache_func);
334 void             g_type_class_unref_uncached    (gpointer            g_class);
335
336
337 /*< private >*/
338 gboolean         g_type_check_instance          (GTypeInstance      *instance);
339 GTypeInstance*   g_type_check_instance_cast     (GTypeInstance      *instance,
340                                                  GType               iface_type);
341 gboolean         g_type_instance_is_a           (GTypeInstance      *instance,
342                                                  GType               iface_type);
343 GTypeClass*      g_type_check_class_cast        (GTypeClass         *g_class,
344                                                  GType               is_a_type);
345 gboolean         g_type_class_is_a              (GTypeClass         *g_class,
346                                                  GType               is_a_type);
347 gboolean         g_type_check_is_value_type     (GType               type);
348 gboolean         g_type_check_value             (GValue             *value);
349 gboolean         g_type_check_value_holds       (GValue             *value,
350                                                  GType               type);
351 GTypeValueTable* g_type_value_table_peek        (GType               type);
352
353
354 /* --- debugging functions --- */
355 G_CONST_RETURN gchar* g_type_name_from_instance (GTypeInstance  *instance);
356 G_CONST_RETURN gchar* g_type_name_from_class    (GTypeClass     *g_class);
357
358
359 /* --- implementation bits --- */
360 #ifndef G_DISABLE_CAST_CHECKS
361 #  define _G_TYPE_CIC(ip, gt, ct) \
362     ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
363 #  define _G_TYPE_CCC(cp, gt, ct) \
364     ((ct*) g_type_check_class_cast ((GTypeClass*) cp, gt))
365 #else /* G_DISABLE_CAST_CHECKS */
366 #  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
367 #  define _G_TYPE_CCC(cp, gt, ct)       ((ct*) cp)
368 #endif /* G_DISABLE_CAST_CHECKS */
369 #define _G_TYPE_CHI(ip)                 (g_type_check_instance ((GTypeInstance*) ip))
370 #define _G_TYPE_CIT(ip, gt)             (g_type_instance_is_a ((GTypeInstance*) ip, gt))
371 #define _G_TYPE_CCT(cp, gt)             (g_type_class_is_a ((GTypeClass*) cp, gt))
372 #define _G_TYPE_CVH(vl, gt)             (g_type_check_value_holds ((GValue*) vl, gt))
373 #define _G_TYPE_CHV(vl)                 (g_type_check_value ((GValue*) vl))
374 #define _G_TYPE_IGC(ip, gt, ct)         ((ct*) (((GTypeInstance*) ip)->g_class))
375 #define _G_TYPE_IGI(ip, gt, ct)         ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
376 #define G_TYPE_FLAG_RESERVED_ID_BIT     (1 << 30)
377 extern GTypeDebugFlags                  _g_type_debug_flags;
378
379 G_END_DECLS
380
381 #endif /* __G_TYPE_H__ */