mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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