BLE Temperature Service Mobile and Ubiquitous Computing Module Birkbeck College

Dependencies:   DS1820

Committer:
gkroussos
Date:
Sun Mar 08 19:42:20 2015 +0000
Revision:
0:dd0fea342ad2
BLE Temperature Service ; Mobile and Ubiquitous Computing Module; Birkbeck College

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gkroussos 0:dd0fea342ad2 1 /* mbed Microcontroller Library
gkroussos 0:dd0fea342ad2 2 * Copyright (c) 2006-2013 ARM Limited
gkroussos 0:dd0fea342ad2 3 *
gkroussos 0:dd0fea342ad2 4 * Licensed under the Apache License, Version 2.0 (the "License");
gkroussos 0:dd0fea342ad2 5 * you may not use this file except in compliance with the License.
gkroussos 0:dd0fea342ad2 6 * You may obtain a copy of the License at
gkroussos 0:dd0fea342ad2 7 *
gkroussos 0:dd0fea342ad2 8 * http://www.apache.org/licenses/LICENSE-2.0
gkroussos 0:dd0fea342ad2 9 *
gkroussos 0:dd0fea342ad2 10 * Unless required by applicable law or agreed to in writing, software
gkroussos 0:dd0fea342ad2 11 * distributed under the License is distributed on an "AS IS" BASIS,
gkroussos 0:dd0fea342ad2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
gkroussos 0:dd0fea342ad2 13 * See the License for the specific language governing permissions and
gkroussos 0:dd0fea342ad2 14 * limitations under the License.
gkroussos 0:dd0fea342ad2 15 */
gkroussos 0:dd0fea342ad2 16 #ifndef MBED_FUNCTIONPOINTER_H
gkroussos 0:dd0fea342ad2 17 #define MBED_FUNCTIONPOINTER_H
gkroussos 0:dd0fea342ad2 18
gkroussos 0:dd0fea342ad2 19 #include <string.h>
gkroussos 0:dd0fea342ad2 20
gkroussos 0:dd0fea342ad2 21 namespace mbed {
gkroussos 0:dd0fea342ad2 22
gkroussos 0:dd0fea342ad2 23 typedef void (*pvoidf_t)(void);
gkroussos 0:dd0fea342ad2 24
gkroussos 0:dd0fea342ad2 25 /** A class for storing and calling a pointer to a static or member void function
gkroussos 0:dd0fea342ad2 26 */
gkroussos 0:dd0fea342ad2 27 class FunctionPointer {
gkroussos 0:dd0fea342ad2 28 public:
gkroussos 0:dd0fea342ad2 29
gkroussos 0:dd0fea342ad2 30 /** Create a FunctionPointer, attaching a static function
gkroussos 0:dd0fea342ad2 31 *
gkroussos 0:dd0fea342ad2 32 * @param function The void static function to attach (default is none)
gkroussos 0:dd0fea342ad2 33 */
gkroussos 0:dd0fea342ad2 34 FunctionPointer(void (*function)(void) = 0);
gkroussos 0:dd0fea342ad2 35
gkroussos 0:dd0fea342ad2 36 /** Create a FunctionPointer, attaching a member function
gkroussos 0:dd0fea342ad2 37 *
gkroussos 0:dd0fea342ad2 38 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
gkroussos 0:dd0fea342ad2 39 * @param function The address of the void member function to attach
gkroussos 0:dd0fea342ad2 40 */
gkroussos 0:dd0fea342ad2 41 template<typename T>
gkroussos 0:dd0fea342ad2 42 FunctionPointer(T *object, void (T::*member)(void)) {
gkroussos 0:dd0fea342ad2 43 attach(object, member);
gkroussos 0:dd0fea342ad2 44 }
gkroussos 0:dd0fea342ad2 45
gkroussos 0:dd0fea342ad2 46 /** Attach a static function
gkroussos 0:dd0fea342ad2 47 *
gkroussos 0:dd0fea342ad2 48 * @param function The void static function to attach (default is none)
gkroussos 0:dd0fea342ad2 49 */
gkroussos 0:dd0fea342ad2 50 void attach(void (*function)(void) = 0);
gkroussos 0:dd0fea342ad2 51
gkroussos 0:dd0fea342ad2 52 /** Attach a member function
gkroussos 0:dd0fea342ad2 53 *
gkroussos 0:dd0fea342ad2 54 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
gkroussos 0:dd0fea342ad2 55 * @param function The address of the void member function to attach
gkroussos 0:dd0fea342ad2 56 */
gkroussos 0:dd0fea342ad2 57 template<typename T>
gkroussos 0:dd0fea342ad2 58 void attach(T *object, void (T::*member)(void)) {
gkroussos 0:dd0fea342ad2 59 _object = static_cast<void*>(object);
gkroussos 0:dd0fea342ad2 60 memcpy(_member, (char*)&member, sizeof(member));
gkroussos 0:dd0fea342ad2 61 _membercaller = &FunctionPointer::membercaller<T>;
gkroussos 0:dd0fea342ad2 62 _function = 0;
gkroussos 0:dd0fea342ad2 63 }
gkroussos 0:dd0fea342ad2 64
gkroussos 0:dd0fea342ad2 65 /** Call the attached static or member function
gkroussos 0:dd0fea342ad2 66 */
gkroussos 0:dd0fea342ad2 67 void call();
gkroussos 0:dd0fea342ad2 68
gkroussos 0:dd0fea342ad2 69 pvoidf_t get_function() const {
gkroussos 0:dd0fea342ad2 70 return (pvoidf_t)_function;
gkroussos 0:dd0fea342ad2 71 }
gkroussos 0:dd0fea342ad2 72
gkroussos 0:dd0fea342ad2 73 #ifdef MBED_OPERATORS
gkroussos 0:dd0fea342ad2 74 void operator ()(void);
gkroussos 0:dd0fea342ad2 75 #endif
gkroussos 0:dd0fea342ad2 76
gkroussos 0:dd0fea342ad2 77 private:
gkroussos 0:dd0fea342ad2 78 template<typename T>
gkroussos 0:dd0fea342ad2 79 static void membercaller(void *object, char *member) {
gkroussos 0:dd0fea342ad2 80 T* o = static_cast<T*>(object);
gkroussos 0:dd0fea342ad2 81 void (T::*m)(void);
gkroussos 0:dd0fea342ad2 82 memcpy((char*)&m, member, sizeof(m));
gkroussos 0:dd0fea342ad2 83 (o->*m)();
gkroussos 0:dd0fea342ad2 84 }
gkroussos 0:dd0fea342ad2 85
gkroussos 0:dd0fea342ad2 86 void (*_function)(void); // static function pointer - 0 if none attached
gkroussos 0:dd0fea342ad2 87 void *_object; // object this pointer - 0 if none attached
gkroussos 0:dd0fea342ad2 88 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
gkroussos 0:dd0fea342ad2 89 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
gkroussos 0:dd0fea342ad2 90 };
gkroussos 0:dd0fea342ad2 91
gkroussos 0:dd0fea342ad2 92 } // namespace mbed
gkroussos 0:dd0fea342ad2 93
gkroussos 0:dd0fea342ad2 94 #endif