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