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