Jonathan Jones
/
Radios
Radio Structures in OOP
utils/RTOS_Management/FunctionPointerRJ.h@6:4a3dbfbc30f1, 2015-01-15 (annotated)
- Committer:
- jjones646
- Date:
- Thu Jan 15 07:15:33 2015 +0000
- Revision:
- 6:4a3dbfbc30f1
- Parent:
- 4:989d51f3e6ef
socket interface confirmed working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jjones646 | 3:dc7e9c6bc26c | 1 | /* mbed Microcontroller Library |
jjones646 | 3:dc7e9c6bc26c | 2 | * Copyright (c) 2006-2013 ARM Limited |
jjones646 | 3:dc7e9c6bc26c | 3 | * |
jjones646 | 3:dc7e9c6bc26c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jjones646 | 3:dc7e9c6bc26c | 5 | * you may not use this file except in compliance with the License. |
jjones646 | 3:dc7e9c6bc26c | 6 | * You may obtain a copy of the License at |
jjones646 | 3:dc7e9c6bc26c | 7 | * |
jjones646 | 3:dc7e9c6bc26c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jjones646 | 3:dc7e9c6bc26c | 9 | * |
jjones646 | 3:dc7e9c6bc26c | 10 | * Unless required by applicable law or agreed to in writing, software |
jjones646 | 3:dc7e9c6bc26c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jjones646 | 3:dc7e9c6bc26c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jjones646 | 3:dc7e9c6bc26c | 13 | * See the License for the specific language governing permissions and |
jjones646 | 3:dc7e9c6bc26c | 14 | * limitations under the License. |
jjones646 | 3:dc7e9c6bc26c | 15 | */ |
jjones646 | 3:dc7e9c6bc26c | 16 | #ifndef FUNCTION_POINTER_RJ_H |
jjones646 | 3:dc7e9c6bc26c | 17 | #define FUNCTION_POINTER_RJ_H |
jjones646 | 3:dc7e9c6bc26c | 18 | |
jjones646 | 3:dc7e9c6bc26c | 19 | #include <string.h> |
jjones646 | 3:dc7e9c6bc26c | 20 | #include "RTP.h" |
jjones646 | 3:dc7e9c6bc26c | 21 | |
jjones646 | 3:dc7e9c6bc26c | 22 | typedef void (*pvoidf_t)(void); |
jjones646 | 3:dc7e9c6bc26c | 23 | |
jjones646 | 3:dc7e9c6bc26c | 24 | /** A class for storing and calling a pointer to a static or member void function |
jjones646 | 3:dc7e9c6bc26c | 25 | */ |
jjones646 | 4:989d51f3e6ef | 26 | class FunctionPointerRJ |
jjones646 | 4:989d51f3e6ef | 27 | { |
jjones646 | 3:dc7e9c6bc26c | 28 | public: |
jjones646 | 3:dc7e9c6bc26c | 29 | |
jjones646 | 3:dc7e9c6bc26c | 30 | /** Create a FunctionPointer, attaching a static function |
jjones646 | 3:dc7e9c6bc26c | 31 | * |
jjones646 | 3:dc7e9c6bc26c | 32 | * @param function The void static function to attach (default is none) |
jjones646 | 3:dc7e9c6bc26c | 33 | */ |
jjones646 | 3:dc7e9c6bc26c | 34 | FunctionPointerRJ(void (*function)(void) = 0); |
jjones646 | 3:dc7e9c6bc26c | 35 | |
jjones646 | 3:dc7e9c6bc26c | 36 | /** Create a FunctionPointer, attaching a member function |
jjones646 | 3:dc7e9c6bc26c | 37 | * |
jjones646 | 3:dc7e9c6bc26c | 38 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
jjones646 | 3:dc7e9c6bc26c | 39 | * @param function The address of the void member function to attach |
jjones646 | 3:dc7e9c6bc26c | 40 | */ |
jjones646 | 3:dc7e9c6bc26c | 41 | template<typename T> |
jjones646 | 3:dc7e9c6bc26c | 42 | FunctionPointerRJ(T *object, void (T::*member)(void)) { |
jjones646 | 3:dc7e9c6bc26c | 43 | attach(object, member); |
jjones646 | 3:dc7e9c6bc26c | 44 | } |
jjones646 | 3:dc7e9c6bc26c | 45 | |
jjones646 | 3:dc7e9c6bc26c | 46 | /** Attach a static function |
jjones646 | 3:dc7e9c6bc26c | 47 | * |
jjones646 | 3:dc7e9c6bc26c | 48 | * @param function The void static function to attach (default is none) |
jjones646 | 3:dc7e9c6bc26c | 49 | */ |
jjones646 | 4:989d51f3e6ef | 50 | void attach(void(*function)(void) = 0); |
jjones646 | 4:989d51f3e6ef | 51 | void attach(void(*function)(RTP_t*) = 0); |
jjones646 | 4:989d51f3e6ef | 52 | void attach(void(*function)(void const*)); |
jjones646 | 3:dc7e9c6bc26c | 53 | |
jjones646 | 3:dc7e9c6bc26c | 54 | /** Attach a member function |
jjones646 | 3:dc7e9c6bc26c | 55 | * |
jjones646 | 3:dc7e9c6bc26c | 56 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
jjones646 | 3:dc7e9c6bc26c | 57 | * @param function The address of the void member function to attach |
jjones646 | 3:dc7e9c6bc26c | 58 | */ |
jjones646 | 3:dc7e9c6bc26c | 59 | template<typename T> |
jjones646 | 3:dc7e9c6bc26c | 60 | void attach(T *object, void (T::*member)(void)) { |
jjones646 | 3:dc7e9c6bc26c | 61 | _object = static_cast<void*>(object); |
jjones646 | 3:dc7e9c6bc26c | 62 | memcpy(_member, (char*)&member, sizeof(member)); |
jjones646 | 3:dc7e9c6bc26c | 63 | _membercaller = &FunctionPointerRJ::membercaller<T>; |
jjones646 | 3:dc7e9c6bc26c | 64 | _function = 0; |
jjones646 | 3:dc7e9c6bc26c | 65 | } |
jjones646 | 4:989d51f3e6ef | 66 | |
jjones646 | 4:989d51f3e6ef | 67 | /** Attach a member function |
jjones646 | 4:989d51f3e6ef | 68 | * |
jjones646 | 4:989d51f3e6ef | 69 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
jjones646 | 4:989d51f3e6ef | 70 | * @param function The address of the void member function to attach |
jjones646 | 4:989d51f3e6ef | 71 | */ |
jjones646 | 3:dc7e9c6bc26c | 72 | template<typename T> |
jjones646 | 4:989d51f3e6ef | 73 | void attach(T *object, void (T::*member)(RTP_t*)) { |
jjones646 | 3:dc7e9c6bc26c | 74 | _object = static_cast<void*>(object); |
jjones646 | 3:dc7e9c6bc26c | 75 | memcpy(_member, (char*)&member, sizeof(member)); |
jjones646 | 4:989d51f3e6ef | 76 | _membercallerRTP = &FunctionPointerRJ::membercallerRTP<T>; |
jjones646 | 3:dc7e9c6bc26c | 77 | _functionRTP = 0; |
jjones646 | 3:dc7e9c6bc26c | 78 | } |
jjones646 | 3:dc7e9c6bc26c | 79 | |
jjones646 | 3:dc7e9c6bc26c | 80 | /** Call the attached static or member function |
jjones646 | 3:dc7e9c6bc26c | 81 | */ |
jjones646 | 3:dc7e9c6bc26c | 82 | void call(); |
jjones646 | 3:dc7e9c6bc26c | 83 | void call(RTP_t*); |
jjones646 | 3:dc7e9c6bc26c | 84 | void call(void const*); |
jjones646 | 3:dc7e9c6bc26c | 85 | |
jjones646 | 3:dc7e9c6bc26c | 86 | pvoidf_t get_function() const { |
jjones646 | 3:dc7e9c6bc26c | 87 | return (pvoidf_t)_function; |
jjones646 | 3:dc7e9c6bc26c | 88 | } |
jjones646 | 3:dc7e9c6bc26c | 89 | |
jjones646 | 3:dc7e9c6bc26c | 90 | #ifdef MBED_OPERATORS |
jjones646 | 3:dc7e9c6bc26c | 91 | void operator ()(void); |
jjones646 | 3:dc7e9c6bc26c | 92 | #endif |
jjones646 | 3:dc7e9c6bc26c | 93 | |
jjones646 | 3:dc7e9c6bc26c | 94 | private: |
jjones646 | 3:dc7e9c6bc26c | 95 | template<typename T> |
jjones646 | 3:dc7e9c6bc26c | 96 | static void membercaller(void *object, char *member) { |
jjones646 | 3:dc7e9c6bc26c | 97 | T* o = static_cast<T*>(object); |
jjones646 | 3:dc7e9c6bc26c | 98 | void (T::*m)(void); |
jjones646 | 3:dc7e9c6bc26c | 99 | memcpy((char*)&m, member, sizeof(m)); |
jjones646 | 3:dc7e9c6bc26c | 100 | (o->*m)(); |
jjones646 | 3:dc7e9c6bc26c | 101 | } |
jjones646 | 3:dc7e9c6bc26c | 102 | |
jjones646 | 4:989d51f3e6ef | 103 | template<typename T> |
jjones646 | 4:989d51f3e6ef | 104 | static void membercallerRTP(void *object, char *member, RTP_t *p) { |
jjones646 | 4:989d51f3e6ef | 105 | T* o = static_cast<T*>(object); |
jjones646 | 4:989d51f3e6ef | 106 | void(T::*m)(RTP_t*); |
jjones646 | 4:989d51f3e6ef | 107 | memcpy((char*)&m, member, sizeof(m)); |
jjones646 | 4:989d51f3e6ef | 108 | (o->*m)(p); |
jjones646 | 4:989d51f3e6ef | 109 | |
jjones646 | 4:989d51f3e6ef | 110 | } |
jjones646 | 4:989d51f3e6ef | 111 | |
jjones646 | 4:989d51f3e6ef | 112 | void (*_functionConst)(void const*); |
jjones646 | 4:989d51f3e6ef | 113 | void (*_functionRTP)(RTP_t*); |
jjones646 | 3:dc7e9c6bc26c | 114 | void (*_function)(void); // static function pointer - 0 if none attached |
jjones646 | 3:dc7e9c6bc26c | 115 | void *_object; // object this pointer - 0 if none attached |
jjones646 | 3:dc7e9c6bc26c | 116 | char _member[16]; // raw member function pointer storage - converted back by registered _membercaller |
jjones646 | 3:dc7e9c6bc26c | 117 | void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object |
jjones646 | 4:989d51f3e6ef | 118 | void (*_membercallerRTP)(void*, char*, RTP_t*); |
jjones646 | 3:dc7e9c6bc26c | 119 | }; |
jjones646 | 3:dc7e9c6bc26c | 120 | |
jjones646 | 3:dc7e9c6bc26c | 121 | #endif // FUNCTIONPOINTER_RJ_H |