Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jul 11 01:57:40 2015 +0000
Revision:
16:3c873f2c8a27
Converted BLE_API lib to folder so the version stays frozen.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AntonLS 16:3c873f2c8a27 1 /* mbed Microcontroller Library
AntonLS 16:3c873f2c8a27 2 * Copyright (c) 2006-2013 ARM Limited
AntonLS 16:3c873f2c8a27 3 *
AntonLS 16:3c873f2c8a27 4 * Licensed under the Apache License, Version 2.0 (the "License");
AntonLS 16:3c873f2c8a27 5 * you may not use this file except in compliance with the License.
AntonLS 16:3c873f2c8a27 6 * You may obtain a copy of the License at
AntonLS 16:3c873f2c8a27 7 *
AntonLS 16:3c873f2c8a27 8 * http://www.apache.org/licenses/LICENSE-2.0
AntonLS 16:3c873f2c8a27 9 *
AntonLS 16:3c873f2c8a27 10 * Unless required by applicable law or agreed to in writing, software
AntonLS 16:3c873f2c8a27 11 * distributed under the License is distributed on an "AS IS" BASIS,
AntonLS 16:3c873f2c8a27 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AntonLS 16:3c873f2c8a27 13 * See the License for the specific language governing permissions and
AntonLS 16:3c873f2c8a27 14 * limitations under the License.
AntonLS 16:3c873f2c8a27 15 */
AntonLS 16:3c873f2c8a27 16
AntonLS 16:3c873f2c8a27 17 #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
AntonLS 16:3c873f2c8a27 18 #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
AntonLS 16:3c873f2c8a27 19
AntonLS 16:3c873f2c8a27 20 #include <string.h>
AntonLS 16:3c873f2c8a27 21
AntonLS 16:3c873f2c8a27 22
AntonLS 16:3c873f2c8a27 23 /** A class for storing and calling a pointer to a static or member void function
AntonLS 16:3c873f2c8a27 24 * which takes a context.
AntonLS 16:3c873f2c8a27 25 */
AntonLS 16:3c873f2c8a27 26 template <typename ContextType>
AntonLS 16:3c873f2c8a27 27 class FunctionPointerWithContext {
AntonLS 16:3c873f2c8a27 28 public:
AntonLS 16:3c873f2c8a27 29 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
AntonLS 16:3c873f2c8a27 30 typedef void (*pvoidfcontext_t)(ContextType context);
AntonLS 16:3c873f2c8a27 31
AntonLS 16:3c873f2c8a27 32 /** Create a FunctionPointerWithContext, attaching a static function
AntonLS 16:3c873f2c8a27 33 *
AntonLS 16:3c873f2c8a27 34 * @param function The void static function to attach (default is none)
AntonLS 16:3c873f2c8a27 35 */
AntonLS 16:3c873f2c8a27 36 FunctionPointerWithContext(void (*function)(ContextType context) = NULL) :
AntonLS 16:3c873f2c8a27 37 _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) {
AntonLS 16:3c873f2c8a27 38 attach(function);
AntonLS 16:3c873f2c8a27 39 }
AntonLS 16:3c873f2c8a27 40
AntonLS 16:3c873f2c8a27 41 /** Create a FunctionPointerWithContext, attaching a member function
AntonLS 16:3c873f2c8a27 42 *
AntonLS 16:3c873f2c8a27 43 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
AntonLS 16:3c873f2c8a27 44 * @param function The address of the void member function to attach
AntonLS 16:3c873f2c8a27 45 */
AntonLS 16:3c873f2c8a27 46 template<typename T>
AntonLS 16:3c873f2c8a27 47 FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) :
AntonLS 16:3c873f2c8a27 48 _function(NULL), _object(NULL), _member(), _membercaller(NULL), _next(NULL) {
AntonLS 16:3c873f2c8a27 49 attach(object, member);
AntonLS 16:3c873f2c8a27 50 }
AntonLS 16:3c873f2c8a27 51
AntonLS 16:3c873f2c8a27 52 /** Attach a static function
AntonLS 16:3c873f2c8a27 53 *
AntonLS 16:3c873f2c8a27 54 * @param function The void static function to attach (default is none)
AntonLS 16:3c873f2c8a27 55 */
AntonLS 16:3c873f2c8a27 56 void attach(void (*function)(ContextType context) = NULL) {
AntonLS 16:3c873f2c8a27 57 _function = function;
AntonLS 16:3c873f2c8a27 58 }
AntonLS 16:3c873f2c8a27 59
AntonLS 16:3c873f2c8a27 60 /** Attach a member function
AntonLS 16:3c873f2c8a27 61 *
AntonLS 16:3c873f2c8a27 62 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
AntonLS 16:3c873f2c8a27 63 * @param function The address of the void member function to attach
AntonLS 16:3c873f2c8a27 64 */
AntonLS 16:3c873f2c8a27 65 template<typename T>
AntonLS 16:3c873f2c8a27 66 void attach(T *object, void (T::*member)(ContextType context)) {
AntonLS 16:3c873f2c8a27 67 _object = static_cast<void *>(object);
AntonLS 16:3c873f2c8a27 68 memcpy(_member, (char *)&member, sizeof(member));
AntonLS 16:3c873f2c8a27 69 _membercaller = &FunctionPointerWithContext::membercaller<T>;
AntonLS 16:3c873f2c8a27 70 }
AntonLS 16:3c873f2c8a27 71
AntonLS 16:3c873f2c8a27 72 /** Call the attached static or member function; and if there are chained
AntonLS 16:3c873f2c8a27 73 * FunctionPointers their callbacks are invoked as well.
AntonLS 16:3c873f2c8a27 74 * @Note: all chained callbacks stack up; so hopefully there won't be too
AntonLS 16:3c873f2c8a27 75 * many FunctionPointers in a chain. */
AntonLS 16:3c873f2c8a27 76 void call(ContextType context) {
AntonLS 16:3c873f2c8a27 77 if (_function) {
AntonLS 16:3c873f2c8a27 78 _function(context);
AntonLS 16:3c873f2c8a27 79 } else if (_object && _membercaller) {
AntonLS 16:3c873f2c8a27 80 _membercaller(_object, _member, context);
AntonLS 16:3c873f2c8a27 81 }
AntonLS 16:3c873f2c8a27 82
AntonLS 16:3c873f2c8a27 83 /* Propagate the call to next in the chain. */
AntonLS 16:3c873f2c8a27 84 if (_next) {
AntonLS 16:3c873f2c8a27 85 _next->call(context);
AntonLS 16:3c873f2c8a27 86 }
AntonLS 16:3c873f2c8a27 87 }
AntonLS 16:3c873f2c8a27 88
AntonLS 16:3c873f2c8a27 89 /**
AntonLS 16:3c873f2c8a27 90 * Setup an external FunctionPointer as a next in the chain of related
AntonLS 16:3c873f2c8a27 91 * callbacks. Invoking call() on the head FunctionPointer will invoke all
AntonLS 16:3c873f2c8a27 92 * chained callbacks.
AntonLS 16:3c873f2c8a27 93 *
AntonLS 16:3c873f2c8a27 94 * Refer to 'CallChain' as an alternative.
AntonLS 16:3c873f2c8a27 95 */
AntonLS 16:3c873f2c8a27 96 void chainAsNext(pFunctionPointerWithContext_t next) {
AntonLS 16:3c873f2c8a27 97 _next = next;
AntonLS 16:3c873f2c8a27 98 }
AntonLS 16:3c873f2c8a27 99
AntonLS 16:3c873f2c8a27 100 pFunctionPointerWithContext_t getNext(void) const {
AntonLS 16:3c873f2c8a27 101 return _next;
AntonLS 16:3c873f2c8a27 102 }
AntonLS 16:3c873f2c8a27 103
AntonLS 16:3c873f2c8a27 104 pvoidfcontext_t get_function() const {
AntonLS 16:3c873f2c8a27 105 return (pvoidfcontext_t)_function;
AntonLS 16:3c873f2c8a27 106 }
AntonLS 16:3c873f2c8a27 107
AntonLS 16:3c873f2c8a27 108 private:
AntonLS 16:3c873f2c8a27 109 template<typename T>
AntonLS 16:3c873f2c8a27 110 static void membercaller(void *object, char *member, ContextType context) {
AntonLS 16:3c873f2c8a27 111 T *o = static_cast<T *>(object);
AntonLS 16:3c873f2c8a27 112 void (T::*m)(ContextType);
AntonLS 16:3c873f2c8a27 113 memcpy((char *)&m, member, sizeof(m));
AntonLS 16:3c873f2c8a27 114 (o->*m)(context);
AntonLS 16:3c873f2c8a27 115 }
AntonLS 16:3c873f2c8a27 116
AntonLS 16:3c873f2c8a27 117 void (*_function)(ContextType context); /**< static function pointer - NULL if none attached */
AntonLS 16:3c873f2c8a27 118 void *_object; /**< object this pointer - NULL if none attached */
AntonLS 16:3c873f2c8a27 119 char _member[16]; /**< raw member function pointer storage - converted back by
AntonLS 16:3c873f2c8a27 120 * registered _membercaller */
AntonLS 16:3c873f2c8a27 121 void (*_membercaller)(void *, char *, ContextType); /**< registered membercaller function to convert back and call
AntonLS 16:3c873f2c8a27 122 * _member on _object passing the context. */
AntonLS 16:3c873f2c8a27 123 pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers; this
AntonLS 16:3c873f2c8a27 124 * allows chaining function pointers without requiring
AntonLS 16:3c873f2c8a27 125 * external memory to manage the chain. Also refer to
AntonLS 16:3c873f2c8a27 126 * 'CallChain' as an alternative. */
AntonLS 16:3c873f2c8a27 127 };
AntonLS 16:3c873f2c8a27 128
AntonLS 16:3c873f2c8a27 129 #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H