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, \
result_type, \
entry_cmd, \
unknown_val_cmd, \
...) \
result_type \
#define _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \
value_type, \
value_type_result, \
entry_cmd, \
unknown_val_cmd, \
get_operator, \
...) \
value_type_result \
fcn_name (const char *name) \
{ \
static const struct { \
const char *name; \
result_type value; \
value_type value; \
} LIST[] = { \
__VA_ARGS__ \
}; \
\
{ entry_cmd; } \
\
if (NM_MORE_ASSERTS > 5) { \
static gboolean checked = FALSE; \
if (NM_MORE_ASSERT_ONCE (5)) { \
int i; \
\
if (!checked) { \
checked = TRUE; \
\
for (i = 0; i < G_N_ELEMENTS (LIST); i++) { \
nm_assert (LIST[i].name); \
if (i > 0) \
nm_assert (strcmp (LIST[i - 1].name, LIST[i].name) < 0); \
} \
for (i = 0; i < G_N_ELEMENTS (LIST); i++) { \
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); \
\
if (G_UNLIKELY (cmp == 0)) \
return LIST[imid].value; \
return get_operator (LIST[imid].value); \
\
if (cmp < 0) \
imin = imid + 1u; \
@@ -1597,6 +1594,32 @@ fcn_name (const char *name) \
{ 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 *