.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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