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_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 flat @b DynamicBuffer with a fixed size internal buffer.
24 Buffer sequences returned by @ref data and @ref prepare
25 will always be of length one.
26 Ownership of the underlying storage belongs to the derived class.
28 @note Variables are usually declared using the template class
29 @ref flat_static_buffer; however, to reduce the number of instantiations
30 of template functions receiving static stream buffer arguments in a
31 deduced context, the signature of the receiving function should use
32 @ref flat_static_buffer_base.
34 When used with @ref flat_static_buffer this implements a dynamic
35 buffer using no memory allocations.
37 @see @ref flat_static_buffer
39 class flat_static_buffer_base
47 flat_static_buffer_base(flat_static_buffer_base const& other) = delete;
48 flat_static_buffer_base& operator=(flat_static_buffer_base const&) = delete;
51 /** The type used to represent the input sequence as a list of buffers.
53 This buffer sequence is guaranteed to have length 1.
55 using const_buffers_type = boost::asio::mutable_buffer;
57 /** The type used to represent the output sequence as a list of buffers.
59 This buffer sequence is guaranteed to have length 1.
61 using mutable_buffers_type = boost::asio::mutable_buffer;
65 This creates a dynamic buffer using the provided storage area.
67 @param p A pointer to valid storage of at least `n` bytes.
69 @param n The number of valid bytes pointed to by `p`.
71 flat_static_buffer_base(void* p, std::size_t n)
76 /// Return the size of the input sequence.
83 /// Return the maximum sum of the input and output sequence sizes.
87 return dist(begin_, end_);
90 /// Return the maximum sum of input and output sizes that can be held without an allocation.
97 /** Get a list of buffers that represent the input sequence.
99 @note These buffers remain valid across subsequent calls to `prepare`.
104 /// Set the input and output sequences to size 0
108 /** Get a list of buffers that represent the output sequence, with the given size.
110 @throws std::length_error if the size would exceed the limit
111 imposed by the underlying mutable buffer sequence.
113 @note Buffers representing the input sequence acquired prior to
114 this call remain valid.
117 prepare(std::size_t n);
119 /** Move bytes from the output sequence to the input sequence.
121 @note Buffers representing the input sequence acquired prior to
122 this call remain valid.
125 commit(std::size_t n)
127 out_ += (std::min<std::size_t>)(n, last_ - out_);
130 /// Remove bytes from the input sequence.
132 consume(std::size_t n)
140 The buffer will be in an undefined state. It is necessary
141 for the derived class to call @ref reset with a pointer
142 and size in order to initialize the object.
144 flat_static_buffer_base() = default;
146 /** Reset the pointed-to buffer.
148 This function resets the internal state to the buffer provided.
149 All input and output sequences are invalidated. This function
150 allows the derived class to construct its members before
151 initializing the static buffer.
153 @param p A pointer to valid storage of at least `n` bytes.
155 @param n The number of valid bytes pointed to by `p`.
158 reset(void* p, std::size_t n);
164 dist(char const* first, char const* last)
166 return static_cast<std::size_t>(last - first);
169 template<class = void>
173 template<class = void>
175 reset_impl(void* p, std::size_t n);
177 template<class = void>
179 prepare_impl(std::size_t n);
181 template<class = void>
183 consume_impl(std::size_t n);
186 //------------------------------------------------------------------------------
188 /** A @b DynamicBuffer with a fixed size internal buffer.
190 Buffer sequences returned by @ref data and @ref prepare
191 will always be of length one.
192 This implements a dynamic buffer using no memory allocations.
194 @tparam N The number of bytes in the internal buffer.
196 @note To reduce the number of template instantiations when passing
197 objects of this type in a deduced context, the signature of the
198 receiving function should use @ref flat_static_buffer_base instead.
200 @see @ref flat_static_buffer_base
202 template<std::size_t N>
203 class flat_static_buffer : public flat_static_buffer_base
209 flat_static_buffer(flat_static_buffer const&);
213 : flat_static_buffer_base(buf_, N)
218 flat_static_buffer& operator=(flat_static_buffer const&);
220 /// Returns the @ref flat_static_buffer_base portion of this object
221 flat_static_buffer_base&
227 /// Returns the @ref flat_static_buffer_base portion of this object
228 flat_static_buffer_base const&
234 /// Return the maximum sum of the input and output sequence sizes.
235 std::size_t constexpr
241 /// Return the maximum sum of input and output sizes that can be held without an allocation.
242 std::size_t constexpr
252 #include <boost/beast/core/impl/flat_static_buffer.ipp>