FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 /* mbed Microcontroller Library
ram54288 0:dbad57390bd1 2 * Copyright (c) 2006-2015 ARM Limited
ram54288 0:dbad57390bd1 3 *
ram54288 0:dbad57390bd1 4 * Licensed under the Apache License, Version 2.0 (the "License");
ram54288 0:dbad57390bd1 5 * you may not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 6 * You may obtain a copy of the License at
ram54288 0:dbad57390bd1 7 *
ram54288 0:dbad57390bd1 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 9 *
ram54288 0:dbad57390bd1 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 11 * distributed under the License is distributed on an "AS IS" BASIS,
ram54288 0:dbad57390bd1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 13 * See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 14 * limitations under the License.
ram54288 0:dbad57390bd1 15 */
ram54288 0:dbad57390bd1 16
ram54288 0:dbad57390bd1 17 #ifndef MBED_FUNCTIONPOINTERBIND_H__
ram54288 0:dbad57390bd1 18 #define MBED_FUNCTIONPOINTERBIND_H__
ram54288 0:dbad57390bd1 19
ram54288 0:dbad57390bd1 20 #include <string.h>
ram54288 0:dbad57390bd1 21 #include <stdint.h>
ram54288 0:dbad57390bd1 22 #include <stddef.h>
ram54288 0:dbad57390bd1 23 #include <stdarg.h>
ram54288 0:dbad57390bd1 24 #include <assert.h>
ram54288 0:dbad57390bd1 25 #include "FunctionPointerBase.h"
ram54288 0:dbad57390bd1 26
ram54288 0:dbad57390bd1 27 #ifndef EVENT_STORAGE_SIZE
ram54288 0:dbad57390bd1 28 #define EVENT_STORAGE_SIZE 32
ram54288 0:dbad57390bd1 29 #endif
ram54288 0:dbad57390bd1 30
ram54288 0:dbad57390bd1 31 #define MBED_STATIC_ASSERT(MBED_STATIC_ASSERT_FAILED,MSG)\
ram54288 0:dbad57390bd1 32 switch(0){\
ram54288 0:dbad57390bd1 33 case 0:case (MBED_STATIC_ASSERT_FAILED): \
ram54288 0:dbad57390bd1 34 break;}
ram54288 0:dbad57390bd1 35
ram54288 0:dbad57390bd1 36 namespace mbed{
ram54288 0:dbad57390bd1 37
ram54288 0:dbad57390bd1 38 template<typename R>
ram54288 0:dbad57390bd1 39 class FunctionPointerBind : public FunctionPointerBase<R> {
ram54288 0:dbad57390bd1 40 public:
ram54288 0:dbad57390bd1 41 // Call the Event
ram54288 0:dbad57390bd1 42 inline R call() {
ram54288 0:dbad57390bd1 43 return FunctionPointerBase<R>::call(static_cast<void *>(_storage));
ram54288 0:dbad57390bd1 44 }
ram54288 0:dbad57390bd1 45 FunctionPointerBind():
ram54288 0:dbad57390bd1 46 FunctionPointerBase<R>(),
ram54288 0:dbad57390bd1 47 _ops(&FunctionPointerBase<R>::_nullops)
ram54288 0:dbad57390bd1 48 {}
ram54288 0:dbad57390bd1 49
ram54288 0:dbad57390bd1 50 FunctionPointerBind(const FunctionPointerBind<R> & fp):
ram54288 0:dbad57390bd1 51 FunctionPointerBase<R>(),
ram54288 0:dbad57390bd1 52 _ops(&FunctionPointerBase<R>::_nullops) {
ram54288 0:dbad57390bd1 53 *this = fp;
ram54288 0:dbad57390bd1 54 }
ram54288 0:dbad57390bd1 55
ram54288 0:dbad57390bd1 56 virtual ~FunctionPointerBind() {
ram54288 0:dbad57390bd1 57 _ops->destructor(_storage);
ram54288 0:dbad57390bd1 58 }
ram54288 0:dbad57390bd1 59
ram54288 0:dbad57390bd1 60 FunctionPointerBind<R> & operator=(const FunctionPointerBind<R>& rhs) {
ram54288 0:dbad57390bd1 61 if (_ops != &FunctionPointerBase<R>::_nullops) {
ram54288 0:dbad57390bd1 62 _ops->destructor(_storage);
ram54288 0:dbad57390bd1 63 }
ram54288 0:dbad57390bd1 64 FunctionPointerBase<R>::copy(&rhs);
ram54288 0:dbad57390bd1 65 _ops = rhs._ops;
ram54288 0:dbad57390bd1 66 _ops->copy_args(_storage, (void *)rhs._storage);
ram54288 0:dbad57390bd1 67 return *this;
ram54288 0:dbad57390bd1 68 }
ram54288 0:dbad57390bd1 69
ram54288 0:dbad57390bd1 70 /**
ram54288 0:dbad57390bd1 71 * Clears the current binding, making this instance unbound
ram54288 0:dbad57390bd1 72 */
ram54288 0:dbad57390bd1 73 virtual void clear() {
ram54288 0:dbad57390bd1 74 if (_ops != &FunctionPointerBase<R>::_nullops) {
ram54288 0:dbad57390bd1 75 _ops->destructor(_storage);
ram54288 0:dbad57390bd1 76 }
ram54288 0:dbad57390bd1 77 _ops = &FunctionPointerBase<R>::_nullops;
ram54288 0:dbad57390bd1 78 FunctionPointerBase<R>::clear();
ram54288 0:dbad57390bd1 79 }
ram54288 0:dbad57390bd1 80
ram54288 0:dbad57390bd1 81 template<typename S>
ram54288 0:dbad57390bd1 82 FunctionPointerBind<R> & bind(const struct FunctionPointerBase<R>::ArgOps * ops , S * argStruct, FunctionPointerBase<R> *fp, ...) {
ram54288 0:dbad57390bd1 83 MBED_STATIC_ASSERT(sizeof(S) <= sizeof(_storage), ERROR: Arguments too large for FunctionPointerBind internal storage)
ram54288 0:dbad57390bd1 84 if (_ops != &FunctionPointerBase<R>::_nullops) {
ram54288 0:dbad57390bd1 85 _ops->destructor(_storage);
ram54288 0:dbad57390bd1 86 }
ram54288 0:dbad57390bd1 87 _ops = ops;
ram54288 0:dbad57390bd1 88 FunctionPointerBase<R>::copy(fp);
ram54288 0:dbad57390bd1 89 assert(this->_ops != NULL);
ram54288 0:dbad57390bd1 90 assert(this->_ops->constructor != NULL);
ram54288 0:dbad57390bd1 91 if (argStruct) {
ram54288 0:dbad57390bd1 92 this->_ops->copy_args(this->_storage, (void *)argStruct);
ram54288 0:dbad57390bd1 93 } else {
ram54288 0:dbad57390bd1 94 va_list args;
ram54288 0:dbad57390bd1 95 va_start(args, fp);
ram54288 0:dbad57390bd1 96 this->_ops->constructor(_storage, args);
ram54288 0:dbad57390bd1 97 va_end(args);
ram54288 0:dbad57390bd1 98 }
ram54288 0:dbad57390bd1 99 return *this;
ram54288 0:dbad57390bd1 100 }
ram54288 0:dbad57390bd1 101
ram54288 0:dbad57390bd1 102 R operator()() {
ram54288 0:dbad57390bd1 103 return call();
ram54288 0:dbad57390bd1 104 }
ram54288 0:dbad57390bd1 105
ram54288 0:dbad57390bd1 106 protected:
ram54288 0:dbad57390bd1 107 const struct FunctionPointerBase<R>::ArgOps * _ops;
ram54288 0:dbad57390bd1 108 uint32_t _storage[(EVENT_STORAGE_SIZE+sizeof(uint32_t)-1)/sizeof(uint32_t)];
ram54288 0:dbad57390bd1 109 };
ram54288 0:dbad57390bd1 110 }
ram54288 0:dbad57390bd1 111
ram54288 0:dbad57390bd1 112 #endif // MBED_FUNCTIONPOINTERBIND_H__