dev01 Brautlecht / mbed-STM32F030F4

Dependents:   STM32F031_Blink_Aug17

Fork of mbed-STM32F030F4 by Nothing Special

Committer:
stupidcode
Date:
Sun Aug 27 18:32:02 2017 +0000
Revision:
12:ee1efe6181e7
Parent:
0:38ccae254a29
Blink;

Who changed what in which revision?

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