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