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/in_place_init.hpp>
15 #include <boost/beast/core/detail/type_traits.hpp>
16 #include <boost/asio/buffer.hpp>
17 #include <boost/optional.hpp>
25 /** Adapter to trim the front of a `BufferSequence`.
27 This adapter wraps a buffer sequence to create a new sequence
28 which may be incrementally consumed. Bytes consumed are removed
29 from the front of the buffer. The underlying memory is not changed,
30 instead the adapter efficiently iterates through a subset of
33 The wrapped buffer is not modified, a copy is made instead.
34 Ownership of the underlying memory is not transferred, the application
35 is still responsible for managing its lifetime.
37 @tparam BufferSequence The buffer sequence to wrap.
41 This function writes the entire contents of a buffer sequence
42 to the specified stream.
45 template<class SyncWriteStream, class ConstBufferSequence>
46 void send(SyncWriteStream& stream, ConstBufferSequence const& buffers)
48 buffers_suffix<ConstBufferSequence> bs{buffers};
49 while(boost::asio::buffer_size(bs) > 0)
50 bs.consume(stream.write_some(bs));
54 template<class BufferSequence>
58 typename std::decay<BufferSequence>::type;
60 using iter_type = typename
61 detail::buffer_sequence_iterator<buffers_type>::type;
65 std::size_t skip_ = 0;
67 template<class Deduced>
68 buffers_suffix(Deduced&& other, std::size_t dist)
69 : bs_(std::forward<Deduced>(other).bs_)
71 boost::asio::buffer_sequence_begin(bs_),
78 /** The type for each element in the list of buffers.
80 If the buffers in the underlying sequence are convertible to
81 `boost::asio::mutable_buffer`, then this type will be
82 `boost::asio::mutable_buffer`, else this type will be
83 `boost::asio::const_buffer`.
85 #if BOOST_BEAST_DOXYGEN
86 using value_type = implementation_defined;
88 using value_type = typename std::conditional<
89 std::is_convertible<typename
90 std::iterator_traits<iter_type>::value_type,
91 boost::asio::mutable_buffer>::value,
92 boost::asio::mutable_buffer,
93 boost::asio::const_buffer>::type;
96 #if BOOST_BEAST_DOXYGEN
97 /// A bidirectional iterator type that may be used to read elements.
98 using const_iterator = implementation_defined;
101 class const_iterator;
109 buffers_suffix(buffers_suffix&&);
112 buffers_suffix(buffers_suffix const&);
116 A copy of the buffer sequence is made. Ownership of the
117 underlying memory is not transferred or copied.
120 buffers_suffix(BufferSequence const& buffers);
124 This constructs the buffer sequence in-place from
127 @param args Arguments forwarded to the buffers constructor.
129 template<class... Args>
130 buffers_suffix(boost::in_place_init_t, Args&&... args);
133 buffers_suffix& operator=(buffers_suffix&&);
136 buffers_suffix& operator=(buffers_suffix const&);
138 /// Get a bidirectional iterator to the first element.
142 /// Get a bidirectional iterator to one past the last element.
146 /** Remove bytes from the beginning of the sequence.
148 @param amount The number of bytes to remove. If this is
149 larger than the number of bytes remaining, all the
150 bytes remaining are removed.
153 consume(std::size_t amount);
159 #include <boost/beast/core/impl/buffers_suffix.ipp>