offset_based_getter.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2017-2018 Chris Beck
  2. // Copyright (c) 2019-2024 Antony Polukhin
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_PFR_DETAIL_OFFSET_BASED_GETTER_HPP
  7. #define BOOST_PFR_DETAIL_OFFSET_BASED_GETTER_HPP
  8. #pragma once
  9. #include <boost/pfr/detail/config.hpp>
  10. #include <type_traits>
  11. #include <utility>
  12. #include <memory> // std::addressof
  13. #include <boost/pfr/detail/sequence_tuple.hpp>
  14. #include <boost/pfr/detail/rvalue_t.hpp>
  15. #include <boost/pfr/detail/size_t_.hpp>
  16. namespace boost { namespace pfr { namespace detail {
  17. // Our own implementation of std::aligned_storage. On godbolt with MSVC, I have compilation errors
  18. // using the standard version, it seems the compiler cannot generate default ctor.
  19. template<std::size_t s, std::size_t a>
  20. struct internal_aligned_storage {
  21. alignas(a) char storage_[s];
  22. };
  23. // Metafunction that replaces tuple<T1, T2, T3, ...> with
  24. // tuple<std::aligned_storage_t<sizeof(T1), alignof(T1)>, std::aligned_storage<sizeof(T2), alignof(T2)>, ...>
  25. //
  26. // The point is, the new tuple is "layout compatible" in the sense that members have the same offsets,
  27. // but this tuple is constexpr constructible.
  28. template <typename T>
  29. struct tuple_of_aligned_storage;
  30. template <typename... Ts>
  31. struct tuple_of_aligned_storage<sequence_tuple::tuple<Ts...>> {
  32. using type = sequence_tuple::tuple<internal_aligned_storage<sizeof(Ts),
  33. #if defined(__GNUC__) && __GNUC__ < 8 && !defined(__x86_64__) && !defined(__CYGWIN__)
  34. // Before GCC-8 the `alignof` was returning the optimal alignment rather than the minimal one.
  35. // We have to adjust the alignment because otherwise we get the wrong offset.
  36. (alignof(Ts) > 4 ? 4 : alignof(Ts))
  37. #else
  38. alignof(Ts)
  39. #endif
  40. >...>;
  41. };
  42. // Note: If pfr has a typelist also, could also have an overload for that here
  43. template <typename T>
  44. using tuple_of_aligned_storage_t = typename tuple_of_aligned_storage<T>::type;
  45. /***
  46. * Given a structure type and its sequence of members, we want to build a function
  47. * object "getter" that implements a version of `std::get` using offset arithmetic
  48. * and reinterpret_cast.
  49. *
  50. * typename U should be a user-defined struct
  51. * typename S should be a sequence_tuple which is layout compatible with U
  52. */
  53. template <typename U, typename S>
  54. class offset_based_getter {
  55. using this_t = offset_based_getter<U, S>;
  56. 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?");
  57. static_assert(alignof(U) == alignof(S), "====================> Boost.PFR: Member sequence does not indicate correct alignment for struct type!");
  58. 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");
  59. 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");
  60. 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");
  61. // Get type of idx'th member
  62. template <std::size_t idx>
  63. using index_t = typename sequence_tuple::tuple_element<idx, S>::type;
  64. // Get offset of idx'th member
  65. // Idea: Layout object has the same offsets as instance of S, so if S and U are layout compatible, then these offset
  66. // calculations are correct.
  67. template <std::size_t idx>
  68. static constexpr std::ptrdiff_t offset() noexcept {
  69. constexpr tuple_of_aligned_storage_t<S> layout{};
  70. return &sequence_tuple::get<idx>(layout).storage_[0] - &sequence_tuple::get<0>(layout).storage_[0];
  71. }
  72. // Encapsulates offset arithmetic and reinterpret_cast
  73. template <std::size_t idx>
  74. static index_t<idx> * get_pointer(U * u) noexcept {
  75. return reinterpret_cast<index_t<idx> *>(reinterpret_cast<char *>(u) + this_t::offset<idx>());
  76. }
  77. template <std::size_t idx>
  78. static const index_t<idx> * get_pointer(const U * u) noexcept {
  79. return reinterpret_cast<const index_t<idx> *>(reinterpret_cast<const char *>(u) + this_t::offset<idx>());
  80. }
  81. template <std::size_t idx>
  82. static volatile index_t<idx> * get_pointer(volatile U * u) noexcept {
  83. return reinterpret_cast<volatile index_t<idx> *>(reinterpret_cast<volatile char *>(u) + this_t::offset<idx>());
  84. }
  85. template <std::size_t idx>
  86. static const volatile index_t<idx> * get_pointer(const volatile U * u) noexcept {
  87. return reinterpret_cast<const volatile index_t<idx> *>(reinterpret_cast<const volatile char *>(u) + this_t::offset<idx>());
  88. }
  89. public:
  90. template <std::size_t idx>
  91. index_t<idx> & get(U & u, size_t_<idx>) const noexcept {
  92. return *this_t::get_pointer<idx>(std::addressof(u));
  93. }
  94. template <std::size_t idx>
  95. index_t<idx> const & get(U const & u, size_t_<idx>) const noexcept {
  96. return *this_t::get_pointer<idx>(std::addressof(u));
  97. }
  98. template <std::size_t idx>
  99. index_t<idx> volatile & get(U volatile & u, size_t_<idx>) const noexcept {
  100. return *this_t::get_pointer<idx>(std::addressof(u));
  101. }
  102. template <std::size_t idx>
  103. index_t<idx> const volatile & get(U const volatile & u, size_t_<idx>) const noexcept {
  104. return *this_t::get_pointer<idx>(std::addressof(u));
  105. }
  106. // rvalues must not be used here, to avoid template instantiation bloats.
  107. template <std::size_t idx>
  108. index_t<idx> && get(rvalue_t<U> u, size_t_<idx>) const = delete;
  109. };
  110. }}} // namespace boost::pfr::detail
  111. #endif // BOOST_PFR_DETAIL_OFFSET_LIST_HPP