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_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_STATIC_BUFFER_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/detail/buffers_pair.hpp>
15 #include <boost/asio/buffer.hpp>
21 /** A dynamic buffer providing a fixed size, circular buffer.
23 A dynamic buffer encapsulates memory storage that may be
24 automatically resized as required, where the memory is
25 divided into two regions: readable bytes followed by
26 writable bytes. These memory regions are internal to
27 the dynamic buffer, but direct access to the elements
28 is provided to permit them to be efficiently used with
31 Objects of this type meet the requirements of <em>DynamicBuffer</em>
32 and have the following additional properties:
34 @li A mutable buffer sequence representing the readable
35 bytes is returned by @ref data when `this` is non-const.
37 @li Buffer sequences representing the readable and writable
38 bytes, returned by @ref data and @ref prepare, may have
41 @li All operations execute in constant time.
43 @li Ownership of the underlying storage belongs to the
46 @note Variables are usually declared using the template class
47 @ref static_buffer; however, to reduce the number of template
48 instantiations, objects should be passed `static_buffer_base&`.
52 class static_buffer_base
55 std::size_t in_off_ = 0;
56 std::size_t in_size_ = 0;
57 std::size_t out_size_ = 0;
58 std::size_t capacity_;
60 static_buffer_base(static_buffer_base const& other) = delete;
61 static_buffer_base& operator=(static_buffer_base const&) = delete;
66 This creates a dynamic buffer using the provided storage area.
68 @param p A pointer to valid storage of at least `n` bytes.
70 @param size The number of valid bytes pointed to by `p`.
73 static_buffer_base(void* p, std::size_t size) noexcept;
75 /** Clear the readable and writable bytes to zero.
77 This function causes the readable and writable bytes
78 to become empty. The capacity is not changed.
80 Buffer sequences previously obtained using @ref data or
81 @ref prepare become invalid.
91 //--------------------------------------------------------------------------
93 #if BOOST_BEAST_DOXYGEN
94 /// The ConstBufferSequence used to represent the readable bytes.
95 using const_buffers_type = __implementation_defined__;
97 /// The MutableBufferSequence used to represent the writable bytes.
98 using mutable_buffers_type = __implementation_defined__;
100 using const_buffers_type = detail::buffers_pair<false>;
102 #ifdef BOOST_BEAST_ALLOW_DEPRECATED
103 using mutable_data_type = detail::buffers_pair<true>;
106 using mutable_buffers_type = detail::buffers_pair<true>;
109 /// Returns the number of readable bytes.
111 size() const noexcept
116 /// Return the maximum number of bytes, both readable and writable, that can ever be held.
118 max_size() const noexcept
123 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
125 capacity() const noexcept
130 /// Returns a constant buffer sequence representing the readable bytes
133 data() const noexcept;
135 /// Returns a constant buffer sequence representing the readable bytes
137 cdata() const noexcept
142 /// Returns a mutable buffer sequence representing the readable bytes
147 /** Returns a mutable buffer sequence representing writable bytes.
149 Returns a mutable buffer sequence representing the writable
150 bytes containing exactly `n` bytes of storage. Memory may be
151 reallocated as needed.
153 All buffers sequences previously obtained using
154 @ref data or @ref prepare are invalidated.
156 @param n The desired number of bytes in the returned buffer
159 @throws std::length_error if `size() + n` exceeds `max_size()`.
167 prepare(std::size_t n);
169 /** Append writable bytes to the readable bytes.
171 Appends n bytes from the start of the writable bytes to the
172 end of the readable bytes. The remainder of the writable bytes
173 are discarded. If n is greater than the number of writable
174 bytes, all writable bytes are appended to the readable bytes.
176 All buffers sequences previously obtained using
177 @ref data or @ref prepare are invalidated.
179 @param n The number of bytes to append. If this number
180 is greater than the number of writable bytes, all
181 writable bytes are appended.
189 commit(std::size_t n) noexcept;
191 /** Remove bytes from beginning of the readable bytes.
193 Removes n bytes from the beginning of the readable bytes.
195 All buffers sequences previously obtained using
196 @ref data or @ref prepare are invalidated.
198 @param n The number of bytes to remove. If this number
199 is greater than the number of readable bytes, all
200 readable bytes are removed.
208 consume(std::size_t n) noexcept;
211 //------------------------------------------------------------------------------
213 /** A dynamic buffer providing a fixed size, circular buffer.
215 A dynamic buffer encapsulates memory storage that may be
216 automatically resized as required, where the memory is
217 divided into two regions: readable bytes followed by
218 writable bytes. These memory regions are internal to
219 the dynamic buffer, but direct access to the elements
220 is provided to permit them to be efficiently used with
223 Objects of this type meet the requirements of <em>DynamicBuffer</em>
224 and have the following additional properties:
226 @li A mutable buffer sequence representing the readable
227 bytes is returned by @ref data when `this` is non-const.
229 @li Buffer sequences representing the readable and writable
230 bytes, returned by @ref data and @ref prepare, may have
233 @li All operations execute in constant time.
235 @tparam N The number of bytes in the internal buffer.
237 @note To reduce the number of template instantiations when passing
238 objects of this type in a deduced context, the signature of the
239 receiving function should use @ref static_buffer_base instead.
241 @see static_buffer_base
243 template<std::size_t N>
244 class static_buffer : public static_buffer_base
250 static_buffer() noexcept
251 : static_buffer_base(buf_, N)
256 static_buffer(static_buffer const&) noexcept;
259 static_buffer& operator=(static_buffer const&) noexcept;
261 /// Returns the @ref static_buffer_base portion of this object
268 /// Returns the @ref static_buffer_base portion of this object
269 static_buffer_base const&
270 base() const noexcept
275 /// Return the maximum sum of the input and output sequence sizes.
276 std::size_t constexpr
277 max_size() const noexcept
282 /// Return the maximum sum of input and output sizes that can be held without an allocation.
283 std::size_t constexpr
284 capacity() const noexcept
293 #include <boost/beast/core/impl/static_buffer.hpp>
294 #ifdef BOOST_BEAST_HEADER_ONLY
295 #include <boost/beast/core/impl/static_buffer.ipp>