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