From 38e27c4a0aa6f444f7a447adf50d7f649eaea620 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 23 Nov 2015 11:27:08 +0000 Subject: [PATCH] FunctionPointer call is not propagated through FunctionPointer call but it is a mechanism internal to CallChain object. This change allow to safelly remove a callback in a callchain while calling it. It also clean responsabilities and reduce coupling. --- ble/CallChainOfFunctionPointersWithContext.h | 26 +++++++++++++++----- ble/FunctionPointerWithContext.h | 5 ---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ble/CallChainOfFunctionPointersWithContext.h b/ble/CallChainOfFunctionPointersWithContext.h index 40b0910..761c651 100644 --- a/ble/CallChainOfFunctionPointersWithContext.h +++ b/ble/CallChainOfFunctionPointersWithContext.h @@ -118,9 +118,15 @@ public: while (current) { if(*current == toDetach) { - if(previous == NULL) { + if(previous == NULL) { + if(currentCalled == current) { + currentCalled = NULL; + } chainHead = current->getNext(); } else { + if(currentCalled == current) { + currentCalled = previous; + } previous->chainAsNext(current->getNext()); } delete current; @@ -155,17 +161,23 @@ public: * chained FunctionPointers. */ void call(ContextType context) { - if (chainHead) { - chainHead->call(context); - } + ((const CallChainOfFunctionPointersWithContext*) this)->call(context); } /** * @brief same as above but const */ void call(ContextType context) const { - if (chainHead) { - chainHead->call(context); + currentCalled = chainHead; + + while(currentCalled) { + currentCalled->call(context); + // if this was the head and the call removed the head + if(currentCalled == NULL) { + currentCalled = chainHead; + } else { + currentCalled = currentCalled->getNext(); + } } } @@ -197,6 +209,8 @@ private: private: pFunctionPointerWithContext_t chainHead; + mutable pFunctionPointerWithContext_t currentCalled; + /* Disallow copy constructor and assignment operators. */ private: diff --git a/ble/FunctionPointerWithContext.h b/ble/FunctionPointerWithContext.h index f1edac6..6e60a44 100644 --- a/ble/FunctionPointerWithContext.h +++ b/ble/FunctionPointerWithContext.h @@ -104,11 +104,6 @@ public: /** Same as above, workaround for mbed os FunctionPointer implementation. */ void call(ContextType context) { _caller(this, context); - - /* Propagate the call to next in the chain. */ - if (_next) { - _next->call(context); - } } typedef void (FunctionPointerWithContext::*bool_type)() const;