From 640cb3bf7f551bf40caee034d022c78187d6cf5c Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 10 Apr 2020 12:40:44 -0700 Subject: [PATCH] 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. --- src/MeshService.cpp | 4 +++- src/MeshService.h | 3 ++- src/Observer.h | 20 +++++++++++++++----- src/esp32/MeshBluetoothService.cpp | 3 ++- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/MeshService.cpp b/src/MeshService.cpp index 6b9dabdc6..38fdc3473 100644 --- a/src/MeshService.cpp +++ b/src/MeshService.cpp @@ -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; } diff --git a/src/MeshService.h b/src/MeshService.h index 30e03017c..e8f8721eb 100644 --- a/src/MeshService.h +++ b/src/MeshService.h @@ -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(); diff --git a/src/Observer.h b/src/Observer.h index b20fe818d..eab1a4a30 100644 --- a/src/Observer.h +++ b/src/Observer.h @@ -24,7 +24,11 @@ template class Observer friend class Observable; 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 Observer */ template class CallbackObserver : public Observer { - typedef void (Callback::*ObserverCallback)(T arg); + typedef int (Callback::*ObserverCallback)(T arg); Callback *objPtr; ObserverCallback method; @@ -41,7 +45,7 @@ template class CallbackObserver : public Observer 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 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 *>::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: diff --git a/src/esp32/MeshBluetoothService.cpp b/src/esp32/MeshBluetoothService.cpp index d1bd273b6..086f0afe6 100644 --- a/src/esp32/MeshBluetoothService.cpp +++ b/src/esp32/MeshBluetoothService.cpp @@ -222,10 +222,11 @@ class FromNumCharacteristic : public CallbackCharacteristic, public Observer