UVic Assistive Technology Lab / Mbed 2 deprecated DSLR_Camera_Gimbal

Dependencies:   mbed ros_lib_kinetic

Committer:
group-UVic-Assistive-Technolog
Date:
Wed Jan 31 05:24:12 2018 +0000
Revision:
0:3a767f41cf04
Initial commit

Who changed what in which revision?

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