type_traits.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  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. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_TYPE_TRAITS_HPP
  10. #define BOOST_BEAST_DETAIL_TYPE_TRAITS_HPP
  11. #include <boost/type_traits/make_void.hpp>
  12. #include <type_traits>
  13. #include <new>
  14. namespace boost {
  15. namespace beast {
  16. namespace detail {
  17. template<class U>
  18. std::size_t constexpr
  19. max_sizeof()
  20. {
  21. return sizeof(U);
  22. }
  23. template<class U0, class U1, class... Us>
  24. std::size_t constexpr
  25. max_sizeof()
  26. {
  27. return
  28. max_sizeof<U0>() > max_sizeof<U1, Us...>() ?
  29. max_sizeof<U0>() : max_sizeof<U1, Us...>();
  30. }
  31. template<class U>
  32. std::size_t constexpr
  33. max_alignof()
  34. {
  35. return alignof(U);
  36. }
  37. template<class U0, class U1, class... Us>
  38. std::size_t constexpr
  39. max_alignof()
  40. {
  41. return
  42. max_alignof<U0>() > max_alignof<U1, Us...>() ?
  43. max_alignof<U0>() : max_alignof<U1, Us...>();
  44. }
  45. // (since C++17)
  46. template<class... Ts>
  47. using make_void = boost::make_void<Ts...>;
  48. template<class... Ts>
  49. using void_t = boost::void_t<Ts...>;
  50. // (since C++11) missing from g++4.8
  51. template<std::size_t Len, class... Ts>
  52. struct aligned_union
  53. {
  54. static
  55. std::size_t constexpr alignment_value =
  56. max_alignof<Ts...>();
  57. using type = typename std::aligned_storage<
  58. (Len > max_sizeof<Ts...>()) ? Len : (max_sizeof<Ts...>()),
  59. alignment_value>::type;
  60. };
  61. template<std::size_t Len, class... Ts>
  62. using aligned_union_t =
  63. typename aligned_union<Len, Ts...>::type;
  64. //------------------------------------------------------------------------------
  65. // for span
  66. template<class T, class E, class = void>
  67. struct is_contiguous_container: std::false_type {};
  68. template<class T, class E>
  69. struct is_contiguous_container<T, E, void_t<
  70. decltype(
  71. std::declval<std::size_t&>() = std::declval<T const&>().size(),
  72. std::declval<E*&>() = std::declval<T&>().data()),
  73. typename std::enable_if<
  74. std::is_same<
  75. typename std::remove_cv<E>::type,
  76. typename std::remove_cv<
  77. typename std::remove_pointer<
  78. decltype(std::declval<T&>().data())
  79. >::type
  80. >::type
  81. >::value
  82. >::type>>: std::true_type
  83. {};
  84. template <class T, class U>
  85. T launder_cast(U* u)
  86. {
  87. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  88. return std::launder(reinterpret_cast<T>(u));
  89. #elif defined(BOOST_GCC) && BOOST_GCC_VERSION > 80000
  90. return __builtin_launder(reinterpret_cast<T>(u));
  91. #else
  92. return reinterpret_cast<T>(u);
  93. #endif
  94. }
  95. } // detail
  96. } // beast
  97. } // boost
  98. #endif