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