jgh
Fork of BLE_API by
ble/CallChainOfFunctionPointersWithContext.h@969:61f13bc8edbf, 2015-11-26 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Nov 26 12:52:35 2015 +0000
- Revision:
- 969:61f13bc8edbf
- Parent:
- 966:9451b90bbb66
- Child:
- 970:b3e45745026d
Synchronized with git rev 184d29c3
Author: Vincent Coubard
Various enhancement:
Add SafeBool class which allow to easily declare a safe bool operator in
c++03.
CallChainOfFunctionPointerswithContext:
- unify syntax of add
- detach function now return true if a function has been detached and
false otherwise
- Explanations about function call operator
- use safe bool idiom
- explanations about iterator and why it is mutable
FunctionPointerWithContext:
- fix call propagation
- use safe bool idiom
Gap:
- add documentation
- onRadioNotification does mot call initRadioNotification anymore
GattClient:
- documentation
GattServer:
- documentation
Who changed what in which revision?
User | Revision | Line number | New 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 | #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H |
rgrover1 | 710:b2e1a2660ec2 | 17 | #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H |
rgrover1 | 710:b2e1a2660ec2 | 18 | |
rgrover1 | 710:b2e1a2660ec2 | 19 | #include <string.h> |
rgrover1 | 710:b2e1a2660ec2 | 20 | #include "FunctionPointerWithContext.h" |
rgrover1 | 969:61f13bc8edbf | 21 | #include "SafeBool.h" |
rgrover1 | 710:b2e1a2660ec2 | 22 | |
rgrover1 | 710:b2e1a2660ec2 | 23 | |
rgrover1 | 710:b2e1a2660ec2 | 24 | /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in |
rgrover1 | 710:b2e1a2660ec2 | 25 | * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code, |
rgrover1 | 710:b2e1a2660ec2 | 26 | * but can be used for other purposes. |
rgrover1 | 710:b2e1a2660ec2 | 27 | * |
rgrover1 | 710:b2e1a2660ec2 | 28 | * Example: |
rgrover1 | 710:b2e1a2660ec2 | 29 | * @code |
rgrover1 | 710:b2e1a2660ec2 | 30 | * |
rgrover1 | 710:b2e1a2660ec2 | 31 | * CallChainOfFunctionPointersWithContext<void *> chain; |
rgrover1 | 710:b2e1a2660ec2 | 32 | * |
rgrover1 | 710:b2e1a2660ec2 | 33 | * void first(void *context) { |
rgrover1 | 710:b2e1a2660ec2 | 34 | * printf("'first' function.\n"); |
rgrover1 | 710:b2e1a2660ec2 | 35 | * } |
rgrover1 | 710:b2e1a2660ec2 | 36 | * |
rgrover1 | 710:b2e1a2660ec2 | 37 | * void second(void *context) { |
rgrover1 | 710:b2e1a2660ec2 | 38 | * printf("'second' function.\n"); |
rgrover1 | 710:b2e1a2660ec2 | 39 | * } |
rgrover1 | 710:b2e1a2660ec2 | 40 | * |
rgrover1 | 710:b2e1a2660ec2 | 41 | * class Test { |
rgrover1 | 710:b2e1a2660ec2 | 42 | * public: |
rgrover1 | 710:b2e1a2660ec2 | 43 | * void f(void *context) { |
rgrover1 | 710:b2e1a2660ec2 | 44 | * printf("A::f (class member).\n"); |
rgrover1 | 710:b2e1a2660ec2 | 45 | * } |
rgrover1 | 710:b2e1a2660ec2 | 46 | * }; |
rgrover1 | 710:b2e1a2660ec2 | 47 | * |
rgrover1 | 710:b2e1a2660ec2 | 48 | * int main() { |
rgrover1 | 710:b2e1a2660ec2 | 49 | * Test test; |
rgrover1 | 710:b2e1a2660ec2 | 50 | * |
rgrover1 | 710:b2e1a2660ec2 | 51 | * chain.add(second); |
rgrover1 | 710:b2e1a2660ec2 | 52 | * chain.add_front(first); |
rgrover1 | 710:b2e1a2660ec2 | 53 | * chain.add(&test, &Test::f); |
rgrover1 | 710:b2e1a2660ec2 | 54 | * chain.call(); |
rgrover1 | 710:b2e1a2660ec2 | 55 | * } |
rgrover1 | 710:b2e1a2660ec2 | 56 | * @endcode |
rgrover1 | 710:b2e1a2660ec2 | 57 | */ |
rgrover1 | 710:b2e1a2660ec2 | 58 | |
rgrover1 | 710:b2e1a2660ec2 | 59 | template <typename ContextType> |
rgrover1 | 969:61f13bc8edbf | 60 | class CallChainOfFunctionPointersWithContext : public SafeBool<CallChainOfFunctionPointersWithContext<ContextType> > { |
rgrover1 | 710:b2e1a2660ec2 | 61 | public: |
rgrover1 | 710:b2e1a2660ec2 | 62 | typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t; |
rgrover1 | 710:b2e1a2660ec2 | 63 | |
rgrover1 | 710:b2e1a2660ec2 | 64 | public: |
rgrover1 | 937:4932e700daf2 | 65 | /** Create an empty chain. |
rgrover1 | 710:b2e1a2660ec2 | 66 | * |
rgrover1 | 937:4932e700daf2 | 67 | * @param size (optional) Initial size of the chain. |
rgrover1 | 710:b2e1a2660ec2 | 68 | */ |
rgrover1 | 710:b2e1a2660ec2 | 69 | CallChainOfFunctionPointersWithContext() : chainHead(NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 70 | /* empty */ |
rgrover1 | 710:b2e1a2660ec2 | 71 | } |
rgrover1 | 710:b2e1a2660ec2 | 72 | |
rgrover1 | 710:b2e1a2660ec2 | 73 | virtual ~CallChainOfFunctionPointersWithContext() { |
rgrover1 | 710:b2e1a2660ec2 | 74 | clear(); |
rgrover1 | 710:b2e1a2660ec2 | 75 | } |
rgrover1 | 710:b2e1a2660ec2 | 76 | |
rgrover1 | 937:4932e700daf2 | 77 | /** Add a function at the front of the chain. |
rgrover1 | 710:b2e1a2660ec2 | 78 | * |
rgrover1 | 937:4932e700daf2 | 79 | * @param function A pointer to a void function. |
rgrover1 | 710:b2e1a2660ec2 | 80 | * |
rgrover1 | 710:b2e1a2660ec2 | 81 | * @returns |
rgrover1 | 937:4932e700daf2 | 82 | * The function object created for 'function'. |
rgrover1 | 710:b2e1a2660ec2 | 83 | */ |
rgrover1 | 710:b2e1a2660ec2 | 84 | pFunctionPointerWithContext_t add(void (*function)(ContextType context)) { |
rgrover1 | 710:b2e1a2660ec2 | 85 | return common_add(new FunctionPointerWithContext<ContextType>(function)); |
rgrover1 | 710:b2e1a2660ec2 | 86 | } |
rgrover1 | 710:b2e1a2660ec2 | 87 | |
rgrover1 | 937:4932e700daf2 | 88 | /** Add a function at the front of the chain. |
rgrover1 | 710:b2e1a2660ec2 | 89 | * |
rgrover1 | 937:4932e700daf2 | 90 | * @param tptr Pointer to the object to call the member function on. |
rgrover1 | 937:4932e700daf2 | 91 | * @param mptr Pointer to the member function to be called. |
rgrover1 | 710:b2e1a2660ec2 | 92 | * |
rgrover1 | 710:b2e1a2660ec2 | 93 | * @returns |
rgrover1 | 937:4932e700daf2 | 94 | * The function object created for 'tptr' and 'mptr'. |
rgrover1 | 710:b2e1a2660ec2 | 95 | */ |
rgrover1 | 710:b2e1a2660ec2 | 96 | template<typename T> |
rgrover1 | 710:b2e1a2660ec2 | 97 | pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) { |
rgrover1 | 710:b2e1a2660ec2 | 98 | return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr)); |
rgrover1 | 710:b2e1a2660ec2 | 99 | } |
rgrover1 | 710:b2e1a2660ec2 | 100 | |
rgrover1 | 969:61f13bc8edbf | 101 | /** Add a function at the front of the chain. |
rgrover1 | 969:61f13bc8edbf | 102 | * |
rgrover1 | 969:61f13bc8edbf | 103 | * @param func The FunctionPointerWithContext to add. |
rgrover1 | 969:61f13bc8edbf | 104 | */ |
rgrover1 | 969:61f13bc8edbf | 105 | pFunctionPointerWithContext_t add(const FunctionPointerWithContext<ContextType>& func) { |
rgrover1 | 969:61f13bc8edbf | 106 | return common_add(new FunctionPointerWithContext<ContextType>(func)); |
rgrover1 | 969:61f13bc8edbf | 107 | } |
rgrover1 | 969:61f13bc8edbf | 108 | |
rgrover1 | 969:61f13bc8edbf | 109 | /** |
rgrover1 | 969:61f13bc8edbf | 110 | * Detach a function pointer from a callchain |
rgrover1 | 969:61f13bc8edbf | 111 | * |
rgrover1 | 969:61f13bc8edbf | 112 | * @oaram toDetach FunctionPointerWithContext to detach from this callchain |
rgrover1 | 969:61f13bc8edbf | 113 | * |
rgrover1 | 969:61f13bc8edbf | 114 | * @return true if a function pointer has been detached and false otherwise |
rgrover1 | 969:61f13bc8edbf | 115 | */ |
rgrover1 | 969:61f13bc8edbf | 116 | bool detach(const FunctionPointerWithContext<ContextType>& toDetach) { |
rgrover1 | 969:61f13bc8edbf | 117 | pFunctionPointerWithContext_t current = chainHead; |
rgrover1 | 969:61f13bc8edbf | 118 | pFunctionPointerWithContext_t previous = NULL; |
rgrover1 | 969:61f13bc8edbf | 119 | |
rgrover1 | 969:61f13bc8edbf | 120 | while (current) { |
rgrover1 | 969:61f13bc8edbf | 121 | if(*current == toDetach) { |
rgrover1 | 969:61f13bc8edbf | 122 | if(previous == NULL) { |
rgrover1 | 969:61f13bc8edbf | 123 | if(currentCalled == current) { |
rgrover1 | 969:61f13bc8edbf | 124 | currentCalled = NULL; |
rgrover1 | 969:61f13bc8edbf | 125 | } |
rgrover1 | 969:61f13bc8edbf | 126 | chainHead = current->getNext(); |
rgrover1 | 969:61f13bc8edbf | 127 | } else { |
rgrover1 | 969:61f13bc8edbf | 128 | if(currentCalled == current) { |
rgrover1 | 969:61f13bc8edbf | 129 | currentCalled = previous; |
rgrover1 | 969:61f13bc8edbf | 130 | } |
rgrover1 | 969:61f13bc8edbf | 131 | previous->chainAsNext(current->getNext()); |
rgrover1 | 969:61f13bc8edbf | 132 | } |
rgrover1 | 969:61f13bc8edbf | 133 | delete current; |
rgrover1 | 969:61f13bc8edbf | 134 | return true; |
rgrover1 | 969:61f13bc8edbf | 135 | } |
rgrover1 | 969:61f13bc8edbf | 136 | |
rgrover1 | 969:61f13bc8edbf | 137 | previous = current; |
rgrover1 | 969:61f13bc8edbf | 138 | current = current->getNext(); |
rgrover1 | 969:61f13bc8edbf | 139 | } |
rgrover1 | 969:61f13bc8edbf | 140 | |
rgrover1 | 969:61f13bc8edbf | 141 | return false; |
rgrover1 | 969:61f13bc8edbf | 142 | } |
rgrover1 | 969:61f13bc8edbf | 143 | |
rgrover1 | 710:b2e1a2660ec2 | 144 | /** Clear the call chain (remove all functions in the chain). |
rgrover1 | 710:b2e1a2660ec2 | 145 | */ |
rgrover1 | 710:b2e1a2660ec2 | 146 | void clear(void) { |
rgrover1 | 710:b2e1a2660ec2 | 147 | pFunctionPointerWithContext_t fptr = chainHead; |
rgrover1 | 710:b2e1a2660ec2 | 148 | while (fptr) { |
rgrover1 | 710:b2e1a2660ec2 | 149 | pFunctionPointerWithContext_t deadPtr = fptr; |
rgrover1 | 710:b2e1a2660ec2 | 150 | fptr = deadPtr->getNext(); |
rgrover1 | 710:b2e1a2660ec2 | 151 | delete deadPtr; |
rgrover1 | 710:b2e1a2660ec2 | 152 | } |
rgrover1 | 710:b2e1a2660ec2 | 153 | |
rgrover1 | 710:b2e1a2660ec2 | 154 | chainHead = NULL; |
rgrover1 | 710:b2e1a2660ec2 | 155 | } |
rgrover1 | 710:b2e1a2660ec2 | 156 | |
rgrover1 | 710:b2e1a2660ec2 | 157 | bool hasCallbacksAttached(void) const { |
rgrover1 | 710:b2e1a2660ec2 | 158 | return (chainHead != NULL); |
rgrover1 | 710:b2e1a2660ec2 | 159 | } |
rgrover1 | 710:b2e1a2660ec2 | 160 | |
rgrover1 | 710:b2e1a2660ec2 | 161 | /** Call all the functions in the chain in sequence |
rgrover1 | 710:b2e1a2660ec2 | 162 | */ |
rgrover1 | 710:b2e1a2660ec2 | 163 | void call(ContextType context) { |
rgrover1 | 969:61f13bc8edbf | 164 | ((const CallChainOfFunctionPointersWithContext*) this)->call(context); |
rgrover1 | 969:61f13bc8edbf | 165 | } |
rgrover1 | 969:61f13bc8edbf | 166 | |
rgrover1 | 969:61f13bc8edbf | 167 | /** |
rgrover1 | 969:61f13bc8edbf | 168 | * @brief same as above but const |
rgrover1 | 969:61f13bc8edbf | 169 | */ |
rgrover1 | 969:61f13bc8edbf | 170 | void call(ContextType context) const { |
rgrover1 | 969:61f13bc8edbf | 171 | currentCalled = chainHead; |
rgrover1 | 969:61f13bc8edbf | 172 | |
rgrover1 | 969:61f13bc8edbf | 173 | while(currentCalled) { |
rgrover1 | 969:61f13bc8edbf | 174 | currentCalled->call(context); |
rgrover1 | 969:61f13bc8edbf | 175 | // if this was the head and the call removed the head |
rgrover1 | 969:61f13bc8edbf | 176 | if(currentCalled == NULL) { |
rgrover1 | 969:61f13bc8edbf | 177 | currentCalled = chainHead; |
rgrover1 | 969:61f13bc8edbf | 178 | } else { |
rgrover1 | 969:61f13bc8edbf | 179 | currentCalled = currentCalled->getNext(); |
rgrover1 | 969:61f13bc8edbf | 180 | } |
rgrover1 | 953:f6eb43f524b2 | 181 | } |
rgrover1 | 953:f6eb43f524b2 | 182 | } |
rgrover1 | 953:f6eb43f524b2 | 183 | |
rgrover1 | 969:61f13bc8edbf | 184 | /** |
rgrover1 | 969:61f13bc8edbf | 185 | * @brief same as above but with function call operator |
rgrover1 | 969:61f13bc8edbf | 186 | * \code |
rgrover1 | 969:61f13bc8edbf | 187 | * |
rgrover1 | 969:61f13bc8edbf | 188 | * void first(bool); |
rgrover1 | 969:61f13bc8edbf | 189 | * void second(bool); |
rgrover1 | 969:61f13bc8edbf | 190 | * |
rgrover1 | 969:61f13bc8edbf | 191 | * CallChainOfFunctionPointerWithContext<bool> foo; |
rgrover1 | 969:61f13bc8edbf | 192 | * |
rgrover1 | 969:61f13bc8edbf | 193 | * foo.attach(first); |
rgrover1 | 969:61f13bc8edbf | 194 | * foo.attach(second); |
rgrover1 | 969:61f13bc8edbf | 195 | * |
rgrover1 | 969:61f13bc8edbf | 196 | * // call the callchain like a function |
rgrover1 | 969:61f13bc8edbf | 197 | * foo(true); |
rgrover1 | 969:61f13bc8edbf | 198 | * |
rgrover1 | 969:61f13bc8edbf | 199 | * \endcode |
rgrover1 | 969:61f13bc8edbf | 200 | */ |
rgrover1 | 969:61f13bc8edbf | 201 | void operator()(ContextType context) const { |
rgrover1 | 969:61f13bc8edbf | 202 | call(context); |
rgrover1 | 969:61f13bc8edbf | 203 | } |
rgrover1 | 969:61f13bc8edbf | 204 | |
rgrover1 | 969:61f13bc8edbf | 205 | /** |
rgrover1 | 969:61f13bc8edbf | 206 | * @brief bool conversion operation |
rgrover1 | 969:61f13bc8edbf | 207 | */ |
rgrover1 | 969:61f13bc8edbf | 208 | bool toBool() const { |
rgrover1 | 969:61f13bc8edbf | 209 | return chainHead != NULL; |
rgrover1 | 969:61f13bc8edbf | 210 | } |
rgrover1 | 969:61f13bc8edbf | 211 | |
rgrover1 | 710:b2e1a2660ec2 | 212 | private: |
rgrover1 | 710:b2e1a2660ec2 | 213 | pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) { |
rgrover1 | 710:b2e1a2660ec2 | 214 | if (chainHead == NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 215 | chainHead = pf; |
rgrover1 | 710:b2e1a2660ec2 | 216 | } else { |
rgrover1 | 710:b2e1a2660ec2 | 217 | pf->chainAsNext(chainHead); |
rgrover1 | 710:b2e1a2660ec2 | 218 | chainHead = pf; |
rgrover1 | 710:b2e1a2660ec2 | 219 | } |
rgrover1 | 710:b2e1a2660ec2 | 220 | |
rgrover1 | 710:b2e1a2660ec2 | 221 | return chainHead; |
rgrover1 | 710:b2e1a2660ec2 | 222 | } |
rgrover1 | 710:b2e1a2660ec2 | 223 | |
rgrover1 | 710:b2e1a2660ec2 | 224 | private: |
rgrover1 | 710:b2e1a2660ec2 | 225 | pFunctionPointerWithContext_t chainHead; |
rgrover1 | 969:61f13bc8edbf | 226 | // iterator during a function call, this has to be mutable because the call function is const. |
rgrover1 | 969:61f13bc8edbf | 227 | // Note: mutable is the correct behaviour here, the iterator never leak outside the object. |
rgrover1 | 969:61f13bc8edbf | 228 | // So the object can still be seen as logically const even if it change its internal state |
rgrover1 | 969:61f13bc8edbf | 229 | mutable pFunctionPointerWithContext_t currentCalled; |
rgrover1 | 969:61f13bc8edbf | 230 | |
rgrover1 | 932:68a113707ba5 | 231 | |
rgrover1 | 937:4932e700daf2 | 232 | /* Disallow copy constructor and assignment operators. */ |
rgrover1 | 710:b2e1a2660ec2 | 233 | private: |
rgrover1 | 710:b2e1a2660ec2 | 234 | CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &); |
rgrover1 | 710:b2e1a2660ec2 | 235 | CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &); |
rgrover1 | 710:b2e1a2660ec2 | 236 | }; |
rgrover1 | 710:b2e1a2660ec2 | 237 | |
rgrover1 | 710:b2e1a2660ec2 | 238 | #endif |