2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_BUFFERS_PREFIX_HPP
11 #define BOOST_BEAST_BUFFERS_PREFIX_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/buffer_traits.hpp>
15 #include <boost/optional/optional.hpp> // for in_place_init_t
18 #include <type_traits>
20 #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
21 #include <boost/type_traits.hpp>
27 /** A buffer sequence adaptor that shortens the sequence size.
29 The class adapts a buffer sequence to efficiently represent
30 a shorter subset of the original list of buffers starting
31 with the first byte of the original sequence.
33 @tparam BufferSequence The buffer sequence to adapt.
35 template<class BufferSequence>
36 class buffers_prefix_view
39 buffers_iterator_type<BufferSequence>;
42 std::size_t size_ = 0;
43 std::size_t remain_ = 0;
47 setup(std::size_t size);
50 buffers_prefix_view const& other,
54 /** The type for each element in the list of buffers.
56 If the type `BufferSequence` meets the requirements of
57 <em>MutableBufferSequence</em>, then `value_type` is
58 `net::mutable_buffer`. Otherwise, `value_type` is
63 #if BOOST_BEAST_DOXYGEN
64 using value_type = __see_below__;
65 #elif BOOST_WORKAROUND(BOOST_MSVC, < 1910)
66 using value_type = typename std::conditional<
67 boost::is_convertible<typename
68 std::iterator_traits<iter_type>::value_type,
69 net::mutable_buffer>::value,
71 net::const_buffer>::type;
73 using value_type = buffers_type<BufferSequence>;
76 #if BOOST_BEAST_DOXYGEN
77 /// A bidirectional iterator type that may be used to read elements.
78 using const_iterator = __implementation_defined__;
86 buffers_prefix_view(buffers_prefix_view const&);
89 buffers_prefix_view& operator=(buffers_prefix_view const&);
91 /** Construct a buffer sequence prefix.
93 @param size The maximum number of bytes in the prefix.
94 If this is larger than the size of passed buffers,
95 the resulting sequence will represent the entire
98 @param buffers The buffer sequence to adapt. A copy of
99 the sequence will be made, but ownership of the underlying
100 memory is not transferred. The copy is maintained for
101 the lifetime of the view.
105 BufferSequence const& buffers);
107 /** Construct a buffer sequence prefix in-place.
109 @param size The maximum number of bytes in the prefix.
110 If this is larger than the size of passed buffers,
111 the resulting sequence will represent the entire
114 @param args Arguments forwarded to the contained buffer's constructor.
116 template<class... Args>
119 boost::in_place_init_t,
122 /// Returns an iterator to the first buffer in the sequence
126 /// Returns an iterator to one past the last buffer in the sequence
130 #if ! BOOST_BEAST_DOXYGEN
132 buffer_bytes_impl() const noexcept
139 //------------------------------------------------------------------------------
141 /** Returns a prefix of a constant or mutable buffer sequence.
143 The returned buffer sequence points to the same memory as the
144 passed buffer sequence, but with a size that is equal to or
145 smaller. No memory allocations are performed; the resulting
146 sequence is calculated as a lazy range.
148 @param size The maximum size of the returned buffer sequence
149 in bytes. If this is greater than or equal to the size of
150 the passed buffer sequence, the result will have the same
151 size as the original buffer sequence.
153 @param buffers An object whose type meets the requirements
154 of <em>BufferSequence</em>. The returned value will
155 maintain a copy of the passed buffers for its lifetime;
156 however, ownership of the underlying memory is not
159 @return A constant buffer sequence that represents the prefix
160 of the original buffer sequence. If the original buffer sequence
161 also meets the requirements of <em>MutableBufferSequence</em>,
162 then the returned value will also be a mutable buffer sequence.
164 template<class BufferSequence>
165 buffers_prefix_view<BufferSequence>
167 std::size_t size, BufferSequence const& buffers)
170 net::is_const_buffer_sequence<BufferSequence>::value,
171 "BufferSequence type requirements not met");
172 return buffers_prefix_view<BufferSequence>(size, buffers);
175 /** Returns the first buffer in a buffer sequence
177 This returns the first buffer in the buffer sequence.
178 If the buffer sequence is an empty range, the returned
179 buffer will have a zero buffer size.
181 @param buffers The buffer sequence. If the sequence is
182 mutable, the returned buffer sequence will also be mutable.
183 Otherwise, the returned buffer sequence will be constant.
185 template<class BufferSequence>
186 buffers_type<BufferSequence>
187 buffers_front(BufferSequence const& buffers)
190 net::buffer_sequence_begin(buffers);
191 if(first == net::buffer_sequence_end(buffers))
199 #include <boost/beast/core/impl/buffers_prefix.hpp>