cast_to_layout_compatible.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2016-2023 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BHO_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP
  6. #define BHO_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP
  7. #pragma once
  8. #include <asio2/bho/pfr/detail/config.hpp>
  9. #include <type_traits>
  10. #include <utility> // metaprogramming stuff
  11. #include <asio2/bho/pfr/detail/rvalue_t.hpp>
  12. namespace bho { namespace pfr { namespace detail {
  13. template <class T, class U>
  14. constexpr void static_assert_layout_compatible() noexcept {
  15. static_assert(
  16. std::alignment_of<T>::value == std::alignment_of<U>::value,
  17. "====================> BHO.PFR: Alignment check failed, probably your structure has user-defined alignment for the whole structure or for some of the fields."
  18. );
  19. static_assert(sizeof(T) == sizeof(U), "====================> BHO.PFR: Size check failed, probably your structure has bitfields or user-defined alignment.");
  20. }
  21. /// @cond
  22. #ifdef __GNUC__
  23. #define MAY_ALIAS __attribute__((__may_alias__))
  24. #else
  25. #define MAY_ALIAS
  26. #endif
  27. /// @endcond
  28. template <class To, class From>
  29. MAY_ALIAS const To& cast_to_layout_compatible(const From& val) noexcept {
  30. MAY_ALIAS const To* const t = reinterpret_cast<const To*>( std::addressof(val) );
  31. detail::static_assert_layout_compatible<To, From>();
  32. return *t;
  33. }
  34. template <class To, class From>
  35. MAY_ALIAS const volatile To& cast_to_layout_compatible(const volatile From& val) noexcept {
  36. MAY_ALIAS const volatile To* const t = reinterpret_cast<const volatile To*>( std::addressof(val) );
  37. detail::static_assert_layout_compatible<To, From>();
  38. return *t;
  39. }
  40. template <class To, class From>
  41. MAY_ALIAS volatile To& cast_to_layout_compatible(volatile From& val) noexcept {
  42. MAY_ALIAS volatile To* const t = reinterpret_cast<volatile To*>( std::addressof(val) );
  43. detail::static_assert_layout_compatible<To, From>();
  44. return *t;
  45. }
  46. template <class To, class From>
  47. MAY_ALIAS To& cast_to_layout_compatible(From& val) noexcept {
  48. MAY_ALIAS To* const t = reinterpret_cast<To*>( std::addressof(val) );
  49. detail::static_assert_layout_compatible<To, From>();
  50. return *t;
  51. }
  52. #ifdef BHO_PFR_DETAIL_STRICT_RVALUE_TESTING
  53. template <class To, class From>
  54. To&& cast_to_layout_compatible(rvalue_t<From> val) noexcept = delete;
  55. #endif
  56. #undef MAY_ALIAS
  57. }}} // namespace bho::pfr::detail
  58. #endif // BHO_PFR_DETAIL_CAST_TO_LAYOUT_COMPATIBLE_HPP