Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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