]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/flat_static_buffer.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / core / flat_static_buffer.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_FLAT_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_FLAT_STATIC_BUFFER_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <algorithm>
16 #include <cstddef>
17 #include <cstring>
18
19 namespace boost {
20 namespace beast {
21
22 /** A dynamic buffer using a fixed size internal buffer.
23
24 A dynamic buffer encapsulates memory storage that may be
25 automatically resized as required, where the memory is
26 divided into two regions: readable bytes followed by
27 writable bytes. These memory regions are internal to
28 the dynamic buffer, but direct access to the elements
29 is provided to permit them to be efficiently used with
30 I/O operations.
31
32 Objects of this type meet the requirements of <em>DynamicBuffer</em>
33 and have the following additional properties:
34
35 @li A mutable buffer sequence representing the readable
36 bytes is returned by @ref data when `this` is non-const.
37
38 @li Buffer sequences representing the readable and writable
39 bytes, returned by @ref data and @ref prepare, will have
40 length one.
41
42 @li Ownership of the underlying storage belongs to the
43 derived class.
44
45 @note Variables are usually declared using the template class
46 @ref flat_static_buffer; however, to reduce the number of template
47 instantiations, objects should be passed `flat_static_buffer_base&`.
48
49 @see flat_static_buffer
50 */
51 class flat_static_buffer_base
52 {
53 char* begin_;
54 char* in_;
55 char* out_;
56 char* last_;
57 char* end_;
58
59 flat_static_buffer_base(
60 flat_static_buffer_base const& other) = delete;
61 flat_static_buffer_base& operator=(
62 flat_static_buffer_base const&) = delete;
63
64 public:
65 /** Constructor
66
67 This creates a dynamic buffer using the provided storage area.
68
69 @param p A pointer to valid storage of at least `n` bytes.
70
71 @param n The number of valid bytes pointed to by `p`.
72 */
73 flat_static_buffer_base(
74 void* p, std::size_t n) noexcept
75 {
76 reset(p, n);
77 }
78
79 /** Clear the readable and writable bytes to zero.
80
81 This function causes the readable and writable bytes
82 to become empty. The capacity is not changed.
83
84 Buffer sequences previously obtained using @ref data or
85 @ref prepare become invalid.
86
87 @esafe
88
89 No-throw guarantee.
90 */
91 BOOST_BEAST_DECL
92 void
93 clear() noexcept;
94
95 #ifdef BOOST_BEAST_ALLOW_DEPRECATED
96 /// Change the number of readable and writable bytes to zero.
97 void
98 reset() noexcept
99 {
100 clear();
101 }
102 #elif ! BOOST_BEAST_DOXYGEN
103 template<std::size_t I = 0>
104 void
105 reset() noexcept
106 {
107 static_assert(I != 0,
108 BOOST_BEAST_DEPRECATION_STRING);
109 }
110 #endif
111
112 //--------------------------------------------------------------------------
113
114 /// The ConstBufferSequence used to represent the readable bytes.
115 using const_buffers_type = net::const_buffer;
116
117 /// The MutableBufferSequence used to represent the readable bytes.
118 using mutable_data_type = net::mutable_buffer;
119
120 /// The MutableBufferSequence used to represent the writable bytes.
121 using mutable_buffers_type = net::mutable_buffer;
122
123 /// Returns the number of readable bytes.
124 std::size_t
125 size() const noexcept
126 {
127 return out_ - in_;
128 }
129
130 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
131 std::size_t
132 max_size() const noexcept
133 {
134 return dist(begin_, end_);
135 }
136
137 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
138 std::size_t
139 capacity() const noexcept
140 {
141 return max_size();
142 }
143
144 /// Returns a constant buffer sequence representing the readable bytes
145 const_buffers_type
146 data() const noexcept
147 {
148 return {in_, dist(in_, out_)};
149 }
150
151 /// Returns a constant buffer sequence representing the readable bytes
152 const_buffers_type
153 cdata() const noexcept
154 {
155 return data();
156 }
157
158 /// Returns a mutable buffer sequence representing the readable bytes
159 mutable_data_type
160 data() noexcept
161 {
162 return {in_, dist(in_, out_)};
163 }
164
165 /** Returns a mutable buffer sequence representing writable bytes.
166
167 Returns a mutable buffer sequence representing the writable
168 bytes containing exactly `n` bytes of storage.
169
170 All buffers sequences previously obtained using
171 @ref data or @ref prepare are invalidated.
172
173 @param n The desired number of bytes in the returned buffer
174 sequence.
175
176 @throws std::length_error if `size() + n` exceeds `max_size()`.
177
178 @esafe
179
180 Strong guarantee.
181 */
182 BOOST_BEAST_DECL
183 mutable_buffers_type
184 prepare(std::size_t n);
185
186 /** Append writable bytes to the readable bytes.
187
188 Appends n bytes from the start of the writable bytes to the
189 end of the readable bytes. The remainder of the writable bytes
190 are discarded. If n is greater than the number of writable
191 bytes, all writable bytes are appended to the readable bytes.
192
193 All buffers sequences previously obtained using
194 @ref data or @ref prepare are invalidated.
195
196 @param n The number of bytes to append. If this number
197 is greater than the number of writable bytes, all
198 writable bytes are appended.
199
200 @esafe
201
202 No-throw guarantee.
203 */
204 void
205 commit(std::size_t n) noexcept
206 {
207 out_ += (std::min<std::size_t>)(n, last_ - out_);
208 }
209
210 /** Remove bytes from beginning of the readable bytes.
211
212 Removes n bytes from the beginning of the readable bytes.
213
214 All buffers sequences previously obtained using
215 @ref data or @ref prepare are invalidated.
216
217 @param n The number of bytes to remove. If this number
218 is greater than the number of readable bytes, all
219 readable bytes are removed.
220
221 @esafe
222
223 No-throw guarantee.
224 */
225 BOOST_BEAST_DECL
226 void
227 consume(std::size_t n) noexcept;
228
229 protected:
230 /** Constructor
231
232 The buffer will be in an undefined state. It is necessary
233 for the derived class to call @ref reset with a pointer
234 and size in order to initialize the object.
235 */
236 flat_static_buffer_base() = default;
237
238 /** Reset the pointed-to buffer.
239
240 This function resets the internal state to the buffer provided.
241 All input and output sequences are invalidated. This function
242 allows the derived class to construct its members before
243 initializing the static buffer.
244
245 @param p A pointer to valid storage of at least `n` bytes.
246
247 @param n The number of valid bytes pointed to by `p`.
248
249 @esafe
250
251 No-throw guarantee.
252 */
253 BOOST_BEAST_DECL
254 void
255 reset(void* p, std::size_t n) noexcept;
256
257 private:
258 static
259 std::size_t
260 dist(char const* first, char const* last) noexcept
261 {
262 return static_cast<std::size_t>(last - first);
263 }
264 };
265
266 //------------------------------------------------------------------------------
267
268 /** A <em>DynamicBuffer</em> with a fixed size internal buffer.
269
270 Buffer sequences returned by @ref data and @ref prepare
271 will always be of length one.
272 This implements a dynamic buffer using no memory allocations.
273
274 @tparam N The number of bytes in the internal buffer.
275
276 @note To reduce the number of template instantiations when passing
277 objects of this type in a deduced context, the signature of the
278 receiving function should use @ref flat_static_buffer_base instead.
279
280 @see flat_static_buffer_base
281 */
282 template<std::size_t N>
283 class flat_static_buffer : public flat_static_buffer_base
284 {
285 char buf_[N];
286
287 public:
288 /// Constructor
289 flat_static_buffer(flat_static_buffer const&);
290
291 /// Constructor
292 flat_static_buffer()
293 : flat_static_buffer_base(buf_, N)
294 {
295 }
296
297 /// Assignment
298 flat_static_buffer& operator=(flat_static_buffer const&);
299
300 /// Returns the @ref flat_static_buffer_base portion of this object
301 flat_static_buffer_base&
302 base()
303 {
304 return *this;
305 }
306
307 /// Returns the @ref flat_static_buffer_base portion of this object
308 flat_static_buffer_base const&
309 base() const
310 {
311 return *this;
312 }
313
314 /// Return the maximum sum of the input and output sequence sizes.
315 std::size_t constexpr
316 max_size() const
317 {
318 return N;
319 }
320
321 /// Return the maximum sum of input and output sizes that can be held without an allocation.
322 std::size_t constexpr
323 capacity() const
324 {
325 return N;
326 }
327 };
328
329 } // beast
330 } // boost
331
332 #include <boost/beast/core/impl/flat_static_buffer.hpp>
333 #ifdef BOOST_BEAST_HEADER_ONLY
334 #include <boost/beast/core/impl/flat_static_buffer.ipp>
335 #endif
336
337 #endif