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