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