Class designed to store and invoke function pointers and method pointers
GenericFunctionHandler.h@3:7e4f282982a6, 2011-12-09 (annotated)
- Committer:
- Blaze513
- Date:
- Fri Dec 09 01:19:08 2011 +0000
- Revision:
- 3:7e4f282982a6
- Parent:
- 1:2418b141f599
Fixed problem with destructor deleting unallocated pointers
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:7e4f282982a6 | 55 | if (FunctionPointerType) |
Blaze513 | 3:7e4f282982a6 | 56 | { |
Blaze513 | 3:7e4f282982a6 | 57 | delete[] ObjectPointer; |
Blaze513 | 3:7e4f282982a6 | 58 | delete[] MethodPointer; |
Blaze513 | 3:7e4f282982a6 | 59 | } |
Blaze513 | 0:2aa4daee328e | 60 | } |
Blaze513 | 0:2aa4daee328e | 61 | |
Blaze513 | 0:2aa4daee328e | 62 | void Call() |
Blaze513 | 0:2aa4daee328e | 63 | { |
Blaze513 | 0:2aa4daee328e | 64 | if (!FunctionPointerType) |
Blaze513 | 0:2aa4daee328e | 65 | { |
Blaze513 | 0:2aa4daee328e | 66 | (*FunctionPointer)(); |
Blaze513 | 0:2aa4daee328e | 67 | } |
Blaze513 | 0:2aa4daee328e | 68 | else |
Blaze513 | 0:2aa4daee328e | 69 | { |
Blaze513 | 0:2aa4daee328e | 70 | (this->*InvokePointer)(); |
Blaze513 | 0:2aa4daee328e | 71 | } |
Blaze513 | 0:2aa4daee328e | 72 | } |
Blaze513 | 1:2418b141f599 | 73 | //This method invokes the function or method pointer that was |
Blaze513 | 1:2418b141f599 | 74 | //passed into the constructor of this class. |
Blaze513 | 0:2aa4daee328e | 75 | |
Blaze513 | 0:2aa4daee328e | 76 | void operator ()() |
Blaze513 | 0:2aa4daee328e | 77 | { |
Blaze513 | 0:2aa4daee328e | 78 | if (!FunctionPointerType) |
Blaze513 | 0:2aa4daee328e | 79 | { |
Blaze513 | 0:2aa4daee328e | 80 | (*FunctionPointer)(); |
Blaze513 | 0:2aa4daee328e | 81 | } |
Blaze513 | 0:2aa4daee328e | 82 | else |
Blaze513 | 0:2aa4daee328e | 83 | { |
Blaze513 | 0:2aa4daee328e | 84 | (this->*InvokePointer)(); |
Blaze513 | 0:2aa4daee328e | 85 | } |
Blaze513 | 0:2aa4daee328e | 86 | } |
Blaze513 | 1:2418b141f599 | 87 | //This overload of the () operator allows shorthand invokation of |
Blaze513 | 1:2418b141f599 | 88 | //a GenericFunctionHandler object's function pointer such that it |
Blaze513 | 1:2418b141f599 | 89 | //appears as a standard function call. For example: |
Blaze513 | 1:2418b141f599 | 90 | //GenericFunctionHandlerObject(); does the same thing as |
Blaze513 | 1:2418b141f599 | 91 | //GenericFunctionHandlerObject.Call(); |
Blaze513 | 0:2aa4daee328e | 92 | }; |
Blaze513 | 0:2aa4daee328e | 93 | |
Blaze513 | 0:2aa4daee328e | 94 | #endif |