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