mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-08 14:10:23 +01:00
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.
This commit is contained in:
parent
a291ecc29c
commit
0a6cca9450
1 changed files with 4 additions and 9 deletions
|
|
@ -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)
|
||||
|
||||
/***********************************************************/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue