jgh
Fork of BLE_API by
Diff: ble/CallChainOfFunctionPointersWithContext.h
- Revision:
- 921:ea542e6519bb
- Parent:
- 920:d18cff0c4b09
- Child:
- 928:624793511b25
diff -r d18cff0c4b09 -r ea542e6519bb ble/CallChainOfFunctionPointersWithContext.h --- a/ble/CallChainOfFunctionPointersWithContext.h Thu Nov 26 12:52:04 2015 +0000 +++ b/ble/CallChainOfFunctionPointersWithContext.h Thu Nov 26 12:52:04 2015 +0000 @@ -97,6 +97,42 @@ return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr)); } + /** Add a function at the front of the chain. + * + * @param func The FunctionPointerWithContext to add. + */ + void add(const FunctionPointerWithContext<ContextType>& func) { + common_add(new FunctionPointerWithContext<ContextType>(func)); + } + + /** + * Detach a function pointer from a callchain + * + * @oaram toDetach FunctionPointerWithContext to detach from this callchain + * + * @return true if a function pointer has been detached and false otherwise + */ + void detach(const FunctionPointerWithContext<ContextType>& toDetach) { + pFunctionPointerWithContext_t current = chainHead; + pFunctionPointerWithContext_t previous = NULL; + + while (current) { + if(*current == toDetach) { + if(previous == NULL) { + chainHead = current->getNext(); + } else { + previous->chainAsNext(current->getNext()); + } + delete current; + return true; + } + + previous = current; + current = current->getNext(); + } + return false; + } + /** Clear the call chain (remove all functions in the chain). */ void clear(void) { @@ -125,6 +161,29 @@ } } + /** + * @brief same as above but const + */ + void call(ContextType context) const { + if (chainHead) { + chainHead->call(context); + } + } + + /** + * @brief same as above but with function call operator + */ + void operator()(ContextType context) const { + call(context); + } + + typedef void (CallChainOfFunctionPointersWithContext::*bool_type)() const; + void True() const {} + + operator bool_type() const { + return chainHead == NULL ? 0 : &CallChainOfFunctionPointersWithContext::True; + } + private: pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) { if (chainHead == NULL) {