The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
ldyz
Date:
Fri Jul 05 13:16:13 2013 +0000
Revision:
64:75c1708b266b
Parent:
59:0883845fe643
test

Who changed what in which revision?

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