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