fft
Dependencies: BSP_DISCO_F746NG SDFileSystem_Warning_Fixed
mbed_src_STM32F7/api/FunctionPointer.h@3:6f7e05dd8d15, 2020-03-06 (annotated)
- Committer:
- aria19970520
- Date:
- Fri Mar 06 06:52:14 2020 +0000
- Revision:
- 3:6f7e05dd8d15
- Parent:
- 0:3e46577dc273
fft
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:3e46577dc273 | 1 | /* mbed Microcontroller Library |
MikamiUitOpen | 0:3e46577dc273 | 2 | * Copyright (c) 2006-2015 ARM Limited |
MikamiUitOpen | 0:3e46577dc273 | 3 | * |
MikamiUitOpen | 0:3e46577dc273 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MikamiUitOpen | 0:3e46577dc273 | 5 | * you may not use this file except in compliance with the License. |
MikamiUitOpen | 0:3e46577dc273 | 6 | * You may obtain a copy of the License at |
MikamiUitOpen | 0:3e46577dc273 | 7 | * |
MikamiUitOpen | 0:3e46577dc273 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MikamiUitOpen | 0:3e46577dc273 | 9 | * |
MikamiUitOpen | 0:3e46577dc273 | 10 | * Unless required by applicable law or agreed to in writing, software |
MikamiUitOpen | 0:3e46577dc273 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MikamiUitOpen | 0:3e46577dc273 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MikamiUitOpen | 0:3e46577dc273 | 13 | * See the License for the specific language governing permissions and |
MikamiUitOpen | 0:3e46577dc273 | 14 | * limitations under the License. |
MikamiUitOpen | 0:3e46577dc273 | 15 | */ |
MikamiUitOpen | 0:3e46577dc273 | 16 | #ifndef MBED_FUNCTIONPOINTER_H |
MikamiUitOpen | 0:3e46577dc273 | 17 | #define MBED_FUNCTIONPOINTER_H |
MikamiUitOpen | 0:3e46577dc273 | 18 | |
MikamiUitOpen | 0:3e46577dc273 | 19 | #include <string.h> |
MikamiUitOpen | 0:3e46577dc273 | 20 | #include <stdint.h> |
MikamiUitOpen | 0:3e46577dc273 | 21 | |
MikamiUitOpen | 0:3e46577dc273 | 22 | namespace mbed { |
MikamiUitOpen | 0:3e46577dc273 | 23 | |
MikamiUitOpen | 0:3e46577dc273 | 24 | /* If we had variaditic templates, this wouldn't be a problem, but until C++11 is enabled, we are stuck with multiple classes... */ |
MikamiUitOpen | 0:3e46577dc273 | 25 | |
MikamiUitOpen | 0:3e46577dc273 | 26 | /** A class for storing and calling a pointer to a static or member function |
MikamiUitOpen | 0:3e46577dc273 | 27 | */ |
MikamiUitOpen | 0:3e46577dc273 | 28 | template <typename R, typename A1> |
MikamiUitOpen | 0:3e46577dc273 | 29 | class FunctionPointerArg1{ |
MikamiUitOpen | 0:3e46577dc273 | 30 | public: |
MikamiUitOpen | 0:3e46577dc273 | 31 | /** Create a FunctionPointer, attaching a static function |
MikamiUitOpen | 0:3e46577dc273 | 32 | * |
MikamiUitOpen | 0:3e46577dc273 | 33 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 0:3e46577dc273 | 34 | */ |
MikamiUitOpen | 0:3e46577dc273 | 35 | FunctionPointerArg1(R (*function)(A1) = 0) { |
MikamiUitOpen | 0:3e46577dc273 | 36 | attach(function); |
MikamiUitOpen | 0:3e46577dc273 | 37 | } |
MikamiUitOpen | 0:3e46577dc273 | 38 | |
MikamiUitOpen | 0:3e46577dc273 | 39 | /** Create a FunctionPointer, attaching a member function |
MikamiUitOpen | 0:3e46577dc273 | 40 | * |
MikamiUitOpen | 0:3e46577dc273 | 41 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 0:3e46577dc273 | 42 | * @param function The address of the member function to attach |
MikamiUitOpen | 0:3e46577dc273 | 43 | */ |
MikamiUitOpen | 0:3e46577dc273 | 44 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 45 | FunctionPointerArg1(T *object, R (T::*member)(A1)) { |
MikamiUitOpen | 0:3e46577dc273 | 46 | attach(object, member); |
MikamiUitOpen | 0:3e46577dc273 | 47 | } |
MikamiUitOpen | 0:3e46577dc273 | 48 | |
MikamiUitOpen | 0:3e46577dc273 | 49 | /** Attach a static function |
MikamiUitOpen | 0:3e46577dc273 | 50 | * |
MikamiUitOpen | 0:3e46577dc273 | 51 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 0:3e46577dc273 | 52 | */ |
MikamiUitOpen | 0:3e46577dc273 | 53 | void attach(R (*function)(A1)) { |
MikamiUitOpen | 0:3e46577dc273 | 54 | _p.function = function; |
MikamiUitOpen | 0:3e46577dc273 | 55 | _membercaller = 0; |
MikamiUitOpen | 0:3e46577dc273 | 56 | } |
MikamiUitOpen | 0:3e46577dc273 | 57 | |
MikamiUitOpen | 0:3e46577dc273 | 58 | /** Attach a member function |
MikamiUitOpen | 0:3e46577dc273 | 59 | * |
MikamiUitOpen | 0:3e46577dc273 | 60 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 0:3e46577dc273 | 61 | * @param function The address of the member function to attach |
MikamiUitOpen | 0:3e46577dc273 | 62 | */ |
MikamiUitOpen | 0:3e46577dc273 | 63 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 64 | void attach(T *object, R (T::*member)(A1)) { |
MikamiUitOpen | 0:3e46577dc273 | 65 | _p.object = static_cast<void*>(object); |
MikamiUitOpen | 0:3e46577dc273 | 66 | *reinterpret_cast<R (T::**)(A1)>(_member) = member; |
MikamiUitOpen | 0:3e46577dc273 | 67 | _membercaller = &FunctionPointerArg1::membercaller<T>; |
MikamiUitOpen | 0:3e46577dc273 | 68 | } |
MikamiUitOpen | 0:3e46577dc273 | 69 | |
MikamiUitOpen | 0:3e46577dc273 | 70 | /** Call the attached static or member function |
MikamiUitOpen | 0:3e46577dc273 | 71 | */ |
MikamiUitOpen | 0:3e46577dc273 | 72 | R call(A1 a) { |
MikamiUitOpen | 0:3e46577dc273 | 73 | if (_membercaller == 0 && _p.function) { |
MikamiUitOpen | 0:3e46577dc273 | 74 | return _p.function(a); |
MikamiUitOpen | 0:3e46577dc273 | 75 | } else if (_membercaller && _p.object) { |
MikamiUitOpen | 0:3e46577dc273 | 76 | return _membercaller(_p.object, _member, a); |
MikamiUitOpen | 0:3e46577dc273 | 77 | } |
MikamiUitOpen | 0:3e46577dc273 | 78 | return (R)0; |
MikamiUitOpen | 0:3e46577dc273 | 79 | } |
MikamiUitOpen | 0:3e46577dc273 | 80 | |
MikamiUitOpen | 0:3e46577dc273 | 81 | /** Get registered static function |
MikamiUitOpen | 0:3e46577dc273 | 82 | */ |
MikamiUitOpen | 0:3e46577dc273 | 83 | R(*get_function(A1))() { |
MikamiUitOpen | 0:3e46577dc273 | 84 | return _membercaller ? (R(*)(A1))0 : (R(*)(A1))_p.function; |
MikamiUitOpen | 0:3e46577dc273 | 85 | } |
MikamiUitOpen | 0:3e46577dc273 | 86 | |
MikamiUitOpen | 0:3e46577dc273 | 87 | #ifdef MBED_OPERATORS |
MikamiUitOpen | 0:3e46577dc273 | 88 | R operator ()(A1 a) { |
MikamiUitOpen | 0:3e46577dc273 | 89 | return call(a); |
MikamiUitOpen | 0:3e46577dc273 | 90 | } |
MikamiUitOpen | 0:3e46577dc273 | 91 | operator bool(void) const { |
MikamiUitOpen | 0:3e46577dc273 | 92 | return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; |
MikamiUitOpen | 0:3e46577dc273 | 93 | } |
MikamiUitOpen | 0:3e46577dc273 | 94 | #endif |
MikamiUitOpen | 0:3e46577dc273 | 95 | private: |
MikamiUitOpen | 0:3e46577dc273 | 96 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 97 | static R membercaller(void *object, uintptr_t *member, A1 a) { |
MikamiUitOpen | 0:3e46577dc273 | 98 | T* o = static_cast<T*>(object); |
MikamiUitOpen | 0:3e46577dc273 | 99 | R (T::**m)(A1) = reinterpret_cast<R (T::**)(A1)>(member); |
MikamiUitOpen | 0:3e46577dc273 | 100 | return (o->**m)(a); |
MikamiUitOpen | 0:3e46577dc273 | 101 | } |
MikamiUitOpen | 0:3e46577dc273 | 102 | |
MikamiUitOpen | 0:3e46577dc273 | 103 | union { |
MikamiUitOpen | 0:3e46577dc273 | 104 | R (*function)(A1); // static function pointer |
MikamiUitOpen | 0:3e46577dc273 | 105 | void *object; // object this pointer |
MikamiUitOpen | 0:3e46577dc273 | 106 | } _p; |
MikamiUitOpen | 0:3e46577dc273 | 107 | uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller |
MikamiUitOpen | 0:3e46577dc273 | 108 | R (*_membercaller)(void*, uintptr_t*, A1); // registered membercaller function to convert back and call _m.member on _object |
MikamiUitOpen | 0:3e46577dc273 | 109 | }; |
MikamiUitOpen | 0:3e46577dc273 | 110 | |
MikamiUitOpen | 0:3e46577dc273 | 111 | /** A class for storing and calling a pointer to a static or member function (R ()(void)) |
MikamiUitOpen | 0:3e46577dc273 | 112 | */ |
MikamiUitOpen | 0:3e46577dc273 | 113 | template <typename R> |
MikamiUitOpen | 0:3e46577dc273 | 114 | class FunctionPointerArg1<R, void>{ |
MikamiUitOpen | 0:3e46577dc273 | 115 | public: |
MikamiUitOpen | 0:3e46577dc273 | 116 | /** Create a FunctionPointer, attaching a static function |
MikamiUitOpen | 0:3e46577dc273 | 117 | * |
MikamiUitOpen | 0:3e46577dc273 | 118 | * @param function The static function to attach (default is none) |
MikamiUitOpen | 0:3e46577dc273 | 119 | */ |
MikamiUitOpen | 0:3e46577dc273 | 120 | FunctionPointerArg1(R (*function)(void) = 0) { |
MikamiUitOpen | 0:3e46577dc273 | 121 | attach(function); |
MikamiUitOpen | 0:3e46577dc273 | 122 | } |
MikamiUitOpen | 0:3e46577dc273 | 123 | |
MikamiUitOpen | 0:3e46577dc273 | 124 | /** Create a FunctionPointer, attaching a member function |
MikamiUitOpen | 0:3e46577dc273 | 125 | * |
MikamiUitOpen | 0:3e46577dc273 | 126 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 0:3e46577dc273 | 127 | * @param function The address of the void member function to attach |
MikamiUitOpen | 0:3e46577dc273 | 128 | */ |
MikamiUitOpen | 0:3e46577dc273 | 129 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 130 | FunctionPointerArg1(T *object, R (T::*member)(void)) { |
MikamiUitOpen | 0:3e46577dc273 | 131 | attach(object, member); |
MikamiUitOpen | 0:3e46577dc273 | 132 | } |
MikamiUitOpen | 0:3e46577dc273 | 133 | |
MikamiUitOpen | 0:3e46577dc273 | 134 | /** Attach a static function |
MikamiUitOpen | 0:3e46577dc273 | 135 | * |
MikamiUitOpen | 0:3e46577dc273 | 136 | * @param function The void static function to attach (default is none) |
MikamiUitOpen | 0:3e46577dc273 | 137 | */ |
MikamiUitOpen | 0:3e46577dc273 | 138 | void attach(R (*function)(void)) { |
MikamiUitOpen | 0:3e46577dc273 | 139 | _p.function = function; |
MikamiUitOpen | 0:3e46577dc273 | 140 | _membercaller = 0; |
MikamiUitOpen | 0:3e46577dc273 | 141 | } |
MikamiUitOpen | 0:3e46577dc273 | 142 | |
MikamiUitOpen | 0:3e46577dc273 | 143 | /** Attach a member function |
MikamiUitOpen | 0:3e46577dc273 | 144 | * |
MikamiUitOpen | 0:3e46577dc273 | 145 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
MikamiUitOpen | 0:3e46577dc273 | 146 | * @param function The address of the void member function to attach |
MikamiUitOpen | 0:3e46577dc273 | 147 | */ |
MikamiUitOpen | 0:3e46577dc273 | 148 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 149 | void attach(T *object, R (T::*member)(void)) { |
MikamiUitOpen | 0:3e46577dc273 | 150 | _p.object = static_cast<void*>(object); |
MikamiUitOpen | 0:3e46577dc273 | 151 | *reinterpret_cast<R (T::**)(void)>(_member) = member; |
MikamiUitOpen | 0:3e46577dc273 | 152 | _membercaller = &FunctionPointerArg1::membercaller<T>; |
MikamiUitOpen | 0:3e46577dc273 | 153 | } |
MikamiUitOpen | 0:3e46577dc273 | 154 | |
MikamiUitOpen | 0:3e46577dc273 | 155 | /** Call the attached static or member function |
MikamiUitOpen | 0:3e46577dc273 | 156 | */ |
MikamiUitOpen | 0:3e46577dc273 | 157 | R call(){ |
MikamiUitOpen | 0:3e46577dc273 | 158 | if (_membercaller == 0 && _p.function) { |
MikamiUitOpen | 0:3e46577dc273 | 159 | return _p.function(); |
MikamiUitOpen | 0:3e46577dc273 | 160 | } else if (_membercaller && _p.object) { |
MikamiUitOpen | 0:3e46577dc273 | 161 | return _membercaller(_p.object, _member); |
MikamiUitOpen | 0:3e46577dc273 | 162 | } |
MikamiUitOpen | 0:3e46577dc273 | 163 | return (R)0; |
MikamiUitOpen | 0:3e46577dc273 | 164 | } |
MikamiUitOpen | 0:3e46577dc273 | 165 | |
MikamiUitOpen | 0:3e46577dc273 | 166 | /** Get registered static function |
MikamiUitOpen | 0:3e46577dc273 | 167 | */ |
MikamiUitOpen | 0:3e46577dc273 | 168 | R(*get_function())() { |
MikamiUitOpen | 0:3e46577dc273 | 169 | return _membercaller ? (R(*)())0 : (R(*)())_p.function; |
MikamiUitOpen | 0:3e46577dc273 | 170 | } |
MikamiUitOpen | 0:3e46577dc273 | 171 | |
MikamiUitOpen | 0:3e46577dc273 | 172 | #ifdef MBED_OPERATORS |
MikamiUitOpen | 0:3e46577dc273 | 173 | R operator ()(void) { |
MikamiUitOpen | 0:3e46577dc273 | 174 | return call(); |
MikamiUitOpen | 0:3e46577dc273 | 175 | } |
MikamiUitOpen | 0:3e46577dc273 | 176 | operator bool(void) const { |
MikamiUitOpen | 0:3e46577dc273 | 177 | return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL; |
MikamiUitOpen | 0:3e46577dc273 | 178 | } |
MikamiUitOpen | 0:3e46577dc273 | 179 | #endif |
MikamiUitOpen | 0:3e46577dc273 | 180 | |
MikamiUitOpen | 0:3e46577dc273 | 181 | private: |
MikamiUitOpen | 0:3e46577dc273 | 182 | template<typename T> |
MikamiUitOpen | 0:3e46577dc273 | 183 | static R membercaller(void *object, uintptr_t *member) { |
MikamiUitOpen | 0:3e46577dc273 | 184 | T* o = static_cast<T*>(object); |
MikamiUitOpen | 0:3e46577dc273 | 185 | R (T::**m)(void) = reinterpret_cast<R (T::**)(void)>(member); |
MikamiUitOpen | 0:3e46577dc273 | 186 | return (o->**m)(); |
MikamiUitOpen | 0:3e46577dc273 | 187 | } |
MikamiUitOpen | 0:3e46577dc273 | 188 | |
MikamiUitOpen | 0:3e46577dc273 | 189 | union { |
MikamiUitOpen | 0:3e46577dc273 | 190 | R (*function)(void); // static function pointer |
MikamiUitOpen | 0:3e46577dc273 | 191 | void *object; // object this pointer |
MikamiUitOpen | 0:3e46577dc273 | 192 | } _p; |
MikamiUitOpen | 0:3e46577dc273 | 193 | uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller |
MikamiUitOpen | 0:3e46577dc273 | 194 | R (*_membercaller)(void*, uintptr_t*); // registered membercaller function to convert back and call _m.member on _object |
MikamiUitOpen | 0:3e46577dc273 | 195 | }; |
MikamiUitOpen | 0:3e46577dc273 | 196 | |
MikamiUitOpen | 0:3e46577dc273 | 197 | typedef FunctionPointerArg1<void, void> FunctionPointer; |
MikamiUitOpen | 0:3e46577dc273 | 198 | typedef FunctionPointerArg1<void, int> event_callback_t; |
MikamiUitOpen | 0:3e46577dc273 | 199 | |
MikamiUitOpen | 0:3e46577dc273 | 200 | } // namespace mbed |
MikamiUitOpen | 0:3e46577dc273 | 201 | |
MikamiUitOpen | 0:3e46577dc273 | 202 | #endif |