build: autogenerate enum/flags string getter/builders
Each enum will have its own #_get_string (), and each flags will have its own #_build_string_from_mask ().
This commit is contained in:
@@ -7,31 +7,97 @@
|
||||
/*** END file-production ***/
|
||||
|
||||
/*** BEGIN value-header ***/
|
||||
static const G@Type@Value @enum_name@_values[] = {
|
||||
/*** END value-header ***/
|
||||
/*** BEGIN value-production ***/
|
||||
{ @VALUENAME@, "@VALUENAME@", "@valuenick@" },
|
||||
/*** END value-production ***/
|
||||
/*** BEGIN value-tail ***/
|
||||
{ 0, NULL, NULL }
|
||||
};
|
||||
|
||||
/* Define type-specific symbols */
|
||||
#undef IS_ENUM
|
||||
#undef IS_FLAGS
|
||||
#define IS_@TYPE@
|
||||
|
||||
GType
|
||||
@enum_name@_get_type (void)
|
||||
{
|
||||
static volatile gsize g_define_type_id__volatile = 0;
|
||||
|
||||
if (g_once_init_enter (&g_define_type_id__volatile))
|
||||
{
|
||||
static const G@Type@Value values[] = {
|
||||
/*** END value-header ***/
|
||||
|
||||
/*** BEGIN value-production ***/
|
||||
{ @VALUENAME@, "@VALUENAME@", "@valuenick@" },
|
||||
/*** END value-production ***/
|
||||
|
||||
/*** BEGIN value-tail ***/
|
||||
{ 0, NULL, NULL }
|
||||
};
|
||||
if (g_once_init_enter (&g_define_type_id__volatile)) {
|
||||
GType g_define_type_id =
|
||||
g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
|
||||
g_@type@_register_static (g_intern_static_string ("@EnumName@"),
|
||||
@enum_name@_values);
|
||||
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
|
||||
}
|
||||
|
||||
return g_define_type_id__volatile;
|
||||
}
|
||||
|
||||
/* Enum-specific method to get the value as a string.
|
||||
* We get the nick of the GEnumValue. Note that this will be
|
||||
* valid even if the GEnumClass is not referenced anywhere. */
|
||||
#if defined IS_ENUM
|
||||
const gchar *
|
||||
@enum_name@_get_string (@EnumName@ val)
|
||||
{
|
||||
guint i;
|
||||
|
||||
for (i = 0; @enum_name@_values[i].value_nick; i++) {
|
||||
if (val == @enum_name@_values[i].value)
|
||||
return @enum_name@_values[i].value_nick;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif /* IS_ENUM */
|
||||
|
||||
/* Flags-specific method to build a string with the given mask.
|
||||
* We get a comma separated list of the nicks of the GFlagsValues.
|
||||
* Note that this will be valid even if the GFlagsClass is not referenced
|
||||
* anywhere. */
|
||||
#if defined IS_FLAGS
|
||||
gchar *
|
||||
@enum_name@_build_string_from_mask (@EnumName@ mask)
|
||||
{
|
||||
guint i;
|
||||
gboolean first = TRUE;
|
||||
GString *str = NULL;
|
||||
|
||||
for (i = 0; @enum_name@_values[i].value_nick; i++) {
|
||||
/* We also look for exact matches */
|
||||
if (mask == @enum_name@_values[i].value) {
|
||||
if (str)
|
||||
g_string_free (str, TRUE);
|
||||
return g_strdup (@enum_name@_values[i].value_nick);
|
||||
}
|
||||
|
||||
/* Build list with single-bit masks */
|
||||
if (mask & @enum_name@_values[i].value) {
|
||||
guint c;
|
||||
gulong number = @enum_name@_values[i].value;
|
||||
|
||||
for (c = 0; number; c++)
|
||||
number &= number - 1;
|
||||
|
||||
if (c == 1) {
|
||||
if (!str)
|
||||
str = g_string_new ("");
|
||||
g_string_append_printf (str, "%s%s",
|
||||
first ? "" : ", ",
|
||||
@enum_name@_values[i].value_nick);
|
||||
if (first)
|
||||
first = FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (str ? g_string_free (str, FALSE) : NULL);
|
||||
}
|
||||
#endif /* IS_FLAGS */
|
||||
|
||||
/*** END value-tail ***/
|
||||
|
||||
/*** BEGIN file-tail ***/
|
||||
|
@@ -13,6 +13,20 @@ G_BEGIN_DECLS
|
||||
/*** BEGIN value-header ***/
|
||||
GType @enum_name@_get_type (void) G_GNUC_CONST;
|
||||
#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
|
||||
|
||||
/* Define type-specific symbols */
|
||||
#undef IS_ENUM
|
||||
#undef IS_FLAGS
|
||||
#define IS_@TYPE@
|
||||
|
||||
#if defined IS_ENUM
|
||||
const gchar *@enum_name@_get_string (@EnumName@ val);
|
||||
#endif
|
||||
|
||||
#if defined IS_FLAGS
|
||||
gchar *@enum_name@_build_string_from_mask (@EnumName@ mask);
|
||||
#endif
|
||||
|
||||
/*** END value-header ***/
|
||||
|
||||
/*** BEGIN file-tail ***/
|
||||
|
@@ -37,14 +37,14 @@ GENERATED_DOC = \
|
||||
# Enum types
|
||||
mm-enums-types.h: Makefile.am $(top_srcdir)/include/ModemManager-enums.h $(top_srcdir)/build-aux/mm-enums-template.h
|
||||
$(AM_V_GEN) glib-mkenums \
|
||||
--fhead "#ifndef __MM_ENUMS_TYPES_H__\n#define __MM_ENUMS_TYPES_H__\n" \
|
||||
--fhead "#include <ModemManager-enums.h>\n#ifndef __MM_ENUMS_TYPES_H__\n#define __MM_ENUMS_TYPES_H__\n" \
|
||||
--template $(top_srcdir)/build-aux/mm-enums-template.h \
|
||||
--ftail "#endif /* __MM_ENUMS_TYPES_H__ */\n" \
|
||||
$(top_srcdir)/include/ModemManager-enums.h > $@
|
||||
|
||||
mm-enums-types.c: Makefile.am $(top_srcdir)/include/ModemManager-enums.h $(top_srcdir)/build-aux/mm-enums-template.c mm-enums-types.h
|
||||
$(AM_V_GEN) glib-mkenums \
|
||||
--fhead "#include <ModemManager-enums.h>\n#include \"mm-enums-types.h\"\n" \
|
||||
--fhead "#include \"mm-enums-types.h\"\n" \
|
||||
--template $(top_srcdir)/build-aux/mm-enums-template.c \
|
||||
$(top_srcdir)/include/ModemManager-enums.h > $@
|
||||
|
||||
|
@@ -58,14 +58,14 @@ WITH_ENUMS = mm-bearer.h
|
||||
|
||||
mm-private-enums-types.h: Makefile.am $(WITH_ENUMS) $(top_srcdir)/build-aux/mm-enums-template.h
|
||||
$(AM_V_GEN) glib-mkenums \
|
||||
--fhead "#ifndef __MM_PRIVATE_ENUMS_TYPES_H__\n#define __MM_PRIVATE_ENUMS_TYPES_H__\n" \
|
||||
--fhead "#include \"mm-bearer.h\"\n#ifndef __MM_PRIVATE_ENUMS_TYPES_H__\n#define __MM_PRIVATE_ENUMS_TYPES_H__\n" \
|
||||
--template $(top_srcdir)/build-aux/mm-enums-template.h \
|
||||
--ftail "#endif /* __MM_PRIVATE_ENUMS_TYPES_H__ */\n" \
|
||||
$(WITH_ENUMS) > $@
|
||||
|
||||
mm-private-enums-types.c: Makefile.am $(top_srcdir)/include/ModemManager-enums.h $(top_srcdir)/build-aux/mm-enums-template.c mm-private-enums-types.h
|
||||
$(AM_V_GEN) glib-mkenums \
|
||||
--fhead "#include \"mm-bearer.h\"\n#include \"mm-private-enums-types.h\"" \
|
||||
--fhead "#include \"mm-private-enums-types.h\"" \
|
||||
--template $(top_srcdir)/build-aux/mm-enums-template.c \
|
||||
$(WITH_ENUMS) > $@
|
||||
|
||||
|
Reference in New Issue
Block a user