From 174b25d98c3ae395f5b41fc2e7d5c222cb6369cf Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 1 Feb 2016 14:43:02 +0100 Subject: [PATCH] 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. --- src/NetworkManagerUtils.c | 39 --------------------------------- src/NetworkManagerUtils.h | 45 +++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 62 deletions(-) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 49fb8cb047..f549875aa2 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -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. diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 1f31e53fef..a873629799 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -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__) /*****************************************************************************/