1c8e0b40b7abf20fb823310194dda340547b3a03
[platform/upstream/glib.git] / gobject / gtype.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998, 1999, 2000 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_gobject;
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_PARAM,
63   G_TYPE_BOXED,
64   G_TYPE_POINTER,
65   G_TYPE_OBJECT,
66
67   /* the following reserved ids should vanish soon */
68   G_TYPE_GTK_SIGNAL,
69
70   /* reserved fundamental type ids,
71    * mail gtk-devel-list@redhat.com for reservations
72    */
73   G_TYPE_BSE_PROCEDURE,
74   G_TYPE_BSE_TIME,
75   G_TYPE_BSE_NOTE,
76   G_TYPE_BSE_DOTS,
77   G_TYPE_GLE_GOBJECT,
78
79   G_TYPE_LAST_RESERVED_FUNDAMENTAL,
80
81   /* derived type ids */
82   /* FIXME: G_TYPE_PARAM_INTERFACE */
83   G_TYPE_PARAM_CHAR             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 1),
84   G_TYPE_PARAM_UCHAR            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 2),
85   G_TYPE_PARAM_BOOLEAN          = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 3),
86   G_TYPE_PARAM_INT              = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 4),
87   G_TYPE_PARAM_UINT             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 5),
88   G_TYPE_PARAM_LONG             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 6),
89   G_TYPE_PARAM_ULONG            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 7),
90   G_TYPE_PARAM_ENUM             = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 8),
91   G_TYPE_PARAM_FLAGS            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 9),
92   G_TYPE_PARAM_FLOAT            = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 10),
93   G_TYPE_PARAM_DOUBLE           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 11),
94   G_TYPE_PARAM_STRING           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 12),
95   /* FIXME: G_TYPE_PARAM_PARAM */
96   G_TYPE_PARAM_OBJECT           = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 13)
97 } GTypeFundamentals;
98
99
100 /* Type Checking Macros
101  */
102 #define G_TYPE_IS_FUNDAMENTAL(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_PARAM(type)                   (G_TYPE_FUNDAMENTAL (type) == G_TYPE_PARAM)
110 #define G_TYPE_IS_VALUE_TYPE(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 _GTypePluginVTable       GTypePluginVTable;
120 typedef struct _GTypeClass              GTypeClass;
121 typedef struct _GTypeInterface          GTypeInterface;
122 typedef struct _GTypeInstance           GTypeInstance;
123 typedef struct _GTypeInfo               GTypeInfo;
124 typedef struct _GTypeFundamentalInfo    GTypeFundamentalInfo;
125 typedef struct _GInterfaceInfo          GInterfaceInfo;
126 typedef struct _GTypeValueTable         GTypeValueTable;
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
148
149 /* Casts, checks and accessors for structured types
150  * usage of these macros is reserved to type implementations only
151  * 
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), 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_CVT ((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 /* --- prototypes --- */
169 void     g_type_init                    (void);
170 gchar*   g_type_name                    (GType                   type);
171 GQuark   g_type_qname                   (GType                   type);
172 GType    g_type_from_name               (const gchar            *name);
173 GType    g_type_parent                  (GType                   type);
174 GType    g_type_next_base               (GType                   type,
175                                          GType                   base_type);
176 gboolean g_type_is_a                    (GType                   type,
177                                          GType                   is_a_type);
178 gboolean g_type_conforms_to             (GType                   type,
179                                          GType                   iface_type);
180 guint    g_type_fundamental_branch_last (GType                   type);
181 gpointer g_type_class_ref               (GType                   type);
182 gpointer g_type_class_peek              (GType                   type);
183 void     g_type_class_unref             (gpointer                g_class);
184 gpointer g_type_class_peek_parent       (gpointer                g_class);
185 gpointer g_type_interface_peek          (gpointer                instance_class,
186                                          GType                   iface_type);
187 /* g_free() the returned arrays */
188 GType*   g_type_children                (GType                   type,
189                                          guint                  *n_children);
190 GType*   g_type_interfaces              (GType                   type,
191                                          guint                  *n_interfaces);
192 /* per-type _static_ data */
193 void     g_type_set_qdata               (GType                   type,
194                                          GQuark                  quark,
195                                          gpointer                data);
196 gpointer g_type_get_qdata               (GType                   type,
197                                          GQuark                  quark);
198                                           
199
200 /* --- type registration --- */
201 typedef void   (*GBaseInitFunc)              (gpointer         g_class);
202 typedef void   (*GBaseFinalizeFunc)          (gpointer         g_class);
203 typedef void   (*GClassInitFunc)             (gpointer         g_class,
204                                               gpointer         class_data);
205 typedef void   (*GClassFinalizeFunc)         (gpointer         g_class,
206                                               gpointer         class_data);
207 typedef void   (*GInstanceInitFunc)          (GTypeInstance   *instance,
208                                               gpointer         g_class);
209 typedef void   (*GInterfaceInitFunc)         (gpointer         g_iface,
210                                               gpointer         iface_data);
211 typedef void   (*GInterfaceFinalizeFunc)     (gpointer         g_iface,
212                                               gpointer         iface_data);
213 typedef void (*GTypePluginRef)               (GTypePlugin     *plugin);
214 typedef void (*GTypePluginUnRef)             (GTypePlugin     *plugin);
215 typedef void (*GTypePluginFillTypeInfo)      (GTypePlugin     *plugin,
216                                               GType            g_type,
217                                               GTypeInfo       *info,
218                                               GTypeValueTable *value_table);
219 typedef void (*GTypePluginFillInterfaceInfo) (GTypePlugin     *plugin,
220                                               GType            interface_type,
221                                               GType            instance_type,
222                                               GInterfaceInfo  *info);
223 typedef gboolean (*GTypeClassCacheFunc)      (gpointer         cache_data,
224                                               GTypeClass      *g_class);
225 struct _GTypePlugin
226 {
227   GTypePluginVTable     *vtable;
228 };
229 struct _GTypePluginVTable
230 {
231   GTypePluginRef                plugin_ref;
232   GTypePluginUnRef              plugin_unref;
233   GTypePluginFillTypeInfo       complete_type_info;
234   GTypePluginFillInterfaceInfo  complete_interface_info;
235 };
236 typedef enum    /*< skip >*/
237 {
238   G_TYPE_FLAG_CLASSED           = (1 << 0),
239   G_TYPE_FLAG_INSTANTIATABLE    = (1 << 1),
240   G_TYPE_FLAG_DERIVABLE         = (1 << 2),
241   G_TYPE_FLAG_DEEP_DERIVABLE    = (1 << 3)
242 } GTypeFundamentalFlags;
243 typedef enum    /*< skip >*/
244 {
245   G_TYPE_FLAG_ABSTRACT          = (1 << 4)
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   guint      collect_type;
287   gchar*   (*collect_value)      (GValue       *value,
288                                   guint         nth_value,
289                                   GType        *collect_type,
290                                   GTypeCValue  *collect_value);
291   guint      lcopy_type;
292   gchar*   (*lcopy_value)        (const GValue *value,
293                                   guint         nth_value,
294                                   GType        *collect_type,
295                                   GTypeCValue  *collect_value);
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
317
318 /* --- protected (for fundamental type implementations) --- */
319 GTypePlugin*     g_type_get_plugin              (GType               type);
320 GType            g_type_fundamental_last        (void);
321 gboolean         g_type_check_flags             (GType               type,
322                                                  guint               flags);
323 GTypeInstance*   g_type_create_instance         (GType               type);
324 void             g_type_free_instance           (GTypeInstance      *instance);
325 void             g_type_add_class_cache_func    (gpointer            cache_data,
326                                                  GTypeClassCacheFunc cache_func);
327 void             g_type_remove_class_cache_func (gpointer            cache_data,
328                                                  GTypeClassCacheFunc cache_func);
329 void             g_type_class_unref_uncached    (gpointer            g_class);
330
331
332 /*< private >*/
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 GTypeInstance*   g_type_check_instance_cast     (GTypeInstance      *instance,
338                                                  GType               iface_type);
339 gboolean         g_type_instance_conforms_to    (GTypeInstance      *instance,
340                                                  GType               iface_type);
341 gboolean         g_type_check_value             (GValue             *value);
342 gboolean         g_type_value_conforms_to       (GValue             *value,
343                                                  GType               type);
344 gboolean         g_type_check_instance          (GTypeInstance      *instance);
345 GTypeValueTable* g_type_value_table_peek        (GType               type);
346
347
348 #ifndef G_DISABLE_CAST_CHECKS
349 #  define _G_TYPE_CIC(ip, gt, ct) \
350     ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
351 #  define _G_TYPE_CCC(cp, gt, ct) \
352     ((ct*) g_type_check_class_cast ((GTypeClass*) cp, gt))
353 #else /* G_DISABLE_CAST_CHECKS */
354 #  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
355 #  define _G_TYPE_CCC(cp, gt, ct)       ((ct*) cp)
356 #endif /* G_DISABLE_CAST_CHECKS */
357 #define _G_TYPE_CHI(ip)                 (g_type_check_instance ((GTypeInstance*) ip))
358 #define _G_TYPE_CIT(ip, gt)             (g_type_instance_conforms_to ((GTypeInstance*) ip, gt))
359 #define _G_TYPE_CCT(cp, gt)             (g_type_class_is_a ((GTypeClass*) cp, gt))
360 #define _G_TYPE_CVT(vl, gt)             (g_type_value_conforms_to ((GValue*) vl, gt))
361 #define _G_TYPE_CHV(vl)                 (g_type_check_value ((GValue*) vl))
362 #define _G_TYPE_IGC(ip, ct)             ((ct*) (((GTypeInstance*) ip)->g_class))
363 #define _G_TYPE_IGI(ip, gt, ct)         ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
364
365
366 #ifdef __cplusplus
367 }
368 #endif /* __cplusplus */
369
370 #endif /* __G_TYPE_H__ */