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::mutable_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 list of buffers that represent the output sequence, with the given size.
99 @param size The number of bytes to request.
101 @throws std::length_error if the size would exceed the capacity.
104 prepare(std::size_t size);
106 /** Move bytes from the output sequence to the input sequence.
108 @param size The nubmer of bytes to commit. If this is greater
109 than the size of the output sequence, the entire output
110 sequence is committed.
113 commit(std::size_t size);
115 /** Remove bytes from the input sequence.
117 @param size The number of bytes to consume. If this is greater
118 than the size of the input sequence, the entire input sequence
122 consume(std::size_t size);
127 The buffer will be in an undefined state. It is necessary
128 for the derived class to call @ref reset in order to
129 initialize the object.
131 static_buffer_base() = default;
133 /** Reset the pointed-to buffer.
135 This function resets the internal state to the buffer provided.
136 All input and output sequences are invalidated. This function
137 allows the derived class to construct its members before
138 initializing the static buffer.
140 @param p A pointer to valid storage of at least `n` bytes.
142 @param size The number of valid bytes pointed to by `p`.
145 reset(void* p, std::size_t size);
148 //------------------------------------------------------------------------------
150 /** A circular @b DynamicBuffer with a fixed size internal buffer.
152 This implements a circular dynamic buffer. Calls to @ref prepare
153 never require moving memory. The buffer sequences returned may
155 Ownership of the underlying storage belongs to the derived class.
157 @tparam N The number of bytes in the internal buffer.
159 @note To reduce the number of template instantiations when passing
160 objects of this type in a deduced context, the signature of the
161 receiving function should use @ref static_buffer_base instead.
163 @see @ref static_buffer_base
165 template<std::size_t N>
166 class static_buffer : public static_buffer_base
172 static_buffer(static_buffer const&);
176 : static_buffer_base(buf_, N)
181 static_buffer& operator=(static_buffer const&);
183 /// Returns the @ref static_buffer_base portion of this object
190 /// Returns the @ref static_buffer_base portion of this object
191 static_buffer_base const&
197 /// Return the maximum sum of the input and output sequence sizes.
198 std::size_t constexpr
204 /// Return the maximum sum of input and output sizes that can be held without an allocation.
205 std::size_t constexpr
215 #include <boost/beast/core/impl/static_buffer.ipp>