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