SD card interface
mbed-export/FunctionPointer.h@0:22612ae617a0, 2012-10-08 (annotated)
- Committer:
- lharoon
- Date:
- Mon Oct 08 11:14:07 2012 +0000
- Revision:
- 0:22612ae617a0
1st edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lharoon | 0:22612ae617a0 | 1 | /* mbed Microcontroller Library - FunctionPointer |
lharoon | 0:22612ae617a0 | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
lharoon | 0:22612ae617a0 | 3 | */ |
lharoon | 0:22612ae617a0 | 4 | |
lharoon | 0:22612ae617a0 | 5 | #ifndef MBED_FUNCTIONPOINTER_H |
lharoon | 0:22612ae617a0 | 6 | #define MBED_FUNCTIONPOINTER_H |
lharoon | 0:22612ae617a0 | 7 | |
lharoon | 0:22612ae617a0 | 8 | #include <string.h> |
lharoon | 0:22612ae617a0 | 9 | |
lharoon | 0:22612ae617a0 | 10 | namespace mbed { |
lharoon | 0:22612ae617a0 | 11 | |
lharoon | 0:22612ae617a0 | 12 | /* Class FunctionPointer |
lharoon | 0:22612ae617a0 | 13 | * A class for storing and calling a pointer to a static or member void function |
lharoon | 0:22612ae617a0 | 14 | */ |
lharoon | 0:22612ae617a0 | 15 | class FunctionPointer { |
lharoon | 0:22612ae617a0 | 16 | |
lharoon | 0:22612ae617a0 | 17 | public: |
lharoon | 0:22612ae617a0 | 18 | |
lharoon | 0:22612ae617a0 | 19 | /* Constructor FunctionPointer |
lharoon | 0:22612ae617a0 | 20 | * Create a FunctionPointer, attaching a static function |
lharoon | 0:22612ae617a0 | 21 | * |
lharoon | 0:22612ae617a0 | 22 | * Variables |
lharoon | 0:22612ae617a0 | 23 | * function - The void static function to attach (default is none) |
lharoon | 0:22612ae617a0 | 24 | */ |
lharoon | 0:22612ae617a0 | 25 | FunctionPointer(void (*function)(void) = 0); |
lharoon | 0:22612ae617a0 | 26 | |
lharoon | 0:22612ae617a0 | 27 | /* Constructor FunctionPointer |
lharoon | 0:22612ae617a0 | 28 | * Create a FunctionPointer, attaching a member function |
lharoon | 0:22612ae617a0 | 29 | * |
lharoon | 0:22612ae617a0 | 30 | * Variables |
lharoon | 0:22612ae617a0 | 31 | * object - The object pointer to invoke the member function on (i.e. the this pointer) |
lharoon | 0:22612ae617a0 | 32 | * function - The address of the void member function to attach |
lharoon | 0:22612ae617a0 | 33 | */ |
lharoon | 0:22612ae617a0 | 34 | template<typename T> |
lharoon | 0:22612ae617a0 | 35 | FunctionPointer(T *object, void (T::*member)(void)) { |
lharoon | 0:22612ae617a0 | 36 | attach(object, member); |
lharoon | 0:22612ae617a0 | 37 | } |
lharoon | 0:22612ae617a0 | 38 | |
lharoon | 0:22612ae617a0 | 39 | /* Function attach |
lharoon | 0:22612ae617a0 | 40 | * Attach a static function |
lharoon | 0:22612ae617a0 | 41 | * |
lharoon | 0:22612ae617a0 | 42 | * Variables |
lharoon | 0:22612ae617a0 | 43 | * function - The void static function to attach (default is none) |
lharoon | 0:22612ae617a0 | 44 | */ |
lharoon | 0:22612ae617a0 | 45 | void attach(void (*function)(void) = 0); |
lharoon | 0:22612ae617a0 | 46 | |
lharoon | 0:22612ae617a0 | 47 | /* Function attach |
lharoon | 0:22612ae617a0 | 48 | * Attach a member function |
lharoon | 0:22612ae617a0 | 49 | * |
lharoon | 0:22612ae617a0 | 50 | * Variables |
lharoon | 0:22612ae617a0 | 51 | * object - The object pointer to invoke the member function on (i.e. the this pointer) |
lharoon | 0:22612ae617a0 | 52 | * function - The address of the void member function to attach |
lharoon | 0:22612ae617a0 | 53 | */ |
lharoon | 0:22612ae617a0 | 54 | template<typename T> |
lharoon | 0:22612ae617a0 | 55 | void attach(T *object, void (T::*member)(void)) { |
lharoon | 0:22612ae617a0 | 56 | _object = static_cast<void*>(object); |
lharoon | 0:22612ae617a0 | 57 | memcpy(_member, (char*)&member, sizeof(member)); |
lharoon | 0:22612ae617a0 | 58 | _membercaller = &FunctionPointer::membercaller<T>; |
lharoon | 0:22612ae617a0 | 59 | _function = 0; |
lharoon | 0:22612ae617a0 | 60 | } |
lharoon | 0:22612ae617a0 | 61 | |
lharoon | 0:22612ae617a0 | 62 | /* Function call |
lharoon | 0:22612ae617a0 | 63 | * Call the attached static or member function |
lharoon | 0:22612ae617a0 | 64 | */ |
lharoon | 0:22612ae617a0 | 65 | void call(); |
lharoon | 0:22612ae617a0 | 66 | |
lharoon | 0:22612ae617a0 | 67 | private: |
lharoon | 0:22612ae617a0 | 68 | |
lharoon | 0:22612ae617a0 | 69 | template<typename T> |
lharoon | 0:22612ae617a0 | 70 | static void membercaller(void *object, char *member) { |
lharoon | 0:22612ae617a0 | 71 | T* o = static_cast<T*>(object); |
lharoon | 0:22612ae617a0 | 72 | void (T::*m)(void); |
lharoon | 0:22612ae617a0 | 73 | memcpy((char*)&m, member, sizeof(m)); |
lharoon | 0:22612ae617a0 | 74 | (o->*m)(); |
lharoon | 0:22612ae617a0 | 75 | } |
lharoon | 0:22612ae617a0 | 76 | |
lharoon | 0:22612ae617a0 | 77 | void (*_function)(void); // static function pointer - 0 if none attached |
lharoon | 0:22612ae617a0 | 78 | void *_object; // object this pointer - 0 if none attached |
lharoon | 0:22612ae617a0 | 79 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
lharoon | 0:22612ae617a0 | 80 | void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
lharoon | 0:22612ae617a0 | 81 | |
lharoon | 0:22612ae617a0 | 82 | }; |
lharoon | 0:22612ae617a0 | 83 | |
lharoon | 0:22612ae617a0 | 84 | } // namespace mbed |
lharoon | 0:22612ae617a0 | 85 | |
lharoon | 0:22612ae617a0 | 86 | #endif |