mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 9:663789d7729f 1 /* mbed Microcontroller Library
emilmont 9:663789d7729f 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 9:663789d7729f 3 *
emilmont 9:663789d7729f 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 9:663789d7729f 5 * you may not use this file except in compliance with the License.
emilmont 9:663789d7729f 6 * You may obtain a copy of the License at
emilmont 9:663789d7729f 7 *
emilmont 9:663789d7729f 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 9:663789d7729f 9 *
emilmont 9:663789d7729f 10 * Unless required by applicable law or agreed to in writing, software
emilmont 9:663789d7729f 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 9:663789d7729f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 9:663789d7729f 13 * See the License for the specific language governing permissions and
emilmont 9:663789d7729f 14 * limitations under the License.
emilmont 7:73c5efe92a6c 15 */
emilmont 0:8024c367e29f 16 #ifndef MBED_FUNCTIONPOINTER_H
emilmont 0:8024c367e29f 17 #define MBED_FUNCTIONPOINTER_H
emilmont 0:8024c367e29f 18
emilmont 0:8024c367e29f 19 #include <string.h>
emilmont 0:8024c367e29f 20
emilmont 9:663789d7729f 21 namespace mbed {
emilmont 0:8024c367e29f 22
emilmont 8:c14af7958ef5 23 /** A class for storing and calling a pointer to a static or member void function
emilmont 0:8024c367e29f 24 */
emilmont 0:8024c367e29f 25 class FunctionPointer {
emilmont 0:8024c367e29f 26 public:
emilmont 8:c14af7958ef5 27
emilmont 8:c14af7958ef5 28 /** Create a FunctionPointer, attaching a static function
emilmont 9:663789d7729f 29 *
emilmont 8:c14af7958ef5 30 * @param function The void static function to attach (default is none)
emilmont 0:8024c367e29f 31 */
emilmont 0:8024c367e29f 32 FunctionPointer(void (*function)(void) = 0);
emilmont 0:8024c367e29f 33
emilmont 8:c14af7958ef5 34 /** Create a FunctionPointer, attaching a member function
emilmont 9:663789d7729f 35 *
emilmont 8:c14af7958ef5 36 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 9:663789d7729f 37 * @param function The address of the void member function to attach
emilmont 0:8024c367e29f 38 */
emilmont 7:73c5efe92a6c 39 template<typename T>
emilmont 0:8024c367e29f 40 FunctionPointer(T *object, void (T::*member)(void)) {
emilmont 0:8024c367e29f 41 attach(object, member);
emilmont 0:8024c367e29f 42 }
emilmont 9:663789d7729f 43
emilmont 8:c14af7958ef5 44 /** Attach a static function
emilmont 9:663789d7729f 45 *
emilmont 8:c14af7958ef5 46 * @param function The void static function to attach (default is none)
emilmont 0:8024c367e29f 47 */
emilmont 0:8024c367e29f 48 void attach(void (*function)(void) = 0);
emilmont 9:663789d7729f 49
emilmont 8:c14af7958ef5 50 /** Attach a member function
emilmont 9:663789d7729f 51 *
emilmont 8:c14af7958ef5 52 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
emilmont 9:663789d7729f 53 * @param function The address of the void member function to attach
emilmont 0:8024c367e29f 54 */
emilmont 0:8024c367e29f 55 template<typename T>
emilmont 0:8024c367e29f 56 void attach(T *object, void (T::*member)(void)) {
emilmont 0:8024c367e29f 57 _object = static_cast<void*>(object);
emilmont 0:8024c367e29f 58 memcpy(_member, (char*)&member, sizeof(member));
emilmont 0:8024c367e29f 59 _membercaller = &FunctionPointer::membercaller<T>;
emilmont 0:8024c367e29f 60 _function = 0;
emilmont 0:8024c367e29f 61 }
emilmont 9:663789d7729f 62
emilmont 8:c14af7958ef5 63 /** Call the attached static or member function
emilmont 7:73c5efe92a6c 64 */
emilmont 0:8024c367e29f 65 void call();
emilmont 7:73c5efe92a6c 66
emilmont 0:8024c367e29f 67 private:
emilmont 0:8024c367e29f 68 template<typename T>
emilmont 7:73c5efe92a6c 69 static void membercaller(void *object, char *member) {
emilmont 0:8024c367e29f 70 T* o = static_cast<T*>(object);
emilmont 0:8024c367e29f 71 void (T::*m)(void);
emilmont 0:8024c367e29f 72 memcpy((char*)&m, member, sizeof(m));
emilmont 0:8024c367e29f 73 (o->*m)();
emilmont 0:8024c367e29f 74 }
emilmont 9:663789d7729f 75
emilmont 0:8024c367e29f 76 void (*_function)(void); // static function pointer - 0 if none attached
emilmont 0:8024c367e29f 77 void *_object; // object this pointer - 0 if none attached
emilmont 0:8024c367e29f 78 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
emilmont 0:8024c367e29f 79 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
emilmont 0:8024c367e29f 80 };
emilmont 0:8024c367e29f 81
emilmont 0:8024c367e29f 82 } // namespace mbed
emilmont 0:8024c367e29f 83
emilmont 0:8024c367e29f 84 #endif