allow observers to return an error code to abort further processing

Will allow me to use observers to generalize the various hooks
that need to run to preflight sleep entry.
This commit is contained in:
geeksville 2020-04-10 12:40:44 -07:00
parent 6ad451eb5f
commit 640cb3bf7f
4 changed files with 22 additions and 8 deletions

View File

@ -322,7 +322,7 @@ void MeshService::sendOurPosition(NodeNum dest, bool wantReplies)
sendToMesh(p);
}
void MeshService::onGPSChanged(void *unused)
int MeshService::onGPSChanged(void *unused)
{
DEBUG_MSG("got gps notify\n");
@ -354,4 +354,6 @@ void MeshService::onGPSChanged(void *unused)
releaseToPool(p);
}
return 0;
}

View File

@ -80,7 +80,8 @@ class MeshService
void sendToMesh(MeshPacket *p);
/// Called when our gps position has changed - updates nodedb and sends Location message out into the mesh
void onGPSChanged(void *arg);
/// returns 0 to allow futher processing
int onGPSChanged(void *arg);
/// handle all the packets that just arrived from the mesh radio
void handleFromRadio();

View File

@ -24,7 +24,11 @@ template <class T> class Observer
friend class Observable<T>;
protected:
virtual void onNotify(T arg) = 0;
/**
* returns 0 if other observers should continue to be called
* returns !0 if the observe calls should be aborted and this result code returned for notifyObservers
**/
virtual int onNotify(T arg) = 0;
};
/**
@ -32,7 +36,7 @@ template <class T> class Observer
*/
template <class Callback, class T> class CallbackObserver : public Observer<T>
{
typedef void (Callback::*ObserverCallback)(T arg);
typedef int (Callback::*ObserverCallback)(T arg);
Callback *objPtr;
ObserverCallback method;
@ -41,7 +45,7 @@ template <class Callback, class T> class CallbackObserver : public Observer<T>
CallbackObserver(Callback *_objPtr, ObserverCallback _method) : objPtr(_objPtr), method(_method) {}
protected:
virtual void onNotify(T arg) { (objPtr->*method)(arg); }
virtual int onNotify(T arg) { return (objPtr->*method)(arg); }
};
/**
@ -55,13 +59,19 @@ template <class T> class Observable
public:
/**
* Tell all observers about a change, observers can process arg as they wish
*
* returns !0 if an observer chose to abort processing by returning this code
*/
void notifyObservers(T arg)
int notifyObservers(T arg)
{
for (typename std::list<Observer<T> *>::const_iterator iterator = observers.begin(); iterator != observers.end();
++iterator) {
(*iterator)->onNotify(arg);
int result = (*iterator)->onNotify(arg);
if (result != 0)
return result;
}
return 0;
}
private:

View File

@ -222,10 +222,11 @@ class FromNumCharacteristic : public CallbackCharacteristic, public Observer<uin
}
/// If the mesh service tells us fromNum has changed, tell the phone
virtual void onNotify(uint32_t newValue)
virtual int onNotify(uint32_t newValue)
{
setValue(newValue);
notify();
return 0;
}
};