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_FLAT_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_FLAT_STATIC_BUFFER_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
22 /** A dynamic buffer using a fixed size internal buffer.
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
32 Objects of this type meet the requirements of <em>DynamicBuffer</em>
33 and have the following additional properties:
35 @li A mutable buffer sequence representing the readable
36 bytes is returned by @ref data when `this` is non-const.
38 @li Buffer sequences representing the readable and writable
39 bytes, returned by @ref data and @ref prepare, will have
42 @li Ownership of the underlying storage belongs to the
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&`.
49 @see flat_static_buffer
51 class flat_static_buffer_base
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;
67 This creates a dynamic buffer using the provided storage area.
69 @param p A pointer to valid storage of at least `n` bytes.
71 @param n The number of valid bytes pointed to by `p`.
73 flat_static_buffer_base(
74 void* p, std::size_t n) noexcept
79 /** Clear the readable and writable bytes to zero.
81 This function causes the readable and writable bytes
82 to become empty. The capacity is not changed.
84 Buffer sequences previously obtained using @ref data or
85 @ref prepare become invalid.
95 #ifdef BOOST_BEAST_ALLOW_DEPRECATED
96 /// Change the number of readable and writable bytes to zero.
102 #elif ! BOOST_BEAST_DOXYGEN
103 template<std::size_t I = 0>
107 static_assert(I != 0,
108 BOOST_BEAST_DEPRECATION_STRING);
112 //--------------------------------------------------------------------------
114 /// The ConstBufferSequence used to represent the readable bytes.
115 using const_buffers_type = net::const_buffer;
117 /// The MutableBufferSequence used to represent the readable bytes.
118 using mutable_data_type = net::mutable_buffer;
120 /// The MutableBufferSequence used to represent the writable bytes.
121 using mutable_buffers_type = net::mutable_buffer;
123 /// Returns the number of readable bytes.
125 size() const noexcept
130 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
132 max_size() const noexcept
134 return dist(begin_, end_);
137 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
139 capacity() const noexcept
144 /// Returns a constant buffer sequence representing the readable bytes
146 data() const noexcept
148 return {in_, dist(in_, out_)};
151 /// Returns a constant buffer sequence representing the readable bytes
153 cdata() const noexcept
158 /// Returns a mutable buffer sequence representing the readable bytes
162 return {in_, dist(in_, out_)};
165 /** Returns a mutable buffer sequence representing writable bytes.
167 Returns a mutable buffer sequence representing the writable
168 bytes containing exactly `n` bytes of storage.
170 All buffers sequences previously obtained using
171 @ref data or @ref prepare are invalidated.
173 @param n The desired number of bytes in the returned buffer
176 @throws std::length_error if `size() + n` exceeds `max_size()`.
184 prepare(std::size_t n);
186 /** Append writable bytes to the readable bytes.
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.
193 All buffers sequences previously obtained using
194 @ref data or @ref prepare are invalidated.
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.
205 commit(std::size_t n) noexcept
207 out_ += (std::min<std::size_t>)(n, last_ - out_);
210 /** Remove bytes from beginning of the readable bytes.
212 Removes n bytes from the beginning of the readable bytes.
214 All buffers sequences previously obtained using
215 @ref data or @ref prepare are invalidated.
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.
227 consume(std::size_t n) noexcept;
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.
236 flat_static_buffer_base() = default;
238 /** Reset the pointed-to buffer.
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.
245 @param p A pointer to valid storage of at least `n` bytes.
247 @param n The number of valid bytes pointed to by `p`.
255 reset(void* p, std::size_t n) noexcept;
260 dist(char const* first, char const* last) noexcept
262 return static_cast<std::size_t>(last - first);
266 //------------------------------------------------------------------------------
268 /** A <em>DynamicBuffer</em> with a fixed size internal buffer.
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.
274 @tparam N The number of bytes in the internal buffer.
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.
280 @see flat_static_buffer_base
282 template<std::size_t N>
283 class flat_static_buffer : public flat_static_buffer_base
289 flat_static_buffer(flat_static_buffer const&);
293 : flat_static_buffer_base(buf_, N)
298 flat_static_buffer& operator=(flat_static_buffer const&);
300 /// Returns the @ref flat_static_buffer_base portion of this object
301 flat_static_buffer_base&
307 /// Returns the @ref flat_static_buffer_base portion of this object
308 flat_static_buffer_base const&
314 /// Return the maximum sum of the input and output sequence sizes.
315 std::size_t constexpr
321 /// Return the maximum sum of input and output sizes that can be held without an allocation.
322 std::size_t constexpr
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>