Bike service

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Nov 21 09:23:22 2014 +0000
Revision:
126:fdebe4d5d62f
Parent:
118:620d28e7a1ba
Child:
127:4e106f4a80b7
Synchronized with git rev 1d0b27e5
Author: Yihui Xiong
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
Rohit Grover 118:620d28e7a1ba 17 #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
Rohit Grover 118:620d28e7a1ba 18 #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
Rohit Grover 118:620d28e7a1ba 19
Rohit Grover 118:620d28e7a1ba 20 #include <string.h>
Rohit Grover 118:620d28e7a1ba 21
Rohit Grover 118:620d28e7a1ba 22
Rohit Grover 118:620d28e7a1ba 23 /** A class for storing and calling a pointer to a static or member void function
Rohit Grover 118:620d28e7a1ba 24 * which takes a context.
Rohit Grover 118:620d28e7a1ba 25 */
Rohit Grover 118:620d28e7a1ba 26 template <typename ContextType>
Rohit Grover 118:620d28e7a1ba 27 class FunctionPointerWithContext {
Rohit Grover 118:620d28e7a1ba 28 public:
Rohit Grover 118:620d28e7a1ba 29 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
Rohit Grover 118:620d28e7a1ba 30 typedef void (*pvoidfcontext_t)(ContextType context);
Rohit Grover 118:620d28e7a1ba 31
Rohit Grover 118:620d28e7a1ba 32 /** Create a FunctionPointerWithContext, attaching a static function
Rohit Grover 118:620d28e7a1ba 33 *
Rohit Grover 118:620d28e7a1ba 34 * @param function The void static function to attach (default is none)
Rohit Grover 118:620d28e7a1ba 35 */
Rohit Grover 118:620d28e7a1ba 36 FunctionPointerWithContext(void (*function)(ContextType context) = NULL) :
Rohit Grover 118:620d28e7a1ba 37 _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) {
Rohit Grover 118:620d28e7a1ba 38 attach(function);
Rohit Grover 118:620d28e7a1ba 39 }
Rohit Grover 118:620d28e7a1ba 40
Rohit Grover 118:620d28e7a1ba 41 /** Create a FunctionPointerWithContext, attaching a member function
Rohit Grover 118:620d28e7a1ba 42 *
Rohit Grover 118:620d28e7a1ba 43 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
Rohit Grover 118:620d28e7a1ba 44 * @param function The address of the void member function to attach
Rohit Grover 118:620d28e7a1ba 45 */
Rohit Grover 118:620d28e7a1ba 46 template<typename T>
Rohit Grover 118:620d28e7a1ba 47 FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) :
Rohit Grover 118:620d28e7a1ba 48 _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) {
Rohit Grover 118:620d28e7a1ba 49 attach(object, member);
Rohit Grover 118:620d28e7a1ba 50 }
Rohit Grover 118:620d28e7a1ba 51
Rohit Grover 118:620d28e7a1ba 52 /** Attach a static function
Rohit Grover 118:620d28e7a1ba 53 *
Rohit Grover 118:620d28e7a1ba 54 * @param function The void static function to attach (default is none)
Rohit Grover 118:620d28e7a1ba 55 */
Rohit Grover 118:620d28e7a1ba 56 void attach(void (*function)(ContextType context) = NULL) {
Rohit Grover 118:620d28e7a1ba 57 _function = function;
Rohit Grover 118:620d28e7a1ba 58 }
Rohit Grover 118:620d28e7a1ba 59
Rohit Grover 118:620d28e7a1ba 60 /** Attach a member function
Rohit Grover 118:620d28e7a1ba 61 *
Rohit Grover 118:620d28e7a1ba 62 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
Rohit Grover 118:620d28e7a1ba 63 * @param function The address of the void member function to attach
Rohit Grover 118:620d28e7a1ba 64 */
Rohit Grover 118:620d28e7a1ba 65 template<typename T>
Rohit Grover 118:620d28e7a1ba 66 void attach(T *object, void (T::*member)(ContextType context)) {
Rohit Grover 118:620d28e7a1ba 67 _object = static_cast<void *>(object);
Rohit Grover 118:620d28e7a1ba 68 memcpy(_member, (char *)&member, sizeof(member));
Rohit Grover 118:620d28e7a1ba 69 _membercaller = &FunctionPointerWithContext::membercaller<T>;
Rohit Grover 118:620d28e7a1ba 70 }
Rohit Grover 118:620d28e7a1ba 71
Rohit Grover 118:620d28e7a1ba 72 /** Call the attached static or member function; and if there are chained
Rohit Grover 118:620d28e7a1ba 73 * FunctionPointers their callbacks are invoked as well.
Rohit Grover 118:620d28e7a1ba 74 * @Note: all chained callbacks stack up; so hopefully there won't be too
Rohit Grover 118:620d28e7a1ba 75 * many FunctionPointers in a chain. */
Rohit Grover 118:620d28e7a1ba 76 void call(ContextType context) {
Rohit Grover 118:620d28e7a1ba 77 if (_function) {
Rohit Grover 118:620d28e7a1ba 78 _function(context);
Rohit Grover 118:620d28e7a1ba 79 } else if (_object && _membercaller) {
Rohit Grover 118:620d28e7a1ba 80 _membercaller(_object, _member, context);
Rohit Grover 118:620d28e7a1ba 81 }
Rohit Grover 118:620d28e7a1ba 82
Rohit Grover 118:620d28e7a1ba 83 /* Propagate the call to next in the chain. */
Rohit Grover 118:620d28e7a1ba 84 if (_next) {
Rohit Grover 118:620d28e7a1ba 85 _next->call(context);
Rohit Grover 118:620d28e7a1ba 86 }
Rohit Grover 118:620d28e7a1ba 87 }
Rohit Grover 118:620d28e7a1ba 88
Rohit Grover 118:620d28e7a1ba 89 /**
Rohit Grover 118:620d28e7a1ba 90 * Setup an external FunctionPointer as a next in the chain of related
Rohit Grover 118:620d28e7a1ba 91 * callbacks. Invoking call() on the head FunctionPointer will invoke all
Rohit Grover 118:620d28e7a1ba 92 * chained callbacks.
Rohit Grover 118:620d28e7a1ba 93 *
Rohit Grover 118:620d28e7a1ba 94 * Refer to 'CallChain' as an alternative.
Rohit Grover 118:620d28e7a1ba 95 */
Rohit Grover 118:620d28e7a1ba 96 void chainAsNext(pFunctionPointerWithContext_t next) {
Rohit Grover 118:620d28e7a1ba 97 _next = next;
Rohit Grover 118:620d28e7a1ba 98 }
Rohit Grover 118:620d28e7a1ba 99
Rohit Grover 118:620d28e7a1ba 100 pFunctionPointerWithContext_t getNext(void) const {
Rohit Grover 118:620d28e7a1ba 101 return _next;
Rohit Grover 118:620d28e7a1ba 102 }
Rohit Grover 118:620d28e7a1ba 103
Rohit Grover 118:620d28e7a1ba 104 pvoidfcontext_t get_function() const {
Rohit Grover 118:620d28e7a1ba 105 return (pvoidfcontext_t)_function;
Rohit Grover 118:620d28e7a1ba 106 }
Rohit Grover 118:620d28e7a1ba 107
Rohit Grover 118:620d28e7a1ba 108 private:
Rohit Grover 118:620d28e7a1ba 109 template<typename T>
Rohit Grover 118:620d28e7a1ba 110 static void membercaller(void *object, char *member, ContextType context) {
Rohit Grover 118:620d28e7a1ba 111 T *o = static_cast<T *>(object);
Rohit Grover 118:620d28e7a1ba 112 void (T::*m)(ContextType);
Rohit Grover 118:620d28e7a1ba 113 memcpy((char *)&m, member, sizeof(m));
Rohit Grover 118:620d28e7a1ba 114 (o->*m)(context);
Rohit Grover 118:620d28e7a1ba 115 }
Rohit Grover 118:620d28e7a1ba 116
Rohit Grover 118:620d28e7a1ba 117 void (*_function)(ContextType context); /**< static function pointer - NULL if none attached */
Rohit Grover 118:620d28e7a1ba 118 void *_object; /**< object this pointer - NULL if none attached */
Rohit Grover 118:620d28e7a1ba 119 char _member[16]; /**< raw member function pointer storage - converted back by
Rohit Grover 118:620d28e7a1ba 120 * registered _membercaller */
Rohit Grover 118:620d28e7a1ba 121 void (*_membercaller)(void *, char *, ContextType); /**< registered membercaller function to convert back and call
Rohit Grover 118:620d28e7a1ba 122 * _member on _object passing the context. */
Rohit Grover 118:620d28e7a1ba 123 pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this
Rohit Grover 118:620d28e7a1ba 124 * allows chaining function pointers without requiring
Rohit Grover 118:620d28e7a1ba 125 * external memory to manage the chain. Also refer to
Rohit Grover 118:620d28e7a1ba 126 * 'CallChain' as an alternative. */
Rohit Grover 118:620d28e7a1ba 127 };
Rohit Grover 118:620d28e7a1ba 128
rgrover1 126:fdebe4d5d62f 129 #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H