TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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