inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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