The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
146:22da6e220af6
Release 147 of the mbed library.

Who changed what in which revision?

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