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