Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ThunkVTableGenerator.h Source File

ThunkVTableGenerator.h

00001 /*
00002  * Copyright (c) 2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_
00018 #define EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_
00019 
00020 // imported from Thunk.h
00021 
00022 namespace eq {
00023 namespace detail {
00024 
00025 /**
00026  * Thunk VTable Generator.
00027  * This class generate the vtable of a type F for a Thunk.
00028  * \tparam F The type of the callable for which the Thunk vtable should be
00029  * generated.
00030  */
00031 template<typename F>
00032 struct ThunkVTableGenerator {
00033     typedef Thunk thunk_t;
00034 
00035     /**
00036      * Implementation of destructor for Thunk holding an F.
00037      * @param self The thunk to destroy
00038      */
00039     static void destroy(thunk_t& self) {
00040         get_ptr(self)->~F();
00041     }
00042 
00043     /**
00044      * Implementation of copy (used by copy constructor and copy assignment)
00045      * for a Thunk holding an F.
00046      * @param dest The thunk receiving the copy.
00047      * @param self The thunk to copy.
00048      */
00049     static void copy(thunk_t& dest, const thunk_t& self) {
00050         new (get_ptr(dest)) F(*get_ptr(self));
00051         dest._vtable = self._vtable;
00052     }
00053 
00054     /**
00055      * Implementation of call operator for a Thunk holding an F.
00056      * @param self The thunk containing the F to call.
00057      */
00058     static void call(const thunk_t& self) {
00059         (*get_ptr(self))();
00060     }
00061 
00062     /**
00063      * The Thunk vtable for an F.
00064      */
00065     static const ThunkVTable vtable;
00066 
00067 private:
00068     /**
00069      * Accessor to the pointer to F contained in the Thunk.
00070      */
00071     static F* get_ptr(thunk_t& thunk) {
00072         return static_cast<F*>(thunk._storage.get_storage(0));
00073     }
00074 
00075     /**
00076      * Accessor to the const pointer to F contained in the const Thunk.
00077      */
00078     static const F* get_ptr(const thunk_t& thunk) {
00079         return static_cast<const F*>(thunk._storage.get_storage(0));
00080     }
00081 };
00082 
00083 /**
00084  * Instantiation of the Thunk vtable of F.
00085  */
00086 template<typename F>
00087 const ThunkVTable ThunkVTableGenerator<F>::vtable = {
00088         ThunkVTableGenerator<F>::destroy,
00089         ThunkVTableGenerator<F>::copy,
00090         ThunkVTableGenerator<F>::call
00091 };
00092 
00093 } // namespace detail
00094 } // namespace eq
00095 
00096 #endif /* EVENTQUEUE_DETAIL_THUNKVTABLEGENERATOR_H_ */