UI: ControlManager: Simplify processCommand()

This commit is contained in:
Nodir Temirkhodjaev 2024-11-09 13:18:04 +05:00
parent 8e64d26859
commit c8f6986edf
2 changed files with 21 additions and 23 deletions

View File

@ -257,29 +257,27 @@ bool processCommandZone(const ProcessCommandArgs &p)
return processCommandZoneAction(zoneAction);
}
bool processCommandRpc(const ProcessCommandArgs &p)
{
return IoC<RpcManager>()->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<RpcManager>()->processCommandRpc(p);
}
const bool ok = func(p);
if (!ok && p.errorMessage.isEmpty()) {
p.errorMessage = "Invalid command";

View File

@ -40,11 +40,11 @@ public:
bool processCommandRpc(const ProcessCommandArgs &p);
template<typename F>
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];