this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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