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