PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

Who changed what in which revision?

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