2 // Copyright (c) 2016-2019 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_ADAPTOR_HPP
11 #define BOOST_BEAST_BUFFERS_ADAPTOR_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/buffer_traits.hpp>
15 #include <boost/optional.hpp>
16 #include <type_traits>
21 /** Adapts a <em>MutableBufferSequence</em> into a <em>DynamicBuffer</em>.
23 This class wraps a <em>MutableBufferSequence</em> to meet the requirements
24 of <em>DynamicBuffer</em>. Upon construction the input and output sequences
25 are empty. A copy of the mutable buffer sequence object is stored; however,
26 ownership of the underlying memory is not transferred. The caller is
27 responsible for making sure that referenced memory remains valid
28 for the duration of any operations.
30 The size of the mutable buffer sequence determines the maximum
31 number of bytes which may be prepared and committed.
33 @tparam MutableBufferSequence The type of mutable buffer sequence to adapt.
35 template<class MutableBufferSequence>
38 static_assert(net::is_mutable_buffer_sequence<
39 MutableBufferSequence>::value,
40 "MutableBufferSequence type requirements not met");
43 buffers_iterator_type<MutableBufferSequence>;
48 MutableBufferSequence bs_;
52 std::size_t max_size_;
53 std::size_t in_pos_ = 0; // offset in *begin_
54 std::size_t in_size_ = 0; // size of input sequence
55 std::size_t out_pos_ = 0; // offset in *out_
56 std::size_t out_end_ = 0; // output end offset
58 iter_type end_impl() const;
61 buffers_adaptor const& other,
67 /// The type of the underlying mutable buffer sequence
68 using value_type = MutableBufferSequence;
70 /** Construct a buffers adaptor.
72 @param buffers The mutable buffer sequence to wrap. A copy of
73 the object will be made, but ownership of the memory is not
77 buffers_adaptor(MutableBufferSequence const& buffers);
81 This constructs the buffer adaptor in-place from
84 @param args Arguments forwarded to the buffers constructor.
86 template<class... Args>
88 buffers_adaptor(boost::in_place_init_t, Args&&... args);
91 buffers_adaptor(buffers_adaptor const& other);
94 buffers_adaptor& operator=(buffers_adaptor const&);
96 /// Returns the original mutable buffer sequence
103 //--------------------------------------------------------------------------
105 #if BOOST_BEAST_DOXYGEN
106 /// The ConstBufferSequence used to represent the readable bytes.
107 using const_buffers_type = __implementation_defined__;
109 /// The MutableBufferSequence used to represent the writable bytes.
110 using mutable_buffers_type = __implementation_defined__;
113 using const_buffers_type = subrange<false>;
115 using mutable_buffers_type = subrange<true>;
118 /// Returns the number of readable bytes.
120 size() const noexcept
125 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
127 max_size() const noexcept
132 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
134 capacity() const noexcept
139 /// Returns a constant buffer sequence representing the readable bytes
141 data() const noexcept;
143 /// Returns a constant buffer sequence representing the readable bytes
145 cdata() const noexcept
150 /// Returns a mutable buffer sequence representing the readable bytes.
154 /** Returns a mutable buffer sequence representing writable bytes.
156 Returns a mutable buffer sequence representing the writable
157 bytes containing exactly `n` bytes of storage. This function
158 does not allocate memory. Instead, the storage comes from
159 the underlying mutable buffer sequence.
161 All buffer sequences previously obtained using @ref prepare are
162 invalidated. Buffer sequences previously obtained using @ref data
165 @param n The desired number of bytes in the returned buffer
168 @throws std::length_error if `size() + n` exceeds `max_size()`.
175 prepare(std::size_t n);
177 /** Append writable bytes to the readable bytes.
179 Appends n bytes from the start of the writable bytes to the
180 end of the readable bytes. The remainder of the writable bytes
181 are discarded. If n is greater than the number of writable
182 bytes, all writable bytes are appended to the readable bytes.
184 All buffer sequences previously obtained using @ref prepare are
185 invalidated. Buffer sequences previously obtained using @ref data
188 @param n The number of bytes to append. If this number
189 is greater than the number of writable bytes, all
190 writable bytes are appended.
197 commit(std::size_t n) noexcept;
199 /** Remove bytes from beginning of the readable bytes.
201 Removes n bytes from the beginning of the readable bytes.
203 All buffers sequences previously obtained using
204 @ref data or @ref prepare are invalidated.
206 @param n The number of bytes to remove. If this number
207 is greater than the number of readable bytes, all
208 readable bytes are removed.
215 consume(std::size_t n) noexcept;
220 make_subrange(std::size_t pos, std::size_t n);
223 make_subrange(std::size_t pos, std::size_t n) const;
225 friend struct buffers_adaptor_test_hook;
232 #include <boost/beast/core/impl/buffers_adaptor.hpp>