shared: add NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE() macro for lookup of structs

This commit is contained in:
Thomas Haller
2020-02-19 12:16:01 +01:00
parent 14461e7217
commit f4d12f7b59

View File

@@ -1536,35 +1536,32 @@ guint8 *nm_utils_hexstr2bin_alloc (const char *hexstr,
/*****************************************************************************/ /*****************************************************************************/
#define NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \ #define _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \
result_type, \ value_type, \
entry_cmd, \ value_type_result, \
unknown_val_cmd, \ entry_cmd, \
...) \ unknown_val_cmd, \
result_type \ get_operator, \
...) \
value_type_result \
fcn_name (const char *name) \ fcn_name (const char *name) \
{ \ { \
static const struct { \ static const struct { \
const char *name; \ const char *name; \
result_type value; \ value_type value; \
} LIST[] = { \ } LIST[] = { \
__VA_ARGS__ \ __VA_ARGS__ \
}; \ }; \
\ \
{ entry_cmd; } \ { entry_cmd; } \
\ \
if (NM_MORE_ASSERTS > 5) { \ if (NM_MORE_ASSERT_ONCE (5)) { \
static gboolean checked = FALSE; \
int i; \ int i; \
\ \
if (!checked) { \ for (i = 0; i < G_N_ELEMENTS (LIST); i++) { \
checked = TRUE; \ nm_assert (LIST[i].name); \
\ if (i > 0) \
for (i = 0; i < G_N_ELEMENTS (LIST); i++) { \ nm_assert (strcmp (LIST[i - 1].name, LIST[i].name) < 0); \
nm_assert (LIST[i].name); \
if (i > 0) \
nm_assert (strcmp (LIST[i - 1].name, LIST[i].name) < 0); \
} \
} \ } \
} \ } \
\ \
@@ -1579,7 +1576,7 @@ fcn_name (const char *name) \
const int cmp = strcmp (LIST[imid].name, name); \ const int cmp = strcmp (LIST[imid].name, name); \
\ \
if (G_UNLIKELY (cmp == 0)) \ if (G_UNLIKELY (cmp == 0)) \
return LIST[imid].value; \ return get_operator (LIST[imid].value); \
\ \
if (cmp < 0) \ if (cmp < 0) \
imin = imid + 1u; \ imin = imid + 1u; \
@@ -1597,6 +1594,32 @@ fcn_name (const char *name) \
{ unknown_val_cmd; } \ { unknown_val_cmd; } \
} }
#define NM_UTILS_STRING_TABLE_LOOKUP_STRUCT_DEFINE(fcn_name, \
result_type, \
entry_cmd, \
unknown_val_cmd, \
...) \
_NM_UTILS_STRING_TABLE_LOOKUP_DEFINE (fcn_name, \
result_type, \
const result_type *, \
entry_cmd, \
unknown_val_cmd, \
&, \
__VA_ARGS__)
#define NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \
result_type, \
entry_cmd, \
unknown_val_cmd, \
...) \
_NM_UTILS_STRING_TABLE_LOOKUP_DEFINE (fcn_name, \
result_type, \
result_type, \
entry_cmd, \
unknown_val_cmd, \
, \
__VA_ARGS__)
/*****************************************************************************/ /*****************************************************************************/
static inline GTask * static inline GTask *