2 // Copyright (c) 2013-2017 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)
8 #ifndef BEAST_CONSUMING_BUFFERS_HPP
9 #define BEAST_CONSUMING_BUFFERS_HPP
11 #include <beast/config.hpp>
12 #include <beast/core/buffer_concepts.hpp>
13 #include <boost/asio/buffer.hpp>
16 #include <type_traits>
21 /** Adapter to trim the front of a `BufferSequence`.
23 This adapter wraps a buffer sequence to create a new sequence
24 which may be incrementally consumed. Bytes consumed are removed
25 from the front of the buffer. The underlying memory is not changed,
26 instead the adapter efficiently iterates through a subset of
29 The wrapped buffer is not modified, a copy is made instead.
30 Ownership of the underlying memory is not transferred, the application
31 is still responsible for managing its lifetime.
33 @tparam BufferSequence The buffer sequence to wrap.
35 template<class BufferSequence>
36 class consuming_buffers
39 typename BufferSequence::const_iterator;
44 std::size_t skip_ = 0;
46 template<class Deduced>
47 consuming_buffers(Deduced&& other, std::size_t nbegin)
48 : bs_(std::forward<Deduced>(other).bs_)
49 , begin_(std::next(bs_.begin(), nbegin))
55 /** The type for each element in the list of buffers.
57 If the buffers in the underlying sequence are convertible to
58 `boost::asio::mutable_buffer`, then this type will be
59 `boost::asio::mutable_buffer`, else this type will be
60 `boost::asio::const_buffer`.
63 using value_type = ...;
65 using value_type = typename std::conditional<
66 std::is_convertible<typename
67 std::iterator_traits<iter_type>::value_type,
68 boost::asio::mutable_buffer>::value,
69 boost::asio::mutable_buffer,
70 boost::asio::const_buffer>::type;
74 /// A bidirectional iterator type that may be used to read elements.
75 using const_iterator = implementation_defined;
83 consuming_buffers(consuming_buffers&&);
86 consuming_buffers(consuming_buffers const&);
89 consuming_buffers& operator=(consuming_buffers&&);
92 consuming_buffers& operator=(consuming_buffers const&);
94 /** Construct to represent a buffer sequence.
96 A copy of the buffer sequence is made. Ownership of the
97 underlying memory is not transferred or copied.
100 consuming_buffers(BufferSequence const& buffers);
102 /// Get a bidirectional iterator to the first element.
106 /// Get a bidirectional iterator to one past the last element.
110 /** Remove bytes from the beginning of the sequence.
112 @param n The number of bytes to remove. If this is
113 larger than the number of bytes remaining, all the
114 bytes remaining are removed.
117 consume(std::size_t n);
122 #include <beast/core/impl/consuming_buffers.ipp>