test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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