mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
Add G_DEFINE macros for enum and flags types
While you might want to use automated tools like glib-mkenums to generate enumeration types for your library, it's often not entirely necessary to complicate your build system in order to handle a couple of enumerations with few values. Just like we have G_DEFINE macros for object, interface, pointer, and boxed types, we should provide macros for defining enum and flags types.
This commit is contained in:
parent
89b34963e7
commit
9a4a10fbc8
@ -274,6 +274,94 @@ void g_flags_complete_type_info (GType g_flags_type,
|
||||
GTypeInfo *info,
|
||||
const GFlagsValue *const_values);
|
||||
|
||||
/* {{{ Macros */
|
||||
|
||||
/**
|
||||
* G_DEFINE_ENUM_VALUE:
|
||||
* @EnumValue: an enumeration value
|
||||
* @EnumNick: a short string representing the enumeration value
|
||||
*
|
||||
* Defines an enumeration value, and maps it to a "nickname".
|
||||
*
|
||||
* This macro can only be used with G_DEFINE_ENUM_TYPE() and
|
||||
* G_DEFINE_FLAGS_TYPE().
|
||||
*
|
||||
* Since: 2.74
|
||||
*/
|
||||
#define G_DEFINE_ENUM_VALUE(EnumValue, EnumNick) { EnumValue, #EnumValue, EnumNick },
|
||||
|
||||
/**
|
||||
* G_DEFINE_ENUM_TYPE:
|
||||
* @TypeName: the enumeration type, in `CamelCase`
|
||||
* @type_name: the enumeration type prefixed, in `snake_case`
|
||||
* @values: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
|
||||
*
|
||||
* A convenience macro for defining enumeration types.
|
||||
*
|
||||
* This macro will generate a `*_get_type()` function for the
|
||||
* given @TypeName, using @type_name as the function prefix.
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* G_DEFINE_ENUM_TYPE (GtkOrientation, gtk_orientation,
|
||||
* G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_HORIZONTAL, "horizontal")
|
||||
* G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_VERTICAL, "vertical"))
|
||||
* ]|
|
||||
*
|
||||
* Since: 2.74
|
||||
*/
|
||||
#define G_DEFINE_ENUM_TYPE(TypeName, type_name, values) \
|
||||
GType \
|
||||
type_name ## _get_type (void) { \
|
||||
static gsize g_define_type__static = 0; \
|
||||
if (g_once_init_enter (&g_define_type__static)) { \
|
||||
static const GEnumValue enum_values[] = { \
|
||||
values \
|
||||
{ 0, NULL, NULL }, \
|
||||
}; \
|
||||
GType g_define_type = g_enum_register_static (g_intern_static_string (#TypeName), enum_values); \
|
||||
g_once_init_leave (&g_define_type__static, g_define_type); \
|
||||
} \
|
||||
return g_define_type__static; \
|
||||
}
|
||||
|
||||
/**
|
||||
* G_DEFINE_FLAGS_TYPE:
|
||||
* @TypeName: the enumeration type, in `CamelCase`
|
||||
* @type_name: the enumeration type prefixed, in `snake_case`
|
||||
* @values: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
|
||||
*
|
||||
* A convenience macro for defining flag types.
|
||||
*
|
||||
* This macro will generate a `*_get_type()` function for the
|
||||
* given @TypeName, using @type_name as the function prefix.
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* G_DEFINE_FLAGS_TYPE (GSettingsBindFlags, g_settings_bind_flags,
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_DEFAULT, "default")
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET, "get")
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_SET, "set")
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_NO_SENSITIVITY, "no-sensitivity")
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET_NO_CHANGES, "get-no-changes")
|
||||
* G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_INVERT_BOOLEAN, "invert-boolean"))
|
||||
* ]|
|
||||
*
|
||||
* Since: 2.74
|
||||
*/
|
||||
#define G_DEFINE_FLAGS_TYPE(TypeName, type_name, values) \
|
||||
GType \
|
||||
type_name ## _get_type (void) { \
|
||||
static gsize g_define_type__static = 0; \
|
||||
if (g_once_init_enter (&g_define_type__static)) { \
|
||||
static const GFlagsValue flags_values[] = { \
|
||||
values \
|
||||
{ 0, NULL, NULL }, \
|
||||
}; \
|
||||
GType g_define_type = g_flags_register_static (g_intern_static_string (#TypeName), flags_values); \
|
||||
g_once_init_leave (&g_define_type__static, g_define_type); \
|
||||
} \
|
||||
return g_define_type__static; \
|
||||
}
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __G_ENUMS_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user