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