From 34dd3434fe572758c87057fb4b7daae82e060b58 Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 27 Oct 2023 20:19:02 +0200 Subject: [PATCH] use template functions instead of statics for taskInterfaces --- lib/api/GwApi.h | 27 +++++++++++++++++---------- lib/buttontask/GwButtonTask.cpp | 9 +++++---- lib/ledtask/GwLedTask.cpp | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/api/GwApi.h b/lib/api/GwApi.h index 9209895..61b2564 100644 --- a/lib/api/GwApi.h +++ b/lib/api/GwApi.h @@ -58,9 +58,19 @@ class GwApi{ } }; using Ptr = std::shared_ptr; - public: + protected: virtual bool iset(const String &file, const String &name, Ptr v) = 0; virtual Ptr iget(const String &name, int &result) = 0; + public: + template + bool set(const T &v){ + return false; + } + template + T get(int &res){ + res=-1; + return T(); + } }; class Status{ public: @@ -225,16 +235,13 @@ class GwApi{ * */ #define DECLARE_TASKIF_IMPL(task,type) \ - static bool apiSet##type(GwApi *a,const type &v){ \ - if (! a || ! a->taskInterfaces()) return false; \ - return a->taskInterfaces()->iset(__FILE__,#type,GwApi::TaskInterfaces::Ptr(new type(v)));\ + template<> \ + inline bool GwApi::TaskInterfaces::set(const type & v) {\ + return iset(__FILE__,#type,GwApi::TaskInterfaces::Ptr(new type(v))); \ }\ - static const type apiGet##type(GwApi *a, int &result) {\ - if (! a || ! a->taskInterfaces()) {\ - result=-1; \ - return type(); \ - }\ - auto ptr=a->taskInterfaces()->iget(#type,result); \ + template<> \ + inline type GwApi::TaskInterfaces::get(int &result) {\ + auto ptr=iget(#type,result); \ if (!ptr) {\ result=-1; \ return type(); \ diff --git a/lib/buttontask/GwButtonTask.cpp b/lib/buttontask/GwButtonTask.cpp index 49a2bb1..9ea4beb 100644 --- a/lib/buttontask/GwButtonTask.cpp +++ b/lib/buttontask/GwButtonTask.cpp @@ -25,8 +25,9 @@ class FactoryResetRequest: public GwMessage{ }; void handleButtons(GwApi *api){ GwLog *logger=api->getLogger(); + GwApi::TaskInterfaces *interfaces=api->taskInterfaces(); IButtonTask state; - if (!apiSetIButtonTask(api,state)){ + if (!interfaces->set(state)){ LOG_DEBUG(GwLog::ERROR,"unable to set button state"); } #ifndef GWBUTTON_PIN @@ -71,7 +72,7 @@ void handleButtons(GwApi *api){ LOG_DEBUG(GwLog::LOG,"Button press stopped"); } if (state.state != lastState){ - apiSetIButtonTask(api,state); + interfaces->set(state); } continue; } @@ -81,7 +82,7 @@ void handleButtons(GwApi *api){ LOG_DEBUG(GwLog::LOG,"Button press started"); state.pressCount++; state.state=IButtonTask::PRESSED; - apiSetIButtonTask(api,state); + interfaces->set(state); lastReport=now; continue; } @@ -96,7 +97,7 @@ void handleButtons(GwApi *api){ state.state=IButtonTask::PRESSED_10; } if (lastState != state.state){ - apiSetIButtonTask(api,state); + interfaces->set(state); } if (now > (firstPressed+PRESS_RESET_TIME)){ LOG_DEBUG(GwLog::ERROR,"Factory reset by button"); diff --git a/lib/ledtask/GwLedTask.cpp b/lib/ledtask/GwLedTask.cpp index 17dcc07..3877a6c 100644 --- a/lib/ledtask/GwLedTask.cpp +++ b/lib/ledtask/GwLedTask.cpp @@ -50,7 +50,7 @@ void handleLeds(GwApi *api){ { delay(50); GwLedMode newMode = currentMode; - IButtonTask buttonState = apiGetIButtonTask(api, apiResult); + IButtonTask buttonState = api->taskInterfaces()->get(apiResult); if (apiResult >= 0) { switch (buttonState.state)