mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-03-11 04:50:31 +01:00
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:
parent
72255b71d7
commit
174b25d98c
2 changed files with 22 additions and 62 deletions
|
|
@ -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[] = { \
|
||||
__VA_ARGS__ \
|
||||
}; \
|
||||
G_STATIC_ASSERT (sizeof (enum_type) <= sizeof (int)); \
|
||||
return nm_utils_enum2str (descs, G_N_ELEMENTS (descs), val, buf, len); \
|
||||
nm_utils_to_string_buffer_init (&buf, &len); \
|
||||
if (len) { \
|
||||
const char *s = NULL; \
|
||||
switch (val) { \
|
||||
(void) 0, \
|
||||
__VA_ARGS__ \
|
||||
(void) 0; \
|
||||
}; \
|
||||
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__)
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue