]> git.draconx.ca Git - gob-dx.git/blobdiff - src/main.c
Use G_DEFINE_xxx type macros in output files.
[gob-dx.git] / src / main.c
index 61125d8727b83100307f729ff384f630c9e8cdcf..783e46e870e349a89e9c587ad4692433dcd43471 100644 (file)
@@ -1252,7 +1252,19 @@ add_get_type(void)
 
        define_add_interfaces(c);
 
-       out_printf(out, "GType %s_get_type(void)\n"
+       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 (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);
@@ -1274,7 +1286,8 @@ add_get_type(void)
 
        out_printf(out, "\t}\n\n"
                        "\treturn type;\n"
-                       "}\n\n");
+                       "}\n\n"
+                       "#endif\n\n");
 }
 
 static void
@@ -1284,7 +1297,37 @@ add_dynamic_get_type(void)
 
        define_dynamic_add_interfaces(c);
 
-       out_printf(out, "static GType %s_type_id;\n\n"
+       /*
+        * G_DEFINE_DYNAMIC_TYPE_EXTENDED is usable if available, except for
+        * some reason it defines an xxx_register_type function with internal
+        * linkage.  This is kind of weird so we have to work around that.
+        */
+       out_printf(out, "#ifdef G_DEFINE_DYNAMIC_TYPE_EXTENDED\n\n"
+                       "static void %s_class_finalize(%sClass *c) { }\n\n"
+                       "#define %s_register_type ___register_type\n",
+                       funcbase, typebase, funcbase);
+
+       out_printf(out, "G_DEFINE_DYNAMIC_TYPE_EXTENDED(%s, %s, %s,\n"
+                       "\t(GTypeFlags)%s,\n",
+                       typebase, funcbase, pmacrotype,
+                       c->abstract ? "G_TYPE_FLAG_ABSTRACT" : "0");
+
+       if (c->interfaces) {
+               out_printf(out, "\t___add_interfaces"
+                                   "(type_module, %s_type_id);\n", funcbase);
+       }
+
+       out_printf(out, ");\n"
+                       "#undef %s_register_type\n\n"
+                       "void %s_register_type(GTypeModule *type_module)\n"
+                       "{\n"
+                       "\t___register_type(type_module);\n"
+                       "}\n\n",
+                       funcbase, funcbase);
+
+       /* Fallback for GLib < 2.14 */
+       out_printf(out, "#else\n\n"
+                       "static GType %s_type_id;\n\n"
                        "GType %s_get_type(void)\n"
                        "{\n"
                        "\treturn %s_type_id;\n"
@@ -1313,7 +1356,8 @@ add_dynamic_get_type(void)
                                funcbase);
        }
 
-       out_printf(out, "}\n\n");
+       out_printf(out, "}\n\n"
+                       "#endif\n\n");
 }
 
 static void