Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
public/FunctionPointerWithContext.h@146:543877bb1f52, 2014-11-28 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 28 14:11:19 2014 +0000
- Revision:
- 146:543877bb1f52
- Parent:
- 145:a7ded9ad83c8
- Child:
- 198:933961a2ac6f
Synchronized with git rev e8e3a7aa
Author: Rohit Grover
add Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS()
Who changed what in which revision?
User | Revision | Line number | New 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 | |
rgrover1 | 146:543877bb1f52 | 22 | namespace mbed { |
Rohit Grover |
118:620d28e7a1ba | 23 | |
Rohit Grover |
118:620d28e7a1ba | 24 | /** A class for storing and calling a pointer to a static or member void function |
Rohit Grover |
118:620d28e7a1ba | 25 | * which takes a context. |
Rohit Grover |
118:620d28e7a1ba | 26 | */ |
Rohit Grover |
118:620d28e7a1ba | 27 | template <typename ContextType> |
Rohit Grover |
118:620d28e7a1ba | 28 | class FunctionPointerWithContext { |
Rohit Grover |
118:620d28e7a1ba | 29 | public: |
Rohit Grover |
118:620d28e7a1ba | 30 | typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t; |
Rohit Grover |
118:620d28e7a1ba | 31 | typedef void (*pvoidfcontext_t)(ContextType context); |
Rohit Grover |
118:620d28e7a1ba | 32 | |
Rohit Grover |
118:620d28e7a1ba | 33 | /** Create a FunctionPointerWithContext, attaching a static function |
Rohit Grover |
118:620d28e7a1ba | 34 | * |
Rohit Grover |
118:620d28e7a1ba | 35 | * @param function The void static function to attach (default is none) |
Rohit Grover |
118:620d28e7a1ba | 36 | */ |
Rohit Grover |
118:620d28e7a1ba | 37 | FunctionPointerWithContext(void (*function)(ContextType context) = NULL) : |
Rohit Grover |
118:620d28e7a1ba | 38 | _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) { |
Rohit Grover |
118:620d28e7a1ba | 39 | attach(function); |
Rohit Grover |
118:620d28e7a1ba | 40 | } |
Rohit Grover |
118:620d28e7a1ba | 41 | |
Rohit Grover |
118:620d28e7a1ba | 42 | /** Create a FunctionPointerWithContext, attaching a member function |
Rohit Grover |
118:620d28e7a1ba | 43 | * |
Rohit Grover |
118:620d28e7a1ba | 44 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
Rohit Grover |
118:620d28e7a1ba | 45 | * @param function The address of the void member function to attach |
Rohit Grover |
118:620d28e7a1ba | 46 | */ |
Rohit Grover |
118:620d28e7a1ba | 47 | template<typename T> |
Rohit Grover |
118:620d28e7a1ba | 48 | FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) : |
Rohit Grover |
118:620d28e7a1ba | 49 | _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) { |
Rohit Grover |
118:620d28e7a1ba | 50 | attach(object, member); |
Rohit Grover |
118:620d28e7a1ba | 51 | } |
Rohit Grover |
118:620d28e7a1ba | 52 | |
Rohit Grover |
118:620d28e7a1ba | 53 | /** Attach a static function |
Rohit Grover |
118:620d28e7a1ba | 54 | * |
Rohit Grover |
118:620d28e7a1ba | 55 | * @param function The void static function to attach (default is none) |
Rohit Grover |
118:620d28e7a1ba | 56 | */ |
Rohit Grover |
118:620d28e7a1ba | 57 | void attach(void (*function)(ContextType context) = NULL) { |
Rohit Grover |
118:620d28e7a1ba | 58 | _function = function; |
Rohit Grover |
118:620d28e7a1ba | 59 | } |
Rohit Grover |
118:620d28e7a1ba | 60 | |
Rohit Grover |
118:620d28e7a1ba | 61 | /** Attach a member function |
Rohit Grover |
118:620d28e7a1ba | 62 | * |
Rohit Grover |
118:620d28e7a1ba | 63 | * @param object The object pointer to invoke the member function on (i.e. the this pointer) |
Rohit Grover |
118:620d28e7a1ba | 64 | * @param function The address of the void member function to attach |
Rohit Grover |
118:620d28e7a1ba | 65 | */ |
Rohit Grover |
118:620d28e7a1ba | 66 | template<typename T> |
Rohit Grover |
118:620d28e7a1ba | 67 | void attach(T *object, void (T::*member)(ContextType context)) { |
Rohit Grover |
118:620d28e7a1ba | 68 | _object = static_cast<void *>(object); |
Rohit Grover |
118:620d28e7a1ba | 69 | memcpy(_member, (char *)&member, sizeof(member)); |
Rohit Grover |
118:620d28e7a1ba | 70 | _membercaller = &FunctionPointerWithContext::membercaller<T>; |
Rohit Grover |
118:620d28e7a1ba | 71 | } |
Rohit Grover |
118:620d28e7a1ba | 72 | |
Rohit Grover |
118:620d28e7a1ba | 73 | /** Call the attached static or member function; and if there are chained |
Rohit Grover |
118:620d28e7a1ba | 74 | * FunctionPointers their callbacks are invoked as well. |
Rohit Grover |
118:620d28e7a1ba | 75 | * @Note: all chained callbacks stack up; so hopefully there won't be too |
Rohit Grover |
118:620d28e7a1ba | 76 | * many FunctionPointers in a chain. */ |
Rohit Grover |
118:620d28e7a1ba | 77 | void call(ContextType context) { |
Rohit Grover |
118:620d28e7a1ba | 78 | if (_function) { |
Rohit Grover |
118:620d28e7a1ba | 79 | _function(context); |
Rohit Grover |
118:620d28e7a1ba | 80 | } else if (_object && _membercaller) { |
Rohit Grover |
118:620d28e7a1ba | 81 | _membercaller(_object, _member, context); |
Rohit Grover |
118:620d28e7a1ba | 82 | } |
Rohit Grover |
118:620d28e7a1ba | 83 | |
Rohit Grover |
118:620d28e7a1ba | 84 | /* Propagate the call to next in the chain. */ |
Rohit Grover |
118:620d28e7a1ba | 85 | if (_next) { |
Rohit Grover |
118:620d28e7a1ba | 86 | _next->call(context); |
Rohit Grover |
118:620d28e7a1ba | 87 | } |
Rohit Grover |
118:620d28e7a1ba | 88 | } |
Rohit Grover |
118:620d28e7a1ba | 89 | |
Rohit Grover |
118:620d28e7a1ba | 90 | /** |
Rohit Grover |
118:620d28e7a1ba | 91 | * Setup an external FunctionPointer as a next in the chain of related |
Rohit Grover |
118:620d28e7a1ba | 92 | * callbacks. Invoking call() on the head FunctionPointer will invoke all |
Rohit Grover |
118:620d28e7a1ba | 93 | * chained callbacks. |
Rohit Grover |
118:620d28e7a1ba | 94 | * |
Rohit Grover |
118:620d28e7a1ba | 95 | * Refer to 'CallChain' as an alternative. |
Rohit Grover |
118:620d28e7a1ba | 96 | */ |
Rohit Grover |
118:620d28e7a1ba | 97 | void chainAsNext(pFunctionPointerWithContext_t next) { |
Rohit Grover |
118:620d28e7a1ba | 98 | _next = next; |
Rohit Grover |
118:620d28e7a1ba | 99 | } |
Rohit Grover |
118:620d28e7a1ba | 100 | |
Rohit Grover |
118:620d28e7a1ba | 101 | pFunctionPointerWithContext_t getNext(void) const { |
Rohit Grover |
118:620d28e7a1ba | 102 | return _next; |
Rohit Grover |
118:620d28e7a1ba | 103 | } |
Rohit Grover |
118:620d28e7a1ba | 104 | |
Rohit Grover |
118:620d28e7a1ba | 105 | pvoidfcontext_t get_function() const { |
Rohit Grover |
118:620d28e7a1ba | 106 | return (pvoidfcontext_t)_function; |
Rohit Grover |
118:620d28e7a1ba | 107 | } |
Rohit Grover |
118:620d28e7a1ba | 108 | |
Rohit Grover |
118:620d28e7a1ba | 109 | private: |
Rohit Grover |
118:620d28e7a1ba | 110 | template<typename T> |
Rohit Grover |
118:620d28e7a1ba | 111 | static void membercaller(void *object, char *member, ContextType context) { |
Rohit Grover |
118:620d28e7a1ba | 112 | T *o = static_cast<T *>(object); |
Rohit Grover |
118:620d28e7a1ba | 113 | void (T::*m)(ContextType); |
Rohit Grover |
118:620d28e7a1ba | 114 | memcpy((char *)&m, member, sizeof(m)); |
Rohit Grover |
118:620d28e7a1ba | 115 | (o->*m)(context); |
Rohit Grover |
118:620d28e7a1ba | 116 | } |
Rohit Grover |
118:620d28e7a1ba | 117 | |
Rohit Grover |
118:620d28e7a1ba | 118 | void (*_function)(ContextType context); /**< static function pointer - NULL if none attached */ |
Rohit Grover |
118:620d28e7a1ba | 119 | void *_object; /**< object this pointer - NULL if none attached */ |
Rohit Grover |
118:620d28e7a1ba | 120 | char _member[16]; /**< raw member function pointer storage - converted back by |
Rohit Grover |
118:620d28e7a1ba | 121 | * registered _membercaller */ |
Rohit Grover |
118:620d28e7a1ba | 122 | void (*_membercaller)(void *, char *, ContextType); /**< registered membercaller function to convert back and call |
Rohit Grover |
118:620d28e7a1ba | 123 | * _member on _object passing the context. */ |
Rohit Grover |
118:620d28e7a1ba | 124 | pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this |
Rohit Grover |
118:620d28e7a1ba | 125 | * allows chaining function pointers without requiring |
Rohit Grover |
118:620d28e7a1ba | 126 | * external memory to manage the chain. Also refer to |
Rohit Grover |
118:620d28e7a1ba | 127 | * 'CallChain' as an alternative. */ |
Rohit Grover |
118:620d28e7a1ba | 128 | }; |
rgrover1 | 146:543877bb1f52 | 129 | } // namespace mbed |
Rohit Grover |
118:620d28e7a1ba | 130 | |
rgrover1 | 126:fdebe4d5d62f | 131 | #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |