1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef MBED_CALLCHAIN_H
17 #define MBED_CALLCHAIN_H
19 #include "FunctionPointer.h"
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.
35 * printf("'first' function.\n");
39 * printf("'second' function.\n");
45 * printf("A::f (class member).\n");
53 * chain.add_front(first);
54 * chain.add(&test, &Test::f);
60 typedef FunctionPointer* pFunctionPointer_t;
64 /** Create an empty chain
66 * @param size (optional) Initial size of the chain
68 CallChain(int size = 4);
71 /** Add a function at the end of the chain
73 * @param function A pointer to a void function
76 * The function object created for 'function'
78 pFunctionPointer_t add(void (*function)(void));
80 /** Add a function at the end of the chain
82 * @param tptr pointer to the object to call the member function on
83 * @param mptr pointer to the member function to be called
86 * The function object created for 'tptr' and 'mptr'
89 pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
90 return common_add(new FunctionPointer(tptr, mptr));
93 /** Add a function at the beginning of the chain
95 * @param function A pointer to a void function
98 * The function object created for 'function'
100 pFunctionPointer_t add_front(void (*function)(void));
102 /** Add a function at the beginning of the chain
104 * @param tptr pointer to the object to call the member function on
105 * @param mptr pointer to the member function to be called
108 * The function object created for 'tptr' and 'mptr'
111 pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
112 return common_add_front(new FunctionPointer(tptr, mptr));
115 /** Get the number of functions in the chain
119 /** Get a function object from the chain
121 * @param i function object index
124 * The function object at position 'i' in the chain
126 pFunctionPointer_t get(int i) const;
128 /** Look for a function object in the call chain
130 * @param f the function object to search
133 * The index of the function object if found, -1 otherwise.
135 int find(pFunctionPointer_t f) const;
137 /** Clear the call chain (remove all functions in the chain).
141 /** Remove a function object from the chain
143 * @arg f the function object to remove
146 * true if the function object was found and removed, false otherwise.
148 bool remove(pFunctionPointer_t f);
150 /** Call all the functions in the chain in sequence
154 #ifdef MBED_OPERATORS
155 void operator ()(void) {
158 pFunctionPointer_t operator [](int i) const {
165 pFunctionPointer_t common_add(pFunctionPointer_t pf);
166 pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
168 pFunctionPointer_t* _chain;
172 /* disallow copy constructor and assignment operators */
174 CallChain(const CallChain&);
175 CallChain & operator = (const CallChain&);