如何在 glib 中查找已注册枚举类型的全名?
How do I lookup the full name of a registered enum type in glib?
我有一些枚举是通过标准 glib 注册函数创建的:
GType foo_type = g_enum_register_static("Foo", foo_enum_values);
但是当我尝试恢复我注册枚举的名称(“Foo”)时,我得到了它的基本 class:
gchar const * type_name = g_type_get_name(foo_type);
printf("%s\n",type_name);
打印“GEnum”而不是“Foo”。如何取回仅给定注册类型 ID 的字符串“Foo”?
我无法完全检查你的代码,因为你没有提供最小的工作复制器,但以下代码对我来说工作正常:
/* 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;
}
我有一些枚举是通过标准 glib 注册函数创建的:
GType foo_type = g_enum_register_static("Foo", foo_enum_values);
但是当我尝试恢复我注册枚举的名称(“Foo”)时,我得到了它的基本 class:
gchar const * type_name = g_type_get_name(foo_type);
printf("%s\n",type_name);
打印“GEnum”而不是“Foo”。如何取回仅给定注册类型 ID 的字符串“Foo”?
我无法完全检查你的代码,因为你没有提供最小的工作复制器,但以下代码对我来说工作正常:
/* 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;
}