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