35bb8da0069db6bb1de95aa87bb2d04b5531806e
1 /* mbed Microcontroller Library - FunctionPointer
2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
5 #ifndef MBED_FUNCTIONPOINTER_H
6 #define MBED_FUNCTIONPOINTER_H
12 /* Class FunctionPointer
13 * A class for storing and calling a pointer to a static or member void function
15 class FunctionPointer
{
19 /* Constructor FunctionPointer
20 * Create a FunctionPointer, attaching a static function
23 * function - The void static function to attach (default is none)
25 FunctionPointer(void (*function
)(void) = 0);
27 /* Constructor FunctionPointer
28 * Create a FunctionPointer, attaching a member function
31 * object - The object pointer to invoke the member function on (i.e. the this pointer)
32 * function - The address of the void member function to attach
35 FunctionPointer(T
*object
, void (T::*member
)(void)) {
36 attach(object
, member
);
40 * Attach a static function
43 * function - The void static function to attach (default is none)
45 void attach(void (*function
)(void) = 0);
48 * Attach a member function
51 * object - The object pointer to invoke the member function on (i.e. the this pointer)
52 * function - The address of the void member function to attach
55 void attach(T
*object
, void (T::*member
)(void)) {
56 _object
= static_cast<void*>(object
);
57 memcpy(_member
, (char*)&member
, sizeof(member
));
58 _membercaller
= &FunctionPointer::membercaller
<T
>;
63 * Call the attached static or member function
70 static void membercaller(void *object
, char *member
) {
71 T
* o
= static_cast<T
*>(object
);
73 memcpy((char*)&m
, member
, sizeof(m
));
77 void (*_function
)(void); // static function pointer - 0 if none attached
78 void *_object
; // object this pointer - 0 if none attached
79 char _member
[16]; // raw member function pointer storage - converted back by registered _membercaller
80 void (*_membercaller
)(void*, char*); // registered membercaller function to convert back and call _member on _object