initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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