Class designed to store and invoke function pointers and method pointers

Committer:
Blaze513
Date:
Tue Nov 29 05:44:40 2011 +0000
Revision:
1:2418b141f599
Parent:
0:2aa4daee328e
Child:
3:7e4f282982a6
added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Blaze513 0:2aa4daee328e 1 //mbed Microcontroller Library
Blaze513 0:2aa4daee328e 2 //Class designed to store and invoke function pointers and method pointers
Blaze513 0:2aa4daee328e 3 //Copyright 2011
Blaze513 0:2aa4daee328e 4 //Thomas Hamilton
Blaze513 0:2aa4daee328e 5
Blaze513 0:2aa4daee328e 6 #ifndef GenericFunctionHandlerLibrary
Blaze513 0:2aa4daee328e 7 #define GenericFunctionHandlerLibrary
Blaze513 0:2aa4daee328e 8
Blaze513 0:2aa4daee328e 9 class GenericFunctionHandler
Blaze513 0:2aa4daee328e 10 {
Blaze513 0:2aa4daee328e 11 private:
Blaze513 0:2aa4daee328e 12 bool FunctionPointerType;
Blaze513 0:2aa4daee328e 13 void (*FunctionPointer)();
Blaze513 0:2aa4daee328e 14 unsigned char* ObjectPointer;
Blaze513 0:2aa4daee328e 15 unsigned char* MethodPointer;
Blaze513 0:2aa4daee328e 16 void (GenericFunctionHandler::*InvokePointer)();
Blaze513 0:2aa4daee328e 17
Blaze513 0:2aa4daee328e 18 template<class Class>
Blaze513 0:2aa4daee328e 19 void InvokeMethod()
Blaze513 0:2aa4daee328e 20 {
Blaze513 0:2aa4daee328e 21 (*(Class**)ObjectPointer->**(void (Class::**)())MethodPointer)();
Blaze513 0:2aa4daee328e 22 }
Blaze513 0:2aa4daee328e 23
Blaze513 0:2aa4daee328e 24 public:
Blaze513 0:2aa4daee328e 25 GenericFunctionHandler(void (*FunctionAddress)())
Blaze513 0:2aa4daee328e 26 : FunctionPointerType(0), FunctionPointer(FunctionAddress)
Blaze513 0:2aa4daee328e 27 {
Blaze513 0:2aa4daee328e 28 }
Blaze513 1:2418b141f599 29 //This constructor accepts a function address and constructs an
Blaze513 1:2418b141f599 30 //instance of this class for handling a static function. The
Blaze513 1:2418b141f599 31 //constructor prepares the function to be called when the the Call
Blaze513 1:2418b141f599 32 //method of this class object is called.
Blaze513 0:2aa4daee328e 33 template<class Class>
Blaze513 1:2418b141f599 34 GenericFunctionHandler(
Blaze513 1:2418b141f599 35 Class* ObjectAddress, void (Class::*MethodAddress)()) :
Blaze513 1:2418b141f599 36 FunctionPointerType(1),
Blaze513 1:2418b141f599 37 ObjectPointer(new unsigned char(sizeof(ObjectAddress))),
Blaze513 1:2418b141f599 38 MethodPointer(new unsigned char(sizeof(MethodAddress))),
Blaze513 1:2418b141f599 39 InvokePointer(&GenericFunctionHandler::InvokeMethod<Class>)
Blaze513 1:2418b141f599 40 {
Blaze513 1:2418b141f599 41 memcpy(ObjectPointer, (unsigned char*)&ObjectAddress,
Blaze513 1:2418b141f599 42 sizeof(ObjectAddress));
Blaze513 1:2418b141f599 43 memcpy(MethodPointer, (unsigned char*)&MethodAddress,
Blaze513 1:2418b141f599 44 sizeof(MethodAddress));
Blaze513 1:2418b141f599 45 }
Blaze513 1:2418b141f599 46 //This constructor template accepts a pointer to a class object
Blaze513 1:2418b141f599 47 //and a pointer to a method of that class and constructs an
Blaze513 1:2418b141f599 48 //instance of this class for handling a method of that class. The
Blaze513 1:2418b141f599 49 //constructor constructor prepares the method to be called on the
Blaze513 1:2418b141f599 50 //class object when the Call method of this class object is
Blaze513 1:2418b141f599 51 //called.
Blaze513 0:2aa4daee328e 52
Blaze513 0:2aa4daee328e 53 ~GenericFunctionHandler()
Blaze513 0:2aa4daee328e 54 {
Blaze513 0:2aa4daee328e 55 delete[] ObjectPointer;
Blaze513 0:2aa4daee328e 56 delete[] MethodPointer;
Blaze513 0:2aa4daee328e 57 }
Blaze513 0:2aa4daee328e 58
Blaze513 0:2aa4daee328e 59 void Call()
Blaze513 0:2aa4daee328e 60 {
Blaze513 0:2aa4daee328e 61 if (!FunctionPointerType)
Blaze513 0:2aa4daee328e 62 {
Blaze513 0:2aa4daee328e 63 (*FunctionPointer)();
Blaze513 0:2aa4daee328e 64 }
Blaze513 0:2aa4daee328e 65 else
Blaze513 0:2aa4daee328e 66 {
Blaze513 0:2aa4daee328e 67 (this->*InvokePointer)();
Blaze513 0:2aa4daee328e 68 }
Blaze513 0:2aa4daee328e 69 }
Blaze513 1:2418b141f599 70 //This method invokes the function or method pointer that was
Blaze513 1:2418b141f599 71 //passed into the constructor of this class.
Blaze513 0:2aa4daee328e 72
Blaze513 0:2aa4daee328e 73 void operator ()()
Blaze513 0:2aa4daee328e 74 {
Blaze513 0:2aa4daee328e 75 if (!FunctionPointerType)
Blaze513 0:2aa4daee328e 76 {
Blaze513 0:2aa4daee328e 77 (*FunctionPointer)();
Blaze513 0:2aa4daee328e 78 }
Blaze513 0:2aa4daee328e 79 else
Blaze513 0:2aa4daee328e 80 {
Blaze513 0:2aa4daee328e 81 (this->*InvokePointer)();
Blaze513 0:2aa4daee328e 82 }
Blaze513 0:2aa4daee328e 83 }
Blaze513 1:2418b141f599 84 //This overload of the () operator allows shorthand invokation of
Blaze513 1:2418b141f599 85 //a GenericFunctionHandler object's function pointer such that it
Blaze513 1:2418b141f599 86 //appears as a standard function call. For example:
Blaze513 1:2418b141f599 87 //GenericFunctionHandlerObject(); does the same thing as
Blaze513 1:2418b141f599 88 //GenericFunctionHandlerObject.Call();
Blaze513 0:2aa4daee328e 89 };
Blaze513 0:2aa4daee328e 90
Blaze513 0:2aa4daee328e 91 #endif