BLE

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Thu Nov 26 12:52:05 2015 +0000
Revision:
933:68a113707ba5
Parent:
929:624793511b25
Child:
934:3ec277a0d780
Synchronized with git rev 38e27c4a
Author: Vincent Coubard
FunctionPointer call is not propagated through FunctionPointer call but it
is a mechanism internal to CallChain object.

This change allow to safelly remove a callback in a callchain while
calling it. It also clean responsabilities and reduce coupling.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 711:b2e1a2660ec2 1 /* mbed Microcontroller Library
rgrover1 711:b2e1a2660ec2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 711:b2e1a2660ec2 3 *
rgrover1 711:b2e1a2660ec2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 711:b2e1a2660ec2 5 * you may not use this file except in compliance with the License.
rgrover1 711:b2e1a2660ec2 6 * You may obtain a copy of the License at
rgrover1 711:b2e1a2660ec2 7 *
rgrover1 711:b2e1a2660ec2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 711:b2e1a2660ec2 9 *
rgrover1 711:b2e1a2660ec2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 711:b2e1a2660ec2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 711:b2e1a2660ec2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 711:b2e1a2660ec2 13 * See the License for the specific language governing permissions and
rgrover1 711:b2e1a2660ec2 14 * limitations under the License.
rgrover1 711:b2e1a2660ec2 15 */
rgrover1 711:b2e1a2660ec2 16 #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
rgrover1 711:b2e1a2660ec2 17 #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
rgrover1 711:b2e1a2660ec2 18
rgrover1 711:b2e1a2660ec2 19 #include <string.h>
rgrover1 711:b2e1a2660ec2 20 #include "FunctionPointerWithContext.h"
rgrover1 711:b2e1a2660ec2 21
rgrover1 711:b2e1a2660ec2 22
rgrover1 711:b2e1a2660ec2 23 /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in
rgrover1 711:b2e1a2660ec2 24 * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code,
rgrover1 711:b2e1a2660ec2 25 * but can be used for other purposes.
rgrover1 711:b2e1a2660ec2 26 *
rgrover1 711:b2e1a2660ec2 27 * Example:
rgrover1 711:b2e1a2660ec2 28 * @code
rgrover1 711:b2e1a2660ec2 29 *
rgrover1 711:b2e1a2660ec2 30 * CallChainOfFunctionPointersWithContext<void *> chain;
rgrover1 711:b2e1a2660ec2 31 *
rgrover1 711:b2e1a2660ec2 32 * void first(void *context) {
rgrover1 711:b2e1a2660ec2 33 * printf("'first' function.\n");
rgrover1 711:b2e1a2660ec2 34 * }
rgrover1 711:b2e1a2660ec2 35 *
rgrover1 711:b2e1a2660ec2 36 * void second(void *context) {
rgrover1 711:b2e1a2660ec2 37 * printf("'second' function.\n");
rgrover1 711:b2e1a2660ec2 38 * }
rgrover1 711:b2e1a2660ec2 39 *
rgrover1 711:b2e1a2660ec2 40 * class Test {
rgrover1 711:b2e1a2660ec2 41 * public:
rgrover1 711:b2e1a2660ec2 42 * void f(void *context) {
rgrover1 711:b2e1a2660ec2 43 * printf("A::f (class member).\n");
rgrover1 711:b2e1a2660ec2 44 * }
rgrover1 711:b2e1a2660ec2 45 * };
rgrover1 711:b2e1a2660ec2 46 *
rgrover1 711:b2e1a2660ec2 47 * int main() {
rgrover1 711:b2e1a2660ec2 48 * Test test;
rgrover1 711:b2e1a2660ec2 49 *
rgrover1 711:b2e1a2660ec2 50 * chain.add(second);
rgrover1 711:b2e1a2660ec2 51 * chain.add_front(first);
rgrover1 711:b2e1a2660ec2 52 * chain.add(&test, &Test::f);
rgrover1 711:b2e1a2660ec2 53 * chain.call();
rgrover1 711:b2e1a2660ec2 54 * }
rgrover1 711:b2e1a2660ec2 55 * @endcode
rgrover1 711:b2e1a2660ec2 56 */
rgrover1 711:b2e1a2660ec2 57
rgrover1 711:b2e1a2660ec2 58 template <typename ContextType>
rgrover1 711:b2e1a2660ec2 59 class CallChainOfFunctionPointersWithContext {
rgrover1 711:b2e1a2660ec2 60 public:
rgrover1 711:b2e1a2660ec2 61 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
rgrover1 711:b2e1a2660ec2 62
rgrover1 711:b2e1a2660ec2 63 public:
rgrover1 914:690bea02c431 64 /** Create an empty chain.
rgrover1 711:b2e1a2660ec2 65 *
rgrover1 914:690bea02c431 66 * @param size (optional) Initial size of the chain.
rgrover1 711:b2e1a2660ec2 67 */
rgrover1 711:b2e1a2660ec2 68 CallChainOfFunctionPointersWithContext() : chainHead(NULL) {
rgrover1 711:b2e1a2660ec2 69 /* empty */
rgrover1 711:b2e1a2660ec2 70 }
rgrover1 711:b2e1a2660ec2 71
rgrover1 711:b2e1a2660ec2 72 virtual ~CallChainOfFunctionPointersWithContext() {
rgrover1 711:b2e1a2660ec2 73 clear();
rgrover1 711:b2e1a2660ec2 74 }
rgrover1 711:b2e1a2660ec2 75
rgrover1 914:690bea02c431 76 /** Add a function at the front of the chain.
rgrover1 711:b2e1a2660ec2 77 *
rgrover1 914:690bea02c431 78 * @param function A pointer to a void function.
rgrover1 711:b2e1a2660ec2 79 *
rgrover1 711:b2e1a2660ec2 80 * @returns
rgrover1 914:690bea02c431 81 * The function object created for 'function'.
rgrover1 711:b2e1a2660ec2 82 */
rgrover1 711:b2e1a2660ec2 83 pFunctionPointerWithContext_t add(void (*function)(ContextType context)) {
rgrover1 711:b2e1a2660ec2 84 return common_add(new FunctionPointerWithContext<ContextType>(function));
rgrover1 711:b2e1a2660ec2 85 }
rgrover1 711:b2e1a2660ec2 86
rgrover1 914:690bea02c431 87 /** Add a function at the front of the chain.
rgrover1 711:b2e1a2660ec2 88 *
rgrover1 914:690bea02c431 89 * @param tptr Pointer to the object to call the member function on.
rgrover1 914:690bea02c431 90 * @param mptr Pointer to the member function to be called.
rgrover1 711:b2e1a2660ec2 91 *
rgrover1 711:b2e1a2660ec2 92 * @returns
rgrover1 914:690bea02c431 93 * The function object created for 'tptr' and 'mptr'.
rgrover1 711:b2e1a2660ec2 94 */
rgrover1 711:b2e1a2660ec2 95 template<typename T>
rgrover1 711:b2e1a2660ec2 96 pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) {
rgrover1 711:b2e1a2660ec2 97 return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
rgrover1 711:b2e1a2660ec2 98 }
rgrover1 711:b2e1a2660ec2 99
rgrover1 922:ea542e6519bb 100 /** Add a function at the front of the chain.
rgrover1 922:ea542e6519bb 101 *
rgrover1 922:ea542e6519bb 102 * @param func The FunctionPointerWithContext to add.
rgrover1 922:ea542e6519bb 103 */
rgrover1 922:ea542e6519bb 104 void add(const FunctionPointerWithContext<ContextType>& func) {
rgrover1 922:ea542e6519bb 105 common_add(new FunctionPointerWithContext<ContextType>(func));
rgrover1 922:ea542e6519bb 106 }
rgrover1 922:ea542e6519bb 107
rgrover1 922:ea542e6519bb 108 /**
rgrover1 922:ea542e6519bb 109 * Detach a function pointer from a callchain
rgrover1 922:ea542e6519bb 110 *
rgrover1 922:ea542e6519bb 111 * @oaram toDetach FunctionPointerWithContext to detach from this callchain
rgrover1 922:ea542e6519bb 112 *
rgrover1 922:ea542e6519bb 113 * @return true if a function pointer has been detached and false otherwise
rgrover1 922:ea542e6519bb 114 */
rgrover1 922:ea542e6519bb 115 void detach(const FunctionPointerWithContext<ContextType>& toDetach) {
rgrover1 922:ea542e6519bb 116 pFunctionPointerWithContext_t current = chainHead;
rgrover1 922:ea542e6519bb 117 pFunctionPointerWithContext_t previous = NULL;
rgrover1 922:ea542e6519bb 118
rgrover1 922:ea542e6519bb 119 while (current) {
rgrover1 922:ea542e6519bb 120 if(*current == toDetach) {
rgrover1 933:68a113707ba5 121 if(previous == NULL) {
rgrover1 933:68a113707ba5 122 if(currentCalled == current) {
rgrover1 933:68a113707ba5 123 currentCalled = NULL;
rgrover1 933:68a113707ba5 124 }
rgrover1 922:ea542e6519bb 125 chainHead = current->getNext();
rgrover1 922:ea542e6519bb 126 } else {
rgrover1 933:68a113707ba5 127 if(currentCalled == current) {
rgrover1 933:68a113707ba5 128 currentCalled = previous;
rgrover1 933:68a113707ba5 129 }
rgrover1 922:ea542e6519bb 130 previous->chainAsNext(current->getNext());
rgrover1 922:ea542e6519bb 131 }
rgrover1 922:ea542e6519bb 132 delete current;
rgrover1 929:624793511b25 133 return;
rgrover1 922:ea542e6519bb 134 }
rgrover1 922:ea542e6519bb 135
rgrover1 922:ea542e6519bb 136 previous = current;
rgrover1 922:ea542e6519bb 137 current = current->getNext();
rgrover1 922:ea542e6519bb 138 }
rgrover1 922:ea542e6519bb 139 }
rgrover1 922:ea542e6519bb 140
rgrover1 711:b2e1a2660ec2 141 /** Clear the call chain (remove all functions in the chain).
rgrover1 711:b2e1a2660ec2 142 */
rgrover1 711:b2e1a2660ec2 143 void clear(void) {
rgrover1 711:b2e1a2660ec2 144 pFunctionPointerWithContext_t fptr = chainHead;
rgrover1 711:b2e1a2660ec2 145 while (fptr) {
rgrover1 711:b2e1a2660ec2 146 pFunctionPointerWithContext_t deadPtr = fptr;
rgrover1 711:b2e1a2660ec2 147 fptr = deadPtr->getNext();
rgrover1 711:b2e1a2660ec2 148 delete deadPtr;
rgrover1 711:b2e1a2660ec2 149 }
rgrover1 711:b2e1a2660ec2 150
rgrover1 711:b2e1a2660ec2 151 chainHead = NULL;
rgrover1 711:b2e1a2660ec2 152 }
rgrover1 711:b2e1a2660ec2 153
rgrover1 711:b2e1a2660ec2 154 bool hasCallbacksAttached(void) const {
rgrover1 711:b2e1a2660ec2 155 return (chainHead != NULL);
rgrover1 711:b2e1a2660ec2 156 }
rgrover1 711:b2e1a2660ec2 157
rgrover1 711:b2e1a2660ec2 158 /** Call all the functions in the chain in sequence
rgrover1 914:690bea02c431 159 * @Note: The stack frames of all the callbacks within the chained
rgrover1 711:b2e1a2660ec2 160 * FunctionPointers will stack up. Hopefully there won't be too many
rgrover1 711:b2e1a2660ec2 161 * chained FunctionPointers.
rgrover1 711:b2e1a2660ec2 162 */
rgrover1 711:b2e1a2660ec2 163 void call(ContextType context) {
rgrover1 933:68a113707ba5 164 ((const CallChainOfFunctionPointersWithContext*) this)->call(context);
rgrover1 711:b2e1a2660ec2 165 }
rgrover1 711:b2e1a2660ec2 166
rgrover1 922:ea542e6519bb 167 /**
rgrover1 922:ea542e6519bb 168 * @brief same as above but const
rgrover1 922:ea542e6519bb 169 */
rgrover1 922:ea542e6519bb 170 void call(ContextType context) const {
rgrover1 933:68a113707ba5 171 currentCalled = chainHead;
rgrover1 933:68a113707ba5 172
rgrover1 933:68a113707ba5 173 while(currentCalled) {
rgrover1 933:68a113707ba5 174 currentCalled->call(context);
rgrover1 933:68a113707ba5 175 // if this was the head and the call removed the head
rgrover1 933:68a113707ba5 176 if(currentCalled == NULL) {
rgrover1 933:68a113707ba5 177 currentCalled = chainHead;
rgrover1 933:68a113707ba5 178 } else {
rgrover1 933:68a113707ba5 179 currentCalled = currentCalled->getNext();
rgrover1 933:68a113707ba5 180 }
rgrover1 922:ea542e6519bb 181 }
rgrover1 922:ea542e6519bb 182 }
rgrover1 922:ea542e6519bb 183
rgrover1 922:ea542e6519bb 184 /**
rgrover1 922:ea542e6519bb 185 * @brief same as above but with function call operator
rgrover1 922:ea542e6519bb 186 */
rgrover1 922:ea542e6519bb 187 void operator()(ContextType context) const {
rgrover1 922:ea542e6519bb 188 call(context);
rgrover1 922:ea542e6519bb 189 }
rgrover1 922:ea542e6519bb 190
rgrover1 922:ea542e6519bb 191 typedef void (CallChainOfFunctionPointersWithContext::*bool_type)() const;
rgrover1 922:ea542e6519bb 192 void True() const {}
rgrover1 922:ea542e6519bb 193
rgrover1 922:ea542e6519bb 194 operator bool_type() const {
rgrover1 922:ea542e6519bb 195 return chainHead == NULL ? 0 : &CallChainOfFunctionPointersWithContext::True;
rgrover1 922:ea542e6519bb 196 }
rgrover1 922:ea542e6519bb 197
rgrover1 711:b2e1a2660ec2 198 private:
rgrover1 711:b2e1a2660ec2 199 pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) {
rgrover1 711:b2e1a2660ec2 200 if (chainHead == NULL) {
rgrover1 711:b2e1a2660ec2 201 chainHead = pf;
rgrover1 711:b2e1a2660ec2 202 } else {
rgrover1 711:b2e1a2660ec2 203 pf->chainAsNext(chainHead);
rgrover1 711:b2e1a2660ec2 204 chainHead = pf;
rgrover1 711:b2e1a2660ec2 205 }
rgrover1 711:b2e1a2660ec2 206
rgrover1 711:b2e1a2660ec2 207 return chainHead;
rgrover1 711:b2e1a2660ec2 208 }
rgrover1 711:b2e1a2660ec2 209
rgrover1 711:b2e1a2660ec2 210 private:
rgrover1 711:b2e1a2660ec2 211 pFunctionPointerWithContext_t chainHead;
rgrover1 933:68a113707ba5 212 mutable pFunctionPointerWithContext_t currentCalled;
rgrover1 933:68a113707ba5 213
rgrover1 711:b2e1a2660ec2 214
rgrover1 914:690bea02c431 215 /* Disallow copy constructor and assignment operators. */
rgrover1 711:b2e1a2660ec2 216 private:
rgrover1 711:b2e1a2660ec2 217 CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &);
rgrover1 711:b2e1a2660ec2 218 CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &);
rgrover1 711:b2e1a2660ec2 219 };
rgrover1 711:b2e1a2660ec2 220
rgrover1 711:b2e1a2660ec2 221 #endif