Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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