Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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