Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:32

0001 // Copyright (c) 2016-2023 Antony Polukhin
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP
0007 #define BOOST_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP
0008 #pragma once
0009 
0010 #include <boost/pfr/detail/config.hpp>
0011 
0012 #include <type_traits>
0013 #include <utility>      // metaprogramming stuff
0014 #include <boost/pfr/detail/rvalue_t.hpp>
0015 
0016 namespace boost { namespace pfr { namespace detail {
0017 
0018 template <class T, class U>
0019 constexpr void static_assert_layout_compatible() noexcept {
0020     static_assert(
0021         std::alignment_of<T>::value == std::alignment_of<U>::value,
0022         "====================> Boost.PFR: Alignment check failed, probably your structure has user-defined alignment for the whole structure or for some of the fields."
0023     );
0024     static_assert(sizeof(T) == sizeof(U), "====================> Boost.PFR: Size check failed, probably your structure has bitfields or user-defined alignment.");
0025 }
0026 
0027 /// @cond
0028 #ifdef __GNUC__
0029 #define MAY_ALIAS __attribute__((__may_alias__))
0030 #else
0031 #define MAY_ALIAS
0032 #endif
0033 /// @endcond
0034 
0035 template <class To, class From>
0036 MAY_ALIAS const To& cast_to_layout_compatible(const From& val) noexcept {
0037     MAY_ALIAS const To* const t = reinterpret_cast<const To*>( std::addressof(val) );
0038     detail::static_assert_layout_compatible<To, From>();
0039     return *t;
0040 }
0041 
0042 template <class To, class From>
0043 MAY_ALIAS const volatile To& cast_to_layout_compatible(const volatile From& val) noexcept {
0044     MAY_ALIAS const volatile To* const t = reinterpret_cast<const volatile To*>( std::addressof(val) );
0045     detail::static_assert_layout_compatible<To, From>();
0046     return *t;
0047 }
0048 
0049 
0050 template <class To, class From>
0051 MAY_ALIAS volatile To& cast_to_layout_compatible(volatile From& val) noexcept {
0052     MAY_ALIAS volatile To* const t = reinterpret_cast<volatile To*>( std::addressof(val) );
0053     detail::static_assert_layout_compatible<To, From>();
0054     return *t;
0055 }
0056 
0057 
0058 template <class To, class From>
0059 MAY_ALIAS To& cast_to_layout_compatible(From& val) noexcept {
0060     MAY_ALIAS To* const t = reinterpret_cast<To*>( std::addressof(val) );
0061     detail::static_assert_layout_compatible<To, From>();
0062     return *t;
0063 }
0064 
0065 #ifdef BOOST_PFR_DETAIL_STRICT_RVALUE_TESTING
0066 template <class To, class From>
0067 To&& cast_to_layout_compatible(rvalue_t<From> val) noexcept = delete;
0068 #endif
0069 
0070 #undef MAY_ALIAS
0071 
0072 
0073 }}} // namespace boost::pfr::detail
0074 
0075 #endif // BOOST_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP