mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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