utils: reimplement NM_UTILS_ENUM2STR_DEFINE() without helper function
The compiler might be able to optimize the switch better. But more importantly, it has the type information of the enum and can give warnings about unmentioned enum values.
This commit is contained in:
@@ -1880,45 +1880,6 @@ nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
const char *
|
||||
nm_utils_enum2str (const NMUtilsEnum2StrDesc *descs,
|
||||
gsize n_descs,
|
||||
int val,
|
||||
char *buf,
|
||||
gsize len)
|
||||
{
|
||||
gsize i;
|
||||
|
||||
#if NM_MORE_ASSERTS > 10
|
||||
nm_assert (descs);
|
||||
nm_assert (n_descs > 0);
|
||||
for (i = 0; i < n_descs; i++) {
|
||||
gsize j;
|
||||
|
||||
nm_assert (descs[i].name && descs[i].name[0]);
|
||||
for (j = 0; j < i; j++)
|
||||
nm_assert (descs[j].value != descs[i].value);
|
||||
}
|
||||
#endif
|
||||
|
||||
nm_utils_to_string_buffer_init (&buf, &len);
|
||||
|
||||
if (!len)
|
||||
return buf;
|
||||
|
||||
for (i = 0; i < n_descs; i++) {
|
||||
if (val == descs[i].value) {
|
||||
g_strlcpy (buf, descs[i].name, len);
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
g_snprintf (buf, len, "(%d)", val);
|
||||
return buf;
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/**
|
||||
* nm_utils_get_shared_wifi_permission:
|
||||
* @connection: the NMConnection to lookup the permission.
|
||||
|
@@ -229,34 +229,33 @@ const char *nm_utils_flags2str (const NMUtilsFlags2StrDesc *descs,
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
int value;
|
||||
const char *name;
|
||||
} NMUtilsEnum2StrDesc;
|
||||
#define NM_UTILS_ENUM2STR(v, n) (void) 0; case v: s = ""n""; (void) 0
|
||||
#define NM_UTILS_ENUM2STR_IGNORE(v) (void) 0; case v: break; (void) 0
|
||||
|
||||
#define NM_UTILS_ENUM2STR(v, n) { .value = v, .name = ""n, }
|
||||
|
||||
#define _NM_UTILS_ENUM2STR_DEFINE(scope, fcn_name, enum_type, ...) \
|
||||
#define _NM_UTILS_ENUM2STR_DEFINE(scope, fcn_name, lookup_type, int_fmt, ...) \
|
||||
scope const char * \
|
||||
fcn_name (enum_type val, char *buf, gsize len) \
|
||||
fcn_name (lookup_type val, char *buf, gsize len) \
|
||||
{ \
|
||||
static const NMUtilsEnum2StrDesc descs[] = { \
|
||||
nm_utils_to_string_buffer_init (&buf, &len); \
|
||||
if (len) { \
|
||||
const char *s = NULL; \
|
||||
switch (val) { \
|
||||
(void) 0, \
|
||||
__VA_ARGS__ \
|
||||
(void) 0; \
|
||||
}; \
|
||||
G_STATIC_ASSERT (sizeof (enum_type) <= sizeof (int)); \
|
||||
return nm_utils_enum2str (descs, G_N_ELEMENTS (descs), val, buf, len); \
|
||||
if (s) \
|
||||
g_strlcpy (buf, s, len); \
|
||||
else \
|
||||
g_snprintf (buf, len, "(%"int_fmt")", val); \
|
||||
} \
|
||||
return buf; \
|
||||
}
|
||||
|
||||
#define NM_UTILS_ENUM2STR_DEFINE(fcn_name, enum_type, ...) \
|
||||
_NM_UTILS_ENUM2STR_DEFINE (, fcn_name, enum_type, __VA_ARGS__)
|
||||
#define NM_UTILS_ENUM2STR_DEFINE_STATIC(fcn_name, enum_type, ...) \
|
||||
_NM_UTILS_ENUM2STR_DEFINE (static, fcn_name, enum_type, __VA_ARGS__)
|
||||
|
||||
const char *nm_utils_enum2str (const NMUtilsEnum2StrDesc *descs,
|
||||
gsize n_descs,
|
||||
int val,
|
||||
char *buf,
|
||||
gsize len);
|
||||
#define NM_UTILS_ENUM2STR_DEFINE(fcn_name, lookup_type, ...) \
|
||||
_NM_UTILS_ENUM2STR_DEFINE (, fcn_name, lookup_type, "d", __VA_ARGS__)
|
||||
#define NM_UTILS_ENUM2STR_DEFINE_STATIC(fcn_name, lookup_type, ...) \
|
||||
_NM_UTILS_ENUM2STR_DEFINE (static, fcn_name, lookup_type, "d", __VA_ARGS__)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
|
Reference in New Issue
Block a user