aa

Dependents:   Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 21 09:23:24 2014 +0000
Revision:
139:baaf1c5f0db2
Parent:
135:6cf6e7bd21c9
Child:
140:407d134c179d
Synchronized with git rev d93ad344
Author: Rohit Grover
Merge pull request #5 from xiongyihui/master

make the library less dependent on the mbed.h header

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rohit Grover 118:620d28e7a1ba 1 /* mbed Microcontroller Library
Rohit Grover 118:620d28e7a1ba 2 * Copyright (c) 2006-2013 ARM Limited
Rohit Grover 118:620d28e7a1ba 3 *
Rohit Grover 118:620d28e7a1ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rohit Grover 118:620d28e7a1ba 5 * you may not use this file except in compliance with the License.
Rohit Grover 118:620d28e7a1ba 6 * You may obtain a copy of the License at
Rohit Grover 118:620d28e7a1ba 7 *
Rohit Grover 118:620d28e7a1ba 8 * http://www.apache.org/licenses/LICENSE-2.0
Rohit Grover 118:620d28e7a1ba 9 *
Rohit Grover 118:620d28e7a1ba 10 * Unless required by applicable law or agreed to in writing, software
Rohit Grover 118:620d28e7a1ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rohit Grover 118:620d28e7a1ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rohit Grover 118:620d28e7a1ba 13 * See the License for the specific language governing permissions and
Rohit Grover 118:620d28e7a1ba 14 * limitations under the License.
Rohit Grover 118:620d28e7a1ba 15 */
Rohit Grover 118:620d28e7a1ba 16 #ifndef MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
Rohit Grover 118:620d28e7a1ba 17 #define MBED_CALLCHAIN_OF_FUNCTION_POINTERS_WITH_CONTEXT_H
Rohit Grover 118:620d28e7a1ba 18
Rohit Grover 118:620d28e7a1ba 19 #include <string.h>
Rohit Grover 118:620d28e7a1ba 20 #include "FunctionPointerWithContext.h"
Rohit Grover 118:620d28e7a1ba 21
Rohit Grover 118:620d28e7a1ba 22
Rohit Grover 118:620d28e7a1ba 23 /** Group one or more functions in an instance of a CallChainOfFunctionPointersWithContext, then call them in
Rohit Grover 118:620d28e7a1ba 24 * sequence using CallChainOfFunctionPointersWithContext::call(). Used mostly by the interrupt chaining code,
Rohit Grover 118:620d28e7a1ba 25 * but can be used for other purposes.
Rohit Grover 118:620d28e7a1ba 26 *
Rohit Grover 118:620d28e7a1ba 27 * Example:
Rohit Grover 118:620d28e7a1ba 28 * @code
Rohit Grover 118:620d28e7a1ba 29 *
Rohit Grover 118:620d28e7a1ba 30 * CallChainOfFunctionPointersWithContext<void *> chain;
Rohit Grover 118:620d28e7a1ba 31 *
Rohit Grover 118:620d28e7a1ba 32 * void first(void *context) {
Rohit Grover 118:620d28e7a1ba 33 * printf("'first' function.\n");
Rohit Grover 118:620d28e7a1ba 34 * }
Rohit Grover 118:620d28e7a1ba 35 *
Rohit Grover 118:620d28e7a1ba 36 * void second(void *context) {
Rohit Grover 118:620d28e7a1ba 37 * printf("'second' function.\n");
Rohit Grover 118:620d28e7a1ba 38 * }
Rohit Grover 118:620d28e7a1ba 39 *
Rohit Grover 118:620d28e7a1ba 40 * class Test {
Rohit Grover 118:620d28e7a1ba 41 * public:
Rohit Grover 118:620d28e7a1ba 42 * void f(void *context) {
Rohit Grover 118:620d28e7a1ba 43 * printf("A::f (class member).\n");
Rohit Grover 118:620d28e7a1ba 44 * }
Rohit Grover 118:620d28e7a1ba 45 * };
Rohit Grover 118:620d28e7a1ba 46 *
Rohit Grover 118:620d28e7a1ba 47 * int main() {
Rohit Grover 118:620d28e7a1ba 48 * Test test;
Rohit Grover 118:620d28e7a1ba 49 *
Rohit Grover 118:620d28e7a1ba 50 * chain.add(second);
Rohit Grover 118:620d28e7a1ba 51 * chain.add_front(first);
Rohit Grover 118:620d28e7a1ba 52 * chain.add(&test, &Test::f);
Rohit Grover 118:620d28e7a1ba 53 * chain.call();
Rohit Grover 118:620d28e7a1ba 54 * }
Rohit Grover 118:620d28e7a1ba 55 * @endcode
Rohit Grover 118:620d28e7a1ba 56 */
Rohit Grover 118:620d28e7a1ba 57
Rohit Grover 118:620d28e7a1ba 58 template <typename ContextType>
Rohit Grover 118:620d28e7a1ba 59 class CallChainOfFunctionPointersWithContext {
Rohit Grover 118:620d28e7a1ba 60 public:
Rohit Grover 118:620d28e7a1ba 61 typedef FunctionPointerWithContext<ContextType>* pFunctionPointerWithContext_t;
Rohit Grover 118:620d28e7a1ba 62
Rohit Grover 118:620d28e7a1ba 63 public:
Rohit Grover 118:620d28e7a1ba 64 /** Create an empty chain
Rohit Grover 118:620d28e7a1ba 65 *
Rohit Grover 118:620d28e7a1ba 66 * @param size (optional) Initial size of the chain
Rohit Grover 118:620d28e7a1ba 67 */
Rohit Grover 118:620d28e7a1ba 68 CallChainOfFunctionPointersWithContext() : chainHead(NULL) {
Rohit Grover 118:620d28e7a1ba 69 /* empty */
Rohit Grover 118:620d28e7a1ba 70 }
Rohit Grover 118:620d28e7a1ba 71
Rohit Grover 118:620d28e7a1ba 72 virtual ~CallChainOfFunctionPointersWithContext() {
Rohit Grover 118:620d28e7a1ba 73 clear();
Rohit Grover 118:620d28e7a1ba 74 }
Rohit Grover 118:620d28e7a1ba 75
Rohit Grover 118:620d28e7a1ba 76 /** Add a function at the front of the chain
Rohit Grover 118:620d28e7a1ba 77 *
Rohit Grover 118:620d28e7a1ba 78 * @param function A pointer to a void function
Rohit Grover 118:620d28e7a1ba 79 *
Rohit Grover 118:620d28e7a1ba 80 * @returns
Rohit Grover 118:620d28e7a1ba 81 * The function object created for 'function'
Rohit Grover 118:620d28e7a1ba 82 */
Rohit Grover 118:620d28e7a1ba 83 pFunctionPointerWithContext_t add(void (*function)(ContextType context)) {
Rohit Grover 118:620d28e7a1ba 84 return common_add(new FunctionPointerWithContext<ContextType>(function));
Rohit Grover 118:620d28e7a1ba 85 }
Rohit Grover 118:620d28e7a1ba 86
Rohit Grover 118:620d28e7a1ba 87 /** Add a function at the front of the chain
Rohit Grover 118:620d28e7a1ba 88 *
Rohit Grover 118:620d28e7a1ba 89 * @param tptr pointer to the object to call the member function on
Rohit Grover 118:620d28e7a1ba 90 * @param mptr pointer to the member function to be called
Rohit Grover 118:620d28e7a1ba 91 *
Rohit Grover 118:620d28e7a1ba 92 * @returns
Rohit Grover 118:620d28e7a1ba 93 * The function object created for 'tptr' and 'mptr'
Rohit Grover 118:620d28e7a1ba 94 */
Rohit Grover 118:620d28e7a1ba 95 template<typename T>
Rohit Grover 118:620d28e7a1ba 96 pFunctionPointerWithContext_t add(T *tptr, void (T::*mptr)(ContextType context)) {
Rohit Grover 118:620d28e7a1ba 97 return common_add(new FunctionPointerWithContext<ContextType>(tptr, mptr));
Rohit Grover 118:620d28e7a1ba 98 }
Rohit Grover 118:620d28e7a1ba 99
Rohit Grover 118:620d28e7a1ba 100 /** Clear the call chain (remove all functions in the chain).
Rohit Grover 118:620d28e7a1ba 101 */
Rohit Grover 118:620d28e7a1ba 102 void clear(void) {
Rohit Grover 118:620d28e7a1ba 103 pFunctionPointerWithContext_t fptr = chainHead;
Rohit Grover 118:620d28e7a1ba 104 while (fptr) {
Rohit Grover 118:620d28e7a1ba 105 pFunctionPointerWithContext_t deadPtr = fptr;
Rohit Grover 118:620d28e7a1ba 106 fptr = deadPtr->getNext();
Rohit Grover 118:620d28e7a1ba 107 delete deadPtr;
Rohit Grover 118:620d28e7a1ba 108 }
Rohit Grover 118:620d28e7a1ba 109
Rohit Grover 118:620d28e7a1ba 110 chainHead = NULL;
Rohit Grover 118:620d28e7a1ba 111 }
Rohit Grover 118:620d28e7a1ba 112
Rohit Grover 118:620d28e7a1ba 113 bool hasCallbacksAttached(void) const {
Rohit Grover 118:620d28e7a1ba 114 return (chainHead != NULL);
Rohit Grover 118:620d28e7a1ba 115 }
Rohit Grover 118:620d28e7a1ba 116
Rohit Grover 118:620d28e7a1ba 117 /** Call all the functions in the chain in sequence
Rohit Grover 118:620d28e7a1ba 118 * @Note: the stack frames of all the callbacks within the chained
Rohit Grover 118:620d28e7a1ba 119 * FunctionPointers will stack up. Hopefully there won't be too many
Rohit Grover 118:620d28e7a1ba 120 * chained FunctionPointers.
Rohit Grover 118:620d28e7a1ba 121 */
Rohit Grover 118:620d28e7a1ba 122 void call(ContextType context) {
Rohit Grover 118:620d28e7a1ba 123 if (chainHead)
Rohit Grover 118:620d28e7a1ba 124 chainHead->call(context);
Rohit Grover 118:620d28e7a1ba 125 }
Rohit Grover 118:620d28e7a1ba 126
Rohit Grover 118:620d28e7a1ba 127 private:
Rohit Grover 118:620d28e7a1ba 128 pFunctionPointerWithContext_t common_add(pFunctionPointerWithContext_t pf) {
Rohit Grover 118:620d28e7a1ba 129 if (chainHead == NULL) {
Rohit Grover 118:620d28e7a1ba 130 chainHead = pf;
Rohit Grover 118:620d28e7a1ba 131 } else {
Rohit Grover 118:620d28e7a1ba 132 pf->chainAsNext(chainHead);
Rohit Grover 118:620d28e7a1ba 133 chainHead = pf;
Rohit Grover 118:620d28e7a1ba 134 }
Rohit Grover 118:620d28e7a1ba 135
Rohit Grover 118:620d28e7a1ba 136 return chainHead;
Rohit Grover 118:620d28e7a1ba 137 }
Rohit Grover 118:620d28e7a1ba 138
Rohit Grover 118:620d28e7a1ba 139 private:
Rohit Grover 118:620d28e7a1ba 140 pFunctionPointerWithContext_t chainHead;
Rohit Grover 118:620d28e7a1ba 141
Rohit Grover 118:620d28e7a1ba 142 /* disallow copy constructor and assignment operators */
Rohit Grover 118:620d28e7a1ba 143 private:
Rohit Grover 118:620d28e7a1ba 144 CallChainOfFunctionPointersWithContext(const CallChainOfFunctionPointersWithContext &);
Rohit Grover 118:620d28e7a1ba 145 CallChainOfFunctionPointersWithContext & operator = (const CallChainOfFunctionPointersWithContext &);
Rohit Grover 118:620d28e7a1ba 146 };
Rohit Grover 118:620d28e7a1ba 147
rgrover1 139:baaf1c5f0db2 148 #endif