offset_based_getter.hpp 5.8 KB

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