IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Sat Oct 29 15:11:28 2016 +0000
Revision:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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