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