]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/static_buffer.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / static_buffer.hpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_STATIC_BUFFER_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <algorithm>
16 #include <array>
17 #include <cstddef>
18 #include <cstring>
19
20 namespace boost {
21 namespace beast {
22
23 /** A circular @b DynamicBuffer with a fixed size internal buffer.
24
25 This implements a circular dynamic buffer. Calls to @ref prepare
26 never require moving memory. The buffer sequences returned may
27 be up to length two.
28 Ownership of the underlying storage belongs to the derived class.
29
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.
35
36 When used with @ref static_buffer this implements a dynamic
37 buffer using no memory allocations.
38
39 @see @ref static_buffer
40 */
41 class static_buffer_base
42 {
43 char* begin_;
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_;
48
49 static_buffer_base(static_buffer_base const& other) = delete;
50 static_buffer_base& operator=(static_buffer_base const&) = delete;
51
52 public:
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>;
56
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>;
60
61 /** Constructor
62
63 This creates a dynamic buffer using the provided storage area.
64
65 @param p A pointer to valid storage of at least `n` bytes.
66
67 @param size The number of valid bytes pointed to by `p`.
68 */
69 static_buffer_base(void* p, std::size_t size);
70
71 /// Return the size of the input sequence.
72 std::size_t
73 size() const
74 {
75 return in_size_;
76 }
77
78 /// Return the maximum sum of the input and output sequence sizes.
79 std::size_t
80 max_size() const
81 {
82 return capacity_;
83 }
84
85 /// Return the maximum sum of input and output sizes that can be held without an allocation.
86 std::size_t
87 capacity() const
88 {
89 return capacity_;
90 }
91
92 /** Get a list of buffers that represent the input sequence.
93 */
94 const_buffers_type
95 data() const;
96
97 /** Get a list of buffers that represent the output sequence, with the given size.
98
99 @param size The number of bytes to request.
100
101 @throws std::length_error if the size would exceed the capacity.
102 */
103 mutable_buffers_type
104 prepare(std::size_t size);
105
106 /** Move bytes from the output sequence to the input sequence.
107
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.
111 */
112 void
113 commit(std::size_t size);
114
115 /** Remove bytes from the input sequence.
116
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
119 is consumed.
120 */
121 void
122 consume(std::size_t size);
123
124 protected:
125 /** Constructor
126
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.
130 */
131 static_buffer_base() = default;
132
133 /** Reset the pointed-to buffer.
134
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.
139
140 @param p A pointer to valid storage of at least `n` bytes.
141
142 @param size The number of valid bytes pointed to by `p`.
143 */
144 void
145 reset(void* p, std::size_t size);
146 };
147
148 //------------------------------------------------------------------------------
149
150 /** A circular @b DynamicBuffer with a fixed size internal buffer.
151
152 This implements a circular dynamic buffer. Calls to @ref prepare
153 never require moving memory. The buffer sequences returned may
154 be up to length two.
155 Ownership of the underlying storage belongs to the derived class.
156
157 @tparam N The number of bytes in the internal buffer.
158
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.
162
163 @see @ref static_buffer_base
164 */
165 template<std::size_t N>
166 class static_buffer : public static_buffer_base
167 {
168 char buf_[N];
169
170 public:
171 /// Constructor
172 static_buffer(static_buffer const&);
173
174 /// Constructor
175 static_buffer()
176 : static_buffer_base(buf_, N)
177 {
178 }
179
180 /// Assignment
181 static_buffer& operator=(static_buffer const&);
182
183 /// Returns the @ref static_buffer_base portion of this object
184 static_buffer_base&
185 base()
186 {
187 return *this;
188 }
189
190 /// Returns the @ref static_buffer_base portion of this object
191 static_buffer_base const&
192 base() const
193 {
194 return *this;
195 }
196
197 /// Return the maximum sum of the input and output sequence sizes.
198 std::size_t constexpr
199 max_size() const
200 {
201 return N;
202 }
203
204 /// Return the maximum sum of input and output sizes that can be held without an allocation.
205 std::size_t constexpr
206 capacity() const
207 {
208 return N;
209 }
210 };
211
212 } // beast
213 } // boost
214
215 #include <boost/beast/core/impl/static_buffer.ipp>
216
217 #endif