2 // Copyright (c) 2016-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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_BUFFERS_SUFFIX_HPP
11 #define BOOST_BEAST_BUFFERS_SUFFIX_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/detail/type_traits.hpp>
15 #include <boost/asio/buffer.hpp>
16 #include <boost/optional.hpp>
24 /** Adapter to trim the front of a `BufferSequence`.
26 This adapter wraps a buffer sequence to create a new sequence
27 which may be incrementally consumed. Bytes consumed are removed
28 from the front of the buffer. The underlying memory is not changed,
29 instead the adapter efficiently iterates through a subset of
32 The wrapped buffer is not modified, a copy is made instead.
33 Ownership of the underlying memory is not transferred, the application
34 is still responsible for managing its lifetime.
36 @tparam BufferSequence The buffer sequence to wrap.
40 This function writes the entire contents of a buffer sequence
41 to the specified stream.
44 template<class SyncWriteStream, class ConstBufferSequence>
45 void send(SyncWriteStream& stream, ConstBufferSequence const& buffers)
47 buffers_suffix<ConstBufferSequence> bs{buffers};
48 while(boost::asio::buffer_size(bs) > 0)
49 bs.consume(stream.write_some(bs));
53 template<class BufferSequence>
57 typename std::decay<BufferSequence>::type;
59 using iter_type = typename
60 detail::buffer_sequence_iterator<buffers_type>::type;
64 std::size_t skip_ = 0;
66 template<class Deduced>
67 buffers_suffix(Deduced&& other, std::size_t dist)
68 : bs_(std::forward<Deduced>(other).bs_)
70 boost::asio::buffer_sequence_begin(bs_),
77 /** The type for each element in the list of buffers.
79 If the buffers in the underlying sequence are convertible to
80 `boost::asio::mutable_buffer`, then this type will be
81 `boost::asio::mutable_buffer`, else this type will be
82 `boost::asio::const_buffer`.
84 #if BOOST_BEAST_DOXYGEN
85 using value_type = implementation_defined;
87 using value_type = typename std::conditional<
88 std::is_convertible<typename
89 std::iterator_traits<iter_type>::value_type,
90 boost::asio::mutable_buffer>::value,
91 boost::asio::mutable_buffer,
92 boost::asio::const_buffer>::type;
95 #if BOOST_BEAST_DOXYGEN
96 /// A bidirectional iterator type that may be used to read elements.
97 using const_iterator = implementation_defined;
100 class const_iterator;
108 buffers_suffix(buffers_suffix&&);
111 buffers_suffix(buffers_suffix const&);
115 A copy of the buffer sequence is made. Ownership of the
116 underlying memory is not transferred or copied.
119 buffers_suffix(BufferSequence const& buffers);
123 This constructs the buffer sequence in-place from
126 @param args Arguments forwarded to the buffers constructor.
128 template<class... Args>
129 buffers_suffix(boost::in_place_init_t, Args&&... args);
132 buffers_suffix& operator=(buffers_suffix&&);
135 buffers_suffix& operator=(buffers_suffix const&);
137 /// Get a bidirectional iterator to the first element.
141 /// Get a bidirectional iterator to one past the last element.
145 /** Remove bytes from the beginning of the sequence.
147 @param amount The number of bytes to remove. If this is
148 larger than the number of bytes remaining, all the
149 bytes remaining are removed.
152 consume(std::size_t amount);
158 #include <boost/beast/core/impl/buffers_suffix.ipp>