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