printer

Dependents:   Good_Serial_HelloWorld_Mbed

Fork of mbed by gokmen ascioglu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FunctionPointer.h Source File

FunctionPointer.h

00001 /* mbed Microcontroller Library - FunctionPointer
00002  * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
00003  */ 
00004  
00005 #ifndef MBED_FUNCTIONPOINTER_H
00006 #define MBED_FUNCTIONPOINTER_H
00007 
00008 #include <string.h>
00009 
00010 namespace mbed { 
00011 
00012 /* Class FunctionPointer
00013  *  A class for storing and calling a pointer to a static or member void function
00014  */
00015 class FunctionPointer {
00016 
00017 public:
00018 
00019     /* Constructor FunctionPointer
00020      *  Create a FunctionPointer, attaching a static function
00021      * 
00022      * Variables
00023      *  function - The void static function to attach (default is none)
00024      */
00025     FunctionPointer(void (*function)(void) = 0);
00026 
00027     /* Constructor FunctionPointer
00028      *  Create a FunctionPointer, attaching a member function
00029      * 
00030      * Variables
00031      *  object - The object pointer to invoke the member function on (i.e. the this pointer)
00032      *  function - The address of the void member function to attach 
00033      */
00034     template<typename T>    
00035     FunctionPointer(T *object, void (T::*member)(void)) {
00036         attach(object, member);
00037     }
00038 
00039     /* Function attach
00040      *  Attach a static function
00041      * 
00042      * Variables
00043      *  function - The void static function to attach (default is none)
00044      */
00045     void attach(void (*function)(void) = 0);
00046     
00047     /* Function attach
00048      *  Attach a member function
00049      * 
00050      * Variables
00051      *  object - The object pointer to invoke the member function on (i.e. the this pointer)
00052      *  function - The address of the void member function to attach 
00053      */
00054     template<typename T>
00055     void attach(T *object, void (T::*member)(void)) {
00056         _object = static_cast<void*>(object);
00057         memcpy(_member, (char*)&member, sizeof(member));
00058         _membercaller = &FunctionPointer::membercaller<T>;
00059         _function = 0;
00060     }
00061 
00062     /* Function call
00063      *  Call the attached static or member function
00064      */     
00065     void call();
00066         
00067 private:
00068 
00069     template<typename T>
00070     static void membercaller(void *object, char *member) {  
00071         T* o = static_cast<T*>(object);
00072         void (T::*m)(void);
00073         memcpy((char*)&m, member, sizeof(m));
00074         (o->*m)();
00075     }
00076     
00077     void (*_function)(void);                // static function pointer - 0 if none attached
00078     void *_object;                          // object this pointer - 0 if none attached
00079     char _member[16];                       // raw member function pointer storage - converted back by registered _membercaller
00080     void (*_membercaller)(void*, char*);    // registered membercaller function to convert back and call _member on _object
00081     
00082 };
00083 
00084 } // namespace mbed
00085 
00086 #endif