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