Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/api/CallChain.h@9:05f0b5a3a70a, 2014-10-29 (annotated)
- Committer:
- yihui
- Date:
- Wed Oct 29 06:23:47 2014 +0000
- Revision:
- 9:05f0b5a3a70a
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #ifndef MBED_CALLCHAIN_H |
yihui | 9:05f0b5a3a70a | 17 | #define MBED_CALLCHAIN_H |
yihui | 9:05f0b5a3a70a | 18 | |
yihui | 9:05f0b5a3a70a | 19 | #include "FunctionPointer.h" |
yihui | 9:05f0b5a3a70a | 20 | #include <string.h> |
yihui | 9:05f0b5a3a70a | 21 | |
yihui | 9:05f0b5a3a70a | 22 | namespace mbed { |
yihui | 9:05f0b5a3a70a | 23 | |
yihui | 9:05f0b5a3a70a | 24 | /** Group one or more functions in an instance of a CallChain, then call them in |
yihui | 9:05f0b5a3a70a | 25 | * sequence using CallChain::call(). Used mostly by the interrupt chaining code, |
yihui | 9:05f0b5a3a70a | 26 | * but can be used for other purposes. |
yihui | 9:05f0b5a3a70a | 27 | * |
yihui | 9:05f0b5a3a70a | 28 | * Example: |
yihui | 9:05f0b5a3a70a | 29 | * @code |
yihui | 9:05f0b5a3a70a | 30 | * #include "mbed.h" |
yihui | 9:05f0b5a3a70a | 31 | * |
yihui | 9:05f0b5a3a70a | 32 | * CallChain chain; |
yihui | 9:05f0b5a3a70a | 33 | * |
yihui | 9:05f0b5a3a70a | 34 | * void first(void) { |
yihui | 9:05f0b5a3a70a | 35 | * printf("'first' function.\n"); |
yihui | 9:05f0b5a3a70a | 36 | * } |
yihui | 9:05f0b5a3a70a | 37 | * |
yihui | 9:05f0b5a3a70a | 38 | * void second(void) { |
yihui | 9:05f0b5a3a70a | 39 | * printf("'second' function.\n"); |
yihui | 9:05f0b5a3a70a | 40 | * } |
yihui | 9:05f0b5a3a70a | 41 | * |
yihui | 9:05f0b5a3a70a | 42 | * class Test { |
yihui | 9:05f0b5a3a70a | 43 | * public: |
yihui | 9:05f0b5a3a70a | 44 | * void f(void) { |
yihui | 9:05f0b5a3a70a | 45 | * printf("A::f (class member).\n"); |
yihui | 9:05f0b5a3a70a | 46 | * } |
yihui | 9:05f0b5a3a70a | 47 | * }; |
yihui | 9:05f0b5a3a70a | 48 | * |
yihui | 9:05f0b5a3a70a | 49 | * int main() { |
yihui | 9:05f0b5a3a70a | 50 | * Test test; |
yihui | 9:05f0b5a3a70a | 51 | * |
yihui | 9:05f0b5a3a70a | 52 | * chain.add(second); |
yihui | 9:05f0b5a3a70a | 53 | * chain.add_front(first); |
yihui | 9:05f0b5a3a70a | 54 | * chain.add(&test, &Test::f); |
yihui | 9:05f0b5a3a70a | 55 | * chain.call(); |
yihui | 9:05f0b5a3a70a | 56 | * } |
yihui | 9:05f0b5a3a70a | 57 | * @endcode |
yihui | 9:05f0b5a3a70a | 58 | */ |
yihui | 9:05f0b5a3a70a | 59 | |
yihui | 9:05f0b5a3a70a | 60 | typedef FunctionPointer* pFunctionPointer_t; |
yihui | 9:05f0b5a3a70a | 61 | |
yihui | 9:05f0b5a3a70a | 62 | class CallChain { |
yihui | 9:05f0b5a3a70a | 63 | public: |
yihui | 9:05f0b5a3a70a | 64 | /** Create an empty chain |
yihui | 9:05f0b5a3a70a | 65 | * |
yihui | 9:05f0b5a3a70a | 66 | * @param size (optional) Initial size of the chain |
yihui | 9:05f0b5a3a70a | 67 | */ |
yihui | 9:05f0b5a3a70a | 68 | CallChain(int size = 4); |
yihui | 9:05f0b5a3a70a | 69 | virtual ~CallChain(); |
yihui | 9:05f0b5a3a70a | 70 | |
yihui | 9:05f0b5a3a70a | 71 | /** Add a function at the end of the chain |
yihui | 9:05f0b5a3a70a | 72 | * |
yihui | 9:05f0b5a3a70a | 73 | * @param function A pointer to a void function |
yihui | 9:05f0b5a3a70a | 74 | * |
yihui | 9:05f0b5a3a70a | 75 | * @returns |
yihui | 9:05f0b5a3a70a | 76 | * The function object created for 'function' |
yihui | 9:05f0b5a3a70a | 77 | */ |
yihui | 9:05f0b5a3a70a | 78 | pFunctionPointer_t add(void (*function)(void)); |
yihui | 9:05f0b5a3a70a | 79 | |
yihui | 9:05f0b5a3a70a | 80 | /** Add a function at the end of the chain |
yihui | 9:05f0b5a3a70a | 81 | * |
yihui | 9:05f0b5a3a70a | 82 | * @param tptr pointer to the object to call the member function on |
yihui | 9:05f0b5a3a70a | 83 | * @param mptr pointer to the member function to be called |
yihui | 9:05f0b5a3a70a | 84 | * |
yihui | 9:05f0b5a3a70a | 85 | * @returns |
yihui | 9:05f0b5a3a70a | 86 | * The function object created for 'tptr' and 'mptr' |
yihui | 9:05f0b5a3a70a | 87 | */ |
yihui | 9:05f0b5a3a70a | 88 | template<typename T> |
yihui | 9:05f0b5a3a70a | 89 | pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) { |
yihui | 9:05f0b5a3a70a | 90 | return common_add(new FunctionPointer(tptr, mptr)); |
yihui | 9:05f0b5a3a70a | 91 | } |
yihui | 9:05f0b5a3a70a | 92 | |
yihui | 9:05f0b5a3a70a | 93 | /** Add a function at the beginning of the chain |
yihui | 9:05f0b5a3a70a | 94 | * |
yihui | 9:05f0b5a3a70a | 95 | * @param function A pointer to a void function |
yihui | 9:05f0b5a3a70a | 96 | * |
yihui | 9:05f0b5a3a70a | 97 | * @returns |
yihui | 9:05f0b5a3a70a | 98 | * The function object created for 'function' |
yihui | 9:05f0b5a3a70a | 99 | */ |
yihui | 9:05f0b5a3a70a | 100 | pFunctionPointer_t add_front(void (*function)(void)); |
yihui | 9:05f0b5a3a70a | 101 | |
yihui | 9:05f0b5a3a70a | 102 | /** Add a function at the beginning of the chain |
yihui | 9:05f0b5a3a70a | 103 | * |
yihui | 9:05f0b5a3a70a | 104 | * @param tptr pointer to the object to call the member function on |
yihui | 9:05f0b5a3a70a | 105 | * @param mptr pointer to the member function to be called |
yihui | 9:05f0b5a3a70a | 106 | * |
yihui | 9:05f0b5a3a70a | 107 | * @returns |
yihui | 9:05f0b5a3a70a | 108 | * The function object created for 'tptr' and 'mptr' |
yihui | 9:05f0b5a3a70a | 109 | */ |
yihui | 9:05f0b5a3a70a | 110 | template<typename T> |
yihui | 9:05f0b5a3a70a | 111 | pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) { |
yihui | 9:05f0b5a3a70a | 112 | return common_add_front(new FunctionPointer(tptr, mptr)); |
yihui | 9:05f0b5a3a70a | 113 | } |
yihui | 9:05f0b5a3a70a | 114 | |
yihui | 9:05f0b5a3a70a | 115 | /** Get the number of functions in the chain |
yihui | 9:05f0b5a3a70a | 116 | */ |
yihui | 9:05f0b5a3a70a | 117 | int size() const; |
yihui | 9:05f0b5a3a70a | 118 | |
yihui | 9:05f0b5a3a70a | 119 | /** Get a function object from the chain |
yihui | 9:05f0b5a3a70a | 120 | * |
yihui | 9:05f0b5a3a70a | 121 | * @param i function object index |
yihui | 9:05f0b5a3a70a | 122 | * |
yihui | 9:05f0b5a3a70a | 123 | * @returns |
yihui | 9:05f0b5a3a70a | 124 | * The function object at position 'i' in the chain |
yihui | 9:05f0b5a3a70a | 125 | */ |
yihui | 9:05f0b5a3a70a | 126 | pFunctionPointer_t get(int i) const; |
yihui | 9:05f0b5a3a70a | 127 | |
yihui | 9:05f0b5a3a70a | 128 | /** Look for a function object in the call chain |
yihui | 9:05f0b5a3a70a | 129 | * |
yihui | 9:05f0b5a3a70a | 130 | * @param f the function object to search |
yihui | 9:05f0b5a3a70a | 131 | * |
yihui | 9:05f0b5a3a70a | 132 | * @returns |
yihui | 9:05f0b5a3a70a | 133 | * The index of the function object if found, -1 otherwise. |
yihui | 9:05f0b5a3a70a | 134 | */ |
yihui | 9:05f0b5a3a70a | 135 | int find(pFunctionPointer_t f) const; |
yihui | 9:05f0b5a3a70a | 136 | |
yihui | 9:05f0b5a3a70a | 137 | /** Clear the call chain (remove all functions in the chain). |
yihui | 9:05f0b5a3a70a | 138 | */ |
yihui | 9:05f0b5a3a70a | 139 | void clear(); |
yihui | 9:05f0b5a3a70a | 140 | |
yihui | 9:05f0b5a3a70a | 141 | /** Remove a function object from the chain |
yihui | 9:05f0b5a3a70a | 142 | * |
yihui | 9:05f0b5a3a70a | 143 | * @arg f the function object to remove |
yihui | 9:05f0b5a3a70a | 144 | * |
yihui | 9:05f0b5a3a70a | 145 | * @returns |
yihui | 9:05f0b5a3a70a | 146 | * true if the function object was found and removed, false otherwise. |
yihui | 9:05f0b5a3a70a | 147 | */ |
yihui | 9:05f0b5a3a70a | 148 | bool remove(pFunctionPointer_t f); |
yihui | 9:05f0b5a3a70a | 149 | |
yihui | 9:05f0b5a3a70a | 150 | /** Call all the functions in the chain in sequence |
yihui | 9:05f0b5a3a70a | 151 | */ |
yihui | 9:05f0b5a3a70a | 152 | void call(); |
yihui | 9:05f0b5a3a70a | 153 | |
yihui | 9:05f0b5a3a70a | 154 | #ifdef MBED_OPERATORS |
yihui | 9:05f0b5a3a70a | 155 | void operator ()(void) { |
yihui | 9:05f0b5a3a70a | 156 | call(); |
yihui | 9:05f0b5a3a70a | 157 | } |
yihui | 9:05f0b5a3a70a | 158 | pFunctionPointer_t operator [](int i) const { |
yihui | 9:05f0b5a3a70a | 159 | return get(i); |
yihui | 9:05f0b5a3a70a | 160 | } |
yihui | 9:05f0b5a3a70a | 161 | #endif |
yihui | 9:05f0b5a3a70a | 162 | |
yihui | 9:05f0b5a3a70a | 163 | private: |
yihui | 9:05f0b5a3a70a | 164 | void _check_size(); |
yihui | 9:05f0b5a3a70a | 165 | pFunctionPointer_t common_add(pFunctionPointer_t pf); |
yihui | 9:05f0b5a3a70a | 166 | pFunctionPointer_t common_add_front(pFunctionPointer_t pf); |
yihui | 9:05f0b5a3a70a | 167 | |
yihui | 9:05f0b5a3a70a | 168 | pFunctionPointer_t* _chain; |
yihui | 9:05f0b5a3a70a | 169 | int _size; |
yihui | 9:05f0b5a3a70a | 170 | int _elements; |
yihui | 9:05f0b5a3a70a | 171 | |
yihui | 9:05f0b5a3a70a | 172 | /* disallow copy constructor and assignment operators */ |
yihui | 9:05f0b5a3a70a | 173 | private: |
yihui | 9:05f0b5a3a70a | 174 | CallChain(const CallChain&); |
yihui | 9:05f0b5a3a70a | 175 | CallChain & operator = (const CallChain&); |
yihui | 9:05f0b5a3a70a | 176 | }; |
yihui | 9:05f0b5a3a70a | 177 | |
yihui | 9:05f0b5a3a70a | 178 | } // namespace mbed |
yihui | 9:05f0b5a3a70a | 179 | |
yihui | 9:05f0b5a3a70a | 180 | #endif |
yihui | 9:05f0b5a3a70a | 181 |