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) {
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:

View file

@ -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;