]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/buffers_prefix.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / buffers_prefix.hpp
1 //
2 // Copyright (c) 2016-2017 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_BUFFERS_PREFIX_HPP
11 #define BOOST_BEAST_BUFFERS_PREFIX_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/type_traits.hpp>
15 #include <boost/beast/core/detail/in_place_init.hpp>
16 #include <boost/asio/buffer.hpp>
17 #include <cstdint>
18 #include <type_traits>
19
20 namespace boost {
21 namespace beast {
22
23 /** A buffer sequence adapter that shortens the sequence size.
24
25 The class adapts a buffer sequence to efficiently represent
26 a shorter subset of the original list of buffers starting
27 with the first byte of the original sequence.
28
29 @tparam BufferSequence The buffer sequence to adapt.
30 */
31 template<class BufferSequence>
32 class buffers_prefix_view
33 {
34 using buffers_type = typename
35 std::decay<BufferSequence>::type;
36
37 using iter_type = typename
38 detail::buffer_sequence_iterator<buffers_type>::type;
39
40 BufferSequence bs_;
41 std::size_t size_;
42 iter_type end_;
43
44 template<class Deduced>
45 buffers_prefix_view(
46 Deduced&& other, std::size_t dist)
47 : bs_(std::forward<Deduced>(other).bs_)
48 , size_(other.size_)
49 , end_(std::next(bs_.begin(), dist))
50 {
51 }
52
53 void
54 setup(std::size_t size);
55
56 public:
57 /// The type for each element in the list of buffers.
58 using value_type = typename std::conditional<
59 std::is_convertible<typename
60 std::iterator_traits<iter_type>::value_type,
61 boost::asio::mutable_buffer>::value,
62 boost::asio::mutable_buffer,
63 boost::asio::const_buffer>::type;
64
65 #if BOOST_BEAST_DOXYGEN
66 /// A bidirectional iterator type that may be used to read elements.
67 using const_iterator = implementation_defined;
68
69 #else
70 class const_iterator;
71
72 #endif
73
74 /// Move constructor.
75 buffers_prefix_view(buffers_prefix_view&&);
76
77 /// Copy constructor.
78 buffers_prefix_view(buffers_prefix_view const&);
79
80 /// Move assignment.
81 buffers_prefix_view& operator=(buffers_prefix_view&&);
82
83 /// Copy assignment.
84 buffers_prefix_view& operator=(buffers_prefix_view const&);
85
86 /** Construct a buffer sequence prefix.
87
88 @param size The maximum number of bytes in the prefix.
89 If this is larger than the size of passed, buffers,
90 the resulting sequence will represent the entire
91 input sequence.
92
93 @param buffers The buffer sequence to adapt. A copy of
94 the sequence will be made, but ownership of the underlying
95 memory is not transferred.
96 */
97 buffers_prefix_view(
98 std::size_t size,
99 BufferSequence const& buffers);
100
101 /** Construct a buffer sequence prefix in-place.
102
103 @param size The maximum number of bytes in the prefix.
104 If this is larger than the size of passed, buffers,
105 the resulting sequence will represent the entire
106 input sequence.
107
108 @param args Arguments forwarded to the contained buffers constructor.
109 */
110 template<class... Args>
111 buffers_prefix_view(
112 std::size_t size,
113 boost::in_place_init_t,
114 Args&&... args);
115
116 /// Get a bidirectional iterator to the first element.
117 const_iterator
118 begin() const;
119
120 /// Get a bidirectional iterator to one past the last element.
121 const_iterator
122 end() const;
123 };
124
125 /** Returns a prefix of a constant buffer.
126
127 The returned buffer points to the same memory as the
128 passed buffer, but with a size that is equal to or less
129 than the size of the original buffer.
130
131 @param size The size of the returned buffer.
132
133 @param buffer The buffer to shorten. The underlying
134 memory is not modified.
135
136 @return A new buffer that points to the first `size`
137 bytes of the original buffer.
138 */
139 inline
140 boost::asio::const_buffer
141 buffers_prefix(std::size_t size,
142 boost::asio::const_buffer buffer)
143 {
144 return {buffer.data(),
145 (std::min)(size, buffer.size())};
146 }
147
148 /** Returns a prefix of a mutable buffer.
149
150 The returned buffer points to the same memory as the
151 passed buffer, but with a size that is equal to or less
152 than the size of the original buffer.
153
154 @param size The size of the returned buffer.
155
156 @param buffer The buffer to shorten. The underlying
157 memory is not modified.
158
159 @return A new buffer that points to the first `size` bytes
160 of the original buffer.
161 */
162 inline
163 boost::asio::mutable_buffer
164 buffers_prefix(std::size_t size,
165 boost::asio::mutable_buffer buffer)
166 {
167 return {buffer.data(),
168 (std::min)(size, buffer.size())};
169 }
170
171 /** Returns a prefix of a buffer sequence.
172
173 This function returns a new buffer sequence which when iterated,
174 presents a shorter subset of the original list of buffers starting
175 with the first byte of the original sequence.
176
177 @param size The maximum number of bytes in the wrapped
178 sequence. If this is larger than the size of passed,
179 buffers, the resulting sequence will represent the
180 entire input sequence.
181
182 @param buffers An instance of @b ConstBufferSequence or
183 @b MutableBufferSequence to adapt. A copy of the sequence
184 will be made, but ownership of the underlying memory is
185 not transferred.
186 */
187 template<class BufferSequence>
188 #if BOOST_BEAST_DOXYGEN
189 buffers_prefix_view<BufferSequence>
190 #else
191 inline
192 typename std::enable_if<
193 ! std::is_same<BufferSequence,
194 boost::asio::const_buffer>::value &&
195 ! std::is_same<BufferSequence,
196 boost::asio::mutable_buffer>::value,
197 buffers_prefix_view<BufferSequence>>::type
198 #endif
199 buffers_prefix(std::size_t size, BufferSequence const& buffers)
200 {
201 static_assert(
202 boost::asio::is_const_buffer_sequence<BufferSequence>::value ||
203 boost::asio::is_mutable_buffer_sequence<BufferSequence>::value,
204 "BufferSequence requirements not met");
205 return buffers_prefix_view<BufferSequence>(size, buffers);
206 }
207
208 /** Returns the first buffer in a buffer sequence
209
210 This returns the first buffer in the buffer sequence.
211 If the buffer sequence is an empty range, the returned
212 buffer will have a zero buffer size.
213
214 @param buffers The buffer sequence. If the sequence is
215 mutable, the returned buffer sequence will also be mutable.
216 Otherwise, the returned buffer sequence will be constant.
217 */
218 template<class BufferSequence>
219 typename std::conditional<
220 boost::asio::is_mutable_buffer_sequence<BufferSequence>::value,
221 boost::asio::mutable_buffer,
222 boost::asio::const_buffer>::type
223 buffers_front(BufferSequence const& buffers)
224 {
225 auto const first =
226 boost::asio::buffer_sequence_begin(buffers);
227 if(first == boost::asio::buffer_sequence_end(buffers))
228 return {};
229 return *first;
230 }
231
232 } // beast
233 } // boost
234
235 #include <boost/beast/core/impl/buffers_prefix.ipp>
236
237 #endif