First programBLE
Fork of Nucleo_BLE_API by
public/FunctionPointerWithContext.h@0:289fd2dae405, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 18:54:46 2014 +0000
- Revision:
- 0:289fd2dae405
BLE_API for Nucleo Bluetoothe Low Energy Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:289fd2dae405 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:289fd2dae405 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:289fd2dae405 | 3 | * |
sjallouli | 0:289fd2dae405 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:289fd2dae405 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:289fd2dae405 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:289fd2dae405 | 7 | * |
sjallouli | 0:289fd2dae405 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:289fd2dae405 | 9 | * |
sjallouli | 0:289fd2dae405 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:289fd2dae405 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:289fd2dae405 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:289fd2dae405 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:289fd2dae405 | 14 | * limitations under the License. |
sjallouli | 0:289fd2dae405 | 15 | */ |
sjallouli | 0:289fd2dae405 | 16 | |
sjallouli | 0:289fd2dae405 | 17 | #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
sjallouli | 0:289fd2dae405 | 18 | #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
sjallouli | 0:289fd2dae405 | 19 | |
sjallouli | 0:289fd2dae405 | 20 | #include <string.h> |
sjallouli | 0:289fd2dae405 | 21 | |
sjallouli | 0:289fd2dae405 | 22 | namespace mbed { |
sjallouli | 0:289fd2dae405 | 23 | |
sjallouli | 0:289fd2dae405 | 24 | /** A class for storing and calling a pointer to a static or member void function |
sjallouli | 0:289fd2dae405 | 25 | * which takes a context. |
sjallouli | 0:289fd2dae405 | 26 | */ |
sjallouli | 0:289fd2dae405 | 27 | template <typename ContextType> |
sjallouli | 0:289fd2dae405 | 28 | class FunctionPointerWithContext { |
sjallouli | 0:289fd2dae405 | 29 | public: |
sjallouli | 0:289fd2dae405 | 30 | typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t; |
sjallouli | 0:289fd2dae405 | 31 | typedef void (*pvoidfcontext_t)(ContextType context); |
sjallouli | 0:289fd2dae405 | 32 | |
sjallouli | 0:289fd2dae405 | 33 | /** Create a FunctionPointerWithContext, attaching a static function |
sjallouli | 0:289fd2dae405 | 34 | * |
sjallouli | 0:289fd2dae405 | 35 | * @param function The void static function to attach (default is none) |
sjallouli | 0:289fd2dae405 | 36 | */ |
sjallouli | 0:289fd2dae405 | 37 | FunctionPointerWithContext(void (*function)(ContextType context) = NULL) : |
sjallouli | 0:289fd2dae405 | 38 | _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) { |
sjallouli | 0:289fd2dae405 | 39 | attach(function); |
sjallouli | 0:289fd2dae405 | 40 | } |
sjallouli | 0:289fd2dae405 | 41 | |
sjallouli | 0:289fd2dae405 | 42 | /** Create a FunctionPointerWithContext, attaching a member function |
sjallouli | 0:289fd2dae405 | 43 | * |
sjallouli | 0:289fd2dae405 | 44 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
sjallouli | 0:289fd2dae405 | 45 | * @param function The address of the void member function to attach |
sjallouli | 0:289fd2dae405 | 46 | */ |
sjallouli | 0:289fd2dae405 | 47 | template<typename T> |
sjallouli | 0:289fd2dae405 | 48 | FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) : |
sjallouli | 0:289fd2dae405 | 49 | _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) { |
sjallouli | 0:289fd2dae405 | 50 | attach(object, member); |
sjallouli | 0:289fd2dae405 | 51 | } |
sjallouli | 0:289fd2dae405 | 52 | |
sjallouli | 0:289fd2dae405 | 53 | /** Attach a static function |
sjallouli | 0:289fd2dae405 | 54 | * |
sjallouli | 0:289fd2dae405 | 55 | * @param function The void static function to attach (default is none) |
sjallouli | 0:289fd2dae405 | 56 | */ |
sjallouli | 0:289fd2dae405 | 57 | void attach(void (*function)(ContextType context) = NULL) { |
sjallouli | 0:289fd2dae405 | 58 | _function = function; |
sjallouli | 0:289fd2dae405 | 59 | } |
sjallouli | 0:289fd2dae405 | 60 | |
sjallouli | 0:289fd2dae405 | 61 | /** Attach a member function |
sjallouli | 0:289fd2dae405 | 62 | * |
sjallouli | 0:289fd2dae405 | 63 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
sjallouli | 0:289fd2dae405 | 64 | * @param function The address of the void member function to attach |
sjallouli | 0:289fd2dae405 | 65 | */ |
sjallouli | 0:289fd2dae405 | 66 | template<typename T> |
sjallouli | 0:289fd2dae405 | 67 | void attach(T *object, void (T::*member)(ContextType context)) { |
sjallouli | 0:289fd2dae405 | 68 | _object = static_cast<void *>(object); |
sjallouli | 0:289fd2dae405 | 69 | memcpy(_member, (char *)&member, sizeof(member)); |
sjallouli | 0:289fd2dae405 | 70 | _membercaller = &FunctionPointerWithContext::membercaller<T>; |
sjallouli | 0:289fd2dae405 | 71 | } |
sjallouli | 0:289fd2dae405 | 72 | |
sjallouli | 0:289fd2dae405 | 73 | /** Call the attached static or member function; and if there are chained |
sjallouli | 0:289fd2dae405 | 74 | * FunctionPointers their callbacks are invoked as well. |
sjallouli | 0:289fd2dae405 | 75 | * @Note: all chained callbacks stack up; so hopefully there won't be too |
sjallouli | 0:289fd2dae405 | 76 | * many FunctionPointers in a chain. */ |
sjallouli | 0:289fd2dae405 | 77 | void call(ContextType context) { |
sjallouli | 0:289fd2dae405 | 78 | if (_function) { |
sjallouli | 0:289fd2dae405 | 79 | _function(context); |
sjallouli | 0:289fd2dae405 | 80 | } else if (_object && _membercaller) { |
sjallouli | 0:289fd2dae405 | 81 | _membercaller(_object, _member, context); |
sjallouli | 0:289fd2dae405 | 82 | } |
sjallouli | 0:289fd2dae405 | 83 | |
sjallouli | 0:289fd2dae405 | 84 | /* Propagate the call to next in the chain. */ |
sjallouli | 0:289fd2dae405 | 85 | if (_next) { |
sjallouli | 0:289fd2dae405 | 86 | _next->call(context); |
sjallouli | 0:289fd2dae405 | 87 | } |
sjallouli | 0:289fd2dae405 | 88 | } |
sjallouli | 0:289fd2dae405 | 89 | |
sjallouli | 0:289fd2dae405 | 90 | /** |
sjallouli | 0:289fd2dae405 | 91 | * Setup an external FunctionPointer as a next in the chain of related |
sjallouli | 0:289fd2dae405 | 92 | * callbacks. Invoking call() on the head FunctionPointer will invoke all |
sjallouli | 0:289fd2dae405 | 93 | * chained callbacks. |
sjallouli | 0:289fd2dae405 | 94 | * |
sjallouli | 0:289fd2dae405 | 95 | * Refer to 'CallChain' as an alternative. |
sjallouli | 0:289fd2dae405 | 96 | */ |
sjallouli | 0:289fd2dae405 | 97 | void chainAsNext(pFunctionPointerWithContext_t next) { |
sjallouli | 0:289fd2dae405 | 98 | _next = next; |
sjallouli | 0:289fd2dae405 | 99 | } |
sjallouli | 0:289fd2dae405 | 100 | |
sjallouli | 0:289fd2dae405 | 101 | pFunctionPointerWithContext_t getNext(void) const { |
sjallouli | 0:289fd2dae405 | 102 | return _next; |
sjallouli | 0:289fd2dae405 | 103 | } |
sjallouli | 0:289fd2dae405 | 104 | |
sjallouli | 0:289fd2dae405 | 105 | pvoidfcontext_t get_function() const { |
sjallouli | 0:289fd2dae405 | 106 | return (pvoidfcontext_t)_function; |
sjallouli | 0:289fd2dae405 | 107 | } |
sjallouli | 0:289fd2dae405 | 108 | |
sjallouli | 0:289fd2dae405 | 109 | private: |
sjallouli | 0:289fd2dae405 | 110 | template<typename T> |
sjallouli | 0:289fd2dae405 | 111 | static void membercaller(void *object, char *member, ContextType context) { |
sjallouli | 0:289fd2dae405 | 112 | T *o = static_cast<T *>(object); |
sjallouli | 0:289fd2dae405 | 113 | void (T::*m)(ContextType); |
sjallouli | 0:289fd2dae405 | 114 | memcpy((char *)&m, member, sizeof(m)); |
sjallouli | 0:289fd2dae405 | 115 | (o->*m)(context); |
sjallouli | 0:289fd2dae405 | 116 | } |
sjallouli | 0:289fd2dae405 | 117 | |
sjallouli | 0:289fd2dae405 | 118 | void (*_function)(ContextType context); /**< static function pointer - NULL if none attached */ |
sjallouli | 0:289fd2dae405 | 119 | void *_object; /**< object this pointer - NULL if none attached */ |
sjallouli | 0:289fd2dae405 | 120 | char _member[16]; /**< raw member function pointer storage - converted back by |
sjallouli | 0:289fd2dae405 | 121 | * registered _membercaller */ |
sjallouli | 0:289fd2dae405 | 122 | void (*_membercaller)(void *, char *, ContextType); /**< registered membercaller function to convert back and call |
sjallouli | 0:289fd2dae405 | 123 | * _member on _object passing the context. */ |
sjallouli | 0:289fd2dae405 | 124 | pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this |
sjallouli | 0:289fd2dae405 | 125 | * allows chaining function pointers without requiring |
sjallouli | 0:289fd2dae405 | 126 | * external memory to manage the chain. Also refer to |
sjallouli | 0:289fd2dae405 | 127 | * 'CallChain' as an alternative. */ |
sjallouli | 0:289fd2dae405 | 128 | }; |
sjallouli | 0:289fd2dae405 | 129 | } // namespace mbed |
sjallouli | 0:289fd2dae405 | 130 | |
sjallouli | 0:289fd2dae405 | 131 | #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |