Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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