Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
elijahorr
Date:
Thu Apr 14 07:28:54 2016 +0000
Revision:
121:672067c3ada4
Parent:
98:8ab26030e058
.

Who changed what in which revision?

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