summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-12-07 14:28:09 +0100
committerThomas Haller <thaller@redhat.com>2015-12-07 14:41:32 +0100
commit0a6cca945099b2b91cea2a7eb763ab44dd4129df (patch)
tree73470c73f5f0aafd187954a0e99c87bba98a329d
parenta291ecc29c77ea55e902f7c30bef859ae47f5e45 (diff)
downloadNetworkManager-0a6cca945099b2b91cea2a7eb763ab44dd4129df.tar.gz
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.
-rw-r--r--src/devices/nm-device.c13
1 files changed, 4 insertions, 9 deletions
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index a3e057c6d4..2eed430df0 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -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)
/***********************************************************/