Roy Want / Mbed OS beaconCompileReadyFork
Committer:
roywant
Date:
Mon Sep 19 00:59:11 2016 +0000
Revision:
0:ed0152b5c495
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roywant 0:ed0152b5c495 1 /*
roywant 0:ed0152b5c495 2 * Copyright (c) 2016, ARM Limited, All Rights Reserved
roywant 0:ed0152b5c495 3 * SPDX-License-Identifier: Apache-2.0
roywant 0:ed0152b5c495 4 *
roywant 0:ed0152b5c495 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
roywant 0:ed0152b5c495 6 * not use this file except in compliance with the License.
roywant 0:ed0152b5c495 7 * You may obtain a copy of the License at
roywant 0:ed0152b5c495 8 *
roywant 0:ed0152b5c495 9 * http://www.apache.org/licenses/LICENSE-2.0
roywant 0:ed0152b5c495 10 *
roywant 0:ed0152b5c495 11 * Unless required by applicable law or agreed to in writing, software
roywant 0:ed0152b5c495 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
roywant 0:ed0152b5c495 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roywant 0:ed0152b5c495 14 * See the License for the specific language governing permissions and
roywant 0:ed0152b5c495 15 * limitations under the License.
roywant 0:ed0152b5c495 16 */
roywant 0:ed0152b5c495 17 #ifndef EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_
roywant 0:ed0152b5c495 18 #define EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_
roywant 0:ed0152b5c495 19
roywant 0:ed0152b5c495 20 // imported from Thunk.h
roywant 0:ed0152b5c495 21
roywant 0:ed0152b5c495 22 namespace eq {
roywant 0:ed0152b5c495 23 namespace detail {
roywant 0:ed0152b5c495 24
roywant 0:ed0152b5c495 25 /**
roywant 0:ed0152b5c495 26 * Thunk VTable Generator.
roywant 0:ed0152b5c495 27 * This class generate the vtable of a type F for a Thunk.
roywant 0:ed0152b5c495 28 * \tparam F The type of the callable for which the Thunk vtable should be
roywant 0:ed0152b5c495 29 * generated.
roywant 0:ed0152b5c495 30 */
roywant 0:ed0152b5c495 31 template<typename F>
roywant 0:ed0152b5c495 32 struct ThunkVTableGenerator {
roywant 0:ed0152b5c495 33 typedef Thunk thunk_t;
roywant 0:ed0152b5c495 34
roywant 0:ed0152b5c495 35 /**
roywant 0:ed0152b5c495 36 * Implementation of destructor for Thunk holding an F.
roywant 0:ed0152b5c495 37 * @param self The thunk to destroy
roywant 0:ed0152b5c495 38 */
roywant 0:ed0152b5c495 39 static void destroy(thunk_t& self) {
roywant 0:ed0152b5c495 40 get_ptr(self)->~F();
roywant 0:ed0152b5c495 41 }
roywant 0:ed0152b5c495 42
roywant 0:ed0152b5c495 43 /**
roywant 0:ed0152b5c495 44 * Implementation of copy (used by copy constructor and copy assignment)
roywant 0:ed0152b5c495 45 * for a Thunk holding an F.
roywant 0:ed0152b5c495 46 * @param dest The thunk receiving the copy.
roywant 0:ed0152b5c495 47 * @param self The thunk to copy.
roywant 0:ed0152b5c495 48 */
roywant 0:ed0152b5c495 49 static void copy(thunk_t& dest, const thunk_t& self) {
roywant 0:ed0152b5c495 50 new (get_ptr(dest)) F(*get_ptr(self));
roywant 0:ed0152b5c495 51 dest._vtable = self._vtable;
roywant 0:ed0152b5c495 52 }
roywant 0:ed0152b5c495 53
roywant 0:ed0152b5c495 54 /**
roywant 0:ed0152b5c495 55 * Implementation of call operator for a Thunk holding an F.
roywant 0:ed0152b5c495 56 * @param self The thunk containing the F to call.
roywant 0:ed0152b5c495 57 */
roywant 0:ed0152b5c495 58 static void call(const thunk_t& self) {
roywant 0:ed0152b5c495 59 (*get_ptr(self))();
roywant 0:ed0152b5c495 60 }
roywant 0:ed0152b5c495 61
roywant 0:ed0152b5c495 62 /**
roywant 0:ed0152b5c495 63 * The Thunk vtable for an F.
roywant 0:ed0152b5c495 64 */
roywant 0:ed0152b5c495 65 static const ThunkVTable vtable;
roywant 0:ed0152b5c495 66
roywant 0:ed0152b5c495 67 private:
roywant 0:ed0152b5c495 68 /**
roywant 0:ed0152b5c495 69 * Accessor to the pointer to F contained in the Thunk.
roywant 0:ed0152b5c495 70 */
roywant 0:ed0152b5c495 71 static F* get_ptr(thunk_t& thunk) {
roywant 0:ed0152b5c495 72 return static_cast<F*>(thunk._storage.get_storage(0));
roywant 0:ed0152b5c495 73 }
roywant 0:ed0152b5c495 74
roywant 0:ed0152b5c495 75 /**
roywant 0:ed0152b5c495 76 * Accessor to the const pointer to F contained in the const Thunk.
roywant 0:ed0152b5c495 77 */
roywant 0:ed0152b5c495 78 static const F* get_ptr(const thunk_t& thunk) {
roywant 0:ed0152b5c495 79 return static_cast<const F*>(thunk._storage.get_storage(0));
roywant 0:ed0152b5c495 80 }
roywant 0:ed0152b5c495 81 };
roywant 0:ed0152b5c495 82
roywant 0:ed0152b5c495 83 /**
roywant 0:ed0152b5c495 84 * Instantiation of the Thunk vtable of F.
roywant 0:ed0152b5c495 85 */
roywant 0:ed0152b5c495 86 template<typename F>
roywant 0:ed0152b5c495 87 const ThunkVTable ThunkVTableGenerator<F>::vtable = {
roywant 0:ed0152b5c495 88 ThunkVTableGenerator<F>::destroy,
roywant 0:ed0152b5c495 89 ThunkVTableGenerator<F>::copy,
roywant 0:ed0152b5c495 90 ThunkVTableGenerator<F>::call
roywant 0:ed0152b5c495 91 };
roywant 0:ed0152b5c495 92
roywant 0:ed0152b5c495 93 } // namespace detail
roywant 0:ed0152b5c495 94 } // namespace eq
roywant 0:ed0152b5c495 95
roywant 0:ed0152b5c495 96 #endif /* EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_ */