High level Bluetooth Low Energy API and radio abstraction layer

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Mon Jan 11 08:51:25 2016 +0000
Revision:
1042:21a86ac7f5b1
Parent:
993:4d62b7967c11
Child:
1045:b9d15970040f
Synchronized with git rev 582789ea
Author: Vincent Coubard
Add function template makeFunctionPointer wich help to create
FunctionPointerWithContext<ContextType> instances.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 710:b2e1a2660ec2 1 /* mbed Microcontroller Library
rgrover1 710:b2e1a2660ec2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 710:b2e1a2660ec2 3 *
rgrover1 710:b2e1a2660ec2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 710:b2e1a2660ec2 5 * you may not use this file except in compliance with the License.
rgrover1 710:b2e1a2660ec2 6 * You may obtain a copy of the License at
rgrover1 710:b2e1a2660ec2 7 *
rgrover1 710:b2e1a2660ec2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 710:b2e1a2660ec2 9 *
rgrover1 710:b2e1a2660ec2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 710:b2e1a2660ec2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 710:b2e1a2660ec2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 710:b2e1a2660ec2 13 * See the License for the specific language governing permissions and
rgrover1 710:b2e1a2660ec2 14 * limitations under the License.
rgrover1 710:b2e1a2660ec2 15 */
rgrover1 710:b2e1a2660ec2 16
rgrover1 710:b2e1a2660ec2 17 #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
rgrover1 710:b2e1a2660ec2 18 #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
rgrover1 710:b2e1a2660ec2 19
rgrover1 710:b2e1a2660ec2 20 #include <string.h>
rgrover1 710:b2e1a2660ec2 21
rgrover1 710:b2e1a2660ec2 22 /** A class for storing and calling a pointer to a static or member void function
vcoubard 1042:21a86ac7f5b1 23 * which takes a context.
rgrover1 710:b2e1a2660ec2 24 */
rgrover1 710:b2e1a2660ec2 25 template <typename ContextType>
vcoubard 1042:21a86ac7f5b1 26 class FunctionPointerWithContext {
rgrover1 710:b2e1a2660ec2 27 public:
rgrover1 710:b2e1a2660ec2 28 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
rgrover1 710:b2e1a2660ec2 29 typedef void (*pvoidfcontext_t)(ContextType context);
rgrover1 710:b2e1a2660ec2 30
vcoubard 1042:21a86ac7f5b1 31 /** Create a FunctionPointerWithContext, attaching a static function
rgrover1 710:b2e1a2660ec2 32 *
vcoubard 1042:21a86ac7f5b1 33 * @param function The void static function to attach (default is none)
rgrover1 710:b2e1a2660ec2 34 */
rgrover1 710:b2e1a2660ec2 35 FunctionPointerWithContext(void (*function)(ContextType context) = NULL) :
vcoubard 1042:21a86ac7f5b1 36 _function(NULL), _caller(NULL), _next(NULL) {
rgrover1 710:b2e1a2660ec2 37 attach(function);
rgrover1 710:b2e1a2660ec2 38 }
rgrover1 710:b2e1a2660ec2 39
vcoubard 1042:21a86ac7f5b1 40 /** Create a FunctionPointerWithContext, attaching a member function
rgrover1 710:b2e1a2660ec2 41 *
vcoubard 1042:21a86ac7f5b1 42 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
vcoubard 1042:21a86ac7f5b1 43 * @param function The address of the void member function to attach
rgrover1 710:b2e1a2660ec2 44 */
rgrover1 710:b2e1a2660ec2 45 template<typename T>
rgrover1 710:b2e1a2660ec2 46 FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) :
rgrover1 897:838e1375dbaa 47 _memberFunctionAndPointer(), _caller(NULL), _next(NULL) {
rgrover1 710:b2e1a2660ec2 48 attach(object, member);
rgrover1 710:b2e1a2660ec2 49 }
rgrover1 710:b2e1a2660ec2 50
vcoubard 1042:21a86ac7f5b1 51 /** Attach a static function
rgrover1 710:b2e1a2660ec2 52 *
vcoubard 1042:21a86ac7f5b1 53 * @param function The void static function to attach (default is none)
rgrover1 710:b2e1a2660ec2 54 */
rgrover1 710:b2e1a2660ec2 55 void attach(void (*function)(ContextType context) = NULL) {
rgrover1 710:b2e1a2660ec2 56 _function = function;
rgrover1 897:838e1375dbaa 57 _caller = functioncaller;
rgrover1 710:b2e1a2660ec2 58 }
rgrover1 710:b2e1a2660ec2 59
vcoubard 1042:21a86ac7f5b1 60 /** Attach a member function
rgrover1 710:b2e1a2660ec2 61 *
vcoubard 1042:21a86ac7f5b1 62 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
vcoubard 1042:21a86ac7f5b1 63 * @param function The address of the void member function to attach
rgrover1 710:b2e1a2660ec2 64 */
rgrover1 710:b2e1a2660ec2 65 template<typename T>
rgrover1 710:b2e1a2660ec2 66 void attach(T *object, void (T::*member)(ContextType context)) {
rgrover1 897:838e1375dbaa 67 _memberFunctionAndPointer._object = static_cast<void *>(object);
rgrover1 897:838e1375dbaa 68 memcpy(_memberFunctionAndPointer._memberFunction, (char*) &member, sizeof(member));
rgrover1 897:838e1375dbaa 69 _caller = &FunctionPointerWithContext::membercaller<T>;
rgrover1 710:b2e1a2660ec2 70 }
rgrover1 710:b2e1a2660ec2 71
vcoubard 1042:21a86ac7f5b1 72 /** Call the attached static or member function; and if there are chained
rgrover1 710:b2e1a2660ec2 73 * FunctionPointers their callbacks are invoked as well.
vcoubard 1042:21a86ac7f5b1 74 * @Note: all chained callbacks stack up; so hopefully there won't be too
rgrover1 710:b2e1a2660ec2 75 * many FunctionPointers in a chain. */
vcoubard 1042:21a86ac7f5b1 76 void call(ContextType context) {
rgrover1 952:8a6c287de1be 77 _caller(this, context);
vcoubard 1042:21a86ac7f5b1 78
vcoubard 1042:21a86ac7f5b1 79 /* Propagate the call to next in the chain. */
vcoubard 1042:21a86ac7f5b1 80 if (_next) {
vcoubard 1042:21a86ac7f5b1 81 _next->call(context);
vcoubard 1042:21a86ac7f5b1 82 }
rgrover1 990:53ac0ac3aa39 83 }
rgrover1 990:53ac0ac3aa39 84
rgrover1 990:53ac0ac3aa39 85 /**
vcoubard 1042:21a86ac7f5b1 86 * Setup an external FunctionPointer as a next in the chain of related
rgrover1 710:b2e1a2660ec2 87 * callbacks. Invoking call() on the head FunctionPointer will invoke all
rgrover1 710:b2e1a2660ec2 88 * chained callbacks.
rgrover1 710:b2e1a2660ec2 89 *
rgrover1 710:b2e1a2660ec2 90 * Refer to 'CallChain' as an alternative.
rgrover1 710:b2e1a2660ec2 91 */
rgrover1 710:b2e1a2660ec2 92 void chainAsNext(pFunctionPointerWithContext_t next) {
rgrover1 710:b2e1a2660ec2 93 _next = next;
rgrover1 710:b2e1a2660ec2 94 }
rgrover1 710:b2e1a2660ec2 95
rgrover1 710:b2e1a2660ec2 96 pFunctionPointerWithContext_t getNext(void) const {
rgrover1 710:b2e1a2660ec2 97 return _next;
rgrover1 710:b2e1a2660ec2 98 }
rgrover1 710:b2e1a2660ec2 99
rgrover1 710:b2e1a2660ec2 100 pvoidfcontext_t get_function() const {
rgrover1 710:b2e1a2660ec2 101 return (pvoidfcontext_t)_function;
rgrover1 710:b2e1a2660ec2 102 }
rgrover1 710:b2e1a2660ec2 103
rgrover1 710:b2e1a2660ec2 104 private:
rgrover1 710:b2e1a2660ec2 105 template<typename T>
vcoubard 1042:21a86ac7f5b1 106 static void membercaller(pFunctionPointerWithContext_t self, ContextType context) {
rgrover1 897:838e1375dbaa 107 if (self->_memberFunctionAndPointer._object) {
rgrover1 897:838e1375dbaa 108 T *o = static_cast<T *>(self->_memberFunctionAndPointer._object);
rgrover1 897:838e1375dbaa 109 void (T::*m)(ContextType);
rgrover1 897:838e1375dbaa 110 memcpy((char*) &m, self->_memberFunctionAndPointer._memberFunction, sizeof(m));
rgrover1 897:838e1375dbaa 111 (o->*m)(context);
rgrover1 897:838e1375dbaa 112 }
rgrover1 897:838e1375dbaa 113 }
rgrover1 897:838e1375dbaa 114
vcoubard 1042:21a86ac7f5b1 115 static void functioncaller(pFunctionPointerWithContext_t self, ContextType context) {
rgrover1 897:838e1375dbaa 116 if (self->_function) {
rgrover1 897:838e1375dbaa 117 self->_function(context);
rgrover1 897:838e1375dbaa 118 }
rgrover1 710:b2e1a2660ec2 119 }
rgrover1 710:b2e1a2660ec2 120
rgrover1 897:838e1375dbaa 121 struct MemberFunctionAndPtr {
rgrover1 897:838e1375dbaa 122 /*
vcoubard 1042:21a86ac7f5b1 123 * forward declaration of a class and a member function to this class.
rgrover1 897:838e1375dbaa 124 * Because the compiler doesn't know anything about the forwarded member
rgrover1 897:838e1375dbaa 125 * function, it will always use the biggest size and the biggest alignment
rgrover1 897:838e1375dbaa 126 * that a member function can take for objects of type UndefinedMemberFunction.
rgrover1 897:838e1375dbaa 127 */
rgrover1 897:838e1375dbaa 128 class UndefinedClass;
rgrover1 897:838e1375dbaa 129 typedef void (UndefinedClass::*UndefinedMemberFunction)(ContextType);
rgrover1 897:838e1375dbaa 130
rgrover1 897:838e1375dbaa 131 void* _object;
rgrover1 897:838e1375dbaa 132 union {
rgrover1 897:838e1375dbaa 133 char _memberFunction[sizeof(UndefinedMemberFunction)];
rgrover1 897:838e1375dbaa 134 UndefinedMemberFunction _alignment;
rgrover1 897:838e1375dbaa 135 };
rgrover1 897:838e1375dbaa 136 };
rgrover1 897:838e1375dbaa 137
rgrover1 897:838e1375dbaa 138 union {
vcoubard 1042:21a86ac7f5b1 139 pvoidfcontext_t _function; /**< static function pointer - NULL if none attached */
rgrover1 897:838e1375dbaa 140 /**
rgrover1 897:838e1375dbaa 141 * object this pointer and pointer to member -
rgrover1 897:838e1375dbaa 142 * _memberFunctionAndPointer._object will be NULL if none attached
rgrover1 897:838e1375dbaa 143 */
vcoubard 1042:21a86ac7f5b1 144 MemberFunctionAndPtr _memberFunctionAndPointer;
rgrover1 897:838e1375dbaa 145 };
rgrover1 897:838e1375dbaa 146
vcoubard 1042:21a86ac7f5b1 147 void (*_caller)(FunctionPointerWithContext*, ContextType);
rgrover1 897:838e1375dbaa 148
vcoubard 1042:21a86ac7f5b1 149 pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this
rgrover1 710:b2e1a2660ec2 150 * allows chaining function pointers without requiring
vcoubard 1042:21a86ac7f5b1 151 * external memory to manage the chain. Also refer to
rgrover1 710:b2e1a2660ec2 152 * 'CallChain' as an alternative. */
rgrover1 710:b2e1a2660ec2 153 };
rgrover1 710:b2e1a2660ec2 154
rgrover1 993:4d62b7967c11 155 /**
rgrover1 993:4d62b7967c11 156 * @brief Create a new FunctionPointerWithContext which bind an instance and a
rgrover1 993:4d62b7967c11 157 * a member function together.
rgrover1 993:4d62b7967c11 158 * @details This little helper is a just here to eliminate the need to write the
rgrover1 993:4d62b7967c11 159 * FunctionPointerWithContext type each time you want to create one by kicking
rgrover1 993:4d62b7967c11 160 * automatic type deduction of function templates. With this function, it is easy
rgrover1 993:4d62b7967c11 161 * to write only one entry point for functions which expect a FunctionPointer
rgrover1 993:4d62b7967c11 162 * in parameters.
rgrover1 993:4d62b7967c11 163 *
rgrover1 993:4d62b7967c11 164 * @param object to bound with member function
rgrover1 993:4d62b7967c11 165 * @param member The member function called
rgrover1 993:4d62b7967c11 166 * @return a new FunctionPointerWithContext
rgrover1 993:4d62b7967c11 167 */
rgrover1 993:4d62b7967c11 168 template<typename T, typename ContextType>
rgrover1 993:4d62b7967c11 169 FunctionPointerWithContext<ContextType> makeFunctionPointer(T *object, void (T::*member)(ContextType context))
rgrover1 993:4d62b7967c11 170 {
rgrover1 993:4d62b7967c11 171 return FunctionPointerWithContext<ContextType>(object, member);
rgrover1 993:4d62b7967c11 172 }
rgrover1 993:4d62b7967c11 173
rgrover1 710:b2e1a2660ec2 174 #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H