]> git.draconx.ca Git - gob-dx.git/commitdiff
Use G_DEFINE_xxx type macros in output files.
authorNick Bowler <nbowler@draconx.ca>
Sun, 9 Feb 2020 01:04:26 +0000 (20:04 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sun, 9 Feb 2020 01:20:26 +0000 (20:20 -0500)
The G_DEFINE_TYPE_EXTENDED and G_DEFINE_DYNAMIC_TYPE_EXTENDED can be
used to produce the default get_type implementations.  They produce
almost identical code to what we already generate in GOB, except that
we can now automatically pick up any improvements made in GLib.  For
example, with modern GLib the G_DEFINE_TYPE_EXTENDED will produce a
thread-safe get_type implementation which is a nice bonus.

These macros do not support the "prealloc" attribute so setting this
in gob will no longer do anything.  According to the documentation, this
has been a no-op in GLib itself since 2.10, and it appears to just be
for performance tuning anyway, so I doubt anyone cares.

The generated output will fall back to the original method when these
macros are not available.

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