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