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