test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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