test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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