I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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