printer

Dependents:   Good_Serial_HelloWorld_Mbed

Fork of mbed by gokmen ascioglu

Committer:
redplam
Date:
Mon Apr 14 14:37:10 2014 +0000
Revision:
1:0b0a5a73f998
Parent:
0:a8fa94490a0a
printer

Who changed what in which revision?

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