2 // Copyright (c) 2016-2017 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/asio/buffer.hpp>
23 /** A circular @b DynamicBuffer with a fixed size internal buffer.
25 This implements a circular dynamic buffer. Calls to @ref prepare
26 never require moving memory. The buffer sequences returned may
28 Ownership of the underlying storage belongs to the derived class.
30 @note Variables are usually declared using the template class
31 @ref static_buffer; however, to reduce the number of instantiations
32 of template functions receiving static stream buffer arguments in a
33 deduced context, the signature of the receiving function should use
34 @ref static_buffer_base.
36 When used with @ref static_buffer this implements a dynamic
37 buffer using no memory allocations.
39 @see @ref static_buffer
41 class static_buffer_base
44 std::size_t in_off_ = 0;
45 std::size_t in_size_ = 0;
46 std::size_t out_size_ = 0;
47 std::size_t capacity_;
49 static_buffer_base(static_buffer_base const& other) = delete;
50 static_buffer_base& operator=(static_buffer_base const&) = delete;
53 /// The type used to represent the input sequence as a list of buffers.
54 using const_buffers_type =
55 std::array<boost::asio::const_buffer, 2>;
57 /// The type used to represent the output sequence as a list of buffers.
58 using mutable_buffers_type =
59 std::array<boost::asio::mutable_buffer, 2>;
63 This creates a dynamic buffer using the provided storage area.
65 @param p A pointer to valid storage of at least `n` bytes.
67 @param size The number of valid bytes pointed to by `p`.
69 static_buffer_base(void* p, std::size_t size);
71 /// Return the size of the input sequence.
78 /// Return the maximum sum of the input and output sequence sizes.
85 /// Return the maximum sum of input and output sizes that can be held without an allocation.
92 /** Get a list of buffers that represent the input sequence.
97 /** Get a mutable list of buffers that represent the input sequence.
102 /** Get a list of buffers that represent the output sequence, with the given size.
104 @param size The number of bytes to request.
106 @throws std::length_error if the size would exceed the capacity.
109 prepare(std::size_t size);
111 /** Move bytes from the output sequence to the input sequence.
113 @param size The nubmer of bytes to commit. If this is greater
114 than the size of the output sequence, the entire output
115 sequence is committed.
118 commit(std::size_t size);
120 /** Remove bytes from the input sequence.
122 @param size The number of bytes to consume. If this is greater
123 than the size of the input sequence, the entire input sequence
127 consume(std::size_t size);
132 The buffer will be in an undefined state. It is necessary
133 for the derived class to call @ref reset in order to
134 initialize the object.
136 static_buffer_base() = default;
138 /** Reset the pointed-to buffer.
140 This function resets the internal state to the buffer provided.
141 All input and output sequences are invalidated. This function
142 allows the derived class to construct its members before
143 initializing the static buffer.
145 @param p A pointer to valid storage of at least `n` bytes.
147 @param size The number of valid bytes pointed to by `p`.
150 reset(void* p, std::size_t size);
153 //------------------------------------------------------------------------------
155 /** A circular @b DynamicBuffer with a fixed size internal buffer.
157 This implements a circular dynamic buffer. Calls to @ref prepare
158 never require moving memory. The buffer sequences returned may
160 Ownership of the underlying storage belongs to the derived class.
162 @tparam N The number of bytes in the internal buffer.
164 @note To reduce the number of template instantiations when passing
165 objects of this type in a deduced context, the signature of the
166 receiving function should use @ref static_buffer_base instead.
168 @see @ref static_buffer_base
170 template<std::size_t N>
171 class static_buffer : public static_buffer_base
177 static_buffer(static_buffer const&);
181 : static_buffer_base(buf_, N)
186 static_buffer& operator=(static_buffer const&);
188 /// Returns the @ref static_buffer_base portion of this object
195 /// Returns the @ref static_buffer_base portion of this object
196 static_buffer_base const&
202 /// Return the maximum sum of the input and output sequence sizes.
203 std::size_t constexpr
209 /// Return the maximum sum of input and output sizes that can be held without an allocation.
210 std::size_t constexpr
220 #include <boost/beast/core/impl/static_buffer.ipp>