mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Fri Nov 09 11:33:53 2012 +0000
Revision:
8:c14af7958ef5
Parent:
7:73c5efe92a6c
Child:
9:663789d7729f
SPI driver; ADC driver; DAC driver; microlib support; general bugfixing

Who changed what in which revision?

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