Preliminary main mbed library for nexpaq development
features/FEATURE_BLE/ble/FunctionPointerWithContext.h@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* mbed Microcontroller Library |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 0:6c56fb4bc5f0 | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 6 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 9 | * |
nexpaq | 0:6c56fb4bc5f0 | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 0:6c56fb4bc5f0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 14 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 15 | */ |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |
nexpaq | 0:6c56fb4bc5f0 | 19 | |
nexpaq | 0:6c56fb4bc5f0 | 20 | #include <string.h> |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include "SafeBool.h" |
nexpaq | 0:6c56fb4bc5f0 | 22 | |
nexpaq | 0:6c56fb4bc5f0 | 23 | /** A class for storing and calling a pointer to a static or member void function |
nexpaq | 0:6c56fb4bc5f0 | 24 | * that takes a context. |
nexpaq | 0:6c56fb4bc5f0 | 25 | */ |
nexpaq | 0:6c56fb4bc5f0 | 26 | template <typename ContextType> |
nexpaq | 0:6c56fb4bc5f0 | 27 | class FunctionPointerWithContext : public SafeBool<FunctionPointerWithContext<ContextType> > { |
nexpaq | 0:6c56fb4bc5f0 | 28 | public: |
nexpaq | 0:6c56fb4bc5f0 | 29 | typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t; |
nexpaq | 0:6c56fb4bc5f0 | 30 | typedef const FunctionPointerWithContext<ContextType> *cpFunctionPointerWithContext_t; |
nexpaq | 0:6c56fb4bc5f0 | 31 | typedef void (*pvoidfcontext_t)(ContextType context); |
nexpaq | 0:6c56fb4bc5f0 | 32 | |
nexpaq | 0:6c56fb4bc5f0 | 33 | /** Create a FunctionPointerWithContext, attaching a static function. |
nexpaq | 0:6c56fb4bc5f0 | 34 | * |
nexpaq | 0:6c56fb4bc5f0 | 35 | * @param function The void static function to attach (default is none). |
nexpaq | 0:6c56fb4bc5f0 | 36 | */ |
nexpaq | 0:6c56fb4bc5f0 | 37 | FunctionPointerWithContext(void (*function)(ContextType context) = NULL) : |
nexpaq | 0:6c56fb4bc5f0 | 38 | _memberFunctionAndPointer(), _caller(NULL), _next(NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 39 | attach(function); |
nexpaq | 0:6c56fb4bc5f0 | 40 | } |
nexpaq | 0:6c56fb4bc5f0 | 41 | |
nexpaq | 0:6c56fb4bc5f0 | 42 | /** Create a FunctionPointerWithContext, attaching a member function. |
nexpaq | 0:6c56fb4bc5f0 | 43 | * |
nexpaq | 0:6c56fb4bc5f0 | 44 | * @param object The object pointer to invoke the member function on (the "this" pointer). |
nexpaq | 0:6c56fb4bc5f0 | 45 | * @param function The address of the void member function to attach. |
nexpaq | 0:6c56fb4bc5f0 | 46 | */ |
nexpaq | 0:6c56fb4bc5f0 | 47 | template<typename T> |
nexpaq | 0:6c56fb4bc5f0 | 48 | FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) : |
nexpaq | 0:6c56fb4bc5f0 | 49 | _memberFunctionAndPointer(), _caller(NULL), _next(NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 50 | attach(object, member); |
nexpaq | 0:6c56fb4bc5f0 | 51 | } |
nexpaq | 0:6c56fb4bc5f0 | 52 | |
nexpaq | 0:6c56fb4bc5f0 | 53 | FunctionPointerWithContext(const FunctionPointerWithContext& that) : |
nexpaq | 0:6c56fb4bc5f0 | 54 | _memberFunctionAndPointer(that._memberFunctionAndPointer), _caller(that._caller), _next(NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 55 | } |
nexpaq | 0:6c56fb4bc5f0 | 56 | |
nexpaq | 0:6c56fb4bc5f0 | 57 | FunctionPointerWithContext& operator=(const FunctionPointerWithContext& that) { |
nexpaq | 0:6c56fb4bc5f0 | 58 | _memberFunctionAndPointer = that._memberFunctionAndPointer; |
nexpaq | 0:6c56fb4bc5f0 | 59 | _caller = that._caller; |
nexpaq | 0:6c56fb4bc5f0 | 60 | _next = NULL; |
nexpaq | 0:6c56fb4bc5f0 | 61 | return *this; |
nexpaq | 0:6c56fb4bc5f0 | 62 | } |
nexpaq | 0:6c56fb4bc5f0 | 63 | |
nexpaq | 0:6c56fb4bc5f0 | 64 | /** Attach a static function. |
nexpaq | 0:6c56fb4bc5f0 | 65 | * |
nexpaq | 0:6c56fb4bc5f0 | 66 | * @param function The void static function to attach (default is none). |
nexpaq | 0:6c56fb4bc5f0 | 67 | */ |
nexpaq | 0:6c56fb4bc5f0 | 68 | void attach(void (*function)(ContextType context) = NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 69 | _function = function; |
nexpaq | 0:6c56fb4bc5f0 | 70 | _caller = functioncaller; |
nexpaq | 0:6c56fb4bc5f0 | 71 | } |
nexpaq | 0:6c56fb4bc5f0 | 72 | |
nexpaq | 0:6c56fb4bc5f0 | 73 | /** Attach a member function. |
nexpaq | 0:6c56fb4bc5f0 | 74 | * |
nexpaq | 0:6c56fb4bc5f0 | 75 | * @param object The object pointer to invoke the member function on (the "this" pointer). |
nexpaq | 0:6c56fb4bc5f0 | 76 | * @param function The address of the void member function to attach. |
nexpaq | 0:6c56fb4bc5f0 | 77 | */ |
nexpaq | 0:6c56fb4bc5f0 | 78 | template<typename T> |
nexpaq | 0:6c56fb4bc5f0 | 79 | void attach(T *object, void (T::*member)(ContextType context)) { |
nexpaq | 0:6c56fb4bc5f0 | 80 | _memberFunctionAndPointer._object = static_cast<void *>(object); |
nexpaq | 0:6c56fb4bc5f0 | 81 | memcpy(_memberFunctionAndPointer._memberFunction, (char*) &member, sizeof(member)); |
nexpaq | 0:6c56fb4bc5f0 | 82 | _caller = &FunctionPointerWithContext::membercaller<T>; |
nexpaq | 0:6c56fb4bc5f0 | 83 | } |
nexpaq | 0:6c56fb4bc5f0 | 84 | |
nexpaq | 0:6c56fb4bc5f0 | 85 | /** Call the attached static or member function; if there are chained |
nexpaq | 0:6c56fb4bc5f0 | 86 | * FunctionPointers their callbacks are invoked as well. |
nexpaq | 0:6c56fb4bc5f0 | 87 | * @Note: All chained callbacks stack up, so hopefully there won't be too |
nexpaq | 0:6c56fb4bc5f0 | 88 | * many FunctionPointers in a chain. */ |
nexpaq | 0:6c56fb4bc5f0 | 89 | void call(ContextType context) const { |
nexpaq | 0:6c56fb4bc5f0 | 90 | _caller(this, context); |
nexpaq | 0:6c56fb4bc5f0 | 91 | } |
nexpaq | 0:6c56fb4bc5f0 | 92 | |
nexpaq | 0:6c56fb4bc5f0 | 93 | /** |
nexpaq | 0:6c56fb4bc5f0 | 94 | * @brief Same as above |
nexpaq | 0:6c56fb4bc5f0 | 95 | */ |
nexpaq | 0:6c56fb4bc5f0 | 96 | void operator()(ContextType context) const { |
nexpaq | 0:6c56fb4bc5f0 | 97 | call(context); |
nexpaq | 0:6c56fb4bc5f0 | 98 | } |
nexpaq | 0:6c56fb4bc5f0 | 99 | |
nexpaq | 0:6c56fb4bc5f0 | 100 | /** Same as above, workaround for mbed os FunctionPointer implementation. */ |
nexpaq | 0:6c56fb4bc5f0 | 101 | void call(ContextType context) { |
nexpaq | 0:6c56fb4bc5f0 | 102 | ((const FunctionPointerWithContext*) this)->call(context); |
nexpaq | 0:6c56fb4bc5f0 | 103 | } |
nexpaq | 0:6c56fb4bc5f0 | 104 | |
nexpaq | 0:6c56fb4bc5f0 | 105 | typedef void (FunctionPointerWithContext::*bool_type)() const; |
nexpaq | 0:6c56fb4bc5f0 | 106 | |
nexpaq | 0:6c56fb4bc5f0 | 107 | /** |
nexpaq | 0:6c56fb4bc5f0 | 108 | * implementation of safe bool operator |
nexpaq | 0:6c56fb4bc5f0 | 109 | */ |
nexpaq | 0:6c56fb4bc5f0 | 110 | bool toBool() const { |
nexpaq | 0:6c56fb4bc5f0 | 111 | return (_function || _memberFunctionAndPointer._object); |
nexpaq | 0:6c56fb4bc5f0 | 112 | } |
nexpaq | 0:6c56fb4bc5f0 | 113 | |
nexpaq | 0:6c56fb4bc5f0 | 114 | /** |
nexpaq | 0:6c56fb4bc5f0 | 115 | * Set up an external FunctionPointer as a next in the chain of related |
nexpaq | 0:6c56fb4bc5f0 | 116 | * callbacks. Invoking call() on the head FunctionPointer will invoke all |
nexpaq | 0:6c56fb4bc5f0 | 117 | * chained callbacks. |
nexpaq | 0:6c56fb4bc5f0 | 118 | * |
nexpaq | 0:6c56fb4bc5f0 | 119 | * Refer to 'CallChain' as an alternative. |
nexpaq | 0:6c56fb4bc5f0 | 120 | */ |
nexpaq | 0:6c56fb4bc5f0 | 121 | void chainAsNext(pFunctionPointerWithContext_t next) { |
nexpaq | 0:6c56fb4bc5f0 | 122 | _next = next; |
nexpaq | 0:6c56fb4bc5f0 | 123 | } |
nexpaq | 0:6c56fb4bc5f0 | 124 | |
nexpaq | 0:6c56fb4bc5f0 | 125 | pFunctionPointerWithContext_t getNext(void) const { |
nexpaq | 0:6c56fb4bc5f0 | 126 | return _next; |
nexpaq | 0:6c56fb4bc5f0 | 127 | } |
nexpaq | 0:6c56fb4bc5f0 | 128 | |
nexpaq | 0:6c56fb4bc5f0 | 129 | pvoidfcontext_t get_function() const { |
nexpaq | 0:6c56fb4bc5f0 | 130 | return (pvoidfcontext_t)_function; |
nexpaq | 0:6c56fb4bc5f0 | 131 | } |
nexpaq | 0:6c56fb4bc5f0 | 132 | |
nexpaq | 0:6c56fb4bc5f0 | 133 | friend bool operator==(const FunctionPointerWithContext& lhs, const FunctionPointerWithContext& rhs) { |
nexpaq | 0:6c56fb4bc5f0 | 134 | return rhs._caller == lhs._caller && |
nexpaq | 0:6c56fb4bc5f0 | 135 | memcmp( |
nexpaq | 0:6c56fb4bc5f0 | 136 | &rhs._memberFunctionAndPointer, |
nexpaq | 0:6c56fb4bc5f0 | 137 | &lhs._memberFunctionAndPointer, |
nexpaq | 0:6c56fb4bc5f0 | 138 | sizeof(rhs._memberFunctionAndPointer) |
nexpaq | 0:6c56fb4bc5f0 | 139 | ) == 0; |
nexpaq | 0:6c56fb4bc5f0 | 140 | } |
nexpaq | 0:6c56fb4bc5f0 | 141 | |
nexpaq | 0:6c56fb4bc5f0 | 142 | private: |
nexpaq | 0:6c56fb4bc5f0 | 143 | template<typename T> |
nexpaq | 0:6c56fb4bc5f0 | 144 | static void membercaller(cpFunctionPointerWithContext_t self, ContextType context) { |
nexpaq | 0:6c56fb4bc5f0 | 145 | if (self->_memberFunctionAndPointer._object) { |
nexpaq | 0:6c56fb4bc5f0 | 146 | T *o = static_cast<T *>(self->_memberFunctionAndPointer._object); |
nexpaq | 0:6c56fb4bc5f0 | 147 | void (T::*m)(ContextType); |
nexpaq | 0:6c56fb4bc5f0 | 148 | memcpy((char*) &m, self->_memberFunctionAndPointer._memberFunction, sizeof(m)); |
nexpaq | 0:6c56fb4bc5f0 | 149 | (o->*m)(context); |
nexpaq | 0:6c56fb4bc5f0 | 150 | } |
nexpaq | 0:6c56fb4bc5f0 | 151 | } |
nexpaq | 0:6c56fb4bc5f0 | 152 | |
nexpaq | 0:6c56fb4bc5f0 | 153 | static void functioncaller(cpFunctionPointerWithContext_t self, ContextType context) { |
nexpaq | 0:6c56fb4bc5f0 | 154 | if (self->_function) { |
nexpaq | 0:6c56fb4bc5f0 | 155 | self->_function(context); |
nexpaq | 0:6c56fb4bc5f0 | 156 | } |
nexpaq | 0:6c56fb4bc5f0 | 157 | } |
nexpaq | 0:6c56fb4bc5f0 | 158 | |
nexpaq | 0:6c56fb4bc5f0 | 159 | struct MemberFunctionAndPtr { |
nexpaq | 0:6c56fb4bc5f0 | 160 | /* |
nexpaq | 0:6c56fb4bc5f0 | 161 | * Forward declaration of a class and a member function to this class. |
nexpaq | 0:6c56fb4bc5f0 | 162 | * Because the compiler doesn't know anything about the forwarded member |
nexpaq | 0:6c56fb4bc5f0 | 163 | * function, it will always use the biggest size and the biggest alignment |
nexpaq | 0:6c56fb4bc5f0 | 164 | * that a member function can take for objects of type UndefinedMemberFunction. |
nexpaq | 0:6c56fb4bc5f0 | 165 | */ |
nexpaq | 0:6c56fb4bc5f0 | 166 | class UndefinedClass; |
nexpaq | 0:6c56fb4bc5f0 | 167 | typedef void (UndefinedClass::*UndefinedMemberFunction)(ContextType); |
nexpaq | 0:6c56fb4bc5f0 | 168 | |
nexpaq | 0:6c56fb4bc5f0 | 169 | void* _object; |
nexpaq | 0:6c56fb4bc5f0 | 170 | union { |
nexpaq | 0:6c56fb4bc5f0 | 171 | char _memberFunction[sizeof(UndefinedMemberFunction)]; |
nexpaq | 0:6c56fb4bc5f0 | 172 | UndefinedMemberFunction _alignment; |
nexpaq | 0:6c56fb4bc5f0 | 173 | }; |
nexpaq | 0:6c56fb4bc5f0 | 174 | }; |
nexpaq | 0:6c56fb4bc5f0 | 175 | |
nexpaq | 0:6c56fb4bc5f0 | 176 | union { |
nexpaq | 0:6c56fb4bc5f0 | 177 | pvoidfcontext_t _function; /**< Static function pointer - NULL if none attached */ |
nexpaq | 0:6c56fb4bc5f0 | 178 | /** |
nexpaq | 0:6c56fb4bc5f0 | 179 | * object this pointer and pointer to member - |
nexpaq | 0:6c56fb4bc5f0 | 180 | * _memberFunctionAndPointer._object will be NULL if none attached |
nexpaq | 0:6c56fb4bc5f0 | 181 | */ |
nexpaq | 0:6c56fb4bc5f0 | 182 | mutable MemberFunctionAndPtr _memberFunctionAndPointer; |
nexpaq | 0:6c56fb4bc5f0 | 183 | }; |
nexpaq | 0:6c56fb4bc5f0 | 184 | |
nexpaq | 0:6c56fb4bc5f0 | 185 | void (*_caller)(const FunctionPointerWithContext*, ContextType); |
nexpaq | 0:6c56fb4bc5f0 | 186 | |
nexpaq | 0:6c56fb4bc5f0 | 187 | pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers. This |
nexpaq | 0:6c56fb4bc5f0 | 188 | * allows chaining function pointers without requiring |
nexpaq | 0:6c56fb4bc5f0 | 189 | * external memory to manage the chain. Refer to |
nexpaq | 0:6c56fb4bc5f0 | 190 | * 'CallChain' as an alternative. */ |
nexpaq | 0:6c56fb4bc5f0 | 191 | }; |
nexpaq | 0:6c56fb4bc5f0 | 192 | |
nexpaq | 0:6c56fb4bc5f0 | 193 | /** |
nexpaq | 0:6c56fb4bc5f0 | 194 | * @brief Create a new FunctionPointerWithContext which bind an instance and a |
nexpaq | 0:6c56fb4bc5f0 | 195 | * a member function together. |
nexpaq | 0:6c56fb4bc5f0 | 196 | * @details This little helper is a just here to eliminate the need to write the |
nexpaq | 0:6c56fb4bc5f0 | 197 | * FunctionPointerWithContext type each time you want to create one by kicking |
nexpaq | 0:6c56fb4bc5f0 | 198 | * automatic type deduction of function templates. With this function, it is easy |
nexpaq | 0:6c56fb4bc5f0 | 199 | * to write only one entry point for functions which expect a FunctionPointer |
nexpaq | 0:6c56fb4bc5f0 | 200 | * in parameters. |
nexpaq | 0:6c56fb4bc5f0 | 201 | * |
nexpaq | 0:6c56fb4bc5f0 | 202 | * @param object to bound with member function |
nexpaq | 0:6c56fb4bc5f0 | 203 | * @param member The member function called |
nexpaq | 0:6c56fb4bc5f0 | 204 | * @return a new FunctionPointerWithContext |
nexpaq | 0:6c56fb4bc5f0 | 205 | */ |
nexpaq | 0:6c56fb4bc5f0 | 206 | template<typename T, typename ContextType> |
nexpaq | 0:6c56fb4bc5f0 | 207 | FunctionPointerWithContext<ContextType> makeFunctionPointer(T *object, void (T::*member)(ContextType context)) |
nexpaq | 0:6c56fb4bc5f0 | 208 | { |
nexpaq | 0:6c56fb4bc5f0 | 209 | return FunctionPointerWithContext<ContextType>(object, member); |
nexpaq | 0:6c56fb4bc5f0 | 210 | } |
nexpaq | 0:6c56fb4bc5f0 | 211 | |
nexpaq | 0:6c56fb4bc5f0 | 212 | #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H |