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