From c8f6986edfd83270ee1f2d59cf01bc96a9814d17 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 9 Nov 2024 13:18:04 +0500 Subject: [PATCH] UI: ControlManager: Simplify processCommand() --- src/ui/control/controlmanager.cpp | 38 +++++++++++++++---------------- src/ui/rpc/rpcmanager.h | 6 ++--- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index 624c8a41..27029acf 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -257,29 +257,27 @@ bool processCommandZone(const ProcessCommandArgs &p) return processCommandZoneAction(zoneAction); } +bool processCommandRpc(const ProcessCommandArgs &p) +{ + return IoC()->processCommandRpc(p); +} + +using processCommand_func = bool (*)(const ProcessCommandArgs &p); + +static const processCommand_func processCommand_funcList[] = { + &processCommandHome, // Control::CommandHome, + &processCommandBlock, // Control::CommandBlock, + &processCommandProg, // Control::CommandProg, + &processCommandBackup, // Control::CommandBackup, + &processCommandZone, // Control::CommandZone, +}; + bool processCommand(const ProcessCommandArgs &p) { - bool ok; + const processCommand_func func = RpcManager::getProcessFunc(p.command, processCommand_funcList, + Control::CommandHome, Control::CommandZone, &processCommandRpc); - switch (p.command) { - case Control::CommandHome: { - ok = processCommandHome(p); - } break; - case Control::CommandBlock: { - ok = processCommandBlock(p); - } break; - case Control::CommandProg: { - ok = processCommandProg(p); - } break; - case Control::CommandBackup: { - ok = processCommandBackup(p); - } break; - case Control::CommandZone: { - ok = processCommandZone(p); - } break; - default: - ok = IoC()->processCommandRpc(p); - } + const bool ok = func(p); if (!ok && p.errorMessage.isEmpty()) { p.errorMessage = "Invalid command"; diff --git a/src/ui/rpc/rpcmanager.h b/src/ui/rpc/rpcmanager.h index 0ad36974..20d6f7bc 100644 --- a/src/ui/rpc/rpcmanager.h +++ b/src/ui/rpc/rpcmanager.h @@ -40,11 +40,11 @@ public: bool processCommandRpc(const ProcessCommandArgs &p); template - constexpr static F *getProcessFunc( - Control::Command command, F *const funcList[], int minIndex, int maxIndex) + constexpr static F *getProcessFunc(Control::Command command, F *const funcList[], int minIndex, + int maxIndex, F *defaultFunc = nullptr) { if (command < minIndex || command > maxIndex) - return nullptr; + return defaultFunc; const int funcIndex = command - minIndex; return funcList[funcIndex];