Rene Greiner / Mbed 2 deprecated FunctionPointers

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FunctionPointers.h Source File

FunctionPointers.h

00001 /* FunctionPointers.h
00002  * Modified version of Andy Kirkhams FPointer.h
00003  * http://mbed.org/users/AjK/libraries/FPointer/ll7nhv
00004  * Advantages:
00005  * - reduced size of from 12 -> 8 byte by usage of a unnamed union
00006  * - speedup function call by removing one error check
00007  * - replaced call() function by () operator
00008  * - templated: you can define the return and argument types
00009  * - implementations for 1 and 2 arguments, easy extendable to more arguments
00010  * Disadvantage:
00011  * - do NOT call operator() without an attached callback function
00012  */ 
00013   
00014 #ifndef FUNCTIONPOINTERS_H 
00015 #define FUNCTIONPOINTERS_H 
00016 
00017 template<class Tret=void>
00018 class FPtrRetT {
00019 public:
00020     FPtrRetT() : obj_callback( NULL), c_callback( NULL)  {
00021     }
00022 
00023     void attach(Tret (*function)()) {
00024         c_callback = function;
00025     }
00026 
00027     template<class T>
00028     void attach(T& item, Tret (T::*method)()) {
00029         obj_callback    = reinterpret_cast<FPtrDummy*>(&item);
00030         method_callback = reinterpret_cast<Tret (FPtrDummy::*)()>(method);
00031     }
00032 
00033     Tret operator()() const {
00034         return obj_callback ? (obj_callback->*method_callback)() : (*c_callback)();
00035     }
00036 
00037 private:
00038     class FPtrDummy;
00039 
00040     FPtrDummy  *obj_callback;
00041     union {
00042         Tret (*c_callback)();
00043         Tret (FPtrDummy::*method_callback)();
00044     };
00045 };
00046 
00047 template<class Tret, class Targ1>
00048 class FPtr1ArgT {
00049 public:
00050     FPtr1ArgT() : obj_callback( NULL), c_callback( NULL)  {
00051     }
00052 
00053     void attach(Tret (*function)(Targ1)) {
00054         c_callback = function;
00055     }
00056 
00057     template<class T>
00058     void attach(T& item, Tret (T::*method)(Targ1)) {
00059         obj_callback    = reinterpret_cast<FPtrDummy*>(&item);
00060         method_callback = reinterpret_cast<Tret (FPtrDummy::*)(Targ1)>(method);
00061     }
00062 
00063     Tret operator()(Targ1 arg1) const {
00064         return obj_callback ? (obj_callback->*method_callback)(arg1) : (*c_callback)(arg1);
00065     }
00066 
00067 private:
00068     class FPtrDummy;
00069 
00070     FPtrDummy  *obj_callback;
00071     union {
00072         Tret (*c_callback)(Targ1);
00073         Tret (FPtrDummy::*method_callback)(Targ1);
00074     };
00075 };
00076 
00077 template<class Tret, class Targ1, class Targ2>
00078 class FPtr2ArgT {
00079 public:
00080     FPtr2ArgT() : obj_callback( NULL), c_callback( NULL)  {
00081     }
00082 
00083     void attach(Tret (*function)(Targ1,Targ2)) {
00084         c_callback = function;
00085     }
00086 
00087     template<class T>
00088     void attach(T& item, Tret (T::*method)(Targ1,Targ2)) {
00089         obj_callback    = reinterpret_cast<FPointerDummy*>(&item);
00090         method_callback = reinterpret_cast<Tret (FPtrDummy::*)(Targ1,Targ2)>(method);
00091     }
00092 
00093     Tret operator()(Targ1 arg1, Targ2 arg2) const {
00094         return obj_callback ? (obj_callback->*method_callback)(arg1, arg2) : (*c_callback)(arg1, arg2);
00095     }
00096 
00097 private:
00098     class FPtrDummy;
00099 
00100     FPtrDummy  *obj_callback;
00101     union {
00102         Tret (*c_callback)(Targ1,Targ2);
00103         Tret (FPtrDummy::*method_callback)(Targ1,Targ2);
00104     };
00105 };
00106 
00107 #endif