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