mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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