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

« Back to documentation index

Show/hide line numbers Thunk.impl.h Source File

Thunk.impl.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_THUNK_IMPL_H_
00018 #define EVENTQUEUE_DETAIL_THUNK_IMPL_H_
00019 
00020 #include <new>
00021 #include "ThunkVTableGenerator.h"
00022 
00023 namespace eq {
00024 
00025 /**
00026  * Thunk constructor Implementation.
00027  * Due to the way templates and forwarding work in C++, it was not possible to
00028  * provide this implementation in Thunk.h
00029  */
00030 template<typename F>
00031 Thunk::Thunk(const F& f) :
00032     _storage(),
00033     _vtable(&detail::ThunkVTableGenerator<F>::vtable) {
00034     typedef  __attribute__((unused)) char F_is_too_big_for_the_Thunk[sizeof(F) <= sizeof(_storage) ? 1 : -1];
00035     new(_storage.get_storage(0)) F(f);
00036 }
00037 
00038 /**
00039  * Specialization for function pointers.
00040  * This overload will be chosen when the tyope in input is a reference to a function.
00041  * @param  f The function to transform in Thunk.
00042  */
00043 inline Thunk::Thunk(void (*f)()) :
00044     _storage(),
00045     _vtable(&detail::ThunkVTableGenerator<void(*)()>::vtable) {
00046     typedef void(*F)();
00047     typedef  __attribute__((unused)) char F_is_too_big_for_the_Thunk[sizeof(F) <= sizeof(_storage) ? 1 : -1];
00048     new(_storage.get_storage(0)) F(f);
00049 }
00050 
00051 /**
00052  * Thunk empty constructor Implementation.
00053  * Due to the way templates and forwarding work in C++, it was not possible to
00054  * provide this implementation in Thunk.h
00055  */
00056 inline Thunk::Thunk() :
00057     _storage(),
00058     _vtable(&detail::ThunkVTableGenerator<void(*)()>::vtable) {
00059     typedef void(*F)();
00060     typedef  __attribute__((unused)) char F_is_too_big_for_the_Thunk[sizeof(F) <= sizeof(_storage) ? 1 : -1];
00061     new(_storage.get_storage(0)) F(empty_thunk);
00062 }
00063 
00064 } // namespace eq
00065 
00066 #endif /* EVENTQUEUE_DETAIL_THUNK_IMPL_H_ */