Search code examples
typesenumsglib

How do I lookup the full name of a registered enum type in glib?


I have a number of enumerations that were created via the standard glib registration function:

GType foo_type = g_enum_register_static("Foo", foo_enum_values);

But when I try to recover the name ("Foo") that I registered the enumeration with, I get its basic class instead:

gchar const * type_name = g_type_get_name(foo_type);
printf("%s\n",type_name);

prints "GEnum" and not "Foo". How can I get back the string "Foo" given just the registered type id?


Solution

  • I can’t check your code fully because you haven’t provided a minimal working reproducer, but the following code works fine for me:

    /* gcc -o test test.c $(pkg-config --cflags --libs glib-2.0 gobject-2.0) */
    #include <glib.h>
    #include <glib-object.h>
    
    static const GEnumValue my_enum_values[] =
    {
      { 1, "the first value", "one" },
      { 2, "the second value", "two" },
      { 3, "the third value", "three" },
      { 0, NULL, NULL }
    };
    
    int
    main (void)
    {
      GType type;
    
      type = g_enum_register_static ("MyEnum", my_enum_values);
    
      g_assert_cmpstr (g_type_name (type), ==, "MyEnum");
    
      return 0;
    }