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