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