Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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