test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

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