stream_traits.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_STREAM_TRAITS_HPP
  10. #define BOOST_BEAST_DETAIL_STREAM_TRAITS_HPP
  11. #include <boost/beast/core/error.hpp>
  12. #include <boost/asio/buffer.hpp>
  13. #include <boost/type_traits/make_void.hpp>
  14. #include <type_traits>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. //------------------------------------------------------------------------------
  19. //
  20. // get_lowest_layer
  21. // lowest_layer_type
  22. // detail::has_next_layer
  23. //
  24. template <class T>
  25. std::false_type has_next_layer_impl(void*);
  26. template <class T>
  27. auto has_next_layer_impl(decltype(nullptr)) ->
  28. decltype(std::declval<T&>().next_layer(), std::true_type{});
  29. template <class T>
  30. using has_next_layer = decltype(has_next_layer_impl<T>(nullptr));
  31. template<class T, bool = has_next_layer<T>::value>
  32. struct lowest_layer_type_impl
  33. {
  34. using type = typename std::remove_reference<T>::type;
  35. };
  36. template<class T>
  37. struct lowest_layer_type_impl<T, true>
  38. {
  39. using type = typename lowest_layer_type_impl<
  40. decltype(std::declval<T&>().next_layer())>::type;
  41. };
  42. template<class T>
  43. using lowest_layer_type = typename
  44. lowest_layer_type_impl<T>::type;
  45. template<class T>
  46. T&
  47. get_lowest_layer_impl(
  48. T& t, std::false_type) noexcept
  49. {
  50. return t;
  51. }
  52. template<class T>
  53. lowest_layer_type<T>&
  54. get_lowest_layer_impl(
  55. T& t, std::true_type) noexcept
  56. {
  57. return get_lowest_layer_impl(t.next_layer(),
  58. has_next_layer<typename std::decay<
  59. decltype(t.next_layer())>::type>{});
  60. }
  61. //------------------------------------------------------------------------------
  62. // Types that meet the requirements,
  63. // for use with std::declval only.
  64. template<class BufferType>
  65. struct BufferSequence
  66. {
  67. using value_type = BufferType;
  68. using const_iterator = BufferType const*;
  69. ~BufferSequence() = default;
  70. BufferSequence(BufferSequence const&) = default;
  71. const_iterator begin() const noexcept { return {}; }
  72. const_iterator end() const noexcept { return {}; }
  73. };
  74. using ConstBufferSequence =
  75. BufferSequence<net::const_buffer>;
  76. using MutableBufferSequence =
  77. BufferSequence<net::mutable_buffer>;
  78. //
  79. // Types that meet the requirements,
  80. // for use with std::declval only.
  81. struct StreamHandler
  82. {
  83. StreamHandler(StreamHandler const&) = default;
  84. void operator()(error_code, std::size_t) {}
  85. };
  86. using ReadHandler = StreamHandler;
  87. using WriteHandler = StreamHandler;
  88. //------------------------------------------------------------------------------
  89. } // detail
  90. } // beast
  91. } // boost
  92. #endif