1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
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.
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.
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.
28 #define G_TYPE_FUNDAMENTAL(type) ((type) & 0xff)
29 #define G_TYPE_FUNDAMENTAL_MAX (0xff)
30 #define G_TYPE_DERIVE_ID(ptype, branch_seqno) (G_TYPE_FUNDAMENTAL (ptype) | ((branch_seqno) << 8))
31 #define G_TYPE_BRANCH_SEQNO(type) ((type) >> 8)
32 #define G_TYPE_FUNDAMENTAL_LAST ((GType) g_type_fundamental_last ())
35 /* predefined fundamental and derived types
37 typedef enum /*< skip >*/
39 /* standard types, introduced by g_type_init() */
64 /* reserved fundamental type ids,
65 * mail gtk-devel-list@redhat.com for reservations
67 G_TYPE_RESERVED_BSE_FIRST,
68 G_TYPE_RESERVED_BSE_LAST = G_TYPE_RESERVED_BSE_FIRST + 15,
69 G_TYPE_RESERVED_LAST_FUNDAMENTAL,
71 /* derived type ids */
72 G_TYPE_CLOSURE = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 1),
73 G_TYPE_VALUE = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 2),
74 G_TYPE_VALUE_ARRAY = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 3),
75 G_TYPE_GSTRING = G_TYPE_DERIVE_ID (G_TYPE_BOXED, 4),
76 G_TYPE_PARAM_CHAR = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 1),
77 G_TYPE_PARAM_UCHAR = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 2),
78 G_TYPE_PARAM_BOOLEAN = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 3),
79 G_TYPE_PARAM_INT = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 4),
80 G_TYPE_PARAM_UINT = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 5),
81 G_TYPE_PARAM_LONG = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 6),
82 G_TYPE_PARAM_ULONG = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 7),
83 G_TYPE_PARAM_INT64 = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 8),
84 G_TYPE_PARAM_UINT64 = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 9),
85 G_TYPE_PARAM_UNICHAR = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 10),
86 G_TYPE_PARAM_ENUM = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 11),
87 G_TYPE_PARAM_FLAGS = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 12),
88 G_TYPE_PARAM_FLOAT = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 13),
89 G_TYPE_PARAM_DOUBLE = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 14),
90 G_TYPE_PARAM_STRING = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 15),
91 G_TYPE_PARAM_PARAM = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 16),
92 G_TYPE_PARAM_BOXED = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 17),
93 G_TYPE_PARAM_POINTER = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 18),
94 G_TYPE_PARAM_VALUE_ARRAY = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 19),
95 G_TYPE_PARAM_OBJECT = G_TYPE_DERIVE_ID (G_TYPE_PARAM, 20)
100 /* Type Checking Macros
102 #define G_TYPE_IS_FUNDAMENTAL(type) (G_TYPE_BRANCH_SEQNO (type) == 0)
103 #define G_TYPE_IS_DERIVED(type) (G_TYPE_BRANCH_SEQNO (type) > 0)
104 #define G_TYPE_IS_INTERFACE(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE)
105 #define G_TYPE_IS_CLASSED(type) (g_type_test_flags ((type), G_TYPE_FLAG_CLASSED))
106 #define G_TYPE_IS_INSTANTIATABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_INSTANTIATABLE))
107 #define G_TYPE_IS_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DERIVABLE))
108 #define G_TYPE_IS_DEEP_DERIVABLE(type) (g_type_test_flags ((type), G_TYPE_FLAG_DEEP_DERIVABLE))
109 #define G_TYPE_IS_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_ABSTRACT))
110 #define G_TYPE_IS_VALUE_ABSTRACT(type) (g_type_test_flags ((type), G_TYPE_FLAG_VALUE_ABSTRACT))
111 #define G_TYPE_IS_PARAM(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_PARAM)
112 #define G_TYPE_IS_VALUE_TYPE(type) (g_type_check_is_value_type (type))
113 #define G_TYPE_HAS_VALUE_TABLE(type) (g_type_value_table_peek (type) != NULL)
118 typedef guint32 GType;
119 typedef struct _GValue GValue;
120 typedef union _GTypeCValue GTypeCValue;
121 typedef struct _GTypePlugin GTypePlugin;
122 typedef struct _GTypeClass GTypeClass;
123 typedef struct _GTypeInterface GTypeInterface;
124 typedef struct _GTypeInstance GTypeInstance;
125 typedef struct _GTypeInfo GTypeInfo;
126 typedef struct _GTypeFundamentalInfo GTypeFundamentalInfo;
127 typedef struct _GInterfaceInfo GInterfaceInfo;
128 typedef struct _GTypeValueTable GTypeValueTable;
129 typedef struct _GTypeQuery GTypeQuery;
132 /* Basic Type Structures
139 struct _GTypeInstance
144 struct _GTypeInterface
147 GType g_type; /* iface type */
148 GType g_instance_type;
153 const gchar *type_name;
159 /* Casts, checks and accessors for structured types
160 * usage of these macros is reserved to type implementations only
163 #define G_TYPE_CHECK_INSTANCE(instance) (_G_TYPE_CHI ((GTypeInstance*) (instance)))
164 #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))
165 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type) (_G_TYPE_CIT ((instance), (g_type)))
166 #define G_TYPE_INSTANCE_GET_CLASS(instance, g_type, c_type) (_G_TYPE_IGC ((instance), (g_type), c_type))
167 #define G_TYPE_INSTANCE_GET_INTERFACE(instance, g_type, c_type) (_G_TYPE_IGI ((instance), (g_type), c_type))
168 #define G_TYPE_CHECK_CLASS_CAST(g_class, g_type, c_type) (_G_TYPE_CCC ((g_class), (g_type), c_type))
169 #define G_TYPE_CHECK_CLASS_TYPE(g_class, g_type) (_G_TYPE_CCT ((g_class), (g_type)))
170 #define G_TYPE_CHECK_VALUE(value) (_G_TYPE_CHV ((value)))
171 #define G_TYPE_CHECK_VALUE_TYPE(value, g_type) (_G_TYPE_CVH ((value), (g_type)))
172 #define G_TYPE_FROM_INSTANCE(instance) (G_TYPE_FROM_CLASS (((GTypeInstance*) (instance))->g_class))
173 #define G_TYPE_FROM_CLASS(g_class) (((GTypeClass*) (g_class))->g_type)
174 #define G_TYPE_FROM_INTERFACE(g_iface) (((GTypeInterface*) (g_iface))->g_type)
177 /* debug flags for g_type_init_with_debug_flags() */
178 typedef enum /*< skip >*/
180 G_TYPE_DEBUG_NONE = 0,
181 G_TYPE_DEBUG_OBJECTS = 1 << 0,
182 G_TYPE_DEBUG_SIGNALS = 1 << 1,
183 G_TYPE_DEBUG_MASK = 0x03
187 /* --- prototypes --- */
188 void g_type_init (void);
189 void g_type_init_with_debug_flags (GTypeDebugFlags debug_flags);
190 G_CONST_RETURN gchar* g_type_name (GType type);
191 GQuark g_type_qname (GType type);
192 GType g_type_from_name (const gchar *name);
193 GType g_type_parent (GType type);
194 guint g_type_depth (GType type);
195 GType g_type_next_base (GType leaf_type,
197 gboolean g_type_is_a (GType type,
199 guint g_type_fundamental_branch_last (GType type);
200 gpointer g_type_class_ref (GType type);
201 gpointer g_type_class_peek (GType type);
202 void g_type_class_unref (gpointer g_class);
203 gpointer g_type_class_peek_parent (gpointer g_class);
204 gpointer g_type_interface_peek (gpointer instance_class,
206 gpointer g_type_interface_peek_parent (gpointer g_iface);
208 /* g_free() the returned arrays */
209 GType* g_type_children (GType type,
211 GType* g_type_interfaces (GType type,
212 guint *n_interfaces);
214 /* per-type _static_ data */
215 void g_type_set_qdata (GType type,
218 gpointer g_type_get_qdata (GType type,
220 void g_type_query (GType type,
224 /* --- type registration --- */
225 typedef void (*GBaseInitFunc) (gpointer g_class);
226 typedef void (*GBaseFinalizeFunc) (gpointer g_class);
227 typedef void (*GClassInitFunc) (gpointer g_class,
228 gpointer class_data);
229 typedef void (*GClassFinalizeFunc) (gpointer g_class,
230 gpointer class_data);
231 typedef void (*GInstanceInitFunc) (GTypeInstance *instance,
233 typedef void (*GInterfaceInitFunc) (gpointer g_iface,
234 gpointer iface_data);
235 typedef void (*GInterfaceFinalizeFunc) (gpointer g_iface,
236 gpointer iface_data);
237 typedef gboolean (*GTypeClassCacheFunc) (gpointer cache_data,
238 GTypeClass *g_class);
239 typedef enum /*< skip >*/
241 G_TYPE_FLAG_CLASSED = (1 << 0),
242 G_TYPE_FLAG_INSTANTIATABLE = (1 << 1),
243 G_TYPE_FLAG_DERIVABLE = (1 << 2),
244 G_TYPE_FLAG_DEEP_DERIVABLE = (1 << 3)
245 } GTypeFundamentalFlags;
246 typedef enum /*< skip >*/
248 G_TYPE_FLAG_ABSTRACT = (1 << 4),
249 G_TYPE_FLAG_VALUE_ABSTRACT = (1 << 5)
253 /* interface types, classed types, instantiated types */
256 GBaseInitFunc base_init;
257 GBaseFinalizeFunc base_finalize;
259 /* classed types, instantiated types */
260 GClassInitFunc class_init;
261 GClassFinalizeFunc class_finalize;
262 gconstpointer class_data;
264 /* instantiated types */
265 guint16 instance_size;
267 GInstanceInitFunc instance_init;
270 const GTypeValueTable *value_table;
272 struct _GTypeFundamentalInfo
274 GTypeFundamentalFlags type_flags;
276 struct _GInterfaceInfo
278 GInterfaceInitFunc interface_init;
279 GInterfaceFinalizeFunc interface_finalize;
280 gpointer interface_data;
282 struct _GTypeValueTable
284 void (*value_init) (GValue *value);
285 void (*value_free) (GValue *value);
286 void (*value_copy) (const GValue *src_value,
288 /* varargs functionality (optional) */
289 gpointer (*value_peek_pointer) (const GValue *value);
290 gchar *collect_format;
291 gchar* (*collect_value) (GValue *value,
292 guint n_collect_values,
293 GTypeCValue *collect_values,
294 guint collect_flags);
296 gchar* (*lcopy_value) (const GValue *value,
297 guint n_collect_values,
298 GTypeCValue *collect_values,
299 guint collect_flags);
301 GType g_type_register_static (GType parent_type,
302 const gchar *type_name,
303 const GTypeInfo *info,
305 GType g_type_register_dynamic (GType parent_type,
306 const gchar *type_name,
309 GType g_type_register_fundamental (GType type_id,
310 const gchar *type_name,
311 const GTypeInfo *info,
312 const GTypeFundamentalInfo *finfo,
314 void g_type_add_interface_static (GType instance_type,
315 GType interface_type,
316 const GInterfaceInfo *info);
317 void g_type_add_interface_dynamic (GType instance_type,
318 GType interface_type,
319 GTypePlugin *plugin);
320 void g_type_interface_add_prerequisite (GType interface_type,
321 GType prerequisite_type);
324 /* --- protected (for fundamental type implementations) --- */
325 GTypePlugin* g_type_get_plugin (GType type);
326 GTypePlugin* g_type_interface_get_plugin (GType instance_type,
327 GType implementation_type);
329 GType g_type_fundamental_last (void);
330 GTypeInstance* g_type_create_instance (GType type);
331 void g_type_free_instance (GTypeInstance *instance);
332 void g_type_add_class_cache_func (gpointer cache_data,
333 GTypeClassCacheFunc cache_func);
334 void g_type_remove_class_cache_func (gpointer cache_data,
335 GTypeClassCacheFunc cache_func);
336 void g_type_class_unref_uncached (gpointer g_class);
337 GTypeValueTable* g_type_value_table_peek (GType type);
341 gboolean g_type_check_instance (GTypeInstance *instance);
342 GTypeInstance* g_type_check_instance_cast (GTypeInstance *instance,
344 gboolean g_type_check_instance_is_a (GTypeInstance *instance,
346 GTypeClass* g_type_check_class_cast (GTypeClass *g_class,
348 gboolean g_type_check_class_is_a (GTypeClass *g_class,
350 gboolean g_type_check_is_value_type (GType type);
351 gboolean g_type_check_value (GValue *value);
352 gboolean g_type_check_value_holds (GValue *value,
354 gboolean g_type_test_flags (GType type,
358 /* --- debugging functions --- */
359 G_CONST_RETURN gchar* g_type_name_from_instance (GTypeInstance *instance);
360 G_CONST_RETURN gchar* g_type_name_from_class (GTypeClass *g_class);
363 /* --- implementation bits --- */
364 #ifndef G_DISABLE_CAST_CHECKS
365 # define _G_TYPE_CIC(ip, gt, ct) \
366 ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
367 # define _G_TYPE_CCC(cp, gt, ct) \
368 ((ct*) g_type_check_class_cast ((GTypeClass*) cp, gt))
369 #else /* G_DISABLE_CAST_CHECKS */
370 # define _G_TYPE_CIC(ip, gt, ct) ((ct*) ip)
371 # define _G_TYPE_CCC(cp, gt, ct) ((ct*) cp)
372 #endif /* G_DISABLE_CAST_CHECKS */
373 #define _G_TYPE_CHI(ip) (g_type_check_instance ((GTypeInstance*) ip))
374 #define _G_TYPE_CVH(vl, gt) (g_type_check_value_holds ((GValue*) vl, gt))
375 #define _G_TYPE_CHV(vl) (g_type_check_value ((GValue*) vl))
376 #define _G_TYPE_IGC(ip, gt, ct) ((ct*) (((GTypeInstance*) ip)->g_class))
377 #define _G_TYPE_IGI(ip, gt, ct) ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
379 # define _G_TYPE_CIT(ip, gt) ({ \
380 GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \
381 if (__inst && __inst->g_class && __inst->g_class->g_type == __t) \
384 __r = g_type_check_instance_is_a (__inst, __t); \
387 # define _G_TYPE_CCT(cp, gt) ({ \
388 GTypeClass *__class = (GTypeClass*) cp; GType __t = gt; gboolean __r; \
389 if (__class && __class->g_type == __t) \
392 __r = g_type_check_class_is_a (__class, __t); \
395 #else /* !__GNUC__ */
396 # define _G_TYPE_CIT(ip, gt) (g_type_check_instance_is_a ((GTypeInstance*) ip, gt))
397 # define _G_TYPE_CCT(cp, gt) (g_type_check_class_is_a ((GTypeClass*) cp, gt))
398 #endif /* !__GNUC__ */
399 #define G_TYPE_FLAG_RESERVED_ID_BIT (1 << 30)
400 extern GTypeDebugFlags _g_type_debug_flags;
404 #endif /* __G_TYPE_H__ */