Commit 0a6cca94 authored by Thomas Haller's avatar Thomas Haller

device: use NM_UTILS_STRING_LOOKUP_TABLE() for reason_to_string()

Showcase for the new macros NM_UTILS_STRING_LOOKUP_TABLE() and
NM_UTILS_STRING_LOOKUP_TABLE_DEFINE_STATIC().

It changes behavior in case of looking up an invalid/unknown
state reason. Previously it would just have returned "unknown"
-- which was indistinguishable from a regular "unknown" value.

Now it returns the numeric id as a string. The string is allocated
with alloca(), which is desired but one should be aware of the pitfalls:

- prevents the caller from being inlined
- bad idea to do in a loop.
parent a291ecc2
......@@ -442,7 +442,7 @@ state_to_string (NMDeviceState state)
return queued_state_to_string (state) + strlen (QUEUED_PREFIX);
}
static const char *reason_table[] = {
NM_UTILS_STRING_LOOKUP_TABLE_DEFINE_STATIC (_reason_to_string, NMDeviceStateReason, NULL,
[NM_DEVICE_STATE_REASON_UNKNOWN] = "unknown",
[NM_DEVICE_STATE_REASON_NONE] = "none",
[NM_DEVICE_STATE_REASON_NOW_MANAGED] = "managed",
......@@ -506,15 +506,10 @@ static const char *reason_table[] = {
[NM_DEVICE_STATE_REASON_NEW_ACTIVATION] = "new-activation",
[NM_DEVICE_STATE_REASON_PARENT_CHANGED] = "parent-changed",
[NM_DEVICE_STATE_REASON_PARENT_MANAGED_CHANGED] = "parent-managed-changed",
};
);
static const char *
reason_to_string (NMDeviceStateReason reason)
{
if ((gsize) reason < G_N_ELEMENTS (reason_table))
return reason_table[reason];
return reason_table[NM_DEVICE_STATE_REASON_UNKNOWN];
}
#define reason_to_string(reason) \
NM_UTILS_STRING_LOOKUP_TABLE (_reason_to_string, reason)
/***********************************************************/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment