IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

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