Bluetooth LE library for Nucleo board
Dependents: Nucleo_BLE_HeartRate Nucleo_BLE_UART Nucleo_BLE_Demo Nucleo_BLE_UART
Warning: Deprecated!
Supported drivers and applications can be found at this link.
public/CallChainOfFunctionPointersWithContext.h@1:79e5c08cbcc7, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 19:52:49 2014 +0000
- Revision:
- 1:79e5c08cbcc7
- Parent:
- 0:289fd2dae405
change the USARTService->write() method access permission to public
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:289fd2dae405 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:289fd2dae405 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:289fd2dae405 | 3 | * |
sjallouli | 0:289fd2dae405 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:289fd2dae405 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:289fd2dae405 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:289fd2dae405 | 7 | * |
sjallouli | 0:289fd2dae405 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:289fd2dae405 | 9 | * |
sjallouli | 0:289fd2dae405 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:289fd2dae405 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:289fd2dae405 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:289fd2dae405 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:289fd2dae405 | 14 | * limitations under the License. |
sjallouli | 0:289fd2dae405 | 15 | */ |
sjallouli | 0:289fd2dae405 | 16 | #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H |
sjallouli | 0:289fd2dae405 | 17 | #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H |
sjallouli | 0:289fd2dae405 | 18 | |
sjallouli | 0:289fd2dae405 | 19 | #include <string.h> |
sjallouli | 0:289fd2dae405 | 20 | #include "FunctionPointerWithContext.h" |
sjallouli | 0:289fd2dae405 | 21 | |
sjallouli | 0:289fd2dae405 | 22 | namespace mbed { |
sjallouli | 0:289fd2dae405 | 23 | |
sjallouli | 0:289fd2dae405 | 24 | /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in |
sjallouli | 0:289fd2dae405 | 25 | * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code, |
sjallouli | 0:289fd2dae405 | 26 | * but can be used for other purposes. |
sjallouli | 0:289fd2dae405 | 27 | * |
sjallouli | 0:289fd2dae405 | 28 | * Example: |
sjallouli | 0:289fd2dae405 | 29 | * @code |
sjallouli | 0:289fd2dae405 | 30 | * #include "mbed.h" |
sjallouli | 0:289fd2dae405 | 31 | * |
sjallouli | 0:289fd2dae405 | 32 | * CallChainOfFunctionPointersWithContext<void *> chain; |
sjallouli | 0:289fd2dae405 | 33 | * |
sjallouli | 0:289fd2dae405 | 34 | * void first(void *context) { |
sjallouli | 0:289fd2dae405 | 35 | * printf("'first' function.\n"); |
sjallouli | 0:289fd2dae405 | 36 | * } |
sjallouli | 0:289fd2dae405 | 37 | * |
sjallouli | 0:289fd2dae405 | 38 | * void second(void *context) { |
sjallouli | 0:289fd2dae405 | 39 | * printf("'second' function.\n"); |
sjallouli | 0:289fd2dae405 | 40 | * } |
sjallouli | 0:289fd2dae405 | 41 | * |
sjallouli | 0:289fd2dae405 | 42 | * class Test { |
sjallouli | 0:289fd2dae405 | 43 | * public: |
sjallouli | 0:289fd2dae405 | 44 | * void f(void *context) { |
sjallouli | 0:289fd2dae405 | 45 | * printf("A::f (class member).\n"); |
sjallouli | 0:289fd2dae405 | 46 | * } |
sjallouli | 0:289fd2dae405 | 47 | * }; |
sjallouli | 0:289fd2dae405 | 48 | * |
sjallouli | 0:289fd2dae405 | 49 | * int main() { |
sjallouli | 0:289fd2dae405 | 50 | * Test test; |
sjallouli | 0:289fd2dae405 | 51 | * |
sjallouli | 0:289fd2dae405 | 52 | * chain.add(second); |
sjallouli | 0:289fd2dae405 | 53 | * chain.add_front(first); |
sjallouli | 0:289fd2dae405 | 54 | * chain.add(&test, &Test::f); |
sjallouli | 0:289fd2dae405 | 55 | * chain.call(); |
sjallouli | 0:289fd2dae405 | 56 | * } |
sjallouli | 0:289fd2dae405 | 57 | * @endcode |
sjallouli | 0:289fd2dae405 | 58 | */ |
sjallouli | 0:289fd2dae405 | 59 | |
sjallouli | 0:289fd2dae405 | 60 | template <typename ContextType> |
sjallouli | 0:289fd2dae405 | 61 | class CallChainOfFunctionPointersWithContext { |
sjallouli | 0:289fd2dae405 | 62 | public: |
sjallouli | 0:289fd2dae405 | 63 | typedef FunctionPointerWithContext<ContextType>* pFunctionPointerWithContext_t; |
sjallouli | 0:289fd2dae405 | 64 | |
sjallouli | 0:289fd2dae405 | 65 | public: |
sjallouli | 0:289fd2dae405 | 66 | /** Create an empty chain |
sjallouli | 0:289fd2dae405 | 67 | * |
sjallouli | 0:289fd2dae405 | 68 | * @param size (optional) Initial size of the chain |
sjallouli | 0:289fd2dae405 | 69 | */ |
sjallouli | 0:289fd2dae405 | 70 | CallChainOfFunctionPointersWithContext() : chainHead(NULL) { |
sjallouli | 0:289fd2dae405 | 71 | /* empty */ |
sjallouli | 0:289fd2dae405 | 72 | } |
sjallouli | 0:289fd2dae405 | 73 | |
sjallouli | 0:289fd2dae405 | 74 | virtual ~CallChainOfFunctionPointersWithContext() { |
sjallouli | 0:289fd2dae405 | 75 | clear(); |
sjallouli | 0:289fd2dae405 | 76 | } |
sjallouli | 0:289fd2dae405 | 77 | |
sjallouli | 0:289fd2dae405 | 78 | /** Add a function at the front of the chain |
sjallouli | 0:289fd2dae405 | 79 | * |
sjallouli | 0:289fd2dae405 | 80 | * @param function A pointer to a void function |
sjallouli | 0:289fd2dae405 | 81 | * |
sjallouli | 0:289fd2dae405 | 82 | * @returns |
sjallouli | 0:289fd2dae405 | 83 | * The function object created for 'function' |
sjallouli | 0:289fd2dae405 | 84 | */ |
sjallouli | 0:289fd2dae405 | 85 | pFunctionPointerWithContext_t add(void (*function)(ContextType context)) { |
sjallouli | 0:289fd2dae405 | 86 | return common_add(new FunctionPointerWithContext<ContextType>(function)); |
sjallouli | 0:289fd2dae405 | 87 | } |
sjallouli | 0:289fd2dae405 | 88 | |
sjallouli | 0:289fd2dae405 | 89 | /** Add a function at the front of the chain |
sjallouli | 0:289fd2dae405 | 90 | * |
sjallouli | 0:289fd2dae405 | 91 | * @param tptr pointer to the object to call the member function on |
sjallouli | 0:289fd2dae405 | 92 | * @param mptr pointer to the member function to be called |
sjallouli | 0:289fd2dae405 | 93 | * |
sjallouli | 0:289fd2dae405 | 94 | * @returns |
sjallouli | 0:289fd2dae405 | 95 | * The function object created for 'tptr' and 'mptr' |
sjallouli | 0:289fd2dae405 | 96 | */ |
sjallouli | 0:289fd2dae405 | 97 | template<typename T> |
sjallouli | 0:289fd2dae405 | 98 | pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) { |
sjallouli | 0:289fd2dae405 | 99 | return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr)); |
sjallouli | 0:289fd2dae405 | 100 | } |
sjallouli | 0:289fd2dae405 | 101 | |
sjallouli | 0:289fd2dae405 | 102 | /** Clear the call chain (remove all functions in the chain). |
sjallouli | 0:289fd2dae405 | 103 | */ |
sjallouli | 0:289fd2dae405 | 104 | void clear(void) { |
sjallouli | 0:289fd2dae405 | 105 | pFunctionPointerWithContext_t fptr = chainHead; |
sjallouli | 0:289fd2dae405 | 106 | while (fptr) { |
sjallouli | 0:289fd2dae405 | 107 | pFunctionPointerWithContext_t deadPtr = fptr; |
sjallouli | 0:289fd2dae405 | 108 | fptr = deadPtr->getNext(); |
sjallouli | 0:289fd2dae405 | 109 | delete deadPtr; |
sjallouli | 0:289fd2dae405 | 110 | } |
sjallouli | 0:289fd2dae405 | 111 | |
sjallouli | 0:289fd2dae405 | 112 | chainHead = NULL; |
sjallouli | 0:289fd2dae405 | 113 | } |
sjallouli | 0:289fd2dae405 | 114 | |
sjallouli | 0:289fd2dae405 | 115 | bool hasCallbacksAttached(void) const { |
sjallouli | 0:289fd2dae405 | 116 | return (chainHead != NULL); |
sjallouli | 0:289fd2dae405 | 117 | } |
sjallouli | 0:289fd2dae405 | 118 | |
sjallouli | 0:289fd2dae405 | 119 | /** Call all the functions in the chain in sequence |
sjallouli | 0:289fd2dae405 | 120 | * @Note: the stack frames of all the callbacks within the chained |
sjallouli | 0:289fd2dae405 | 121 | * FunctionPointers will stack up. Hopefully there won't be too many |
sjallouli | 0:289fd2dae405 | 122 | * chained FunctionPointers. |
sjallouli | 0:289fd2dae405 | 123 | */ |
sjallouli | 0:289fd2dae405 | 124 | void call(ContextType context) { |
sjallouli | 0:289fd2dae405 | 125 | if (chainHead) |
sjallouli | 0:289fd2dae405 | 126 | chainHead->call(context); |
sjallouli | 0:289fd2dae405 | 127 | } |
sjallouli | 0:289fd2dae405 | 128 | |
sjallouli | 0:289fd2dae405 | 129 | private: |
sjallouli | 0:289fd2dae405 | 130 | pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) { |
sjallouli | 0:289fd2dae405 | 131 | if (chainHead == NULL) { |
sjallouli | 0:289fd2dae405 | 132 | chainHead = pf; |
sjallouli | 0:289fd2dae405 | 133 | } else { |
sjallouli | 0:289fd2dae405 | 134 | pf->chainAsNext(chainHead); |
sjallouli | 0:289fd2dae405 | 135 | chainHead = pf; |
sjallouli | 0:289fd2dae405 | 136 | } |
sjallouli | 0:289fd2dae405 | 137 | |
sjallouli | 0:289fd2dae405 | 138 | return chainHead; |
sjallouli | 0:289fd2dae405 | 139 | } |
sjallouli | 0:289fd2dae405 | 140 | |
sjallouli | 0:289fd2dae405 | 141 | private: |
sjallouli | 0:289fd2dae405 | 142 | pFunctionPointerWithContext_t chainHead; |
sjallouli | 0:289fd2dae405 | 143 | |
sjallouli | 0:289fd2dae405 | 144 | /* disallow copy constructor and assignment operators */ |
sjallouli | 0:289fd2dae405 | 145 | private: |
sjallouli | 0:289fd2dae405 | 146 | CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &); |
sjallouli | 0:289fd2dae405 | 147 | CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &); |
sjallouli | 0:289fd2dae405 | 148 | }; |
sjallouli | 0:289fd2dae405 | 149 | |
sjallouli | 0:289fd2dae405 | 150 | } // namespace mbed |
sjallouli | 0:289fd2dae405 | 151 | |
sjallouli | 0:289fd2dae405 | 152 | #endif |
sjallouli | 0:289fd2dae405 | 153 |