テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8468a4403fea 1 /* mbed Microcontroller Library
jksoft 0:8468a4403fea 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 0:8468a4403fea 3 *
jksoft 0:8468a4403fea 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:8468a4403fea 5 * you may not use this file except in compliance with the License.
jksoft 0:8468a4403fea 6 * You may obtain a copy of the License at
jksoft 0:8468a4403fea 7 *
jksoft 0:8468a4403fea 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:8468a4403fea 9 *
jksoft 0:8468a4403fea 10 * Unless required by applicable law or agreed to in writing, software
jksoft 0:8468a4403fea 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:8468a4403fea 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:8468a4403fea 13 * See the License for the specific language governing permissions and
jksoft 0:8468a4403fea 14 * limitations under the License.
jksoft 0:8468a4403fea 15 */
jksoft 0:8468a4403fea 16 #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
jksoft 0:8468a4403fea 17 #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
jksoft 0:8468a4403fea 18
jksoft 0:8468a4403fea 19 #include <string.h>
jksoft 0:8468a4403fea 20 #include "FunctionPointerWithContext.h"
jksoft 0:8468a4403fea 21
jksoft 0:8468a4403fea 22
jksoft 0:8468a4403fea 23 /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in
jksoft 0:8468a4403fea 24 * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code,
jksoft 0:8468a4403fea 25 * but can be used for other purposes.
jksoft 0:8468a4403fea 26 *
jksoft 0:8468a4403fea 27 * Example:
jksoft 0:8468a4403fea 28 * @code
jksoft 0:8468a4403fea 29 *
jksoft 0:8468a4403fea 30 * CallChainOfFunctionPointersWithContext<void *> chain;
jksoft 0:8468a4403fea 31 *
jksoft 0:8468a4403fea 32 * void first(void *context) {
jksoft 0:8468a4403fea 33 * printf("'first' function.\n");
jksoft 0:8468a4403fea 34 * }
jksoft 0:8468a4403fea 35 *
jksoft 0:8468a4403fea 36 * void second(void *context) {
jksoft 0:8468a4403fea 37 * printf("'second' function.\n");
jksoft 0:8468a4403fea 38 * }
jksoft 0:8468a4403fea 39 *
jksoft 0:8468a4403fea 40 * class Test {
jksoft 0:8468a4403fea 41 * public:
jksoft 0:8468a4403fea 42 * void f(void *context) {
jksoft 0:8468a4403fea 43 * printf("A::f (class member).\n");
jksoft 0:8468a4403fea 44 * }
jksoft 0:8468a4403fea 45 * };
jksoft 0:8468a4403fea 46 *
jksoft 0:8468a4403fea 47 * int main() {
jksoft 0:8468a4403fea 48 * Test test;
jksoft 0:8468a4403fea 49 *
jksoft 0:8468a4403fea 50 * chain.add(second);
jksoft 0:8468a4403fea 51 * chain.add_front(first);
jksoft 0:8468a4403fea 52 * chain.add(&test, &Test::f);
jksoft 0:8468a4403fea 53 * chain.call();
jksoft 0:8468a4403fea 54 * }
jksoft 0:8468a4403fea 55 * @endcode
jksoft 0:8468a4403fea 56 */
jksoft 0:8468a4403fea 57
jksoft 0:8468a4403fea 58 template <typename ContextType>
jksoft 0:8468a4403fea 59 class CallChainOfFunctionPointersWithContext {
jksoft 0:8468a4403fea 60 public:
jksoft 0:8468a4403fea 61 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
jksoft 0:8468a4403fea 62
jksoft 0:8468a4403fea 63 public:
jksoft 0:8468a4403fea 64 /** Create an empty chain
jksoft 0:8468a4403fea 65 *
jksoft 0:8468a4403fea 66 * @param size (optional) Initial size of the chain
jksoft 0:8468a4403fea 67 */
jksoft 0:8468a4403fea 68 CallChainOfFunctionPointersWithContext() : chainHead(NULL) {
jksoft 0:8468a4403fea 69 /* empty */
jksoft 0:8468a4403fea 70 }
jksoft 0:8468a4403fea 71
jksoft 0:8468a4403fea 72 virtual ~CallChainOfFunctionPointersWithContext() {
jksoft 0:8468a4403fea 73 clear();
jksoft 0:8468a4403fea 74 }
jksoft 0:8468a4403fea 75
jksoft 0:8468a4403fea 76 /** Add a function at the front of the chain
jksoft 0:8468a4403fea 77 *
jksoft 0:8468a4403fea 78 * @param function A pointer to a void function
jksoft 0:8468a4403fea 79 *
jksoft 0:8468a4403fea 80 * @returns
jksoft 0:8468a4403fea 81 * The function object created for 'function'
jksoft 0:8468a4403fea 82 */
jksoft 0:8468a4403fea 83 pFunctionPointerWithContext_t add(void (*function)(ContextType context)) {
jksoft 0:8468a4403fea 84 return common_add(new FunctionPointerWithContext<ContextType>(function));
jksoft 0:8468a4403fea 85 }
jksoft 0:8468a4403fea 86
jksoft 0:8468a4403fea 87 /** Add a function at the front of the chain
jksoft 0:8468a4403fea 88 *
jksoft 0:8468a4403fea 89 * @param tptr pointer to the object to call the member function on
jksoft 0:8468a4403fea 90 * @param mptr pointer to the member function to be called
jksoft 0:8468a4403fea 91 *
jksoft 0:8468a4403fea 92 * @returns
jksoft 0:8468a4403fea 93 * The function object created for 'tptr' and 'mptr'
jksoft 0:8468a4403fea 94 */
jksoft 0:8468a4403fea 95 template<typename T>
jksoft 0:8468a4403fea 96 pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) {
jksoft 0:8468a4403fea 97 return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
jksoft 0:8468a4403fea 98 }
jksoft 0:8468a4403fea 99
jksoft 0:8468a4403fea 100 /** Clear the call chain (remove all functions in the chain).
jksoft 0:8468a4403fea 101 */
jksoft 0:8468a4403fea 102 void clear(void) {
jksoft 0:8468a4403fea 103 pFunctionPointerWithContext_t fptr = chainHead;
jksoft 0:8468a4403fea 104 while (fptr) {
jksoft 0:8468a4403fea 105 pFunctionPointerWithContext_t deadPtr = fptr;
jksoft 0:8468a4403fea 106 fptr = deadPtr->getNext();
jksoft 0:8468a4403fea 107 delete deadPtr;
jksoft 0:8468a4403fea 108 }
jksoft 0:8468a4403fea 109
jksoft 0:8468a4403fea 110 chainHead = NULL;
jksoft 0:8468a4403fea 111 }
jksoft 0:8468a4403fea 112
jksoft 0:8468a4403fea 113 bool hasCallbacksAttached(void) const {
jksoft 0:8468a4403fea 114 return (chainHead != NULL);
jksoft 0:8468a4403fea 115 }
jksoft 0:8468a4403fea 116
jksoft 0:8468a4403fea 117 /** Call all the functions in the chain in sequence
jksoft 0:8468a4403fea 118 * @Note: the stack frames of all the callbacks within the chained
jksoft 0:8468a4403fea 119 * FunctionPointers will stack up. Hopefully there won't be too many
jksoft 0:8468a4403fea 120 * chained FunctionPointers.
jksoft 0:8468a4403fea 121 */
jksoft 0:8468a4403fea 122 void call(ContextType context) {
jksoft 0:8468a4403fea 123 if (chainHead) {
jksoft 0:8468a4403fea 124 chainHead->call(context);
jksoft 0:8468a4403fea 125 }
jksoft 0:8468a4403fea 126 }
jksoft 0:8468a4403fea 127
jksoft 0:8468a4403fea 128 private:
jksoft 0:8468a4403fea 129 pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) {
jksoft 0:8468a4403fea 130 if (chainHead == NULL) {
jksoft 0:8468a4403fea 131 chainHead = pf;
jksoft 0:8468a4403fea 132 } else {
jksoft 0:8468a4403fea 133 pf->chainAsNext(chainHead);
jksoft 0:8468a4403fea 134 chainHead = pf;
jksoft 0:8468a4403fea 135 }
jksoft 0:8468a4403fea 136
jksoft 0:8468a4403fea 137 return chainHead;
jksoft 0:8468a4403fea 138 }
jksoft 0:8468a4403fea 139
jksoft 0:8468a4403fea 140 private:
jksoft 0:8468a4403fea 141 pFunctionPointerWithContext_t chainHead;
jksoft 0:8468a4403fea 142
jksoft 0:8468a4403fea 143 /* disallow copy constructor and assignment operators */
jksoft 0:8468a4403fea 144 private:
jksoft 0:8468a4403fea 145 CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &);
jksoft 0:8468a4403fea 146 CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &);
jksoft 0:8468a4403fea 147 };
jksoft 0:8468a4403fea 148
jksoft 0:8468a4403fea 149 #endif