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