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 readable bytes.
110 using mutable_data_type = __implementation_defined__;
112 /// The MutableBufferSequence used to represent the writable bytes.
113 using mutable_buffers_type = __implementation_defined__;
116 using const_buffers_type = readable_bytes<false>;
117 using mutable_data_type = readable_bytes<true>;
118 class mutable_buffers_type;
121 /// Returns the number of readable bytes.
123 size() const noexcept
128 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
130 max_size() const noexcept
135 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
137 capacity() const noexcept
142 /// Returns a constant buffer sequence representing the readable bytes
144 data() const noexcept;
146 /// Returns a constant buffer sequence representing the readable bytes
148 cdata() const noexcept
153 /// Returns a mutable buffer sequence representing the readable bytes.
157 /** Returns a mutable buffer sequence representing writable bytes.
159 Returns a mutable buffer sequence representing the writable
160 bytes containing exactly `n` bytes of storage. This function
161 does not allocate memory. Instead, the storage comes from
162 the underlying mutable buffer sequence.
164 All buffer sequences previously obtained using @ref prepare are
165 invalidated. Buffer sequences previously obtained using @ref data
168 @param n The desired number of bytes in the returned buffer
171 @throws std::length_error if `size() + n` exceeds `max_size()`.
178 prepare(std::size_t n);
180 /** Append writable bytes to the readable bytes.
182 Appends n bytes from the start of the writable bytes to the
183 end of the readable bytes. The remainder of the writable bytes
184 are discarded. If n is greater than the number of writable
185 bytes, all writable bytes are appended to the readable bytes.
187 All buffer sequences previously obtained using @ref prepare are
188 invalidated. Buffer sequences previously obtained using @ref data
191 @param n The number of bytes to append. If this number
192 is greater than the number of writable bytes, all
193 writable bytes are appended.
200 commit(std::size_t n) noexcept;
202 /** Remove bytes from beginning of the readable bytes.
204 Removes n bytes from the beginning of the readable bytes.
206 All buffers sequences previously obtained using
207 @ref data or @ref prepare are invalidated.
209 @param n The number of bytes to remove. If this number
210 is greater than the number of readable bytes, all
211 readable bytes are removed.
218 consume(std::size_t n) noexcept;
224 #include <boost/beast/core/impl/buffers_adaptor.hpp>