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