customized mbed library sources for nrf51822
Dependents: Grove_Node Potentiometer BLE_Beacon I2C_Scanner
api/FunctionPointer.h@0:700cadd8b708, 2014-11-04 (annotated)
- Committer:
- yihui
- Date:
- Tue Nov 04 07:38:53 2014 +0000
- Revision:
- 0:700cadd8b708
customized mbed-src library for nrf51822
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:700cadd8b708 | 1 | /* mbed Microcontroller Library |
yihui | 0:700cadd8b708 | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 0:700cadd8b708 | 3 | * |
yihui | 0:700cadd8b708 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 0:700cadd8b708 | 5 | * you may not use this file except in compliance with the License. |
yihui | 0:700cadd8b708 | 6 | * You may obtain a copy of the License at |
yihui | 0:700cadd8b708 | 7 | * |
yihui | 0:700cadd8b708 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 0:700cadd8b708 | 9 | * |
yihui | 0:700cadd8b708 | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 0:700cadd8b708 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 0:700cadd8b708 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 0:700cadd8b708 | 13 | * See the License for the specific language governing permissions and |
yihui | 0:700cadd8b708 | 14 | * limitations under the License. |
yihui | 0:700cadd8b708 | 15 | */ |
yihui | 0:700cadd8b708 | 16 | #ifndef MBED_FUNCTIONPOINTER_H |
yihui | 0:700cadd8b708 | 17 | #define MBED_FUNCTIONPOINTER_H |
yihui | 0:700cadd8b708 | 18 | |
yihui | 0:700cadd8b708 | 19 | #include <string.h> |
yihui | 0:700cadd8b708 | 20 | |
yihui | 0:700cadd8b708 | 21 | namespace mbed { |
yihui | 0:700cadd8b708 | 22 | |
yihui | 0:700cadd8b708 | 23 | typedef void (*pvoidf_t)(void); |
yihui | 0:700cadd8b708 | 24 | |
yihui | 0:700cadd8b708 | 25 | /** A class for storing and calling a pointer to a static or member void function |
yihui | 0:700cadd8b708 | 26 | */ |
yihui | 0:700cadd8b708 | 27 | class FunctionPointer { |
yihui | 0:700cadd8b708 | 28 | public: |
yihui | 0:700cadd8b708 | 29 | |
yihui | 0:700cadd8b708 | 30 | /** Create a FunctionPointer, attaching a static function |
yihui | 0:700cadd8b708 | 31 | * |
yihui | 0:700cadd8b708 | 32 | * @param function The void static function to attach (default is none) |
yihui | 0:700cadd8b708 | 33 | */ |
yihui | 0:700cadd8b708 | 34 | FunctionPointer(void (*function)(void) = 0); |
yihui | 0:700cadd8b708 | 35 | |
yihui | 0:700cadd8b708 | 36 | /** Create a FunctionPointer, attaching a member function |
yihui | 0:700cadd8b708 | 37 | * |
yihui | 0:700cadd8b708 | 38 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
yihui | 0:700cadd8b708 | 39 | * @param function The address of the void member function to attach |
yihui | 0:700cadd8b708 | 40 | */ |
yihui | 0:700cadd8b708 | 41 | template<typename T> |
yihui | 0:700cadd8b708 | 42 | FunctionPointer(T *object, void (T::*member)(void)) { |
yihui | 0:700cadd8b708 | 43 | attach(object, member); |
yihui | 0:700cadd8b708 | 44 | } |
yihui | 0:700cadd8b708 | 45 | |
yihui | 0:700cadd8b708 | 46 | /** Attach a static function |
yihui | 0:700cadd8b708 | 47 | * |
yihui | 0:700cadd8b708 | 48 | * @param function The void static function to attach (default is none) |
yihui | 0:700cadd8b708 | 49 | */ |
yihui | 0:700cadd8b708 | 50 | void attach(void (*function)(void) = 0); |
yihui | 0:700cadd8b708 | 51 | |
yihui | 0:700cadd8b708 | 52 | /** Attach a member function |
yihui | 0:700cadd8b708 | 53 | * |
yihui | 0:700cadd8b708 | 54 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
yihui | 0:700cadd8b708 | 55 | * @param function The address of the void member function to attach |
yihui | 0:700cadd8b708 | 56 | */ |
yihui | 0:700cadd8b708 | 57 | template<typename T> |
yihui | 0:700cadd8b708 | 58 | void attach(T *object, void (T::*member)(void)) { |
yihui | 0:700cadd8b708 | 59 | _object = static_cast<void*>(object); |
yihui | 0:700cadd8b708 | 60 | memcpy(_member, (char*)&member, sizeof(member)); |
yihui | 0:700cadd8b708 | 61 | _membercaller = &FunctionPointer::membercaller<T>; |
yihui | 0:700cadd8b708 | 62 | _function = 0; |
yihui | 0:700cadd8b708 | 63 | } |
yihui | 0:700cadd8b708 | 64 | |
yihui | 0:700cadd8b708 | 65 | /** Call the attached static or member function |
yihui | 0:700cadd8b708 | 66 | */ |
yihui | 0:700cadd8b708 | 67 | void call(); |
yihui | 0:700cadd8b708 | 68 | |
yihui | 0:700cadd8b708 | 69 | pvoidf_t get_function() const { |
yihui | 0:700cadd8b708 | 70 | return (pvoidf_t)_function; |
yihui | 0:700cadd8b708 | 71 | } |
yihui | 0:700cadd8b708 | 72 | |
yihui | 0:700cadd8b708 | 73 | #ifdef MBED_OPERATORS |
yihui | 0:700cadd8b708 | 74 | void operator ()(void); |
yihui | 0:700cadd8b708 | 75 | #endif |
yihui | 0:700cadd8b708 | 76 | |
yihui | 0:700cadd8b708 | 77 | private: |
yihui | 0:700cadd8b708 | 78 | template<typename T> |
yihui | 0:700cadd8b708 | 79 | static void membercaller(void *object, char *member) { |
yihui | 0:700cadd8b708 | 80 | T* o = static_cast<T*>(object); |
yihui | 0:700cadd8b708 | 81 | void (T::*m)(void); |
yihui | 0:700cadd8b708 | 82 | memcpy((char*)&m, member, sizeof(m)); |
yihui | 0:700cadd8b708 | 83 | (o->*m)(); |
yihui | 0:700cadd8b708 | 84 | } |
yihui | 0:700cadd8b708 | 85 | |
yihui | 0:700cadd8b708 | 86 | void (*_function)(void); // static function pointer - 0 if none attached |
yihui | 0:700cadd8b708 | 87 | void *_object; // object this pointer - 0 if none attached |
yihui | 0:700cadd8b708 | 88 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
yihui | 0:700cadd8b708 | 89 | void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
yihui | 0:700cadd8b708 | 90 | }; |
yihui | 0:700cadd8b708 | 91 | |
yihui | 0:700cadd8b708 | 92 | } // namespace mbed |
yihui | 0:700cadd8b708 | 93 | |
yihui | 0:700cadd8b708 | 94 | #endif |