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