CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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