Class designed to store and invoke function pointers and method pointers
Diff: GenericFunctionHandler.h
- Revision:
- 1:2418b141f599
- Parent:
- 0:2aa4daee328e
- Child:
- 3:7e4f282982a6
--- a/GenericFunctionHandler.h Wed Nov 23 00:23:53 2011 +0000 +++ b/GenericFunctionHandler.h Tue Nov 29 05:44:40 2011 +0000 @@ -26,15 +26,29 @@ : FunctionPointerType(0), FunctionPointer(FunctionAddress) { } + //This constructor accepts a function address and constructs an + //instance of this class for handling a static function. The + //constructor prepares the function to be called when the the Call + //method of this class object is called. 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( + 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)); + } + //This constructor template accepts a pointer to a class object + //and a pointer to a method of that class and constructs an + //instance of this class for handling a method of that class. The + //constructor constructor prepares the method to be called on the + //class object when the Call method of this class object is + //called. ~GenericFunctionHandler() { @@ -53,6 +67,8 @@ (this->*InvokePointer)(); } } + //This method invokes the function or method pointer that was + //passed into the constructor of this class. void operator ()() { @@ -65,6 +81,11 @@ (this->*InvokePointer)(); } } + //This overload of the () operator allows shorthand invokation of + //a GenericFunctionHandler object's function pointer such that it + //appears as a standard function call. For example: + //GenericFunctionHandlerObject(); does the same thing as + //GenericFunctionHandlerObject.Call(); }; #endif \ No newline at end of file