test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
youkee 0:1ad0e04b1bc5 1 /* mbed Microcontroller Library
youkee 0:1ad0e04b1bc5 2 * Copyright (c) 2006-2013 ARM Limited
youkee 0:1ad0e04b1bc5 3 *
youkee 0:1ad0e04b1bc5 4 * Licensed under the Apache License, Version 2.0 (the "License");
youkee 0:1ad0e04b1bc5 5 * you may not use this file except in compliance with the License.
youkee 0:1ad0e04b1bc5 6 * You may obtain a copy of the License at
youkee 0:1ad0e04b1bc5 7 *
youkee 0:1ad0e04b1bc5 8 * http://www.apache.org/licenses/LICENSE-2.0
youkee 0:1ad0e04b1bc5 9 *
youkee 0:1ad0e04b1bc5 10 * Unless required by applicable law or agreed to in writing, software
youkee 0:1ad0e04b1bc5 11 * distributed under the License is distributed on an "AS IS" BASIS,
youkee 0:1ad0e04b1bc5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
youkee 0:1ad0e04b1bc5 13 * See the License for the specific language governing permissions and
youkee 0:1ad0e04b1bc5 14 * limitations under the License.
youkee 0:1ad0e04b1bc5 15 */
youkee 0:1ad0e04b1bc5 16 #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
youkee 0:1ad0e04b1bc5 17 #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
youkee 0:1ad0e04b1bc5 18
youkee 0:1ad0e04b1bc5 19 #include <string.h>
youkee 0:1ad0e04b1bc5 20 #include "FunctionPointerWithContext.h"
youkee 0:1ad0e04b1bc5 21 #include "SafeBool.h"
youkee 0:1ad0e04b1bc5 22
youkee 0:1ad0e04b1bc5 23
youkee 0:1ad0e04b1bc5 24 /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in
youkee 0:1ad0e04b1bc5 25 * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code,
youkee 0:1ad0e04b1bc5 26 * but can be used for other purposes.
youkee 0:1ad0e04b1bc5 27 *
youkee 0:1ad0e04b1bc5 28 * Example:
youkee 0:1ad0e04b1bc5 29 * @code
youkee 0:1ad0e04b1bc5 30 *
youkee 0:1ad0e04b1bc5 31 * CallChainOfFunctionPointersWithContext<void *> chain;
youkee 0:1ad0e04b1bc5 32 *
youkee 0:1ad0e04b1bc5 33 * void first(void *context) {
youkee 0:1ad0e04b1bc5 34 * printf("'first' function.\n");
youkee 0:1ad0e04b1bc5 35 * }
youkee 0:1ad0e04b1bc5 36 *
youkee 0:1ad0e04b1bc5 37 * void second(void *context) {
youkee 0:1ad0e04b1bc5 38 * printf("'second' function.\n");
youkee 0:1ad0e04b1bc5 39 * }
youkee 0:1ad0e04b1bc5 40 *
youkee 0:1ad0e04b1bc5 41 * class Test {
youkee 0:1ad0e04b1bc5 42 * public:
youkee 0:1ad0e04b1bc5 43 * void f(void *context) {
youkee 0:1ad0e04b1bc5 44 * printf("A::f (class member).\n");
youkee 0:1ad0e04b1bc5 45 * }
youkee 0:1ad0e04b1bc5 46 * };
youkee 0:1ad0e04b1bc5 47 *
youkee 0:1ad0e04b1bc5 48 * int main() {
youkee 0:1ad0e04b1bc5 49 * Test test;
youkee 0:1ad0e04b1bc5 50 *
youkee 0:1ad0e04b1bc5 51 * chain.add(second);
youkee 0:1ad0e04b1bc5 52 * chain.add_front(first);
youkee 0:1ad0e04b1bc5 53 * chain.add(&test, &Test::f);
youkee 0:1ad0e04b1bc5 54 * chain.call();
youkee 0:1ad0e04b1bc5 55 * }
youkee 0:1ad0e04b1bc5 56 * @endcode
youkee 0:1ad0e04b1bc5 57 */
youkee 0:1ad0e04b1bc5 58
youkee 0:1ad0e04b1bc5 59 template <typename ContextType>
youkee 0:1ad0e04b1bc5 60 class CallChainOfFunctionPointersWithContext : public SafeBool<CallChainOfFunctionPointersWithContext<ContextType> > {
youkee 0:1ad0e04b1bc5 61 public:
youkee 0:1ad0e04b1bc5 62 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
youkee 0:1ad0e04b1bc5 63
youkee 0:1ad0e04b1bc5 64 public:
youkee 0:1ad0e04b1bc5 65 /** Create an empty chain.
youkee 0:1ad0e04b1bc5 66 *
youkee 0:1ad0e04b1bc5 67 * @param size (optional) Initial size of the chain.
youkee 0:1ad0e04b1bc5 68 */
youkee 0:1ad0e04b1bc5 69 CallChainOfFunctionPointersWithContext() : chainHead(NULL) {
youkee 0:1ad0e04b1bc5 70 /* empty */
youkee 0:1ad0e04b1bc5 71 }
youkee 0:1ad0e04b1bc5 72
youkee 0:1ad0e04b1bc5 73 virtual ~CallChainOfFunctionPointersWithContext() {
youkee 0:1ad0e04b1bc5 74 clear();
youkee 0:1ad0e04b1bc5 75 }
youkee 0:1ad0e04b1bc5 76
youkee 0:1ad0e04b1bc5 77 /** Add a function at the front of the chain.
youkee 0:1ad0e04b1bc5 78 *
youkee 0:1ad0e04b1bc5 79 * @param function A pointer to a void function.
youkee 0:1ad0e04b1bc5 80 *
youkee 0:1ad0e04b1bc5 81 * @returns
youkee 0:1ad0e04b1bc5 82 * The function object created for 'function'.
youkee 0:1ad0e04b1bc5 83 */
youkee 0:1ad0e04b1bc5 84 pFunctionPointerWithContext_t add(void (*function)(ContextType context)) {
youkee 0:1ad0e04b1bc5 85 return common_add(new FunctionPointerWithContext<ContextType>(function));
youkee 0:1ad0e04b1bc5 86 }
youkee 0:1ad0e04b1bc5 87
youkee 0:1ad0e04b1bc5 88 /** Add a function at the front of the chain.
youkee 0:1ad0e04b1bc5 89 *
youkee 0:1ad0e04b1bc5 90 * @param tptr Pointer to the object to call the member function on.
youkee 0:1ad0e04b1bc5 91 * @param mptr Pointer to the member function to be called.
youkee 0:1ad0e04b1bc5 92 *
youkee 0:1ad0e04b1bc5 93 * @returns
youkee 0:1ad0e04b1bc5 94 * The function object created for 'tptr' and 'mptr'.
youkee 0:1ad0e04b1bc5 95 */
youkee 0:1ad0e04b1bc5 96 template<typename T>
youkee 0:1ad0e04b1bc5 97 pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) {
youkee 0:1ad0e04b1bc5 98 return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
youkee 0:1ad0e04b1bc5 99 }
youkee 0:1ad0e04b1bc5 100
youkee 0:1ad0e04b1bc5 101 /** Add a function at the front of the chain.
youkee 0:1ad0e04b1bc5 102 *
youkee 0:1ad0e04b1bc5 103 * @param func The FunctionPointerWithContext to add.
youkee 0:1ad0e04b1bc5 104 */
youkee 0:1ad0e04b1bc5 105 pFunctionPointerWithContext_t add(const FunctionPointerWithContext<ContextType>& func) {
youkee 0:1ad0e04b1bc5 106 return common_add(new FunctionPointerWithContext<ContextType>(func));
youkee 0:1ad0e04b1bc5 107 }
youkee 0:1ad0e04b1bc5 108
youkee 0:1ad0e04b1bc5 109 /**
youkee 0:1ad0e04b1bc5 110 * Detach a function pointer from a callchain
youkee 0:1ad0e04b1bc5 111 *
youkee 0:1ad0e04b1bc5 112 * @oaram toDetach FunctionPointerWithContext to detach from this callchain
youkee 0:1ad0e04b1bc5 113 *
youkee 0:1ad0e04b1bc5 114 * @return true if a function pointer has been detached and false otherwise
youkee 0:1ad0e04b1bc5 115 */
youkee 0:1ad0e04b1bc5 116 bool detach(const FunctionPointerWithContext<ContextType>& toDetach) {
youkee 0:1ad0e04b1bc5 117 pFunctionPointerWithContext_t current = chainHead;
youkee 0:1ad0e04b1bc5 118 pFunctionPointerWithContext_t previous = NULL;
youkee 0:1ad0e04b1bc5 119
youkee 0:1ad0e04b1bc5 120 while (current) {
youkee 0:1ad0e04b1bc5 121 if(*current == toDetach) {
youkee 0:1ad0e04b1bc5 122 if(previous == NULL) {
youkee 0:1ad0e04b1bc5 123 if(currentCalled == current) {
youkee 0:1ad0e04b1bc5 124 currentCalled = NULL;
youkee 0:1ad0e04b1bc5 125 }
youkee 0:1ad0e04b1bc5 126 chainHead = current->getNext();
youkee 0:1ad0e04b1bc5 127 } else {
youkee 0:1ad0e04b1bc5 128 if(currentCalled == current) {
youkee 0:1ad0e04b1bc5 129 currentCalled = previous;
youkee 0:1ad0e04b1bc5 130 }
youkee 0:1ad0e04b1bc5 131 previous->chainAsNext(current->getNext());
youkee 0:1ad0e04b1bc5 132 }
youkee 0:1ad0e04b1bc5 133 delete current;
youkee 0:1ad0e04b1bc5 134 return true;
youkee 0:1ad0e04b1bc5 135 }
youkee 0:1ad0e04b1bc5 136
youkee 0:1ad0e04b1bc5 137 previous = current;
youkee 0:1ad0e04b1bc5 138 current = current->getNext();
youkee 0:1ad0e04b1bc5 139 }
youkee 0:1ad0e04b1bc5 140
youkee 0:1ad0e04b1bc5 141 return false;
youkee 0:1ad0e04b1bc5 142 }
youkee 0:1ad0e04b1bc5 143
youkee 0:1ad0e04b1bc5 144 /** Clear the call chain (remove all functions in the chain).
youkee 0:1ad0e04b1bc5 145 */
youkee 0:1ad0e04b1bc5 146 void clear(void) {
youkee 0:1ad0e04b1bc5 147 pFunctionPointerWithContext_t fptr = chainHead;
youkee 0:1ad0e04b1bc5 148 while (fptr) {
youkee 0:1ad0e04b1bc5 149 pFunctionPointerWithContext_t deadPtr = fptr;
youkee 0:1ad0e04b1bc5 150 fptr = deadPtr->getNext();
youkee 0:1ad0e04b1bc5 151 delete deadPtr;
youkee 0:1ad0e04b1bc5 152 }
youkee 0:1ad0e04b1bc5 153
youkee 0:1ad0e04b1bc5 154 chainHead = NULL;
youkee 0:1ad0e04b1bc5 155 }
youkee 0:1ad0e04b1bc5 156
youkee 0:1ad0e04b1bc5 157 bool hasCallbacksAttached(void) const {
youkee 0:1ad0e04b1bc5 158 return (chainHead != NULL);
youkee 0:1ad0e04b1bc5 159 }
youkee 0:1ad0e04b1bc5 160
youkee 0:1ad0e04b1bc5 161 /** Call all the functions in the chain in sequence
youkee 0:1ad0e04b1bc5 162 */
youkee 0:1ad0e04b1bc5 163 void call(ContextType context) {
youkee 0:1ad0e04b1bc5 164 ((const CallChainOfFunctionPointersWithContext*) this)->call(context);
youkee 0:1ad0e04b1bc5 165 }
youkee 0:1ad0e04b1bc5 166
youkee 0:1ad0e04b1bc5 167 /**
youkee 0:1ad0e04b1bc5 168 * @brief same as above but const
youkee 0:1ad0e04b1bc5 169 */
youkee 0:1ad0e04b1bc5 170 void call(ContextType context) const {
youkee 0:1ad0e04b1bc5 171 currentCalled = chainHead;
youkee 0:1ad0e04b1bc5 172
youkee 0:1ad0e04b1bc5 173 while(currentCalled) {
youkee 0:1ad0e04b1bc5 174 currentCalled->call(context);
youkee 0:1ad0e04b1bc5 175 // if this was the head and the call removed the head
youkee 0:1ad0e04b1bc5 176 if(currentCalled == NULL) {
youkee 0:1ad0e04b1bc5 177 currentCalled = chainHead;
youkee 0:1ad0e04b1bc5 178 } else {
youkee 0:1ad0e04b1bc5 179 currentCalled = currentCalled->getNext();
youkee 0:1ad0e04b1bc5 180 }
youkee 0:1ad0e04b1bc5 181 }
youkee 0:1ad0e04b1bc5 182 }
youkee 0:1ad0e04b1bc5 183
youkee 0:1ad0e04b1bc5 184 /**
youkee 0:1ad0e04b1bc5 185 * @brief same as above but with function call operator
youkee 0:1ad0e04b1bc5 186 * \code
youkee 0:1ad0e04b1bc5 187 *
youkee 0:1ad0e04b1bc5 188 * void first(bool);
youkee 0:1ad0e04b1bc5 189 * void second(bool);
youkee 0:1ad0e04b1bc5 190 *
youkee 0:1ad0e04b1bc5 191 * CallChainOfFunctionPointerWithContext<bool> foo;
youkee 0:1ad0e04b1bc5 192 *
youkee 0:1ad0e04b1bc5 193 * foo.attach(first);
youkee 0:1ad0e04b1bc5 194 * foo.attach(second);
youkee 0:1ad0e04b1bc5 195 *
youkee 0:1ad0e04b1bc5 196 * // call the callchain like a function
youkee 0:1ad0e04b1bc5 197 * foo(true);
youkee 0:1ad0e04b1bc5 198 *
youkee 0:1ad0e04b1bc5 199 * \endcode
youkee 0:1ad0e04b1bc5 200 */
youkee 0:1ad0e04b1bc5 201 void operator()(ContextType context) const {
youkee 0:1ad0e04b1bc5 202 call(context);
youkee 0:1ad0e04b1bc5 203 }
youkee 0:1ad0e04b1bc5 204
youkee 0:1ad0e04b1bc5 205 /**
youkee 0:1ad0e04b1bc5 206 * @brief bool conversion operation
youkee 0:1ad0e04b1bc5 207 */
youkee 0:1ad0e04b1bc5 208 bool toBool() const {
youkee 0:1ad0e04b1bc5 209 return chainHead != NULL;
youkee 0:1ad0e04b1bc5 210 }
youkee 0:1ad0e04b1bc5 211
youkee 0:1ad0e04b1bc5 212 private:
youkee 0:1ad0e04b1bc5 213 pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) {
youkee 0:1ad0e04b1bc5 214 if (chainHead == NULL) {
youkee 0:1ad0e04b1bc5 215 chainHead = pf;
youkee 0:1ad0e04b1bc5 216 } else {
youkee 0:1ad0e04b1bc5 217 pf->chainAsNext(chainHead);
youkee 0:1ad0e04b1bc5 218 chainHead = pf;
youkee 0:1ad0e04b1bc5 219 }
youkee 0:1ad0e04b1bc5 220
youkee 0:1ad0e04b1bc5 221 return chainHead;
youkee 0:1ad0e04b1bc5 222 }
youkee 0:1ad0e04b1bc5 223
youkee 0:1ad0e04b1bc5 224 private:
youkee 0:1ad0e04b1bc5 225 pFunctionPointerWithContext_t chainHead;
youkee 0:1ad0e04b1bc5 226 // iterator during a function call, this has to be mutable because the call function is const.
youkee 0:1ad0e04b1bc5 227 // Note: mutable is the correct behaviour here, the iterator never leak outside the object.
youkee 0:1ad0e04b1bc5 228 // So the object can still be seen as logically const even if it change its internal state
youkee 0:1ad0e04b1bc5 229 mutable pFunctionPointerWithContext_t currentCalled;
youkee 0:1ad0e04b1bc5 230
youkee 0:1ad0e04b1bc5 231
youkee 0:1ad0e04b1bc5 232 /* Disallow copy constructor and assignment operators. */
youkee 0:1ad0e04b1bc5 233 private:
youkee 0:1ad0e04b1bc5 234 CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &);
youkee 0:1ad0e04b1bc5 235 CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &);
youkee 0:1ad0e04b1bc5 236 };
youkee 0:1ad0e04b1bc5 237
youkee 0:1ad0e04b1bc5 238 #endif