2 // detail/consuming_buffers.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_CONSUMING_BUFFERS_HPP
12 #define BOOST_ASIO_DETAIL_CONSUMING_BUFFERS_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/buffer.hpp>
21 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
22 #include <boost/asio/detail/limits.hpp>
24 #include <boost/asio/detail/push_options.hpp>
30 // Helper template to determine the maximum number of prepared buffers.
31 template <typename Buffers>
32 struct prepared_buffers_max
34 enum { value = buffer_sequence_adapter_base::max_buffers };
37 template <typename Elem, std::size_t N>
38 struct prepared_buffers_max<boost::array<Elem, N> >
43 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
45 template <typename Elem, std::size_t N>
46 struct prepared_buffers_max<std::array<Elem, N> >
51 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
53 // A buffer sequence used to represent a subsequence of the buffers.
54 template <typename Buffer, std::size_t MaxBuffers>
55 struct prepared_buffers
57 typedef Buffer value_type;
58 typedef const Buffer* const_iterator;
60 enum { max_buffers = MaxBuffers < 16 ? MaxBuffers : 16 };
62 prepared_buffers() : count(0) {}
63 const_iterator begin() const { return elems; }
64 const_iterator end() const { return elems + count; }
66 Buffer elems[max_buffers];
70 // A proxy for a sub-range in a list of buffers.
71 template <typename Buffer, typename Buffers, typename Buffer_Iterator>
72 class consuming_buffers
75 typedef prepared_buffers<Buffer, prepared_buffers_max<Buffers>::value>
76 prepared_buffers_type;
78 // Construct to represent the entire list of buffers.
79 explicit consuming_buffers(const Buffers& buffers)
85 using boost::asio::buffer_size;
86 total_size_ = buffer_size(buffers);
89 // Determine if we are at the end of the buffers.
92 return total_consumed_ >= total_size_;
95 // Get the buffer for a single transfer, with a size.
96 prepared_buffers_type prepare(std::size_t max_size)
98 prepared_buffers_type result;
100 Buffer_Iterator next = boost::asio::buffer_sequence_begin(buffers_);
101 Buffer_Iterator end = boost::asio::buffer_sequence_end(buffers_);
103 std::advance(next, next_elem_);
104 std::size_t elem_offset = next_elem_offset_;
105 while (next != end && max_size > 0 && (result.count) < result.max_buffers)
107 Buffer next_buf = Buffer(*next) + elem_offset;
108 result.elems[result.count] = boost::asio::buffer(next_buf, max_size);
109 max_size -= result.elems[result.count].size();
111 if (result.elems[result.count].size() > 0)
119 // Consume the specified number of bytes from the buffers.
120 void consume(std::size_t size)
122 total_consumed_ += size;
124 Buffer_Iterator next = boost::asio::buffer_sequence_begin(buffers_);
125 Buffer_Iterator end = boost::asio::buffer_sequence_end(buffers_);
127 std::advance(next, next_elem_);
128 while (next != end && size > 0)
130 Buffer next_buf = Buffer(*next) + next_elem_offset_;
131 if (size < next_buf.size())
133 next_elem_offset_ += size;
138 size -= next_buf.size();
139 next_elem_offset_ = 0;
146 // Get the total number of bytes consumed from the buffers.
147 std::size_t total_consumed() const
149 return total_consumed_;
154 std::size_t total_size_;
155 std::size_t total_consumed_;
156 std::size_t next_elem_;
157 std::size_t next_elem_offset_;
160 // Base class of all consuming_buffers specialisations for single buffers.
161 template <typename Buffer>
162 class consuming_single_buffer
165 // Construct to represent the entire list of buffers.
166 template <typename Buffer1>
167 explicit consuming_single_buffer(const Buffer1& buffer)
173 // Determine if we are at the end of the buffers.
176 return total_consumed_ >= buffer_.size();
179 // Get the buffer for a single transfer, with a size.
180 Buffer prepare(std::size_t max_size)
182 return boost::asio::buffer(buffer_ + total_consumed_, max_size);
185 // Consume the specified number of bytes from the buffers.
186 void consume(std::size_t size)
188 total_consumed_ += size;
191 // Get the total number of bytes consumed from the buffers.
192 std::size_t total_consumed() const
194 return total_consumed_;
199 std::size_t total_consumed_;
203 class consuming_buffers<mutable_buffer, mutable_buffer, const mutable_buffer*>
204 : public consuming_single_buffer<BOOST_ASIO_MUTABLE_BUFFER>
207 explicit consuming_buffers(const mutable_buffer& buffer)
208 : consuming_single_buffer<BOOST_ASIO_MUTABLE_BUFFER>(buffer)
214 class consuming_buffers<const_buffer, mutable_buffer, const mutable_buffer*>
215 : public consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>
218 explicit consuming_buffers(const mutable_buffer& buffer)
219 : consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>(buffer)
225 class consuming_buffers<const_buffer, const_buffer, const const_buffer*>
226 : public consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>
229 explicit consuming_buffers(const const_buffer& buffer)
230 : consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>(buffer)
235 #if !defined(BOOST_ASIO_NO_DEPRECATED)
238 class consuming_buffers<mutable_buffer,
239 mutable_buffers_1, const mutable_buffer*>
240 : public consuming_single_buffer<BOOST_ASIO_MUTABLE_BUFFER>
243 explicit consuming_buffers(const mutable_buffers_1& buffer)
244 : consuming_single_buffer<BOOST_ASIO_MUTABLE_BUFFER>(buffer)
250 class consuming_buffers<const_buffer, mutable_buffers_1, const mutable_buffer*>
251 : public consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>
254 explicit consuming_buffers(const mutable_buffers_1& buffer)
255 : consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>(buffer)
261 class consuming_buffers<const_buffer, const_buffers_1, const const_buffer*>
262 : public consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>
265 explicit consuming_buffers(const const_buffers_1& buffer)
266 : consuming_single_buffer<BOOST_ASIO_CONST_BUFFER>(buffer)
271 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
273 template <typename Buffer, typename Elem>
274 class consuming_buffers<Buffer, boost::array<Elem, 2>,
275 typename boost::array<Elem, 2>::const_iterator>
278 // Construct to represent the entire list of buffers.
279 explicit consuming_buffers(const boost::array<Elem, 2>& buffers)
285 // Determine if we are at the end of the buffers.
288 return total_consumed_ >=
289 Buffer(buffers_[0]).size() + Buffer(buffers_[1]).size();
292 // Get the buffer for a single transfer, with a size.
293 boost::array<Buffer, 2> prepare(std::size_t max_size)
295 boost::array<Buffer, 2> result = {{
296 Buffer(buffers_[0]), Buffer(buffers_[1]) }};
297 std::size_t buffer0_size = result[0].size();
298 result[0] = boost::asio::buffer(result[0] + total_consumed_, max_size);
299 result[1] = boost::asio::buffer(
300 result[1] + (total_consumed_ < buffer0_size
301 ? 0 : total_consumed_ - buffer0_size),
302 max_size - result[0].size());
306 // Consume the specified number of bytes from the buffers.
307 void consume(std::size_t size)
309 total_consumed_ += size;
312 // Get the total number of bytes consumed from the buffers.
313 std::size_t total_consumed() const
315 return total_consumed_;
319 boost::array<Elem, 2> buffers_;
320 std::size_t total_consumed_;
323 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
325 template <typename Buffer, typename Elem>
326 class consuming_buffers<Buffer, std::array<Elem, 2>,
327 typename std::array<Elem, 2>::const_iterator>
330 // Construct to represent the entire list of buffers.
331 explicit consuming_buffers(const std::array<Elem, 2>& buffers)
337 // Determine if we are at the end of the buffers.
340 return total_consumed_ >=
341 Buffer(buffers_[0]).size() + Buffer(buffers_[1]).size();
344 // Get the buffer for a single transfer, with a size.
345 std::array<Buffer, 2> prepare(std::size_t max_size)
347 std::array<Buffer, 2> result = {{
348 Buffer(buffers_[0]), Buffer(buffers_[1]) }};
349 std::size_t buffer0_size = result[0].size();
350 result[0] = boost::asio::buffer(result[0] + total_consumed_, max_size);
351 result[1] = boost::asio::buffer(
352 result[1] + (total_consumed_ < buffer0_size
353 ? 0 : total_consumed_ - buffer0_size),
354 max_size - result[0].size());
358 // Consume the specified number of bytes from the buffers.
359 void consume(std::size_t size)
361 total_consumed_ += size;
364 // Get the total number of bytes consumed from the buffers.
365 std::size_t total_consumed() const
367 return total_consumed_;
371 std::array<Elem, 2> buffers_;
372 std::size_t total_consumed_;
375 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
377 // Specialisation for null_buffers to ensure that the null_buffers type is
378 // always passed through to the underlying read or write operation.
379 template <typename Buffer>
380 class consuming_buffers<Buffer, null_buffers, const mutable_buffer*>
381 : public boost::asio::null_buffers
384 consuming_buffers(const null_buffers&)
394 null_buffers prepare(std::size_t)
396 return null_buffers();
399 void consume(std::size_t)
404 std::size_t total_consumed() const
410 } // namespace detail
414 #include <boost/asio/detail/pop_options.hpp>
416 #endif // BOOST_ASIO_DETAIL_CONSUMING_BUFFERS_HPP