Add detach operation to CallChainOfFunctionPointersWithContext, Add

operator== to functionpointerWithContext class
This commit is contained in:
Vincent Coubard 2015-11-20 12:23:02 +00:00
parent 04d10f6547
commit 162fb4f75b
2 changed files with 83 additions and 5 deletions

View file

@ -97,6 +97,34 @@ public:
return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
}
/**
* 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) {

View file

@ -26,6 +26,7 @@ template <typename ContextType>
class FunctionPointerWithContext {
public:
typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
typedef const FunctionPointerWithContext<ContextType> *cpFunctionPointerWithContext_t;
typedef void (*pvoidfcontext_t)(ContextType context);
/** Create a FunctionPointerWithContext, attaching a static function.
@ -33,7 +34,7 @@ public:
* @param function The void static function to attach (default is none).
*/
FunctionPointerWithContext(void (*function)(ContextType context) = NULL) :
_function(NULL), _caller(NULL), _next(NULL) {
_memberFunctionAndPointer(), _caller(NULL), _next(NULL) {
attach(function);
}
@ -48,6 +49,17 @@ public:
attach(object, member);
}
FunctionPointerWithContext(const FunctionPointerWithContext& that) :
_memberFunctionAndPointer(that._memberFunctionAndPointer), _caller(that._caller), _next(NULL) {
}
FunctionPointerWithContext& operator=(const FunctionPointerWithContext& that) {
_memberFunctionAndPointer = that._memberFunctionAndPointer;
_caller = that._caller;
_next = NULL;
return *this;
}
/** Attach a static function.
*
* @param function The void static function to attach (default is none).
@ -73,6 +85,16 @@ public:
* FunctionPointers their callbacks are invoked as well.
* @Note: All chained callbacks stack up, so hopefully there won't be too
* many FunctionPointers in a chain. */
void call(ContextType context) const {
_caller(this, context);
/* Propagate the call to next in the chain. */
if (_next) {
_next->call(context);
}
}
/** Same as above, workaround for mbed os FunctionPointer implementation. */
void call(ContextType context) {
_caller(this, context);
@ -101,9 +123,18 @@ public:
return (pvoidfcontext_t)_function;
}
friend bool operator==(const FunctionPointerWithContext& lhs, const FunctionPointerWithContext& rhs) {
return rhs._caller == lhs._caller &&
memcmp(
&rhs._memberFunctionAndPointer,
&lhs._memberFunctionAndPointer,
sizeof(rhs._memberFunctionAndPointer)
) == 0;
}
private:
template<typename T>
static void membercaller(pFunctionPointerWithContext_t self, ContextType context) {
static void membercaller(cpFunctionPointerWithContext_t self, ContextType context) {
if (self->_memberFunctionAndPointer._object) {
T *o = static_cast<T *>(self->_memberFunctionAndPointer._object);
void (T::*m)(ContextType);
@ -112,7 +143,7 @@ private:
}
}
static void functioncaller(pFunctionPointerWithContext_t self, ContextType context) {
static void functioncaller(cpFunctionPointerWithContext_t self, ContextType context) {
if (self->_function) {
self->_function(context);
}
@ -141,10 +172,10 @@ private:
* object this pointer and pointer to member -
* _memberFunctionAndPointer._object will be NULL if none attached
*/
MemberFunctionAndPtr _memberFunctionAndPointer;
mutable MemberFunctionAndPtr _memberFunctionAndPointer;
};
void (*_caller)(FunctionPointerWithContext*, ContextType);
void (*_caller)(const FunctionPointerWithContext*, ContextType);
pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers. This
* allows chaining function pointers without requiring
@ -152,4 +183,23 @@ private:
* 'CallChain' as an alternative. */
};
/**
* @brief Create a new FunctionPointerWithContext which bind an instance and a
* a member function together.
* @details This little helper is a just here to eliminate the need to write the
* FunctionPointerWithContext type each time you want to create one by kicking
* automatic type deduction of function templates. With this function, it is easy
* to write only one entry point for functions which expect a FunctionPointer
* in parameters.
*
* @param object to bound with member function
* @param member The member function called
* @return a new FunctionPointerWithContext
*/
template<typename T, typename ContextType>
FunctionPointerWithContext<ContextType> makeFunctionPointer(T *object, void (T::*member)(ContextType context))
{
return FunctionPointerWithContext<ContextType>(object, member);
}
#endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H