]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/buffers_adaptor.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / core / buffers_adaptor.hpp
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_BUFFERS_ADAPTOR_HPP
11 #define BOOST_BEAST_BUFFERS_ADAPTOR_HPP
12
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>
17
18 namespace boost {
19 namespace beast {
20
21 /** Adapts a <em>MutableBufferSequence</em> into a <em>DynamicBuffer</em>.
22
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.
29
30 The size of the mutable buffer sequence determines the maximum
31 number of bytes which may be prepared and committed.
32
33 @tparam MutableBufferSequence The type of mutable buffer sequence to adapt.
34 */
35 template<class MutableBufferSequence>
36 class buffers_adaptor
37 {
38 static_assert(net::is_mutable_buffer_sequence<
39 MutableBufferSequence>::value,
40 "MutableBufferSequence type requirements not met");
41
42 using iter_type =
43 buffers_iterator_type<MutableBufferSequence>;
44
45 template<bool>
46 class readable_bytes;
47
48 MutableBufferSequence bs_;
49 iter_type begin_;
50 iter_type out_;
51 iter_type end_;
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
57
58 iter_type end_impl() const;
59
60 buffers_adaptor(
61 buffers_adaptor const& other,
62 std::size_t nbegin,
63 std::size_t nout,
64 std::size_t nend);
65
66 public:
67 /// The type of the underlying mutable buffer sequence
68 using value_type = MutableBufferSequence;
69
70 /** Construct a buffers adaptor.
71
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
74 transferred.
75 */
76 explicit
77 buffers_adaptor(MutableBufferSequence const& buffers);
78
79 /** Constructor
80
81 This constructs the buffer adaptor in-place from
82 a list of arguments.
83
84 @param args Arguments forwarded to the buffers constructor.
85 */
86 template<class... Args>
87 explicit
88 buffers_adaptor(boost::in_place_init_t, Args&&... args);
89
90 /// Copy Constructor
91 buffers_adaptor(buffers_adaptor const& other);
92
93 /// Copy Assignment
94 buffers_adaptor& operator=(buffers_adaptor const&);
95
96 /// Returns the original mutable buffer sequence
97 value_type const&
98 value() const
99 {
100 return bs_;
101 }
102
103 //--------------------------------------------------------------------------
104
105 #if BOOST_BEAST_DOXYGEN
106 /// The ConstBufferSequence used to represent the readable bytes.
107 using const_buffers_type = __implementation_defined__;
108
109 /// The MutableBufferSequence used to represent the readable bytes.
110 using mutable_data_type = __implementation_defined__;
111
112 /// The MutableBufferSequence used to represent the writable bytes.
113 using mutable_buffers_type = __implementation_defined__;
114
115 #else
116 using const_buffers_type = readable_bytes<false>;
117 using mutable_data_type = readable_bytes<true>;
118 class mutable_buffers_type;
119 #endif
120
121 /// Returns the number of readable bytes.
122 std::size_t
123 size() const noexcept
124 {
125 return in_size_;
126 }
127
128 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
129 std::size_t
130 max_size() const noexcept
131 {
132 return max_size_;
133 }
134
135 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
136 std::size_t
137 capacity() const noexcept
138 {
139 return max_size_;
140 }
141
142 /// Returns a constant buffer sequence representing the readable bytes
143 const_buffers_type
144 data() const noexcept;
145
146 /// Returns a constant buffer sequence representing the readable bytes
147 const_buffers_type
148 cdata() const noexcept
149 {
150 return data();
151 }
152
153 /// Returns a mutable buffer sequence representing the readable bytes.
154 mutable_data_type
155 data() noexcept;
156
157 /** Returns a mutable buffer sequence representing writable bytes.
158
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.
163
164 All buffer sequences previously obtained using @ref prepare are
165 invalidated. Buffer sequences previously obtained using @ref data
166 remain valid.
167
168 @param n The desired number of bytes in the returned buffer
169 sequence.
170
171 @throws std::length_error if `size() + n` exceeds `max_size()`.
172
173 @esafe
174
175 Strong guarantee.
176 */
177 mutable_buffers_type
178 prepare(std::size_t n);
179
180 /** Append writable bytes to the readable bytes.
181
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.
186
187 All buffer sequences previously obtained using @ref prepare are
188 invalidated. Buffer sequences previously obtained using @ref data
189 remain valid.
190
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.
194
195 @esafe
196
197 No-throw guarantee.
198 */
199 void
200 commit(std::size_t n) noexcept;
201
202 /** Remove bytes from beginning of the readable bytes.
203
204 Removes n bytes from the beginning of the readable bytes.
205
206 All buffers sequences previously obtained using
207 @ref data or @ref prepare are invalidated.
208
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.
212
213 @esafe
214
215 No-throw guarantee.
216 */
217 void
218 consume(std::size_t n) noexcept;
219 };
220
221 } // beast
222 } // boost
223
224 #include <boost/beast/core/impl/buffers_adaptor.hpp>
225
226 #endif