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