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_BUFFER_TRAITS_HPP
11 #define BOOST_BEAST_DETAIL_BUFFER_TRAITS_HPP
12 
13 #include <boost/asio/buffer.hpp>
14 #include <boost/config/workaround.hpp>
15 #include <boost/type_traits/make_void.hpp>
16 #include <cstdint>
17 #include <type_traits>
18 
19 namespace boost {
20 namespace beast {
21 namespace detail {
22 
23 #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
24 
25 template<class T>
26 struct buffers_iterator_type_helper
27 {
28     using type = decltype(
29         net::buffer_sequence_begin(
30             std::declval<T const&>()));
31 };
32 
33 template<>
34 struct buffers_iterator_type_helper<
35     net::const_buffer>
36 {
37     using type = net::const_buffer const*;
38 };
39 
40 template<>
41 struct buffers_iterator_type_helper<
42     net::mutable_buffer>
43 {
44     using type = net::mutable_buffer const*;
45 };
46 
47 #endif
48 
49 struct buffer_bytes_impl
50 {
51     std::size_t
operator ()boost::beast::detail::buffer_bytes_impl52     operator()(net::const_buffer b) const noexcept
53     {
54         return net::const_buffer(b).size();
55     }
56 
57     std::size_t
operator ()boost::beast::detail::buffer_bytes_impl58     operator()(net::mutable_buffer b) const noexcept
59     {
60         return net::mutable_buffer(b).size();
61     }
62 
63     template<
64         class B,
65         class = typename std::enable_if<
66             net::is_const_buffer_sequence<B>::value>::type>
67     std::size_t
operator ()boost::beast::detail::buffer_bytes_impl68     operator()(B const& b) const noexcept
69     {
70         using net::buffer_size;
71         return buffer_size(b);
72     }
73 };
74 
75 /** Return `true` if a buffer sequence is empty
76 
77     This is sometimes faster than using @ref buffer_bytes
78 */
79 template<class ConstBufferSequence>
80 bool
buffers_empty(ConstBufferSequence const & buffers)81 buffers_empty(ConstBufferSequence const& buffers)
82 {
83     auto it = net::buffer_sequence_begin(buffers);
84     auto end = net::buffer_sequence_end(buffers);
85     while(it != end)
86     {
87         if(net::const_buffer(*it).size() > 0)
88             return false;
89         ++it;
90     }
91     return true;
92 }
93 
94 } // detail
95 } // beast
96 } // boost
97 
98 #endif
99