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