SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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