fka mod
Fork of BLE_API by
Diff: ble/FunctionPointerWithContext.h
- Revision:
- 966:9451b90bbb66
- Parent:
- 965:212c16f6247f
- Child:
- 969:61f13bc8edbf
--- a/ble/FunctionPointerWithContext.h Thu Nov 26 12:52:34 2015 +0000 +++ b/ble/FunctionPointerWithContext.h Thu Nov 26 12:52:34 2015 +0000 @@ -26,7 +26,6 @@ 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. @@ -34,7 +33,7 @@ * @param function The void static function to attach (default is none). */ FunctionPointerWithContext(void (*function)(ContextType context) = NULL) : - _memberFunctionAndPointer(), _caller(NULL), _next(NULL) { + _function(NULL), _caller(NULL), _next(NULL) { attach(function); } @@ -49,17 +48,6 @@ 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). @@ -85,7 +73,7 @@ * 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 { + void call(ContextType context) { _caller(this, context); /* Propagate the call to next in the chain. */ @@ -95,31 +83,6 @@ } /** - * @brief Same as above - */ - void operator()(ContextType context) const { - call(context); - } - - /** Same as above, workaround for mbed os FunctionPointer implementation. */ - void call(ContextType context) { - _caller(this, context); - } - - typedef void (FunctionPointerWithContext::*bool_type)() const; - - /** - * implementation of safe bool operator - */ - operator bool_type() const { - if(_function || _memberFunctionAndPointer._object) { - return &FunctionPointerWithContext::trueValue; - } - - return 0; - } - - /** * Set up an external FunctionPointer as a next in the chain of related * callbacks. Invoking call() on the head FunctionPointer will invoke all * chained callbacks. @@ -138,18 +101,9 @@ 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(cpFunctionPointerWithContext_t self, ContextType context) { + static void membercaller(pFunctionPointerWithContext_t self, ContextType context) { if (self->_memberFunctionAndPointer._object) { T *o = static_cast<T *>(self->_memberFunctionAndPointer._object); void (T::*m)(ContextType); @@ -158,18 +112,12 @@ } } - static void functioncaller(cpFunctionPointerWithContext_t self, ContextType context) { + static void functioncaller(pFunctionPointerWithContext_t self, ContextType context) { if (self->_function) { self->_function(context); } } - /** - * @brief True value used in conversion to bool, this function is useless - * beside this usage - */ - void trueValue() const {} - struct MemberFunctionAndPtr { /* * Forward declaration of a class and a member function to this class. @@ -193,10 +141,10 @@ * object this pointer and pointer to member - * _memberFunctionAndPointer._object will be NULL if none attached */ - mutable MemberFunctionAndPtr _memberFunctionAndPointer; + MemberFunctionAndPtr _memberFunctionAndPointer; }; - void (*_caller)(const FunctionPointerWithContext*, ContextType); + void (*_caller)(FunctionPointerWithContext*, ContextType); pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers. This * allows chaining function pointers without requiring @@ -204,23 +152,4 @@ * '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 \ No newline at end of file