libmm-glib: separate files for flags and enums types

This allows us to skip needing to include the non-existent
build_string_from_mask() or get_string() counterparts in the
documentation index.
This commit is contained in:
Aleksander Morgado
2023-04-27 12:23:04 +00:00
committed by Aleksander Morgado
parent fdf03f9b2c
commit bf2843ad77
20 changed files with 166 additions and 130 deletions
@@ -16,11 +16,6 @@ static const G@Type@Value @enum_name@_values[] = {
{ 0, NULL, NULL }
};
/* Define type-specific symbols */
#undef __MM_IS_ENUM__
#undef __MM_IS_FLAGS__
#define __MM_IS_@TYPE@__
GType
@enum_name@_get_type (void)
{
@@ -36,15 +31,6 @@ GType
return g_define_type_id_initialized;
}
/**
* @enum_name@_get_string:
* @val: a @EnumName@.
*
* Gets the nickname string for the #@EnumName@ specified at @val.
*
* Returns: (transfer none): a string with the nickname, or %NULL if not found. Do not free the returned value.
*/
#if defined __MM_IS_ENUM__
const gchar *
@enum_name@_get_string (@EnumName@ val)
{
@@ -57,56 +43,6 @@ const gchar *
return NULL;
}
#endif /* __MM_IS_ENUM_ */
/**
* @enum_name@_build_string_from_mask:
* @mask: bitmask of @EnumName@ values.
*
* Builds a string containing a comma-separated list of nicknames for
* each #@EnumName@ in @mask.
*
* Returns: (transfer full): a string with the list of nicknames, or %NULL if none given. The returned value should be freed with g_free().
*/
#if defined __MM_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 /* __MM_IS_FLAGS__ */
/*** END value-tail ***/
+8 -11
View File
@@ -14,18 +14,15 @@ G_BEGIN_DECLS
GType @enum_name@_get_type (void) G_GNUC_CONST;
#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
/* Define type-specific symbols */
#undef __MM_IS_ENUM__
#undef __MM_IS_FLAGS__
#define __MM_IS_@TYPE@__
#if defined __MM_IS_ENUM__
/**
* @enum_name@_get_string:
* @val: a @EnumName@.
*
* Gets the nickname string for the #@EnumName@ specified at @val.
*
* Returns: (transfer none): a string with the nickname, or %NULL if not found. Do not free the returned value.
*/
const gchar *@enum_name@_get_string (@EnumName@ val);
#endif
#if defined __MM_IS_FLAGS__
gchar *@enum_name@_build_string_from_mask (@EnumName@ mask);
#endif
/*** END value-header ***/
@@ -0,0 +1,75 @@
/*** BEGIN file-header ***/
/*** END file-header ***/
/*** BEGIN file-production ***/
/* enumerations from "@filename@" */
/*** 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 }
};
GType
@enum_name@_get_type (void)
{
static gsize g_define_type_id_initialized = 0;
if (g_once_init_enter (&g_define_type_id_initialized)) {
GType g_define_type_id =
g_@type@_register_static (g_intern_static_string ("@EnumName@"),
@enum_name@_values);
g_once_init_leave (&g_define_type_id_initialized, g_define_type_id);
}
return g_define_type_id_initialized;
}
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);
}
/*** END value-tail ***/
/*** BEGIN file-tail ***/
/*** END file-tail ***/
@@ -0,0 +1,33 @@
/*** BEGIN file-header ***/
#include <glib-object.h>
G_BEGIN_DECLS
/*** END file-header ***/
/*** BEGIN file-production ***/
/* enumerations from "@basename@" */
/*** END file-production ***/
/*** BEGIN value-header ***/
GType @enum_name@_get_type (void) G_GNUC_CONST;
#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
/**
* @enum_name@_build_string_from_mask:
* @mask: bitmask of @EnumName@ values.
*
* Builds a string containing a comma-separated list of nicknames for
* each #@EnumName@ in @mask.
*
* Returns: (transfer full): a string with the list of nicknames, or %NULL if none given. The returned value should be freed with g_free().
*/
gchar *@enum_name@_build_string_from_mask (@EnumName@ mask);
/*** END value-header ***/
/*** BEGIN file-tail ***/
G_END_DECLS
/*** END file-tail ***/