Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

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