Lab 1 Program C
Fork of mbed by
FunctionPointer.h@44:1c5f591fce58, 2015-09-29 (annotated)
- Committer:
- mattsims12
- Date:
- Tue Sep 29 03:04:58 2015 +0000
- Revision:
- 44:1c5f591fce58
- Parent:
- 43:aff670d0d510
Lab 1 Program C
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 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 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 | 18:b3c9f16cbb96 | 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 | |
screamer | 43:aff670d0d510 | 12 | /** A class for storing and calling a pointer to a static or member void function |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 13 | */ |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 14 | class FunctionPointer { |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 15 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 16 | public: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 17 | |
screamer | 43:aff670d0d510 | 18 | /** Create a FunctionPointer, attaching a static function |
screamer | 43:aff670d0d510 | 19 | * |
screamer | 43:aff670d0d510 | 20 | * @param function The void static function to attach (default is none) |
screamer | 43:aff670d0d510 | 21 | */ |
screamer | 43:aff670d0d510 | 22 | FunctionPointer(void (*function)(void) = 0); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 23 | |
screamer | 43:aff670d0d510 | 24 | /** Create a FunctionPointer, attaching a member function |
screamer | 43:aff670d0d510 | 25 | * |
screamer | 43:aff670d0d510 | 26 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
screamer | 43:aff670d0d510 | 27 | * @param function The address of the void member function to attach |
screamer | 43:aff670d0d510 | 28 | */ |
screamer | 43:aff670d0d510 | 29 | template<typename T> |
screamer | 43:aff670d0d510 | 30 | FunctionPointer(T *object, void (T::*member)(void)) { |
screamer | 43:aff670d0d510 | 31 | attach(object, member); |
screamer | 43:aff670d0d510 | 32 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 33 | |
screamer | 43:aff670d0d510 | 34 | /** Attach a static function |
screamer | 43:aff670d0d510 | 35 | * |
screamer | 43:aff670d0d510 | 36 | * @param function The void static function to attach (default is none) |
screamer | 43:aff670d0d510 | 37 | */ |
screamer | 43:aff670d0d510 | 38 | void attach(void (*function)(void) = 0); |
screamer | 43:aff670d0d510 | 39 | |
screamer | 43:aff670d0d510 | 40 | /** Attach a member function |
screamer | 43:aff670d0d510 | 41 | * |
screamer | 43:aff670d0d510 | 42 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
screamer | 43:aff670d0d510 | 43 | * @param function The address of the void member function to attach |
screamer | 43:aff670d0d510 | 44 | */ |
screamer | 43:aff670d0d510 | 45 | template<typename T> |
screamer | 43:aff670d0d510 | 46 | void attach(T *object, void (T::*member)(void)) { |
screamer | 43:aff670d0d510 | 47 | _object = static_cast<void*>(object); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 48 | memcpy(_member, (char*)&member, sizeof(member)); |
screamer | 43:aff670d0d510 | 49 | _membercaller = &FunctionPointer::membercaller<T>; |
screamer | 43:aff670d0d510 | 50 | _function = 0; |
screamer | 43:aff670d0d510 | 51 | } |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 52 | |
screamer | 43:aff670d0d510 | 53 | /** Call the attached static or member function |
screamer | 43:aff670d0d510 | 54 | */ |
screamer | 43:aff670d0d510 | 55 | void call(); |
screamer | 43:aff670d0d510 | 56 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 57 | private: |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 58 | |
screamer | 43:aff670d0d510 | 59 | template<typename T> |
screamer | 43:aff670d0d510 | 60 | static void membercaller(void *object, char *member) { |
screamer | 43:aff670d0d510 | 61 | T* o = static_cast<T*>(object); |
screamer | 43:aff670d0d510 | 62 | void (T::*m)(void); |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 63 | memcpy((char*)&m, member, sizeof(m)); |
screamer | 43:aff670d0d510 | 64 | (o->*m)(); |
screamer | 43:aff670d0d510 | 65 | } |
screamer | 43:aff670d0d510 | 66 | |
screamer | 43:aff670d0d510 | 67 | /** Static function pointer - 0 if none attached |
screamer | 43:aff670d0d510 | 68 | */ |
screamer | 43:aff670d0d510 | 69 | void (*_function)(void); |
screamer | 43:aff670d0d510 | 70 | |
screamer | 43:aff670d0d510 | 71 | /** Object this pointer - 0 if none attached |
screamer | 43:aff670d0d510 | 72 | */ |
screamer | 43:aff670d0d510 | 73 | void *_object; |
screamer | 43:aff670d0d510 | 74 | |
screamer | 43:aff670d0d510 | 75 | /** Raw member function pointer storage - converted back by registered _membercaller |
screamer | 43:aff670d0d510 | 76 | */ |
screamer | 43:aff670d0d510 | 77 | char _member[16]; |
screamer | 43:aff670d0d510 | 78 | |
screamer | 43:aff670d0d510 | 79 | /** Registered membercaller function to convert back and call _member on _object |
screamer | 43:aff670d0d510 | 80 | */ |
screamer | 43:aff670d0d510 | 81 | void (*_membercaller)(void*, char*); |
screamer | 43:aff670d0d510 | 82 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 83 | }; |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 84 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 85 | } // namespace mbed |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 86 | |
simon.ford@mbed.co.uk | 9:cf0d45ce28a6 | 87 | #endif |