test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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