Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
GenericFunctionHandler.h
00001 //mbed Microcontroller Library 00002 //Class designed to store and invoke function pointers and method pointers 00003 //Copyright 2011 00004 //Thomas Hamilton 00005 00006 #ifndef GenericFunctionHandlerLibrary 00007 #define GenericFunctionHandlerLibrary 00008 00009 class GenericFunctionHandler 00010 { 00011 private: 00012 bool FunctionPointerType; 00013 void (*FunctionPointer)(); 00014 unsigned char* ObjectPointer; 00015 unsigned char* MethodPointer; 00016 void (GenericFunctionHandler::*InvokePointer)(); 00017 00018 template<class Class> 00019 void InvokeMethod() 00020 { 00021 (*(Class**)ObjectPointer->**(void (Class::**)())MethodPointer)(); 00022 } 00023 00024 public: 00025 GenericFunctionHandler(void (*FunctionAddress)()) 00026 : FunctionPointerType(0), FunctionPointer(FunctionAddress) 00027 { 00028 } 00029 //This constructor accepts a function address and constructs an 00030 //instance of this class for handling a static function. The 00031 //constructor prepares the function to be called when the the Call 00032 //method of this class object is called. 00033 template<class Class> 00034 GenericFunctionHandler( 00035 Class* ObjectAddress, void (Class::*MethodAddress)()) : 00036 FunctionPointerType(1), 00037 ObjectPointer(new unsigned char(sizeof(ObjectAddress))), 00038 MethodPointer(new unsigned char(sizeof(MethodAddress))), 00039 InvokePointer(&GenericFunctionHandler::InvokeMethod<Class>) 00040 { 00041 memcpy(ObjectPointer, (unsigned char*)&ObjectAddress, 00042 sizeof(ObjectAddress)); 00043 memcpy(MethodPointer, (unsigned char*)&MethodAddress, 00044 sizeof(MethodAddress)); 00045 } 00046 //This constructor template accepts a pointer to a class object 00047 //and a pointer to a method of that class and constructs an 00048 //instance of this class for handling a method of that class. The 00049 //constructor constructor prepares the method to be called on the 00050 //class object when the Call method of this class object is 00051 //called. 00052 00053 ~GenericFunctionHandler() 00054 { 00055 if (FunctionPointerType) 00056 { 00057 delete[] ObjectPointer; 00058 delete[] MethodPointer; 00059 } 00060 } 00061 00062 void Call() 00063 { 00064 if (!FunctionPointerType) 00065 { 00066 (*FunctionPointer)(); 00067 } 00068 else 00069 { 00070 (this->*InvokePointer)(); 00071 } 00072 } 00073 //This method invokes the function or method pointer that was 00074 //passed into the constructor of this class. 00075 00076 void operator ()() 00077 { 00078 if (!FunctionPointerType) 00079 { 00080 (*FunctionPointer)(); 00081 } 00082 else 00083 { 00084 (this->*InvokePointer)(); 00085 } 00086 } 00087 //This overload of the () operator allows shorthand invokation of 00088 //a GenericFunctionHandler object's function pointer such that it 00089 //appears as a standard function call. For example: 00090 //GenericFunctionHandlerObject(); does the same thing as 00091 //GenericFunctionHandlerObject.Call(); 00092 }; 00093 00094 #endif
Generated on Sun Aug 7 2022 11:06:44 by
1.7.2