use template functions instead of statics for taskInterfaces

This commit is contained in:
andreas 2023-10-27 20:19:02 +02:00
parent 9e05a86820
commit 34dd3434fe
3 changed files with 23 additions and 15 deletions

View File

@ -58,9 +58,19 @@ class GwApi{
} }
}; };
using Ptr = std::shared_ptr<Base>; using Ptr = std::shared_ptr<Base>;
public: protected:
virtual bool iset(const String &file, const String &name, Ptr v) = 0; virtual bool iset(const String &file, const String &name, Ptr v) = 0;
virtual Ptr iget(const String &name, int &result) = 0; virtual Ptr iget(const String &name, int &result) = 0;
public:
template <typename T>
bool set(const T &v){
return false;
}
template <typename T>
T get(int &res){
res=-1;
return T();
}
}; };
class Status{ class Status{
public: public:
@ -225,16 +235,13 @@ class GwApi{
* *
*/ */
#define DECLARE_TASKIF_IMPL(task,type) \ #define DECLARE_TASKIF_IMPL(task,type) \
static bool apiSet##type(GwApi *a,const type &v){ \ template<> \
if (! a || ! a->taskInterfaces()) return false; \ inline bool GwApi::TaskInterfaces::set(const type & v) {\
return a->taskInterfaces()->iset(__FILE__,#type,GwApi::TaskInterfaces::Ptr(new type(v)));\ return iset(__FILE__,#type,GwApi::TaskInterfaces::Ptr(new type(v))); \
}\ }\
static const type apiGet##type(GwApi *a, int &result) {\ template<> \
if (! a || ! a->taskInterfaces()) {\ inline type GwApi::TaskInterfaces::get<type>(int &result) {\
result=-1; \ auto ptr=iget(#type,result); \
return type(); \
}\
auto ptr=a->taskInterfaces()->iget(#type,result); \
if (!ptr) {\ if (!ptr) {\
result=-1; \ result=-1; \
return type(); \ return type(); \

View File

@ -25,8 +25,9 @@ class FactoryResetRequest: public GwMessage{
}; };
void handleButtons(GwApi *api){ void handleButtons(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
GwApi::TaskInterfaces *interfaces=api->taskInterfaces();
IButtonTask state; IButtonTask state;
if (!apiSetIButtonTask(api,state)){ if (!interfaces->set(state)){
LOG_DEBUG(GwLog::ERROR,"unable to set button state"); LOG_DEBUG(GwLog::ERROR,"unable to set button state");
} }
#ifndef GWBUTTON_PIN #ifndef GWBUTTON_PIN
@ -71,7 +72,7 @@ void handleButtons(GwApi *api){
LOG_DEBUG(GwLog::LOG,"Button press stopped"); LOG_DEBUG(GwLog::LOG,"Button press stopped");
} }
if (state.state != lastState){ if (state.state != lastState){
apiSetIButtonTask(api,state); interfaces->set(state);
} }
continue; continue;
} }
@ -81,7 +82,7 @@ void handleButtons(GwApi *api){
LOG_DEBUG(GwLog::LOG,"Button press started"); LOG_DEBUG(GwLog::LOG,"Button press started");
state.pressCount++; state.pressCount++;
state.state=IButtonTask::PRESSED; state.state=IButtonTask::PRESSED;
apiSetIButtonTask(api,state); interfaces->set(state);
lastReport=now; lastReport=now;
continue; continue;
} }
@ -96,7 +97,7 @@ void handleButtons(GwApi *api){
state.state=IButtonTask::PRESSED_10; state.state=IButtonTask::PRESSED_10;
} }
if (lastState != state.state){ if (lastState != state.state){
apiSetIButtonTask(api,state); interfaces->set(state);
} }
if (now > (firstPressed+PRESS_RESET_TIME)){ if (now > (firstPressed+PRESS_RESET_TIME)){
LOG_DEBUG(GwLog::ERROR,"Factory reset by button"); LOG_DEBUG(GwLog::ERROR,"Factory reset by button");

View File

@ -50,7 +50,7 @@ void handleLeds(GwApi *api){
{ {
delay(50); delay(50);
GwLedMode newMode = currentMode; GwLedMode newMode = currentMode;
IButtonTask buttonState = apiGetIButtonTask(api, apiResult); IButtonTask buttonState = api->taskInterfaces()->get<IButtonTask>(apiResult);
if (apiResult >= 0) if (apiResult >= 0)
{ {
switch (buttonState.state) switch (buttonState.state)