Sarah Marsh / Mbed OS EddystoneBeacon
Committer:
sarahmarshy
Date:
Tue Nov 29 06:29:10 2016 +0000
Revision:
0:1c7da5f83647
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 0:1c7da5f83647 1 /*
sarahmarshy 0:1c7da5f83647 2 * Copyright (c) 2016, ARM Limited, All Rights Reserved
sarahmarshy 0:1c7da5f83647 3 * SPDX-License-Identifier: Apache-2.0
sarahmarshy 0:1c7da5f83647 4 *
sarahmarshy 0:1c7da5f83647 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
sarahmarshy 0:1c7da5f83647 6 * not use this file except in compliance with the License.
sarahmarshy 0:1c7da5f83647 7 * You may obtain a copy of the License at
sarahmarshy 0:1c7da5f83647 8 *
sarahmarshy 0:1c7da5f83647 9 * http://www.apache.org/licenses/LICENSE-2.0
sarahmarshy 0:1c7da5f83647 10 *
sarahmarshy 0:1c7da5f83647 11 * Unless required by applicable law or agreed to in writing, software
sarahmarshy 0:1c7da5f83647 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
sarahmarshy 0:1c7da5f83647 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sarahmarshy 0:1c7da5f83647 14 * See the License for the specific language governing permissions and
sarahmarshy 0:1c7da5f83647 15 * limitations under the License.
sarahmarshy 0:1c7da5f83647 16 */
sarahmarshy 0:1c7da5f83647 17 #ifndef EVENTQUEUE_THUNK_H_
sarahmarshy 0:1c7da5f83647 18 #define EVENTQUEUE_THUNK_H_
sarahmarshy 0:1c7da5f83647 19
sarahmarshy 0:1c7da5f83647 20 #include "AlignedStorage.h"
sarahmarshy 0:1c7da5f83647 21 #include "detail/ThunkVTable.h"
sarahmarshy 0:1c7da5f83647 22
sarahmarshy 0:1c7da5f83647 23 namespace eq {
sarahmarshy 0:1c7da5f83647 24
sarahmarshy 0:1c7da5f83647 25 // forward declaration of ThunkVTableGenerator
sarahmarshy 0:1c7da5f83647 26 namespace detail {
sarahmarshy 0:1c7da5f83647 27 template<typename T>
sarahmarshy 0:1c7da5f83647 28 class ThunkVTableGenerator;
sarahmarshy 0:1c7da5f83647 29 }
sarahmarshy 0:1c7da5f83647 30
sarahmarshy 0:1c7da5f83647 31 /**
sarahmarshy 0:1c7da5f83647 32 * A Thunk is a container holding any kind of nullary callable.
sarahmarshy 0:1c7da5f83647 33 * It wrap value semantic and function call operations of the inner callable
sarahmarshy 0:1c7da5f83647 34 * held.
sarahmarshy 0:1c7da5f83647 35 * \note Thunk of callable bound to arguments should be generated by the
sarahmarshy 0:1c7da5f83647 36 * function make_thunk.
sarahmarshy 0:1c7da5f83647 37 */
sarahmarshy 0:1c7da5f83647 38 class Thunk {
sarahmarshy 0:1c7da5f83647 39 // Size for the internal buffer of the Thunk
sarahmarshy 0:1c7da5f83647 40 static const std::size_t BufferSize = 24;
sarahmarshy 0:1c7da5f83647 41
sarahmarshy 0:1c7da5f83647 42 template<typename T>
sarahmarshy 0:1c7da5f83647 43 friend class detail::ThunkVTableGenerator;
sarahmarshy 0:1c7da5f83647 44
sarahmarshy 0:1c7da5f83647 45 public:
sarahmarshy 0:1c7da5f83647 46
sarahmarshy 0:1c7da5f83647 47 /**
sarahmarshy 0:1c7da5f83647 48 * Thunk Empty constructor.
sarahmarshy 0:1c7da5f83647 49 * When this thunk is called, if does nothing.
sarahmarshy 0:1c7da5f83647 50 */
sarahmarshy 0:1c7da5f83647 51 Thunk();
sarahmarshy 0:1c7da5f83647 52
sarahmarshy 0:1c7da5f83647 53 /**
sarahmarshy 0:1c7da5f83647 54 * Construct a Thunk from a nullary callable of type F.
sarahmarshy 0:1c7da5f83647 55 * When the call operator is invoked, it call a copy of f ( f() ).
sarahmarshy 0:1c7da5f83647 56 */
sarahmarshy 0:1c7da5f83647 57 template<typename F>
sarahmarshy 0:1c7da5f83647 58 Thunk(const F& f);
sarahmarshy 0:1c7da5f83647 59
sarahmarshy 0:1c7da5f83647 60 /**
sarahmarshy 0:1c7da5f83647 61 * Special constructor for pointer to function.
sarahmarshy 0:1c7da5f83647 62 * Allow references to functions to gracefully decay into pointer to function.
sarahmarshy 0:1c7da5f83647 63 * Otherwise, reference to function are not copy constructible (their is no
sarahmarshy 0:1c7da5f83647 64 * constructible function type in C++).
sarahmarshy 0:1c7da5f83647 65 * When the call operator is invoked, it call a copy of f ( f() ).
sarahmarshy 0:1c7da5f83647 66 */
sarahmarshy 0:1c7da5f83647 67 Thunk(void (*f)());
sarahmarshy 0:1c7da5f83647 68
sarahmarshy 0:1c7da5f83647 69 /**
sarahmarshy 0:1c7da5f83647 70 * Copy construction of a thunk.
sarahmarshy 0:1c7da5f83647 71 * Take care that the inner F is correctly copied.
sarahmarshy 0:1c7da5f83647 72 */
sarahmarshy 0:1c7da5f83647 73 Thunk(const Thunk& other) : _storage(), _vtable() {
sarahmarshy 0:1c7da5f83647 74 other._vtable->copy(*this, other);
sarahmarshy 0:1c7da5f83647 75 }
sarahmarshy 0:1c7da5f83647 76
sarahmarshy 0:1c7da5f83647 77 /**
sarahmarshy 0:1c7da5f83647 78 * Destruction of the Thunk correctly call the destructor of the
sarahmarshy 0:1c7da5f83647 79 * inner callable.
sarahmarshy 0:1c7da5f83647 80 */
sarahmarshy 0:1c7da5f83647 81 ~Thunk() {
sarahmarshy 0:1c7da5f83647 82 _vtable->destroy(*this);
sarahmarshy 0:1c7da5f83647 83 }
sarahmarshy 0:1c7da5f83647 84
sarahmarshy 0:1c7da5f83647 85 /**
sarahmarshy 0:1c7da5f83647 86 * Copy assignement from another thunk.
sarahmarshy 0:1c7da5f83647 87 * Ensure that the callable held is correctly destroyed then copy
sarahmarshy 0:1c7da5f83647 88 * the correctly copy the new one.
sarahmarshy 0:1c7da5f83647 89 */
sarahmarshy 0:1c7da5f83647 90 Thunk& operator=(const Thunk& other) {
sarahmarshy 0:1c7da5f83647 91 if (this == &other) {
sarahmarshy 0:1c7da5f83647 92 return *this;
sarahmarshy 0:1c7da5f83647 93 }
sarahmarshy 0:1c7da5f83647 94 _vtable->destroy(*this);
sarahmarshy 0:1c7da5f83647 95 other._vtable->copy(*this, other);
sarahmarshy 0:1c7da5f83647 96 return *this;
sarahmarshy 0:1c7da5f83647 97 }
sarahmarshy 0:1c7da5f83647 98
sarahmarshy 0:1c7da5f83647 99 /**
sarahmarshy 0:1c7da5f83647 100 * Call operator. Invoke the inner callable.
sarahmarshy 0:1c7da5f83647 101 */
sarahmarshy 0:1c7da5f83647 102 void operator()() const {
sarahmarshy 0:1c7da5f83647 103 _vtable->call(*this);
sarahmarshy 0:1c7da5f83647 104 }
sarahmarshy 0:1c7da5f83647 105
sarahmarshy 0:1c7da5f83647 106 private:
sarahmarshy 0:1c7da5f83647 107 static void empty_thunk() { }
sarahmarshy 0:1c7da5f83647 108
sarahmarshy 0:1c7da5f83647 109 AlignedStorage<char[BufferSize]> _storage;
sarahmarshy 0:1c7da5f83647 110 const detail::ThunkVTable* _vtable;
sarahmarshy 0:1c7da5f83647 111 };
sarahmarshy 0:1c7da5f83647 112
sarahmarshy 0:1c7da5f83647 113 } // namespace eq
sarahmarshy 0:1c7da5f83647 114
sarahmarshy 0:1c7da5f83647 115 #include "detail/Thunk.impl.h"
sarahmarshy 0:1c7da5f83647 116
sarahmarshy 0:1c7da5f83647 117 #endif /* EVENTQUEUE_THUNK_H_ */