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