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