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