1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998, 1999, 2000 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.
23 #define FIXME_DISABLE_PREALLOCATIONS
25 /* NOTE: some functions (some internal variants and exported ones)
26 * invalidate data portions of the TypeNodes. if external functions/callbacks
27 * are called, pointers to memory maintained by TypeNodes have to be looked up
28 * again. this affects most of the struct TypeNode fields, e.g. ->children or
29 * ->iface_entries (not ->supers[] as of recently), as all those memory portions can
30 * get realloc()ed during callback invocation.
33 * - g_type_from_name() should do an ordered array lookup after fetching the
34 * the quark, instead of a second hashtable lookup.
35 * - speedup checks for virtual types, steal a bit somewhere
38 * - force interface initialization for already existing classes
39 * - make things threadsafe
42 #define TYPE_FUNDAMENTAL_FLAG_MASK (G_TYPE_FLAG_CLASSED | \
43 G_TYPE_FLAG_INSTANTIATABLE | \
44 G_TYPE_FLAG_DERIVABLE | \
45 G_TYPE_FLAG_DEEP_DERIVABLE)
46 #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT)
48 #define g_type_plugin_ref(p) ((p)->vtable->plugin_ref (p))
49 #define g_type_plugin_unref(p) ((p)->vtable->plugin_unref (p))
50 #define g_type_plugin_complete_type_info(p,t,i,v) ((p)->vtable->complete_type_info ((p), (t), (i), (v)))
51 #define g_type_plugin_complete_interface_info(p,f,t,i) ((p)->vtable->complete_interface_info ((p), (f), (t), (i)))
54 /* --- typedefs --- */
55 typedef struct _TypeNode TypeNode;
56 typedef struct _CommonData CommonData;
57 typedef struct _IFaceData IFaceData;
58 typedef struct _ClassData ClassData;
59 typedef struct _InstanceData InstanceData;
60 typedef union _TypeData TypeData;
61 typedef struct _IFaceEntry IFaceEntry;
62 typedef struct _IFaceHolder IFaceHolder;
65 /* --- prototypes --- */
66 static inline GTypeFundamentalInfo* type_node_fundamental_info (TypeNode *node);
67 static void type_add_flags (TypeNode *node,
69 static void type_data_make (TypeNode *node,
70 const GTypeInfo *info,
71 const GTypeValueTable *value_table);
72 static inline void type_data_ref (TypeNode *node);
73 static inline void type_data_unref (TypeNode *node,
75 static void type_data_last_unref (GType type,
79 /* --- structures --- */
80 struct _GValue /* kludge, keep in sync with gvalue.h */
87 guint n_children : 12;
91 guint is_instantiatable : 1;
98 IFaceEntry *iface_entries;
99 IFaceHolder *iface_conformants;
101 GType supers[1]; /* flexible array */
103 #define SIZEOF_BASE_TYPE_NODE() (G_STRUCT_OFFSET (TypeNode, supers))
104 #define MAX_N_SUPERS (255)
105 #define MAX_N_CHILDREN (4095)
106 #define MAX_N_IFACES (511)
111 GInterfaceInfo *info;
118 GTypeInterface *vtable;
123 GTypeValueTable *value_table;
129 GBaseInitFunc vtable_init_base;
130 GBaseFinalizeFunc vtable_finalize_base;
136 GBaseInitFunc class_init_base;
137 GBaseFinalizeFunc class_finalize_base;
138 GClassInitFunc class_init;
139 GClassFinalizeFunc class_finalize;
140 gconstpointer class_data;
147 GBaseInitFunc class_init_base;
148 GBaseFinalizeFunc class_finalize_base;
149 GClassInitFunc class_init;
150 GClassFinalizeFunc class_finalize;
151 gconstpointer class_data;
153 guint16 instance_size;
155 GInstanceInitFunc instance_init;
156 GMemChunk *mem_chunk;
163 InstanceData instance;
167 GTypeClassCacheFunc cache_func;
171 /* --- variables --- */
172 static guint n_class_cache_funcs = 0;
173 static ClassCacheFunc *class_cache_funcs = NULL;
174 static GType last_fundamental_id = 0;
175 static GQuark quark_type_flags = 0;
178 /* --- externs --- */
179 const char *g_log_domain_gobject = "GLib-Object";
182 /* --- type nodes --- */
183 static GHashTable *g_type_nodes_ht = NULL;
184 static GType *g_branch_seqnos = NULL;
185 static TypeNode ***g_type_nodes = NULL;
187 static inline TypeNode*
188 LOOKUP_TYPE_NODE (register GType utype)
190 register GType ftype = G_TYPE_FUNDAMENTAL (utype);
191 register GType b_seqno = G_TYPE_BRANCH_SEQNO (utype);
193 if (ftype < last_fundamental_id && b_seqno < g_branch_seqnos[ftype])
194 return g_type_nodes[ftype][b_seqno];
198 #define NODE_TYPE(node) (node->supers[0])
199 #define NODE_PARENT_TYPE(node) (node->supers[1])
200 #define NODE_NAME(node) (g_quark_to_string (node->qname))
203 type_node_any_new (TypeNode *pnode,
207 GTypeFundamentalFlags type_flags)
209 guint branch_last, n_supers = pnode ? pnode->n_supers + 1 : 0;
212 guint i, node_size = 0;
214 branch_last = g_branch_seqnos[ftype]++;
215 type = G_TYPE_DERIVE_ID (ftype, branch_last);
216 if (!branch_last || g_bit_storage (branch_last - 1) < g_bit_storage (g_branch_seqnos[ftype] - 1))
217 g_type_nodes[ftype] = g_renew (TypeNode*, g_type_nodes[ftype], 1 << g_bit_storage (g_branch_seqnos[ftype] - 1));
220 node_size += sizeof (GTypeFundamentalInfo); /* fundamental type info */
221 node_size += SIZEOF_BASE_TYPE_NODE (); /* TypeNode structure */
222 node_size += (sizeof (GType) * (1 + n_supers + 1)); /* self + ancestors + 0 for ->supers[] */
223 node = g_malloc0 (node_size);
224 if (!pnode) /* offset fundamental types */
225 node = G_STRUCT_MEMBER_P (node, sizeof (GTypeFundamentalInfo));
226 g_type_nodes[ftype][branch_last] = node;
228 node->n_supers = n_supers;
231 node->supers[0] = type;
234 node->is_classed = (type_flags & G_TYPE_FLAG_CLASSED) != 0;
235 node->is_instantiatable = (type_flags & G_TYPE_FLAG_INSTANTIATABLE) != 0;
236 node->is_iface = G_TYPE_IS_INTERFACE (type);
240 node->private.iface_conformants = NULL;
242 node->private.iface_entries = NULL;
246 node->supers[0] = type;
247 memcpy (node->supers + 1, pnode->supers, sizeof (GType) * (1 + pnode->n_supers + 1));
249 node->is_classed = pnode->is_classed;
250 node->is_instantiatable = pnode->is_instantiatable;
251 node->is_iface = pnode->is_iface;
256 node->private.iface_conformants = NULL;
260 node->n_ifaces = pnode->n_ifaces;
261 node->private.iface_entries = g_memdup (pnode->private.iface_entries,
262 sizeof (pnode->private.iface_entries[0]) * node->n_ifaces);
265 i = pnode->n_children++;
266 pnode->children = g_renew (GType, pnode->children, pnode->n_children);
267 pnode->children[i] = type;
270 node->plugin = plugin;
271 node->n_children = 0;
272 node->children = NULL;
274 node->qname = g_quark_from_string (name);
275 node->static_gdata = NULL;
277 g_hash_table_insert (g_type_nodes_ht,
278 GUINT_TO_POINTER (node->qname),
279 GUINT_TO_POINTER (type));
284 static inline GTypeFundamentalInfo*
285 type_node_fundamental_info (TypeNode *node)
287 GType ftype = G_TYPE_FUNDAMENTAL (NODE_TYPE (node));
289 if (ftype != NODE_TYPE (node))
290 node = LOOKUP_TYPE_NODE (ftype);
292 return node ? G_STRUCT_MEMBER_P (node, - sizeof (GTypeFundamentalInfo)) : NULL;
296 type_node_fundamental_new (GType ftype,
298 GTypeFundamentalFlags type_flags)
300 GTypeFundamentalInfo *finfo;
302 guint i, flast = last_fundamental_id;
304 g_assert (ftype == G_TYPE_FUNDAMENTAL (ftype));
306 type_flags &= TYPE_FUNDAMENTAL_FLAG_MASK;
308 last_fundamental_id = MAX (last_fundamental_id, ftype + 1);
309 if (last_fundamental_id > flast)
311 g_type_nodes = g_renew (TypeNode**, g_type_nodes, last_fundamental_id);
312 g_branch_seqnos = g_renew (GType, g_branch_seqnos, last_fundamental_id);
313 for (i = flast; i < last_fundamental_id; i++)
315 g_type_nodes[i] = NULL;
316 g_branch_seqnos[i] = 0;
319 g_assert (g_branch_seqnos[ftype] == 0);
321 node = type_node_any_new (NULL, ftype, name, NULL, type_flags);
322 finfo = type_node_fundamental_info (node);
323 finfo->type_flags = type_flags;
329 type_node_new (TypeNode *pnode,
335 g_assert (pnode->n_supers < MAX_N_SUPERS);
336 g_assert (pnode->n_children < MAX_N_CHILDREN);
338 return type_node_any_new (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (pnode)), name, plugin, 0);
341 static inline IFaceEntry*
342 type_lookup_iface_entry (TypeNode *node,
345 if (iface->is_iface && node->n_ifaces)
347 IFaceEntry *ifaces = node->private.iface_entries - 1;
348 guint n_ifaces = node->n_ifaces;
349 GType iface_type = NODE_TYPE (iface);
351 do /* FIXME: should optimize iface lookups for <= 4 */
356 i = (n_ifaces + 1) / 2;
358 if (iface_type == check->iface_type)
360 else if (iface_type > check->iface_type)
365 else /* if (iface_type < check->iface_type) */
375 type_descriptive_name (GType type)
379 gchar *name = g_type_name (type);
381 return name ? name : "<unknown>";
388 /* --- type consistency checks --- */
390 check_plugin (GTypePlugin *plugin,
391 gboolean need_complete_type_info,
392 gboolean need_complete_interface_info,
393 const gchar *type_name)
397 g_warning ("plugin handle for type `%s' is NULL",
403 g_warning ("plugin for type `%s' has no function table",
407 if (!plugin->vtable->plugin_ref)
409 g_warning ("plugin for type `%s' has no plugin_ref() implementation",
413 if (!plugin->vtable->plugin_unref)
415 g_warning ("plugin for type `%s' has no plugin_unref() implementation",
419 if (need_complete_type_info && !plugin->vtable->complete_type_info)
421 g_warning ("plugin for type `%s' has no complete_type_info() implementation",
425 if (need_complete_interface_info && !plugin->vtable->complete_interface_info)
427 g_warning ("plugin for type `%s' has no complete_interface_info() implementation",
435 check_type_name (const gchar *type_name)
437 static const gchar *extra_chars = "-_+";
438 const gchar *p = type_name;
441 if (!type_name[0] || !type_name[1] || !type_name[2])
443 g_warning ("type name `%s' is too short", type_name);
446 /* check the first letter */
447 name_valid = (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z') || p[0] == '_';
448 for (p = type_name + 1; *p; p++)
449 name_valid &= ((p[0] >= 'A' && p[0] <= 'Z') ||
450 (p[0] >= 'a' && p[0] <= 'z') ||
451 (p[0] >= '0' && p[0] <= '9') ||
452 strchr (extra_chars, p[0]));
455 g_warning ("type name `%s' contains invalid characters", type_name);
458 if (g_type_from_name (type_name))
460 g_warning ("cannot register existing type `%s'", type_name);
468 check_derivation (GType parent_type,
469 const gchar *type_name)
471 TypeNode *pnode = LOOKUP_TYPE_NODE (parent_type);
472 GTypeFundamentalInfo* finfo = type_node_fundamental_info (pnode);
476 g_warning ("cannot derive type `%s' from invalid parent type `%s'",
478 type_descriptive_name (parent_type));
481 /* ensure flat derivability */
482 if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE))
484 g_warning ("cannot derive `%s' from non-derivable parent type `%s'",
489 /* ensure deep derivability */
490 if (parent_type != G_TYPE_FUNDAMENTAL (parent_type) &&
491 !(finfo->type_flags & G_TYPE_FLAG_DEEP_DERIVABLE))
493 g_warning ("cannot derive `%s' from non-fundamental parent type `%s'",
503 check_value_table (const gchar *type_name,
504 const GTypeValueTable *value_table)
508 else if (value_table->value_init == NULL)
510 if (value_table->value_free || value_table->value_copy ||
511 value_table->value_peek_pointer ||
512 value_table->collect_type || value_table->collect_value ||
513 value_table->lcopy_type || value_table->lcopy_value)
514 g_warning ("cannot handle uninitializable values of type `%s'",
519 else /* value_table->value_init != NULL */
521 if (!value_table->value_free)
524 * g_warning ("missing `value_free()' for type `%s'", type_name);
528 if (!value_table->value_copy)
530 g_warning ("missing `value_copy()' for type `%s'", type_name);
533 if ((value_table->collect_type || value_table->collect_value) &&
534 (!value_table->collect_type || !value_table->collect_value))
536 g_warning ("one of `collect_type' and `collect_value()' is unspecified for type `%s'",
540 if ((value_table->lcopy_type || value_table->lcopy_value) &&
541 (!value_table->lcopy_type || !value_table->lcopy_value))
543 g_warning ("one of `lcopy_type' and `lcopy_value()' is unspecified for type `%s'",
553 check_type_info (TypeNode *pnode,
555 const gchar *type_name,
556 const GTypeInfo *info)
558 GTypeFundamentalInfo *finfo = type_node_fundamental_info (LOOKUP_TYPE_NODE (ftype));
559 gboolean is_interface = G_TYPE_IS_INTERFACE (ftype);
561 /* check instance members */
562 if (!(finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
563 (info->instance_size || info->n_preallocs || info->instance_init))
566 g_warning ("cannot instantiate `%s', derived from non-instantiatable parent type `%s'",
570 g_warning ("cannot instantiate `%s' as non-instantiatable fundamental",
574 /* check class & interface members */
575 if (!(finfo->type_flags & G_TYPE_FLAG_CLASSED) &&
576 (info->class_init || info->class_finalize || info->class_data ||
577 (!is_interface && (info->class_size || info->base_init || info->base_finalize))))
580 g_warning ("cannot create class for `%s', derived from non-classed parent type `%s'",
584 g_warning ("cannot create class for `%s' as non-classed fundamental",
588 /* check interface size */
589 if (is_interface && info->class_size < sizeof (GTypeInterface))
591 g_warning ("specified interface size for type `%s' is smaller than `GTypeInterface' size",
595 /* check class size */
596 if (finfo->type_flags & G_TYPE_FLAG_CLASSED)
598 if (info->class_size < sizeof (GTypeClass))
600 g_warning ("specified class size for type `%s' is smaller than `GTypeClass' size",
604 if (pnode && info->class_size < pnode->data->class.class_size)
606 g_warning ("specified class size for type `%s' is smaller "
607 "than the parent type's `%s' class size",
613 /* check instance size */
614 if (finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE)
616 if (info->instance_size < sizeof (GTypeInstance))
618 g_warning ("specified instance size for type `%s' is smaller than `GTypeInstance' size",
622 if (pnode && info->instance_size < pnode->data->instance.instance_size)
624 g_warning ("specified instance size for type `%s' is smaller "
625 "than the parent type's `%s' instance size",
636 find_conforming_type (TypeNode *pnode,
639 TypeNode *node = NULL;
642 if (type_lookup_iface_entry (pnode, iface))
645 for (i = 0; i < pnode->n_children && !node; i++)
646 node = find_conforming_type (LOOKUP_TYPE_NODE (pnode->children[i]), iface);
652 check_add_interface (GType instance_type,
655 TypeNode *node = LOOKUP_TYPE_NODE (instance_type);
656 TypeNode *iface = LOOKUP_TYPE_NODE (iface_type);
659 if (!node || !node->is_instantiatable)
661 g_warning ("cannot add interfaces to invalid (non-instantiatable) type `%s'",
662 type_descriptive_name (instance_type));
665 if (!iface || !iface->is_iface)
667 g_warning ("cannot add invalid (non-interface) type `%s' to type `%s'",
668 type_descriptive_name (iface_type),
672 tnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (iface));
673 if (NODE_PARENT_TYPE (tnode) && !type_lookup_iface_entry (node, tnode))
675 g_warning ("cannot add sub-interface `%s' to type `%s' which does not conform to super-interface `%s'",
681 tnode = find_conforming_type (node, iface);
684 g_warning ("cannot add interface type `%s' to type `%s', since type `%s' already conforms to interface",
695 check_interface_info (TypeNode *iface,
697 const GInterfaceInfo *info)
699 if ((info->interface_finalize || info->interface_data) && !info->interface_init)
701 g_warning ("interface type `%s' for type `%s' comes without initializer",
703 type_descriptive_name (instance_type));
711 /* --- type info (type node data) --- */
713 type_add_flags (TypeNode *node,
718 g_return_if_fail ((flags & ~TYPE_FLAG_MASK) == 0);
719 g_return_if_fail (node != NULL);
721 if (!quark_type_flags)
722 quark_type_flags = g_quark_from_static_string ("GTypeFlags");
723 if ((flags & G_TYPE_FLAG_ABSTRACT) && node->is_classed &&
724 node->data && node->data->class.class)
725 g_warning ("tagging type `%s' as abstract after class initialization", NODE_NAME (node));
726 dflags = GPOINTER_TO_UINT (g_type_get_qdata (NODE_TYPE (node), quark_type_flags));
728 g_type_set_qdata (NODE_TYPE (node), quark_type_flags, GUINT_TO_POINTER (dflags));
732 type_data_make (TypeNode *node,
733 const GTypeInfo *info,
734 const GTypeValueTable *value_table)
737 GTypeValueTable *vtable = NULL;
738 guint vtable_size = 0;
740 g_assert (node->data == NULL && info != NULL);
744 TypeNode *pnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
747 vtable = pnode->data->common.value_table;
750 static const GTypeValueTable zero_vtable = { NULL, };
752 value_table = &zero_vtable;
756 vtable_size = sizeof (GTypeValueTable);
758 if (node->is_instantiatable) /* carefull, is_instantiatable is also is_classed */
760 data = g_malloc0 (sizeof (InstanceData) + vtable_size);
762 vtable = G_STRUCT_MEMBER_P (data, sizeof (InstanceData));
763 data->instance.class_size = info->class_size;
764 data->instance.class_init_base = info->base_init;
765 data->instance.class_finalize_base = info->base_finalize;
766 data->instance.class_init = info->class_init;
767 data->instance.class_finalize = info->class_finalize;
768 data->instance.class_data = info->class_data;
769 data->instance.class = NULL;
770 data->instance.instance_size = info->instance_size;
771 data->instance.n_preallocs = MIN (info->n_preallocs, 1024);
772 #ifdef FIXME_DISABLE_PREALLOCATIONS
773 data->instance.n_preallocs = 0;
775 data->instance.instance_init = info->instance_init;
776 data->instance.mem_chunk = NULL;
778 else if (node->is_classed) /* only classed */
780 data = g_malloc0 (sizeof (ClassData) + vtable_size);
782 vtable = G_STRUCT_MEMBER_P (data, sizeof (ClassData));
783 data->class.class_size = info->class_size;
784 data->class.class_init_base = info->base_init;
785 data->class.class_finalize_base = info->base_finalize;
786 data->class.class_init = info->class_init;
787 data->class.class_finalize = info->class_finalize;
788 data->class.class_data = info->class_data;
789 data->class.class = NULL;
791 else if (node->is_iface)
793 data = g_malloc0 (sizeof (IFaceData) + vtable_size);
795 vtable = G_STRUCT_MEMBER_P (data, sizeof (IFaceData));
796 data->iface.vtable_size = info->class_size;
797 data->iface.vtable_init_base = info->base_init;
798 data->iface.vtable_finalize_base = info->base_finalize;
802 data = g_malloc0 (sizeof (CommonData) + vtable_size);
804 vtable = G_STRUCT_MEMBER_P (data, sizeof (CommonData));
808 node->data->common.ref_count = 1;
811 *vtable = *value_table;
812 node->data->common.value_table = vtable;
814 g_assert (node->data->common.value_table != NULL); /* FIXME: paranoid */
818 type_data_ref (TypeNode *node)
822 TypeNode *pnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
824 GTypeValueTable tmp_value_table;
826 g_assert (node->plugin != NULL);
829 type_data_ref (pnode);
831 memset (&tmp_info, 0, sizeof (tmp_info));
832 memset (&tmp_value_table, 0, sizeof (tmp_value_table));
833 g_type_plugin_ref (node->plugin);
834 g_type_plugin_complete_type_info (node->plugin, NODE_TYPE (node), &tmp_info, &tmp_value_table);
835 check_type_info (pnode, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), NODE_NAME (node), &tmp_info);
836 type_data_make (node, &tmp_info,
837 check_value_table (NODE_NAME (node), &tmp_value_table) ? &tmp_value_table : NULL);
841 g_assert (node->data->common.ref_count > 0);
843 node->data->common.ref_count += 1;
848 type_data_unref (TypeNode *node,
851 g_assert (node->data && node->data->common.ref_count);
853 if (node->data->common.ref_count > 1)
854 node->data->common.ref_count -= 1;
859 g_warning ("static type `%s' unreferenced too often",
864 type_data_last_unref (NODE_TYPE (node), uncached);
869 type_node_add_iface_entry (TypeNode *node,
875 g_assert (node->is_instantiatable && node->n_ifaces < MAX_N_IFACES);
878 node->private.iface_entries = g_renew (IFaceEntry, node->private.iface_entries, node->n_ifaces);
879 entries = node->private.iface_entries;
880 for (i = 0; i < node->n_ifaces - 1; i++)
881 if (entries[i].iface_type > iface_type)
883 g_memmove (entries + i + 1, entries + i, sizeof (entries[0]) * (node->n_ifaces - i - 1));
884 entries[i].iface_type = iface_type;
885 entries[i].vtable = NULL;
887 for (i = 0; i < node->n_children; i++)
888 type_node_add_iface_entry (LOOKUP_TYPE_NODE (node->children[i]), iface_type);
892 type_add_interface (TypeNode *node,
894 const GInterfaceInfo *info,
897 IFaceHolder *iholder = g_new0 (IFaceHolder, 1);
899 /* we must not call any functions of GInterfaceInfo from within here, since
900 * we got most probably called from _within_ a type registration function
902 g_assert (node->is_instantiatable && iface->is_iface && ((info && !plugin) || (!info && plugin)));
904 iholder->next = iface->private.iface_conformants;
905 iface->private.iface_conformants = iholder;
906 iholder->instance_type = NODE_TYPE (node);
907 iholder->info = info ? g_memdup (info, sizeof (*info)) : NULL;
908 iholder->plugin = plugin;
910 type_node_add_iface_entry (node, NODE_TYPE (iface));
914 type_iface_retrive_holder_info (TypeNode *iface,
917 IFaceHolder *iholder = iface->private.iface_conformants;
919 g_assert (iface->is_iface);
921 while (iholder->instance_type != instance_type)
922 iholder = iholder->next;
926 GInterfaceInfo tmp_info;
928 g_assert (iholder->plugin != NULL);
930 type_data_ref (iface);
932 memset (&tmp_info, 0, sizeof (tmp_info));
933 g_type_plugin_ref (iholder->plugin);
934 g_type_plugin_complete_interface_info (iholder->plugin, NODE_TYPE (iface), instance_type, &tmp_info);
935 check_interface_info (iface, instance_type, &tmp_info);
936 iholder->info = g_memdup (&tmp_info, sizeof (tmp_info));
943 type_iface_blow_holder_info (TypeNode *iface,
946 IFaceHolder *iholder = iface->private.iface_conformants;
948 g_assert (iface->is_iface);
950 while (iholder->instance_type != instance_type)
951 iholder = iholder->next;
953 if (iholder->info && iholder->plugin)
955 g_free (iholder->info);
956 iholder->info = NULL;
957 g_type_plugin_unref (iholder->plugin);
959 type_data_unref (iface, FALSE);
964 /* --- type structure creation/destruction --- */
966 g_type_create_instance (GType type)
968 TypeNode *node = LOOKUP_TYPE_NODE (type);
969 GTypeInstance *instance;
973 if (!node || !node->is_instantiatable)
975 g_warning ("cannot create new instance of invalid (non-instantiatable) type `%s'",
976 type_descriptive_name (type));
979 if (G_TYPE_IS_ABSTRACT (type))
981 g_warning ("cannot create instance of abstract (non-instantiatable) type `%s'",
982 type_descriptive_name (type));
986 class = g_type_class_ref (type);
988 if (node->data->instance.n_preallocs)
990 if (!node->data->instance.mem_chunk)
991 node->data->instance.mem_chunk = g_mem_chunk_new (NODE_NAME (node),
992 node->data->instance.instance_size,
993 (node->data->instance.instance_size *
994 node->data->instance.n_preallocs),
996 instance = g_chunk_new0 (GTypeInstance, node->data->instance.mem_chunk);
999 instance = g_malloc0 (node->data->instance.instance_size);
1001 for (i = node->n_supers; i > 0; i--)
1003 TypeNode *pnode = LOOKUP_TYPE_NODE (node->supers[i]);
1005 if (pnode->data->instance.instance_init)
1007 instance->g_class = pnode->data->instance.class;
1008 pnode->data->instance.instance_init (instance, class);
1011 instance->g_class = class;
1012 if (node->data->instance.instance_init)
1013 node->data->instance.instance_init (instance, class);
1019 g_type_free_instance (GTypeInstance *instance)
1024 g_return_if_fail (instance != NULL && instance->g_class != NULL);
1026 class = instance->g_class;
1027 node = LOOKUP_TYPE_NODE (class->g_type);
1028 if (!node || !node->is_instantiatable || !node->data || node->data->class.class != (gpointer) class)
1030 g_warning ("cannot free instance of invalid (non-instantiatable) type `%s'",
1031 type_descriptive_name (class->g_type));
1034 if (G_TYPE_IS_ABSTRACT (NODE_TYPE (node)))
1036 g_warning ("cannot free instance of abstract (non-instantiatable) type `%s'",
1041 instance->g_class = NULL;
1042 memset (instance, 0xaa, node->data->instance.instance_size); // FIXME
1043 if (node->data->instance.n_preallocs)
1044 g_chunk_free (instance, node->data->instance.mem_chunk);
1048 g_type_class_unref (class);
1052 type_propagate_iface_vtable (TypeNode *pnode,
1054 GTypeInterface *vtable)
1056 IFaceEntry *entry = type_lookup_iface_entry (pnode, iface);
1059 entry->vtable = vtable;
1060 for (i = 0; i < pnode->n_children; i++)
1062 TypeNode *node = LOOKUP_TYPE_NODE (pnode->children[i]);
1064 type_propagate_iface_vtable (node, iface, vtable);
1069 type_iface_vtable_init (TypeNode *iface,
1072 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1073 IFaceHolder *iholder = type_iface_retrive_holder_info (iface, NODE_TYPE (node));
1074 GTypeInterface *vtable;
1076 g_assert (iface->data && entry && entry->vtable == NULL && iholder && iholder->info);
1078 vtable = g_malloc0 (iface->data->iface.vtable_size);
1079 type_propagate_iface_vtable (node, iface, vtable);
1080 vtable->g_type = NODE_TYPE (iface);
1081 vtable->g_instance_type = NODE_TYPE (node);
1083 if (iface->data->iface.vtable_init_base)
1084 iface->data->iface.vtable_init_base (vtable);
1085 if (iholder->info->interface_init)
1086 iholder->info->interface_init (vtable, iholder->info->interface_data);
1090 type_iface_vtable_finalize (TypeNode *iface,
1092 GTypeInterface *vtable)
1094 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1095 IFaceHolder *iholder = iface->private.iface_conformants;
1097 g_assert (entry && entry->vtable == vtable);
1099 while (iholder->instance_type != NODE_TYPE (node))
1100 iholder = iholder->next;
1101 g_assert (iholder && iholder->info);
1103 type_propagate_iface_vtable (node, iface, NULL);
1104 if (iholder->info->interface_finalize)
1105 iholder->info->interface_finalize (vtable, iholder->info->interface_data);
1106 if (iface->data->iface.vtable_finalize_base)
1107 iface->data->iface.vtable_finalize_base (vtable);
1110 vtable->g_instance_type = 0;
1113 type_iface_blow_holder_info (iface, NODE_TYPE (node));
1117 type_class_init (TypeNode *node,
1120 GSList *slist, *init_slist = NULL;
1126 g_assert (node->is_classed && node->data &&
1127 node->data->class.class_size &&
1128 !node->data->class.class);
1130 class = g_malloc0 (node->data->class.class_size);
1131 node->data->class.class = class;
1135 TypeNode *pnode = LOOKUP_TYPE_NODE (pclass->g_type);
1137 memcpy (class, pclass, pnode->data->class.class_size);
1140 class->g_type = NODE_TYPE (node);
1142 /* stack all base class initialization functions, so we
1143 * call them in ascending order.
1145 for (bnode = node; bnode; bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (bnode)))
1146 if (bnode->data->class.class_init_base)
1147 init_slist = g_slist_prepend (init_slist, (gpointer) bnode->data->class.class_init_base);
1148 for (slist = init_slist; slist; slist = slist->next)
1150 GBaseInitFunc class_init_base = (GBaseInitFunc) slist->data;
1152 class_init_base (class);
1154 g_slist_free (init_slist);
1156 if (node->data->class.class_init)
1157 node->data->class.class_init (class, (gpointer) node->data->class.class_data);
1159 /* ok, we got the class done, now initialize all interfaces */
1160 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1161 if (!node->private.iface_entries[i].vtable)
1162 entry = node->private.iface_entries + i;
1165 type_iface_vtable_init (LOOKUP_TYPE_NODE (entry->iface_type), node);
1167 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1168 if (!node->private.iface_entries[i].vtable)
1169 entry = node->private.iface_entries + i;
1174 type_data_finalize_class_ifaces (TypeNode *node)
1179 g_assert (node->is_instantiatable && node->data && node->data->class.class && node->data->common.ref_count == 0);
1181 g_message ("finalizing interfaces for %sClass `%s'",
1182 type_descriptive_name (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1183 type_descriptive_name (NODE_TYPE (node)));
1185 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1186 if (node->private.iface_entries[i].vtable &&
1187 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1188 entry = node->private.iface_entries + i;
1191 type_iface_vtable_finalize (LOOKUP_TYPE_NODE (entry->iface_type), node, entry->vtable);
1193 for (entry = NULL, i = 0; i < node->n_ifaces; i++)
1194 if (node->private.iface_entries[i].vtable &&
1195 node->private.iface_entries[i].vtable->g_instance_type == NODE_TYPE (node))
1196 entry = node->private.iface_entries + i;
1201 type_data_finalize_class (TypeNode *node,
1204 GTypeClass *class = cdata->class;
1207 g_assert (cdata->class && cdata->common.ref_count == 0);
1209 g_message ("finalizing %sClass `%s'",
1210 type_descriptive_name (G_TYPE_FUNDAMENTAL (NODE_TYPE (node))),
1211 type_descriptive_name (NODE_TYPE (node)));
1213 if (cdata->class_finalize)
1214 cdata->class_finalize (class, (gpointer) cdata->class_data);
1216 /* call all base class destruction functions in descending order
1218 if (cdata->class_finalize_base)
1219 cdata->class_finalize_base (class);
1220 for (bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node)); bnode; bnode = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (bnode)))
1221 if (bnode->data->class.class_finalize_base)
1222 bnode->data->class.class_finalize_base (class);
1225 g_free (cdata->class);
1229 type_data_last_unref (GType type,
1232 TypeNode *node = LOOKUP_TYPE_NODE (type);
1234 g_return_if_fail (node != NULL && node->plugin != NULL);
1236 if (!node->data || node->data->common.ref_count == 0)
1238 g_warning ("cannot drop last reference to unreferenced type `%s'",
1239 type_descriptive_name (type));
1243 if (node->is_classed && node->data && node->data->class.class)
1247 for (i = 0; i < n_class_cache_funcs; i++)
1248 if (class_cache_funcs[i].cache_func (class_cache_funcs[i].cache_data, node->data->class.class))
1252 if (node->data->common.ref_count > 1) /* may have been re-referenced meanwhile */
1253 node->data->common.ref_count -= 1;
1256 GType ptype = NODE_PARENT_TYPE (node);
1259 node->data->common.ref_count = 0;
1261 if (node->is_instantiatable && node->data->instance.mem_chunk)
1263 g_mem_chunk_destroy (node->data->instance.mem_chunk);
1264 node->data->instance.mem_chunk = NULL;
1268 if (node->is_classed && tdata->class.class)
1271 type_data_finalize_class_ifaces (node);
1273 type_data_finalize_class (node, &tdata->class);
1281 type_data_unref (LOOKUP_TYPE_NODE (ptype), FALSE);
1282 g_type_plugin_unref (node->plugin);
1287 g_type_add_class_cache_func (gpointer cache_data,
1288 GTypeClassCacheFunc cache_func)
1292 g_return_if_fail (cache_func != NULL);
1294 i = n_class_cache_funcs++;
1295 class_cache_funcs = g_renew (ClassCacheFunc, class_cache_funcs, n_class_cache_funcs);
1296 class_cache_funcs[i].cache_data = cache_data;
1297 class_cache_funcs[i].cache_func = cache_func;
1301 g_type_remove_class_cache_func (gpointer cache_data,
1302 GTypeClassCacheFunc cache_func)
1306 g_return_if_fail (cache_func != NULL);
1308 for (i = 0; i < n_class_cache_funcs; i++)
1309 if (class_cache_funcs[i].cache_data == cache_data &&
1310 class_cache_funcs[i].cache_func == cache_func)
1312 n_class_cache_funcs--;
1313 g_memmove (class_cache_funcs + i,
1314 class_cache_funcs + i + 1,
1315 sizeof (class_cache_funcs[0]) * (n_class_cache_funcs - i));
1316 class_cache_funcs = g_renew (ClassCacheFunc, class_cache_funcs, n_class_cache_funcs);
1321 g_warning (G_STRLOC ": cannot remove unregistered class cache func %p with data %p",
1322 cache_func, cache_data);
1326 /* --- type registration --- */
1328 g_type_register_fundamental (GType type_id,
1329 const gchar *type_name,
1330 const GTypeInfo *info,
1331 const GTypeFundamentalInfo *finfo,
1334 GTypeFundamentalInfo *node_finfo;
1337 g_return_val_if_fail (type_id > 0, 0);
1338 g_return_val_if_fail (type_name != NULL, 0);
1339 g_return_val_if_fail (info != NULL, 0);
1340 g_return_val_if_fail (finfo != NULL, 0);
1342 if (!check_type_name (type_name))
1344 if (G_TYPE_FUNDAMENTAL (type_id) != type_id)
1346 g_warning ("cannot register fundamental type `%s' with non-fundamental id (%u)",
1351 if (LOOKUP_TYPE_NODE (type_id))
1353 g_warning ("cannot register existing fundamental type `%s' (as `%s')",
1354 type_descriptive_name (type_id),
1358 if ((finfo->type_flags & G_TYPE_FLAG_INSTANTIATABLE) &&
1359 !(finfo->type_flags & G_TYPE_FLAG_CLASSED))
1361 g_warning ("cannot register instantiatable fundamental type `%s' as non-classed",
1366 node = type_node_fundamental_new (type_id, type_name, finfo->type_flags);
1367 node_finfo = type_node_fundamental_info (node);
1368 type_add_flags (node, flags);
1370 if (!check_type_info (NULL, G_TYPE_FUNDAMENTAL (NODE_TYPE (node)), type_name, info))
1371 return NODE_TYPE (node);
1372 type_data_make (node, info,
1373 check_value_table (type_name, info->value_table) ? info->value_table : NULL);
1375 return NODE_TYPE (node);
1379 g_type_register_static (GType parent_type,
1380 const gchar *type_name,
1381 const GTypeInfo *info,
1384 TypeNode *pnode, *node;
1387 g_return_val_if_fail (parent_type > 0, 0);
1388 g_return_val_if_fail (type_name != NULL, 0);
1389 g_return_val_if_fail (info != NULL, 0);
1391 if (!check_type_name (type_name))
1393 if (!check_derivation (parent_type, type_name))
1396 pnode = LOOKUP_TYPE_NODE (parent_type);
1397 type_data_ref (pnode);
1399 if (!check_type_info (pnode, G_TYPE_FUNDAMENTAL (parent_type), type_name, info))
1401 if (info->class_finalize)
1403 g_warning ("class finalizer specified for static type `%s'",
1408 node = type_node_new (pnode, type_name, NULL);
1409 type_add_flags (node, flags);
1410 type = NODE_TYPE (node);
1411 type_data_make (node, info,
1412 check_value_table (type_name, info->value_table) ? info->value_table : NULL);
1418 g_type_register_dynamic (GType parent_type,
1419 const gchar *type_name,
1420 GTypePlugin *plugin,
1423 TypeNode *pnode, *node;
1426 g_return_val_if_fail (parent_type > 0, 0);
1427 g_return_val_if_fail (type_name != NULL, 0);
1428 g_return_val_if_fail (plugin != NULL, 0);
1430 if (!check_type_name (type_name))
1432 if (!check_derivation (parent_type, type_name))
1434 if (!check_plugin (plugin, TRUE, FALSE, type_name))
1436 pnode = LOOKUP_TYPE_NODE (parent_type);
1438 node = type_node_new (pnode, type_name, plugin);
1439 type_add_flags (node, flags);
1440 type = NODE_TYPE (node);
1446 g_type_add_interface_static (GType instance_type,
1447 GType interface_type,
1448 const GInterfaceInfo *info)
1453 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1454 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1456 if (!check_add_interface (instance_type, interface_type))
1458 node = LOOKUP_TYPE_NODE (instance_type);
1459 iface = LOOKUP_TYPE_NODE (interface_type);
1460 if (!check_interface_info (iface, NODE_TYPE (node), info))
1462 type_add_interface (node, iface, info, NULL);
1466 g_type_add_interface_dynamic (GType instance_type,
1467 GType interface_type,
1468 GTypePlugin *plugin)
1473 g_return_if_fail (G_TYPE_IS_INSTANTIATABLE (instance_type));
1474 g_return_if_fail (g_type_parent (interface_type) == G_TYPE_INTERFACE);
1476 if (!check_add_interface (instance_type, interface_type))
1478 node = LOOKUP_TYPE_NODE (instance_type);
1479 iface = LOOKUP_TYPE_NODE (interface_type);
1480 if (!check_plugin (plugin, FALSE, TRUE, NODE_NAME (node)))
1482 type_add_interface (node, iface, NULL, plugin);
1486 /* --- public API functions --- */
1488 g_type_class_ref (GType type)
1490 TypeNode *node = LOOKUP_TYPE_NODE (type);
1492 /* optimize for common code path
1494 if (node && node->is_classed && node->data &&
1495 node->data->class.class && node->data->common.ref_count > 0)
1497 type_data_ref (node);
1499 return node->data->class.class;
1502 if (!node || !node->is_classed ||
1503 (node->data && node->data->common.ref_count < 1))
1505 g_warning ("cannot retrive class for invalid (unclassed) type `%s'",
1506 type_descriptive_name (type));
1510 type_data_ref (node);
1512 if (!node->data->class.class)
1514 GType ptype = NODE_PARENT_TYPE (node);
1515 GTypeClass *pclass = ptype ? g_type_class_ref (ptype) : NULL;
1517 type_class_init (node, pclass);
1520 return node->data->class.class;
1524 g_type_class_unref (gpointer g_class)
1527 GTypeClass *class = g_class;
1529 g_return_if_fail (g_class != NULL);
1531 node = LOOKUP_TYPE_NODE (class->g_type);
1532 if (node && node->is_classed && node->data &&
1533 node->data->class.class == class && node->data->common.ref_count > 0)
1534 type_data_unref (node, FALSE);
1536 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1537 type_descriptive_name (class->g_type));
1541 g_type_class_unref_uncached (gpointer g_class)
1544 GTypeClass *class = g_class;
1546 g_return_if_fail (g_class != NULL);
1548 node = LOOKUP_TYPE_NODE (class->g_type);
1549 if (node && node->is_classed && node->data &&
1550 node->data->class.class == class && node->data->common.ref_count > 0)
1551 type_data_unref (node, TRUE);
1553 g_warning ("cannot unreference class of invalid (unclassed) type `%s'",
1554 type_descriptive_name (class->g_type));
1558 g_type_class_peek (GType type)
1560 TypeNode *node = LOOKUP_TYPE_NODE (type);
1562 if (node && node->is_classed && node->data && node->data->class.class) /* common.ref_count _may_ be 0 */
1563 return node->data->class.class;
1569 g_type_class_peek_parent (gpointer g_class)
1573 g_return_val_if_fail (g_class != NULL, NULL);
1575 node = LOOKUP_TYPE_NODE (G_TYPE_FROM_CLASS (g_class));
1576 if (node && node->is_classed && node->data && NODE_PARENT_TYPE (node))
1578 node = LOOKUP_TYPE_NODE (NODE_PARENT_TYPE (node));
1580 return node->data->class.class;
1587 g_type_interface_peek (gpointer instance_class,
1592 GTypeClass *class = instance_class;
1594 g_return_val_if_fail (instance_class != NULL, NULL);
1596 node = LOOKUP_TYPE_NODE (class->g_type);
1597 iface = LOOKUP_TYPE_NODE (iface_type);
1598 if (node && node->is_instantiatable && iface)
1600 IFaceEntry *entry = type_lookup_iface_entry (node, iface);
1602 if (entry && entry->vtable)
1603 return entry->vtable;
1610 g_type_value_table_peek (GType type)
1612 TypeNode *node = LOOKUP_TYPE_NODE (type);
1614 if (node && node->data && node->data->common.ref_count > 0)
1615 return node->data->common.value_table->value_init ? node->data->common.value_table : NULL;
1621 g_type_name (GType type)
1623 TypeNode *node = LOOKUP_TYPE_NODE (type);
1625 return node ? NODE_NAME (node) : NULL;
1629 g_type_qname (GType type)
1631 TypeNode *node = LOOKUP_TYPE_NODE (type);
1633 return node ? node->qname : 0;
1637 g_type_from_name (const gchar *name)
1641 g_return_val_if_fail (name != NULL, 0);
1643 quark = g_quark_try_string (name);
1646 GType type = GPOINTER_TO_UINT (g_hash_table_lookup (g_type_nodes_ht, GUINT_TO_POINTER (quark)));
1656 g_type_parent (GType type)
1658 TypeNode *node = LOOKUP_TYPE_NODE (type);
1660 return node ? NODE_PARENT_TYPE (node) : 0;
1664 g_type_next_base (GType type,
1667 TypeNode *node = LOOKUP_TYPE_NODE (type);
1671 TypeNode *base_node = LOOKUP_TYPE_NODE (base_type);
1673 if (base_node && base_node->n_supers < node->n_supers)
1675 guint n = node->n_supers - base_node->n_supers;
1677 if (node->supers[n] == base_type)
1678 return node->supers[n - 1];
1686 g_type_is_a (GType type,
1689 if (type != is_a_type)
1691 TypeNode *node = LOOKUP_TYPE_NODE (type);
1695 TypeNode *a_node = LOOKUP_TYPE_NODE (is_a_type);
1697 if (a_node && a_node->n_supers <= node->n_supers)
1698 return node->supers[node->n_supers - a_node->n_supers] == is_a_type;
1702 return LOOKUP_TYPE_NODE (type) != NULL;
1708 g_type_conforms_to (GType type,
1711 if (type != iface_type)
1713 TypeNode *node = LOOKUP_TYPE_NODE (type);
1717 TypeNode *iface_node = LOOKUP_TYPE_NODE (iface_type);
1721 if (iface_node->is_iface && node->is_instantiatable)
1722 return type_lookup_iface_entry (node, iface_node) != NULL;
1723 else if (iface_node->n_supers <= node->n_supers)
1724 return node->supers[node->n_supers - iface_node->n_supers] == iface_type;
1729 return LOOKUP_TYPE_NODE (type) != NULL;
1735 g_type_fundamental_branch_last (GType type)
1737 GType ftype = G_TYPE_FUNDAMENTAL (type);
1739 return ftype < last_fundamental_id ? g_branch_seqnos[ftype] : 0;
1742 GType* /* free result */
1743 g_type_children (GType type,
1746 TypeNode *node = LOOKUP_TYPE_NODE (type);
1750 GType *children = g_new (GType, node->n_children + 1);
1752 memcpy (children, node->children, sizeof (GType) * node->n_children);
1753 children[node->n_children] = 0;
1756 *n_children = node->n_children;
1769 GType* /* free result */
1770 g_type_interfaces (GType type,
1771 guint *n_interfaces)
1773 TypeNode *node = LOOKUP_TYPE_NODE (type);
1775 if (node && node->is_instantiatable)
1777 GType *ifaces = g_new (GType, node->n_ifaces + 1);
1780 for (i = 0; i < node->n_ifaces; i++)
1781 ifaces[i] = node->private.iface_entries[i].iface_type;
1785 *n_interfaces = node->n_ifaces;
1798 typedef struct _QData QData;
1811 g_type_get_qdata (GType type,
1814 TypeNode *node = LOOKUP_TYPE_NODE (type);
1817 g_return_val_if_fail (node != NULL, NULL);
1819 gdata = node->static_gdata;
1820 if (quark && gdata && gdata->n_qdatas)
1822 QData *qdatas = gdata->qdatas - 1;
1823 guint n_qdatas = gdata->n_qdatas;
1825 do /* FIXME: should optimize qdata lookups for <= 4 */
1830 i = (n_qdatas + 1) / 2;
1832 if (quark == check->quark)
1834 else if (quark > check->quark)
1839 else /* if (quark < check->quark) */
1849 g_type_set_qdata (GType type,
1853 TypeNode *node = LOOKUP_TYPE_NODE (type);
1858 g_return_if_fail (node != NULL);
1859 g_return_if_fail (quark != 0);
1861 /* setup qdata list if necessary */
1862 if (!node->static_gdata)
1863 node->static_gdata = g_new0 (GData, 1);
1864 gdata = node->static_gdata;
1866 /* try resetting old data */
1867 qdata = gdata->qdatas;
1868 for (i = 0; i < gdata->n_qdatas; i++)
1869 if (qdata[i].quark == quark)
1871 qdata[i].data = data;
1877 gdata->qdatas = g_renew (QData, gdata->qdatas, gdata->n_qdatas);
1878 qdata = gdata->qdatas;
1879 for (i = 0; i < gdata->n_qdatas - 1; i++)
1880 if (qdata[i].quark > quark)
1882 g_memmove (qdata + i + 1, qdata + i, sizeof (qdata[0]) * (gdata->n_qdatas - i - 1));
1883 qdata[i].quark = quark;
1884 qdata[i].data = data;
1888 /* --- implementation details --- */
1890 g_type_check_flags (GType type,
1893 TypeNode *node = LOOKUP_TYPE_NODE (type);
1894 gboolean result = FALSE;
1898 guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK;
1899 guint tflags = flags & TYPE_FLAG_MASK;
1903 GTypeFundamentalInfo *finfo = type_node_fundamental_info (node);
1905 fflags = (finfo->type_flags & fflags) == fflags;
1911 tflags = (tflags & GPOINTER_TO_UINT (g_type_get_qdata (type, quark_type_flags))) == tflags;
1915 result = tflags && fflags;
1921 g_type_get_plugin (GType type)
1923 TypeNode *node = LOOKUP_TYPE_NODE (type);
1925 return node ? node->plugin : NULL;
1929 g_type_fundamental_last (void)
1931 return last_fundamental_id;
1935 g_type_instance_conforms_to (GTypeInstance *type_instance,
1938 return (type_instance && type_instance->g_class &&
1939 G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type) &&
1940 g_type_conforms_to (type_instance->g_class->g_type, iface_type));
1944 g_type_class_is_a (GTypeClass *type_class,
1947 return (type_class && G_TYPE_IS_CLASSED (type_class->g_type) &&
1948 g_type_is_a (type_class->g_type, is_a_type));
1952 g_type_value_conforms_to (GValue *value,
1959 node = LOOKUP_TYPE_NODE (value->g_type);
1961 if (!G_TYPE_IS_FUNDAMENTAL (value->g_type) && !node || !node->data)
1962 node = LOOKUP_TYPE_NODE (G_TYPE_FUNDAMENTAL (value->g_type));
1964 if (!node || !node->data || node->data->common.ref_count < 1 ||
1965 !node->data->common.value_table->value_init ||
1966 !g_type_conforms_to (value->g_type, type))
1973 g_type_check_value (GValue *value)
1975 return value && g_type_value_conforms_to (value, value->g_type);
1979 g_type_check_instance_cast (GTypeInstance *type_instance,
1984 g_warning ("invalid cast from (NULL) pointer to `%s'",
1985 type_descriptive_name (iface_type));
1986 return type_instance;
1988 if (!type_instance->g_class)
1990 g_warning ("invalid unclassed pointer in cast to `%s'",
1991 type_descriptive_name (iface_type));
1992 return type_instance;
1994 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
1996 g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
1997 type_descriptive_name (type_instance->g_class->g_type),
1998 type_descriptive_name (iface_type));
1999 return type_instance;
2001 if (!g_type_conforms_to (type_instance->g_class->g_type, iface_type))
2003 g_warning ("invalid cast from `%s' to `%s'",
2004 type_descriptive_name (type_instance->g_class->g_type),
2005 type_descriptive_name (iface_type));
2006 return type_instance;
2009 return type_instance;
2013 g_type_check_class_cast (GTypeClass *type_class,
2018 g_warning ("invalid class cast from (NULL) pointer to `%s'",
2019 type_descriptive_name (is_a_type));
2022 if (!G_TYPE_IS_CLASSED (type_class->g_type))
2024 g_warning ("invalid unclassed type `%s' in class cast to `%s'",
2025 type_descriptive_name (type_class->g_type),
2026 type_descriptive_name (is_a_type));
2029 if (!g_type_is_a (type_class->g_type, is_a_type))
2031 g_warning ("invalid class cast from `%s' to `%s'",
2032 type_descriptive_name (type_class->g_type),
2033 type_descriptive_name (is_a_type));
2041 g_type_check_instance (GTypeInstance *type_instance)
2043 /* this function is just here to make the signal system
2044 * conveniently elaborated on instance checks
2048 g_warning ("instance is invalid (NULL) pointer");
2051 if (!type_instance->g_class)
2053 g_warning ("instance with invalid (NULL) class pointer");
2056 if (!G_TYPE_IS_CLASSED (type_instance->g_class->g_type))
2058 g_warning ("instance of invalid unclassed type `%s'",
2059 type_descriptive_name (type_instance->g_class->g_type));
2062 if (!G_TYPE_IS_INSTANTIATABLE (type_instance->g_class->g_type))
2064 g_warning ("instance of invalid non-instantiatable type `%s'",
2065 type_descriptive_name (type_instance->g_class->g_type));
2073 /* --- foreign prototypes --- */
2074 extern void g_value_types_init (void); /* sync with gvaluetypes.c */
2075 extern void g_enum_types_init (void); /* sync with genums.c */
2076 extern void g_param_type_init (void); /* sync with gparam.c */
2077 extern void g_boxed_type_init (void); /* sync with gboxed.c */
2078 extern void g_object_type_init (void); /* sync with gobject.c */
2079 extern void g_param_spec_types_init (void); /* sync with gparamspecs.c */
2080 extern void g_signal_init (void); /* sync with gsignal.c */
2083 /* --- initialization --- */
2087 static TypeNode *type0_node = NULL;
2092 if (last_fundamental_id)
2095 /* type qname hash table */
2096 g_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
2098 /* invalid type G_TYPE_INVALID (0)
2100 last_fundamental_id = 1;
2101 g_type_nodes = g_renew (TypeNode**, g_type_nodes, last_fundamental_id);
2102 g_type_nodes[0] = &type0_node;
2103 g_branch_seqnos = g_renew (GType, g_branch_seqnos, last_fundamental_id);
2104 g_branch_seqnos[0] = 1;
2106 /* void type G_TYPE_NONE
2108 node = type_node_fundamental_new (G_TYPE_NONE, "void", 0);
2109 type = NODE_TYPE (node);
2110 g_assert (type == G_TYPE_NONE);
2112 /* interface fundamental type G_TYPE_INTERFACE (!classed)
2114 memset (&info, 0, sizeof (info));
2115 node = type_node_fundamental_new (G_TYPE_INTERFACE, "GInterface", G_TYPE_FLAG_DERIVABLE);
2116 type = NODE_TYPE (node);
2117 type_data_make (node, &info, NULL); /* FIXME */
2118 g_assert (type == G_TYPE_INTERFACE);
2120 /* G_TYPE_* value types
2122 g_value_types_init ();
2124 /* G_TYPE_ENUM & G_TYPE_FLAGS
2126 g_enum_types_init ();
2130 g_param_type_init ();
2134 g_boxed_type_init ();
2138 g_object_type_init ();
2140 /* G_TYPE_PARAM_* pspec types
2142 g_param_spec_types_init ();