File indexing completed on 2025-01-18 09:43:38
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_PFR_DETAIL_OFFSET_BASED_GETTER_HPP
0008 #define BOOST_PFR_DETAIL_OFFSET_BASED_GETTER_HPP
0009 #pragma once
0010
0011 #include <boost/pfr/detail/config.hpp>
0012
0013 #include <type_traits>
0014 #include <utility>
0015 #include <memory> // std::addressof
0016 #include <boost/pfr/detail/sequence_tuple.hpp>
0017 #include <boost/pfr/detail/rvalue_t.hpp>
0018 #include <boost/pfr/detail/size_t_.hpp>
0019
0020
0021 namespace boost { namespace pfr { namespace detail {
0022
0023
0024
0025
0026 template<std::size_t s, std::size_t a>
0027 struct internal_aligned_storage {
0028 alignas(a) char storage_[s];
0029 };
0030
0031
0032
0033
0034
0035
0036
0037 template <typename T>
0038 struct tuple_of_aligned_storage;
0039
0040 template <typename... Ts>
0041 struct tuple_of_aligned_storage<sequence_tuple::tuple<Ts...>> {
0042 using type = sequence_tuple::tuple<internal_aligned_storage<sizeof(Ts),
0043 #if defined(__GNUC__) && __GNUC__ < 8 && !defined(__x86_64__) && !defined(__CYGWIN__)
0044
0045
0046 (alignof(Ts) > 4 ? 4 : alignof(Ts))
0047 #else
0048 alignof(Ts)
0049 #endif
0050 >...>;
0051 };
0052
0053
0054
0055 template <typename T>
0056 using tuple_of_aligned_storage_t = typename tuple_of_aligned_storage<T>::type;
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 template <typename U, typename S>
0068 class offset_based_getter {
0069 using this_t = offset_based_getter<U, S>;
0070
0071 static_assert(sizeof(U) == sizeof(S), "====================> Boost.PFR: Member sequence does not indicate correct size for struct type! Maybe the user-provided type is not a SimpleAggregate?");
0072 static_assert(alignof(U) == alignof(S), "====================> Boost.PFR: Member sequence does not indicate correct alignment for struct type!");
0073
0074 static_assert(!std::is_const<U>::value, "====================> Boost.PFR: const should be stripped from user-defined type when using offset_based_getter or overload resolution will be ambiguous later, this indicates an error within pfr");
0075 static_assert(!std::is_reference<U>::value, "====================> Boost.PFR: reference should be stripped from user-defined type when using offset_based_getter or overload resolution will be ambiguous later, this indicates an error within pfr");
0076 static_assert(!std::is_volatile<U>::value, "====================> Boost.PFR: volatile should be stripped from user-defined type when using offset_based_getter or overload resolution will be ambiguous later. this indicates an error within pfr");
0077
0078
0079 template <std::size_t idx>
0080 using index_t = typename sequence_tuple::tuple_element<idx, S>::type;
0081
0082
0083
0084
0085 template <std::size_t idx>
0086 static constexpr std::ptrdiff_t offset() noexcept {
0087 constexpr tuple_of_aligned_storage_t<S> layout{};
0088 return &sequence_tuple::get<idx>(layout).storage_[0] - &sequence_tuple::get<0>(layout).storage_[0];
0089 }
0090
0091
0092 template <std::size_t idx>
0093 static index_t<idx> * get_pointer(U * u) noexcept {
0094 return reinterpret_cast<index_t<idx> *>(reinterpret_cast<char *>(u) + this_t::offset<idx>());
0095 }
0096
0097 template <std::size_t idx>
0098 static const index_t<idx> * get_pointer(const U * u) noexcept {
0099 return reinterpret_cast<const index_t<idx> *>(reinterpret_cast<const char *>(u) + this_t::offset<idx>());
0100 }
0101
0102 template <std::size_t idx>
0103 static volatile index_t<idx> * get_pointer(volatile U * u) noexcept {
0104 return reinterpret_cast<volatile index_t<idx> *>(reinterpret_cast<volatile char *>(u) + this_t::offset<idx>());
0105 }
0106
0107 template <std::size_t idx>
0108 static const volatile index_t<idx> * get_pointer(const volatile U * u) noexcept {
0109 return reinterpret_cast<const volatile index_t<idx> *>(reinterpret_cast<const volatile char *>(u) + this_t::offset<idx>());
0110 }
0111
0112 public:
0113 template <std::size_t idx>
0114 index_t<idx> & get(U & u, size_t_<idx>) const noexcept {
0115 return *this_t::get_pointer<idx>(std::addressof(u));
0116 }
0117
0118 template <std::size_t idx>
0119 index_t<idx> const & get(U const & u, size_t_<idx>) const noexcept {
0120 return *this_t::get_pointer<idx>(std::addressof(u));
0121 }
0122
0123 template <std::size_t idx>
0124 index_t<idx> volatile & get(U volatile & u, size_t_<idx>) const noexcept {
0125 return *this_t::get_pointer<idx>(std::addressof(u));
0126 }
0127
0128 template <std::size_t idx>
0129 index_t<idx> const volatile & get(U const volatile & u, size_t_<idx>) const noexcept {
0130 return *this_t::get_pointer<idx>(std::addressof(u));
0131 }
0132
0133
0134 template <std::size_t idx>
0135 index_t<idx> && get(rvalue_t<U> u, size_t_<idx>) const = delete;
0136 };
0137
0138
0139 }}}
0140
0141 #endif