Radio Structures in OOP

Dependencies:   mbed mbed-rtos

Committer:
jjones646
Date:
Sat Jan 03 04:35:32 2015 +0000
Revision:
3:dc7e9c6bc26c
Child:
4:989d51f3e6ef
updating with threaded tasks for communication classes

Who changed what in which revision?

UserRevisionLine numberNew 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 namespace mbed {
jjones646 3:dc7e9c6bc26c 23
jjones646 3:dc7e9c6bc26c 24 typedef void (*pvoidf_t)(void);
jjones646 3:dc7e9c6bc26c 25
jjones646 3:dc7e9c6bc26c 26 /** A class for storing and calling a pointer to a static or member void function
jjones646 3:dc7e9c6bc26c 27 */
jjones646 3:dc7e9c6bc26c 28 class FunctionPointerRJ {
jjones646 3:dc7e9c6bc26c 29 public:
jjones646 3:dc7e9c6bc26c 30
jjones646 3:dc7e9c6bc26c 31 /** Create a FunctionPointer, attaching a static function
jjones646 3:dc7e9c6bc26c 32 *
jjones646 3:dc7e9c6bc26c 33 * @param function The void static function to attach (default is none)
jjones646 3:dc7e9c6bc26c 34 */
jjones646 3:dc7e9c6bc26c 35 FunctionPointerRJ(void (*function)(void) = 0);
jjones646 3:dc7e9c6bc26c 36
jjones646 3:dc7e9c6bc26c 37 /** Create a FunctionPointer, attaching a member function
jjones646 3:dc7e9c6bc26c 38 *
jjones646 3:dc7e9c6bc26c 39 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
jjones646 3:dc7e9c6bc26c 40 * @param function The address of the void member function to attach
jjones646 3:dc7e9c6bc26c 41 */
jjones646 3:dc7e9c6bc26c 42 template<typename T>
jjones646 3:dc7e9c6bc26c 43 FunctionPointerRJ(T *object, void (T::*member)(void)) {
jjones646 3:dc7e9c6bc26c 44 attach(object, member);
jjones646 3:dc7e9c6bc26c 45 }
jjones646 3:dc7e9c6bc26c 46
jjones646 3:dc7e9c6bc26c 47 /** Attach a static function
jjones646 3:dc7e9c6bc26c 48 *
jjones646 3:dc7e9c6bc26c 49 * @param function The void static function to attach (default is none)
jjones646 3:dc7e9c6bc26c 50 */
jjones646 3:dc7e9c6bc26c 51 void attach(void (*function)(void) = 0);
jjones646 3:dc7e9c6bc26c 52 void attach(void (*function)(RTP_t*));
jjones646 3:dc7e9c6bc26c 53 void attach(void (*function)(void const*));
jjones646 3:dc7e9c6bc26c 54
jjones646 3:dc7e9c6bc26c 55 /** Attach a member function
jjones646 3:dc7e9c6bc26c 56 *
jjones646 3:dc7e9c6bc26c 57 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
jjones646 3:dc7e9c6bc26c 58 * @param function The address of the void member function to attach
jjones646 3:dc7e9c6bc26c 59 */
jjones646 3:dc7e9c6bc26c 60 template<typename T>
jjones646 3:dc7e9c6bc26c 61 void attach(T *object, void (T::*member)(void)) {
jjones646 3:dc7e9c6bc26c 62 _object = static_cast<void*>(object);
jjones646 3:dc7e9c6bc26c 63 memcpy(_member, (char*)&member, sizeof(member));
jjones646 3:dc7e9c6bc26c 64 _membercaller = &FunctionPointerRJ::membercaller<T>;
jjones646 3:dc7e9c6bc26c 65 _function = 0;
jjones646 3:dc7e9c6bc26c 66 }
jjones646 3:dc7e9c6bc26c 67
jjones646 3:dc7e9c6bc26c 68 /** Attach a member function
jjones646 3:dc7e9c6bc26c 69 *
jjones646 3:dc7e9c6bc26c 70 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
jjones646 3:dc7e9c6bc26c 71 * @param function The address of the void member function to attach
jjones646 3:dc7e9c6bc26c 72 */
jjones646 3:dc7e9c6bc26c 73 template<typename T>
jjones646 3:dc7e9c6bc26c 74 void attach(T *object, void (T::*member)(RTP_t&)) {
jjones646 3:dc7e9c6bc26c 75 _object = static_cast<void*>(object);
jjones646 3:dc7e9c6bc26c 76 memcpy(_member, (char*)&member, sizeof(member));
jjones646 3:dc7e9c6bc26c 77 _membercaller = &FunctionPointerRJ::membercaller<T>;
jjones646 3:dc7e9c6bc26c 78 _functionRTP = 0;
jjones646 3:dc7e9c6bc26c 79 }
jjones646 3:dc7e9c6bc26c 80
jjones646 3:dc7e9c6bc26c 81 /** Call the attached static or member function
jjones646 3:dc7e9c6bc26c 82 */
jjones646 3:dc7e9c6bc26c 83 void call();
jjones646 3:dc7e9c6bc26c 84 void call(RTP_t*);
jjones646 3:dc7e9c6bc26c 85 void call(void const*);
jjones646 3:dc7e9c6bc26c 86
jjones646 3:dc7e9c6bc26c 87 pvoidf_t get_function() const {
jjones646 3:dc7e9c6bc26c 88 return (pvoidf_t)_function;
jjones646 3:dc7e9c6bc26c 89 }
jjones646 3:dc7e9c6bc26c 90
jjones646 3:dc7e9c6bc26c 91 #ifdef MBED_OPERATORS
jjones646 3:dc7e9c6bc26c 92 void operator ()(void);
jjones646 3:dc7e9c6bc26c 93 #endif
jjones646 3:dc7e9c6bc26c 94
jjones646 3:dc7e9c6bc26c 95 private:
jjones646 3:dc7e9c6bc26c 96 template<typename T>
jjones646 3:dc7e9c6bc26c 97 static void membercaller(void *object, char *member) {
jjones646 3:dc7e9c6bc26c 98 T* o = static_cast<T*>(object);
jjones646 3:dc7e9c6bc26c 99 void (T::*m)(void);
jjones646 3:dc7e9c6bc26c 100 memcpy((char*)&m, member, sizeof(m));
jjones646 3:dc7e9c6bc26c 101 (o->*m)();
jjones646 3:dc7e9c6bc26c 102 }
jjones646 3:dc7e9c6bc26c 103
jjones646 3:dc7e9c6bc26c 104 void (*_functionConst)(void const*);
jjones646 3:dc7e9c6bc26c 105 void (*_functionRTP)(RTP_t*);
jjones646 3:dc7e9c6bc26c 106 void (*_function)(void); // static function pointer - 0 if none attached
jjones646 3:dc7e9c6bc26c 107 void *_object; // object this pointer - 0 if none attached
jjones646 3:dc7e9c6bc26c 108 char _member[16]; // raw member function pointer storage - converted back by registered _membercaller
jjones646 3:dc7e9c6bc26c 109 void (*_membercaller)(void*, char*); // registered membercaller function to convert back and call _member on _object
jjones646 3:dc7e9c6bc26c 110 };
jjones646 3:dc7e9c6bc26c 111
jjones646 3:dc7e9c6bc26c 112 } // namespace mbed
jjones646 3:dc7e9c6bc26c 113
jjones646 3:dc7e9c6bc26c 114 #endif // FUNCTIONPOINTER_RJ_H