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