Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Is this a more efficient implementation than "FunctionPointer.h"?
I'm especially interested in the invoking function. FunctionPointer.h uses a memcpy to copy the address over into a new pointer and then invokes the pointer. This one type casts the array pointers into object and function pointers and then invokes it.
May I please have comments or suggestions? Thank you.
#ifndef GenericFunctionHandlerLibrary #define GenericFunctionHandlerLibrary class GenericFunctionHandler { private: bool FunctionPointerType; void (*FunctionPointer)(); unsigned char* ObjectPointer; unsigned char* MethodPointer; void (GenericFunctionHandler::*InvokePointer)(); template<class Class> void InvokeMethod() { (*(Class**)ObjectPointer->**(void (Class::**)())MethodPointer)(); } public: GenericFunctionHandler(void (*FunctionAddress)()) : FunctionPointerType(0), FunctionPointer(FunctionAddress) { } template<class Class> GenericFunctionHandler(Class* ObjectAddress, void (Class::*MethodAddress)()) : FunctionPointerType(1), ObjectPointer(new unsigned char(sizeof(ObjectAddress))), MethodPointer(new unsigned char(sizeof(MethodAddress))), InvokePointer(&GenericFunctionHandler::InvokeMethod<Class>) { memcpy(ObjectPointer, (unsigned char*)&ObjectAddress, sizeof(ObjectAddress)); memcpy(MethodPointer, (unsigned char*)&MethodAddress, sizeof(MethodAddress)); } ~GenericFunctionHandler() { delete[] ObjectPointer; delete[] MethodPointer; } void Call() { if (!FunctionPointerType) { (*FunctionPointer)(); } else { (this->*InvokePointer)(); } } void operator ()() { if (!FunctionPointerType) { (*FunctionPointer)(); } else { (this->*InvokePointer)(); } } }; #endif