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