Fork of the official mbed C/C++ SDK provides the software platform and libraries to build your applications. The fork has the documentation converted to Doxygen format

Dependents:   NervousPuppySprintOne NervousPuppySprint2602 Robot WarehouseBot1 ... more

Fork of mbed by mbed official

Committer:
rolf.meyer@arm.com
Date:
Fri Aug 28 12:10:11 2009 +0000
Revision:
11:1c1ebd0324fa
Parent:
9:cf0d45ce28a6
Child:
18:b3c9f16cbb96
A shiny new version

Who changed what in which revision?

UserRevisionLine numberNew 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 * sford
rolf.meyer@arm.com 11:1c1ebd0324fa 4 */
rolf.meyer@arm.com 11:1c1ebd0324fa 5
simon.ford@mbed.co.uk 9:cf0d45ce28a6 6 #ifndef MBED_FUNCTIONPOINTER_H
simon.ford@mbed.co.uk 9:cf0d45ce28a6 7 #define MBED_FUNCTIONPOINTER_H
simon.ford@mbed.co.uk 9:cf0d45ce28a6 8
simon.ford@mbed.co.uk 9:cf0d45ce28a6 9 #include "string.h"
simon.ford@mbed.co.uk 9:cf0d45ce28a6 10
simon.ford@mbed.co.uk 9:cf0d45ce28a6 11 namespace mbed {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 12
simon.ford@mbed.co.uk 9:cf0d45ce28a6 13 /* Class FunctionPointer
simon.ford@mbed.co.uk 9:cf0d45ce28a6 14 * A class for storing and calling a pointer to a static or member void function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 15 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 16 class FunctionPointer {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 17
simon.ford@mbed.co.uk 9:cf0d45ce28a6 18 public:
simon.ford@mbed.co.uk 9:cf0d45ce28a6 19
simon.ford@mbed.co.uk 9:cf0d45ce28a6 20 /* Constructor FunctionPointer
simon.ford@mbed.co.uk 9:cf0d45ce28a6 21 * Create a FunctionPointer, attaching a static function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 22 *
simon.ford@mbed.co.uk 9:cf0d45ce28a6 23 * Variables
simon.ford@mbed.co.uk 9:cf0d45ce28a6 24 * function - The void static function to attach (default is none)
simon.ford@mbed.co.uk 9:cf0d45ce28a6 25 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 26 FunctionPointer(void (*function)(void) = 0);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 27
simon.ford@mbed.co.uk 9:cf0d45ce28a6 28 /* Constructor FunctionPointer
simon.ford@mbed.co.uk 9:cf0d45ce28a6 29 * Create a FunctionPointer, attaching a member function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 30 *
simon.ford@mbed.co.uk 9:cf0d45ce28a6 31 * Variables
simon.ford@mbed.co.uk 9:cf0d45ce28a6 32 * object - The object pointer to invoke the member function on (i.e. the this pointer)
simon.ford@mbed.co.uk 9:cf0d45ce28a6 33 * function - The address of the void member function to attach
simon.ford@mbed.co.uk 9:cf0d45ce28a6 34 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 35 template<typename T>
simon.ford@mbed.co.uk 9:cf0d45ce28a6 36 FunctionPointer(T *object, void (T::*member)(void)) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 37 attach(object, member);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 38 }
simon.ford@mbed.co.uk 9:cf0d45ce28a6 39
simon.ford@mbed.co.uk 9:cf0d45ce28a6 40 /* Function attach
simon.ford@mbed.co.uk 9:cf0d45ce28a6 41 * Attach a static function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 42 *
simon.ford@mbed.co.uk 9:cf0d45ce28a6 43 * Variables
simon.ford@mbed.co.uk 9:cf0d45ce28a6 44 * function - The void static function to attach (default is none)
simon.ford@mbed.co.uk 9:cf0d45ce28a6 45 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 46 void attach(void (*function)(void) = 0);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 47
simon.ford@mbed.co.uk 9:cf0d45ce28a6 48 /* Function attach
simon.ford@mbed.co.uk 9:cf0d45ce28a6 49 * Attach a member function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 50 *
simon.ford@mbed.co.uk 9:cf0d45ce28a6 51 * Variables
simon.ford@mbed.co.uk 9:cf0d45ce28a6 52 * object - The object pointer to invoke the member function on (i.e. the this pointer)
simon.ford@mbed.co.uk 9:cf0d45ce28a6 53 * function - The address of the void member function to attach
simon.ford@mbed.co.uk 9:cf0d45ce28a6 54 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 55 template<typename T>
simon.ford@mbed.co.uk 9:cf0d45ce28a6 56 void attach(T *object, void (T::*member)(void)) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 57 _object = static_cast<void*>(object);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 58 memcpy(_member, (char*)&member, sizeof(member));
simon.ford@mbed.co.uk 9:cf0d45ce28a6 59 _membercaller = &FunctionPointer::membercaller<T>;
simon.ford@mbed.co.uk 9:cf0d45ce28a6 60 _function = 0;
simon.ford@mbed.co.uk 9:cf0d45ce28a6 61 }
simon.ford@mbed.co.uk 9:cf0d45ce28a6 62
simon.ford@mbed.co.uk 9:cf0d45ce28a6 63 /* Function call
simon.ford@mbed.co.uk 9:cf0d45ce28a6 64 * Call the attached static or member function
simon.ford@mbed.co.uk 9:cf0d45ce28a6 65 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 66 void call();
simon.ford@mbed.co.uk 9:cf0d45ce28a6 67
simon.ford@mbed.co.uk 9:cf0d45ce28a6 68 private:
simon.ford@mbed.co.uk 9:cf0d45ce28a6 69
simon.ford@mbed.co.uk 9:cf0d45ce28a6 70 template<typename T>
simon.ford@mbed.co.uk 9:cf0d45ce28a6 71 static void membercaller(void *object, char *member) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 72 T* o = static_cast<T*>(object);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 73 void (T::*m)(void);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 74 memcpy((char*)&m, member, sizeof(m));
simon.ford@mbed.co.uk 9:cf0d45ce28a6 75 (o->*m)();
simon.ford@mbed.co.uk 9:cf0d45ce28a6 76 }
simon.ford@mbed.co.uk 9:cf0d45ce28a6 77
simon.ford@mbed.co.uk 9:cf0d45ce28a6 78 void (*_function)(void); // static function pointer - 0 if none attached
simon.ford@mbed.co.uk 9:cf0d45ce28a6 79 void *_object; // object this pointer - 0 if none attached
simon.ford@mbed.co.uk 9:cf0d45ce28a6 80 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
simon.ford@mbed.co.uk 9:cf0d45ce28a6 81 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
simon.ford@mbed.co.uk 9:cf0d45ce28a6 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