+ out_printf(out, "static void ___add_interfaces"
+ "(GTypeModule *type_module, GType type)\n"
+ "{\n");
+ add_interface_infos(c, FALSE);
+
+ /*
+ * Hack to work around bug in g_type_module_add_interface,
+ * which will fail to add an interface to types that derive
+ * from something that also implements the same interface.
+ *
+ * The actual GType system does not have any such problem,
+ * and the GTypeModule implementation details relied upon
+ * here have not changed once since the feature was first
+ * implemented almost 20 years ago.
+ */
+ out_printf(out, "\tstruct _ModuleInterfaceInfo {\n"
+ "\t\tgboolean loaded;\n"
+ "\t\tGType instance_type;\n"
+ "\t\tGType interface_type;\n"
+ "\t\tGInterfaceInfo info;\n"
+ "\t} *modinfo;\n");
+
+ for (li = c->interfaces; li; li = li->next) {
+ char *type = make_pre_macro(li->data, "TYPE");
+ char *name = replace_sep(li->data, '_');
+
+ out_printf(out, "\n"
+ "\tmodinfo = g_malloc(sizeof *modinfo);\n"
+ "\tmodinfo->loaded = TRUE;\n"
+ "\tmodinfo->instance_type = type;\n"
+ "\tmodinfo->interface_type = %s;\n"
+ "\tmodinfo->info = %s_info;\n"
+ "\tg_type_add_interface_dynamic\n"
+ "\t\t( modinfo->instance_type\n"
+ "\t\t, modinfo->interface_type\n"
+ "\t\t, G_TYPE_PLUGIN(type_module)\n"
+ "\t\t);\n"
+ "\ttype_module->interface_infos = g_slist_prepend\n"
+ "\t\t( type_module->interface_infos\n"
+ "\t\t, modinfo\n"
+ "\t\t);\n",
+ type, name);
+
+ g_free(name);
+ g_free(type);
+ }
+
+ out_printf(out, "}\n\n");
+}
+
+static void define_add_privates(Class *c)
+{
+ const char *addprivate = c->dynamic
+ ? "G_ADD_PRIVATE_DYNAMIC"
+ : "G_ADD_PRIVATE";
+
+ if (!privates)
+ return;
+
+ out_printf(out, "#ifdef %s\n"
+ "#define ___add_privates() %s(%s)\n"
+ "#else\n"
+ "#define ___add_privates()\n"
+ "#endif\n\n",
+ addprivate, addprivate, typebase);
+}
+
+static void add_type_info(void)
+{
+ out_printf(out, "\tstatic const GTypeInfo info = {\n"
+ "\t\tsizeof (%sClass),\n"
+ "\t\t(GBaseInitFunc) NULL,\n"
+ "\t\t(GBaseFinalizeFunc) NULL,\n"
+ "\t\t(GClassInitFunc) %s_class_init,\n"
+ "\t\t(GClassFinalizeFunc) NULL,\n"
+ "\t\tNULL /* class_data */,\n"
+ "\t\tsizeof (%s),\n"
+ "\t\t%d /* n_preallocs */,\n"
+ "\t\t(GInstanceInitFunc) %s_init,\n"
+ "\t\tNULL\n"
+ "\t};\n\n",
+ typebase, funcbase, typebase, prealloc, funcbase);
+}
+
+static void
+add_get_type(void)
+{
+ Class *c = (Class *)class;
+
+ define_add_interfaces(c);
+ define_add_privates(c);
+
+ out_printf(out, "#ifdef G_DEFINE_TYPE_EXTENDED\n\n"
+ "G_DEFINE_TYPE_EXTENDED(%s, %s, %s,\n"
+ "\t(GTypeFlags)%s,\n",
+ typebase, funcbase, pmacrotype,
+ c->abstract ? "G_TYPE_FLAG_ABSTRACT" : "0");
+
+ if (privates)
+ out_printf(out, "\t___add_privates();\n");
+
+ if (c->interfaces)
+ out_printf(out, "\t___add_interfaces(g_define_type_id);\n");
+
+ /* Fallback for GLib < 2.4 */
+ out_printf(out, ");\n\n"
+ "#else\n\n"
+ "GType %s_get_type(void)\n"
+ "{\n"
+ "\tstatic GType type = 0;\n",
+ funcbase);
+
+ add_type_info();
+
+ out_printf(out, "\tif ___GOB_UNLIKELY(type == 0) {\n"
+ "\t\ttype = g_type_register_static\n"
+ "\t\t\t( %s\n"
+ "\t\t\t, \"%s\"\n"
+ "\t\t\t, &info\n"
+ "\t\t\t, (GTypeFlags)%s\n"
+ "\t\t\t);\n",
+ pmacrotype, typebase,
+ c->abstract ? "G_TYPE_FLAG_ABSTRACT" : "0");
+
+ if (c->interfaces)
+ out_printf(out, "\t\t___add_interfaces(type);\n");
+
+ out_printf(out, "\t}\n\n"
+ "\treturn type;\n"
+ "}\n\n"
+ "#endif\n\n");
+}