Jonathan Jones
/
Radios
Radio Structures in OOP
Embed:
(wiki syntax)
Show/hide line numbers
FunctionPointerRJ.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef FUNCTION_POINTER_RJ_H 00017 #define FUNCTION_POINTER_RJ_H 00018 00019 #include <string.h> 00020 #include "RTP.h" 00021 00022 typedef void (*pvoidf_t)(void); 00023 00024 /** A class for storing and calling a pointer to a static or member void function 00025 */ 00026 class FunctionPointerRJ 00027 { 00028 public: 00029 00030 /** Create a FunctionPointer, attaching a static function 00031 * 00032 * @param function The void static function to attach (default is none) 00033 */ 00034 FunctionPointerRJ(void (*function)(void) = 0); 00035 00036 /** Create a FunctionPointer, attaching a member function 00037 * 00038 * @param object The object pointer to invoke the member function on (i.e. the this pointer) 00039 * @param function The address of the void member function to attach 00040 */ 00041 template<typename T> 00042 FunctionPointerRJ(T *object, void (T::*member)(void)) { 00043 attach(object, member); 00044 } 00045 00046 /** Attach a static function 00047 * 00048 * @param function The void static function to attach (default is none) 00049 */ 00050 void attach(void(*function)(void) = 0); 00051 void attach(void(*function)(RTP_t*) = 0); 00052 void attach(void(*function)(void const*)); 00053 00054 /** Attach a member function 00055 * 00056 * @param object The object pointer to invoke the member function on (i.e. the this pointer) 00057 * @param function The address of the void member function to attach 00058 */ 00059 template<typename T> 00060 void attach(T *object, void (T::*member)(void)) { 00061 _object = static_cast<void*>(object); 00062 memcpy(_member, (char*)&member, sizeof(member)); 00063 _membercaller = &FunctionPointerRJ::membercaller<T>; 00064 _function = 0; 00065 } 00066 00067 /** Attach a member function 00068 * 00069 * @param object The object pointer to invoke the member function on (i.e. the this pointer) 00070 * @param function The address of the void member function to attach 00071 */ 00072 template<typename T> 00073 void attach(T *object, void (T::*member)(RTP_t*)) { 00074 _object = static_cast<void*>(object); 00075 memcpy(_member, (char*)&member, sizeof(member)); 00076 _membercallerRTP = &FunctionPointerRJ::membercallerRTP<T>; 00077 _functionRTP = 0; 00078 } 00079 00080 /** Call the attached static or member function 00081 */ 00082 void call(); 00083 void call(RTP_t*); 00084 void call(void const*); 00085 00086 pvoidf_t get_function() const { 00087 return (pvoidf_t)_function; 00088 } 00089 00090 #ifdef MBED_OPERATORS 00091 void operator ()(void); 00092 #endif 00093 00094 private: 00095 template<typename T> 00096 static void membercaller(void *object, char *member) { 00097 T* o = static_cast<T*>(object); 00098 void (T::*m)(void); 00099 memcpy((char*)&m, member, sizeof(m)); 00100 (o->*m)(); 00101 } 00102 00103 template<typename T> 00104 static void membercallerRTP(void *object, char *member, RTP_t *p) { 00105 T* o = static_cast<T*>(object); 00106 void(T::*m)(RTP_t*); 00107 memcpy((char*)&m, member, sizeof(m)); 00108 (o->*m)(p); 00109 00110 } 00111 00112 void (*_functionConst)(void const*); 00113 void (*_functionRTP)(RTP_t*); 00114 void (*_function)(void); // static function pointer - 0 if none attached 00115 void *_object; // object this pointer - 0 if none attached 00116 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller 00117 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object 00118 void (*_membercallerRTP)(void*, char*, RTP_t*); 00119 }; 00120 00121 #endif // FUNCTIONPOINTER_RJ_H
Generated on Mon Jul 18 2022 20:09:01 by 1.7.2