Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/EventQueue/detail/ThunkVTableGenerator.h@0:1c7da5f83647, 2016-11-29 (annotated)
- Committer:
 - sarahmarshy
 - Date:
 - Tue Nov 29 06:29:10 2016 +0000
 - Revision:
 - 0:1c7da5f83647
 
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New 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_DETAIL_THUNKVTABLEGENERATOR_H_ | 
| sarahmarshy | 0:1c7da5f83647 | 18 | #define EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_ | 
| sarahmarshy | 0:1c7da5f83647 | 19 | |
| sarahmarshy | 0:1c7da5f83647 | 20 | // imported from Thunk.h | 
| sarahmarshy | 0:1c7da5f83647 | 21 | |
| sarahmarshy | 0:1c7da5f83647 | 22 | namespace eq { | 
| sarahmarshy | 0:1c7da5f83647 | 23 | namespace detail { | 
| sarahmarshy | 0:1c7da5f83647 | 24 | |
| sarahmarshy | 0:1c7da5f83647 | 25 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 26 | * Thunk VTable Generator. | 
| sarahmarshy | 0:1c7da5f83647 | 27 | * This class generate the vtable of a type F for a Thunk. | 
| sarahmarshy | 0:1c7da5f83647 | 28 | * \tparam F The type of the callable for which the Thunk vtable should be | 
| sarahmarshy | 0:1c7da5f83647 | 29 | * generated. | 
| sarahmarshy | 0:1c7da5f83647 | 30 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 31 | template<typename F> | 
| sarahmarshy | 0:1c7da5f83647 | 32 | struct ThunkVTableGenerator { | 
| sarahmarshy | 0:1c7da5f83647 | 33 | typedef Thunk thunk_t; | 
| sarahmarshy | 0:1c7da5f83647 | 34 | |
| sarahmarshy | 0:1c7da5f83647 | 35 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 36 | * Implementation of destructor for Thunk holding an F. | 
| sarahmarshy | 0:1c7da5f83647 | 37 | * @param self The thunk to destroy | 
| sarahmarshy | 0:1c7da5f83647 | 38 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 39 | static void destroy(thunk_t& self) { | 
| sarahmarshy | 0:1c7da5f83647 | 40 | get_ptr(self)->~F(); | 
| sarahmarshy | 0:1c7da5f83647 | 41 | } | 
| sarahmarshy | 0:1c7da5f83647 | 42 | |
| sarahmarshy | 0:1c7da5f83647 | 43 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 44 | * Implementation of copy (used by copy constructor and copy assignment) | 
| sarahmarshy | 0:1c7da5f83647 | 45 | * for a Thunk holding an F. | 
| sarahmarshy | 0:1c7da5f83647 | 46 | * @param dest The thunk receiving the copy. | 
| sarahmarshy | 0:1c7da5f83647 | 47 | * @param self The thunk to copy. | 
| sarahmarshy | 0:1c7da5f83647 | 48 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 49 | static void copy(thunk_t& dest, const thunk_t& self) { | 
| sarahmarshy | 0:1c7da5f83647 | 50 | new (get_ptr(dest)) F(*get_ptr(self)); | 
| sarahmarshy | 0:1c7da5f83647 | 51 | dest._vtable = self._vtable; | 
| sarahmarshy | 0:1c7da5f83647 | 52 | } | 
| sarahmarshy | 0:1c7da5f83647 | 53 | |
| sarahmarshy | 0:1c7da5f83647 | 54 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 55 | * Implementation of call operator for a Thunk holding an F. | 
| sarahmarshy | 0:1c7da5f83647 | 56 | * @param self The thunk containing the F to call. | 
| sarahmarshy | 0:1c7da5f83647 | 57 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 58 | static void call(const thunk_t& self) { | 
| sarahmarshy | 0:1c7da5f83647 | 59 | (*get_ptr(self))(); | 
| sarahmarshy | 0:1c7da5f83647 | 60 | } | 
| sarahmarshy | 0:1c7da5f83647 | 61 | |
| sarahmarshy | 0:1c7da5f83647 | 62 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 63 | * The Thunk vtable for an F. | 
| sarahmarshy | 0:1c7da5f83647 | 64 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 65 | static const ThunkVTable vtable; | 
| sarahmarshy | 0:1c7da5f83647 | 66 | |
| sarahmarshy | 0:1c7da5f83647 | 67 | private: | 
| sarahmarshy | 0:1c7da5f83647 | 68 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 69 | * Accessor to the pointer to F contained in the Thunk. | 
| sarahmarshy | 0:1c7da5f83647 | 70 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 71 | static F* get_ptr(thunk_t& thunk) { | 
| sarahmarshy | 0:1c7da5f83647 | 72 | return static_cast<F*>(thunk._storage.get_storage(0)); | 
| sarahmarshy | 0:1c7da5f83647 | 73 | } | 
| sarahmarshy | 0:1c7da5f83647 | 74 | |
| sarahmarshy | 0:1c7da5f83647 | 75 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 76 | * Accessor to the const pointer to F contained in the const Thunk. | 
| sarahmarshy | 0:1c7da5f83647 | 77 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 78 | static const F* get_ptr(const thunk_t& thunk) { | 
| sarahmarshy | 0:1c7da5f83647 | 79 | return static_cast<const F*>(thunk._storage.get_storage(0)); | 
| sarahmarshy | 0:1c7da5f83647 | 80 | } | 
| sarahmarshy | 0:1c7da5f83647 | 81 | }; | 
| sarahmarshy | 0:1c7da5f83647 | 82 | |
| sarahmarshy | 0:1c7da5f83647 | 83 | /** | 
| sarahmarshy | 0:1c7da5f83647 | 84 | * Instantiation of the Thunk vtable of F. | 
| sarahmarshy | 0:1c7da5f83647 | 85 | */ | 
| sarahmarshy | 0:1c7da5f83647 | 86 | template<typename F> | 
| sarahmarshy | 0:1c7da5f83647 | 87 | const ThunkVTable ThunkVTableGenerator<F>::vtable = { | 
| sarahmarshy | 0:1c7da5f83647 | 88 | ThunkVTableGenerator<F>::destroy, | 
| sarahmarshy | 0:1c7da5f83647 | 89 | ThunkVTableGenerator<F>::copy, | 
| sarahmarshy | 0:1c7da5f83647 | 90 | ThunkVTableGenerator<F>::call | 
| sarahmarshy | 0:1c7da5f83647 | 91 | }; | 
| sarahmarshy | 0:1c7da5f83647 | 92 | |
| sarahmarshy | 0:1c7da5f83647 | 93 | } // namespace detail | 
| sarahmarshy | 0:1c7da5f83647 | 94 | } // namespace eq | 
| sarahmarshy | 0:1c7da5f83647 | 95 | |
| sarahmarshy | 0:1c7da5f83647 | 96 | #endif /* EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_ */ |