++

Fork of mbed-stm32l0/l1-src by lzbp li

Committer:
lzbpli
Date:
Thu Sep 22 02:11:40 2016 +0000
Revision:
638:e94dc43c7733
Parent:
212:34d62c0b2af6
jianjianjian

Who changed what in which revision?

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