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