mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Tue Oct 23 09:20:18 2012 +0000
Revision:
7:73c5efe92a6c
Parent:
0:8024c367e29f
Child:
8:c14af7958ef5
Make the C++ library completely TARGET independent.; Implement "gpio_irq_api" and "port_api" to KL25Z.

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 0:8024c367e29f 11 /* Class FunctionPointer
emilmont 0:8024c367e29f 12 * A class for storing and calling a pointer to a static or member void function
emilmont 0:8024c367e29f 13 */
emilmont 0:8024c367e29f 14 class FunctionPointer {
emilmont 0:8024c367e29f 15 public:
emilmont 0:8024c367e29f 16 /* Constructor FunctionPointer
emilmont 0:8024c367e29f 17 * Create a FunctionPointer, attaching a static function
emilmont 0:8024c367e29f 18 *
emilmont 0:8024c367e29f 19 * Variables
emilmont 0:8024c367e29f 20 * function - The void static function to attach (default is none)
emilmont 0:8024c367e29f 21 */
emilmont 0:8024c367e29f 22 FunctionPointer(void (*function)(void) = 0);
emilmont 0:8024c367e29f 23
emilmont 0:8024c367e29f 24 /* Constructor FunctionPointer
emilmont 0:8024c367e29f 25 * Create a FunctionPointer, attaching a member function
emilmont 0:8024c367e29f 26 *
emilmont 0:8024c367e29f 27 * Variables
emilmont 0:8024c367e29f 28 * object - The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 0:8024c367e29f 29 * function - The address of the void member function to attach
emilmont 0:8024c367e29f 30 */
emilmont 7:73c5efe92a6c 31 template<typename T>
emilmont 0:8024c367e29f 32 FunctionPointer(T *object, void (T::*member)(void)) {
emilmont 0:8024c367e29f 33 attach(object, member);
emilmont 0:8024c367e29f 34 }
emilmont 7:73c5efe92a6c 35
emilmont 0:8024c367e29f 36 /* Function attach
emilmont 0:8024c367e29f 37 * Attach a static function
emilmont 0:8024c367e29f 38 *
emilmont 0:8024c367e29f 39 * Variables
emilmont 0:8024c367e29f 40 * function - The void static function to attach (default is none)
emilmont 0:8024c367e29f 41 */
emilmont 0:8024c367e29f 42 void attach(void (*function)(void) = 0);
emilmont 0:8024c367e29f 43
emilmont 0:8024c367e29f 44 /* Function attach
emilmont 0:8024c367e29f 45 * Attach a member function
emilmont 0:8024c367e29f 46 *
emilmont 0:8024c367e29f 47 * Variables
emilmont 0:8024c367e29f 48 * object - The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 0:8024c367e29f 49 * function - The address of the void member function to attach
emilmont 0:8024c367e29f 50 */
emilmont 0:8024c367e29f 51 template<typename T>
emilmont 0:8024c367e29f 52 void attach(T *object, void (T::*member)(void)) {
emilmont 0:8024c367e29f 53 _object = static_cast<void*>(object);
emilmont 0:8024c367e29f 54 memcpy(_member, (char*)&member, sizeof(member));
emilmont 0:8024c367e29f 55 _membercaller = &FunctionPointer::membercaller<T>;
emilmont 0:8024c367e29f 56 _function = 0;
emilmont 0:8024c367e29f 57 }
emilmont 7:73c5efe92a6c 58
emilmont 0:8024c367e29f 59 /* Function call
emilmont 7:73c5efe92a6c 60 * Call the attached static or member function (safe to be called even if there is no handler)
emilmont 7:73c5efe92a6c 61 */
emilmont 0:8024c367e29f 62 void call();
emilmont 7:73c5efe92a6c 63
emilmont 0:8024c367e29f 64 private:
emilmont 0:8024c367e29f 65 template<typename T>
emilmont 7:73c5efe92a6c 66 static void membercaller(void *object, char *member) {
emilmont 0:8024c367e29f 67 T* o = static_cast<T*>(object);
emilmont 0:8024c367e29f 68 void (T::*m)(void);
emilmont 0:8024c367e29f 69 memcpy((char*)&m, member, sizeof(m));
emilmont 0:8024c367e29f 70 (o->*m)();
emilmont 0:8024c367e29f 71 }
emilmont 0:8024c367e29f 72
emilmont 0:8024c367e29f 73 void (*_function)(void); // static function pointer - 0 if none attached
emilmont 0:8024c367e29f 74 void *_object; // object this pointer - 0 if none attached
emilmont 0:8024c367e29f 75 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
emilmont 0:8024c367e29f 76 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
emilmont 0:8024c367e29f 77
emilmont 0:8024c367e29f 78 };
emilmont 0:8024c367e29f 79
emilmont 0:8024c367e29f 80 } // namespace mbed
emilmont 0:8024c367e29f 81
emilmont 0:8024c367e29f 82 #endif