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.
This commit is contained in:
Vincent Coubard 2015-11-23 11:27:08 +00:00
parent 381345fe65
commit 38e27c4a0a
2 changed files with 20 additions and 11 deletions

View File

@ -118,9 +118,15 @@ public:
while (current) { while (current) {
if(*current == toDetach) { if(*current == toDetach) {
if(previous == NULL) { if(previous == NULL) {
if(currentCalled == current) {
currentCalled = NULL;
}
chainHead = current->getNext(); chainHead = current->getNext();
} else { } else {
if(currentCalled == current) {
currentCalled = previous;
}
previous->chainAsNext(current->getNext()); previous->chainAsNext(current->getNext());
} }
delete current; delete current;
@ -155,17 +161,23 @@ public:
* chained FunctionPointers. * chained FunctionPointers.
*/ */
void call(ContextType context) { void call(ContextType context) {
if (chainHead) { ((const CallChainOfFunctionPointersWithContext*) this)->call(context);
chainHead->call(context);
}
} }
/** /**
* @brief same as above but const * @brief same as above but const
*/ */
void call(ContextType context) const { void call(ContextType context) const {
if (chainHead) { currentCalled = chainHead;
chainHead->call(context);
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: private:
pFunctionPointerWithContext_t chainHead; pFunctionPointerWithContext_t chainHead;
mutable pFunctionPointerWithContext_t currentCalled;
/* Disallow copy constructor and assignment operators. */ /* Disallow copy constructor and assignment operators. */
private: private:

View File

@ -104,11 +104,6 @@ public:
/** Same as above, workaround for mbed os FunctionPointer implementation. */ /** Same as above, workaround for mbed os FunctionPointer implementation. */
void call(ContextType context) { void call(ContextType context) {
_caller(this, context); _caller(this, context);
/* Propagate the call to next in the chain. */
if (_next) {
_next->call(context);
}
} }
typedef void (FunctionPointerWithContext::*bool_type)() const; typedef void (FunctionPointerWithContext::*bool_type)() const;