discover: simplify handler switch statement
authorJeremy Kerr <jk@ozlabs.org>
Tue, 5 Mar 2013 05:49:15 +0000 (13:49 +0800)
committerGeoff Levand <geoff@infradead.org>
Tue, 5 Mar 2013 14:06:17 +0000 (06:06 -0800)
Instead of using nested switch statements in device_handler_event,
define an array of handlers, and lookup instead.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Geoff Levand <geoff@infradead.org>
discover/device-handler.c
discover/event.h

index 6e03ef3fa159136b6015983a0054411cb7f126c8..e65eb61ebc2e353fb4880b4b59260c1f9c969a73 100644 (file)
@@ -387,46 +387,31 @@ static int handle_remove_user_event(struct device_handler *handler,
        return 0;
 }
 
+typedef int (*event_handler)(struct device_handler *, struct event *);
+
+static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
+       [EVENT_TYPE_UDEV] = {
+               [EVENT_ACTION_ADD]      = handle_add_udev_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
+       },
+       [EVENT_TYPE_USER] = {
+               [EVENT_ACTION_ADD]      = handle_add_user_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
+       }
+};
+
 int device_handler_event(struct device_handler *handler,
                struct event *event)
 {
-       int rc = 0;
-
-       switch (event->type) {
-       case EVENT_TYPE_UDEV:
-               switch (event->action) {
-               case EVENT_ACTION_ADD:
-                       rc = handle_add_udev_event(handler, event);
-                       break;
-               case EVENT_ACTION_REMOVE:
-                       rc = handle_remove_udev_event(handler, event);
-                       break;
-               default:
-                       pb_log("%s unknown action: %d\n", __func__,
-                               event->action);
-                       break;
-               }
-               break;
-       case EVENT_TYPE_USER:
-               switch (event->action) {
-               case EVENT_ACTION_ADD:
-                       rc = handle_add_user_event(handler, event);
-                       break;
-               case EVENT_ACTION_REMOVE:
-                       rc = handle_remove_user_event(handler, event);
-                       break;
-               default:
-                       pb_log("%s unknown action: %d\n", __func__,
-                               event->action);
-                       break;
-               }
-               break;
-       default:
-               pb_log("%s unknown type: %d\n", __func__, event->type);
-               break;
+       if (event->type >= EVENT_TYPE_MAX ||
+                       event->action >= EVENT_ACTION_MAX ||
+                       !handlers[event->type][event->action]) {
+               pb_log("%s unknown type/action: %d/%d\n", __func__,
+                               event->type, event->action);
+               return 0;
        }
 
-       return rc;
+       return handlers[event->type][event->action](handler, event);
 }
 
 struct device_handler *device_handler_init(struct discover_server *server)
index 20585f2525d4a5e492fb3f2871462a2565bf652f..91575ccd0de02004b94ff032566841e7c528e1b3 100644 (file)
@@ -4,11 +4,13 @@
 enum event_type {
        EVENT_TYPE_UDEV = 10,
        EVENT_TYPE_USER,
+       EVENT_TYPE_MAX,
 };
 
 enum event_action {
        EVENT_ACTION_ADD = 20,
        EVENT_ACTION_REMOVE,
+       EVENT_ACTION_MAX,
 };
 
 struct event {