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