]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/flat_static_buffer.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / flat_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_FLAT_STATIC_BUFFER_HPP
11 #define BOOST_BEAST_FLAT_STATIC_BUFFER_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <algorithm>
16 #include <cstddef>
17 #include <cstring>
18
19 namespace boost {
20 namespace beast {
21
22 /** A flat @b DynamicBuffer with a fixed size internal buffer.
23
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.
27
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.
33
34 When used with @ref flat_static_buffer this implements a dynamic
35 buffer using no memory allocations.
36
37 @see @ref flat_static_buffer
38 */
39 class flat_static_buffer_base
40 {
41 char* begin_;
42 char* in_;
43 char* out_;
44 char* last_;
45 char* end_;
46
47 flat_static_buffer_base(flat_static_buffer_base const& other) = delete;
48 flat_static_buffer_base& operator=(flat_static_buffer_base const&) = delete;
49
50 public:
51 /** The type used to represent the input sequence as a list of buffers.
52
53 This buffer sequence is guaranteed to have length 1.
54 */
55 using const_buffers_type = boost::asio::mutable_buffer;
56
57 /** The type used to represent the output sequence as a list of buffers.
58
59 This buffer sequence is guaranteed to have length 1.
60 */
61 using mutable_buffers_type = boost::asio::mutable_buffer;
62
63 /** Constructor
64
65 This creates a dynamic buffer using the provided storage area.
66
67 @param p A pointer to valid storage of at least `n` bytes.
68
69 @param n The number of valid bytes pointed to by `p`.
70 */
71 flat_static_buffer_base(void* p, std::size_t n)
72 {
73 reset_impl(p, n);
74 }
75
76 /// Return the size of the input sequence.
77 std::size_t
78 size() const
79 {
80 return out_ - in_;
81 }
82
83 /// Return the maximum sum of the input and output sequence sizes.
84 std::size_t
85 max_size() const
86 {
87 return dist(begin_, end_);
88 }
89
90 /// Return the maximum sum of input and output sizes that can be held without an allocation.
91 std::size_t
92 capacity() const
93 {
94 return max_size();
95 }
96
97 /** Get a list of buffers that represent the input sequence.
98
99 @note These buffers remain valid across subsequent calls to `prepare`.
100 */
101 const_buffers_type
102 data() const;
103
104 /// Set the input and output sequences to size 0
105 void
106 reset();
107
108 /** Get a list of buffers that represent the output sequence, with the given size.
109
110 @throws std::length_error if the size would exceed the limit
111 imposed by the underlying mutable buffer sequence.
112
113 @note Buffers representing the input sequence acquired prior to
114 this call remain valid.
115 */
116 mutable_buffers_type
117 prepare(std::size_t n);
118
119 /** Move bytes from the output sequence to the input sequence.
120
121 @note Buffers representing the input sequence acquired prior to
122 this call remain valid.
123 */
124 void
125 commit(std::size_t n)
126 {
127 out_ += (std::min<std::size_t>)(n, last_ - out_);
128 }
129
130 /// Remove bytes from the input sequence.
131 void
132 consume(std::size_t n)
133 {
134 consume_impl(n);
135 }
136
137 protected:
138 /** Constructor
139
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.
143 */
144 flat_static_buffer_base() = default;
145
146 /** Reset the pointed-to buffer.
147
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.
152
153 @param p A pointer to valid storage of at least `n` bytes.
154
155 @param n The number of valid bytes pointed to by `p`.
156 */
157 void
158 reset(void* p, std::size_t n);
159
160 private:
161 static
162 inline
163 std::size_t
164 dist(char const* first, char const* last)
165 {
166 return static_cast<std::size_t>(last - first);
167 }
168
169 template<class = void>
170 void
171 reset_impl();
172
173 template<class = void>
174 void
175 reset_impl(void* p, std::size_t n);
176
177 template<class = void>
178 mutable_buffers_type
179 prepare_impl(std::size_t n);
180
181 template<class = void>
182 void
183 consume_impl(std::size_t n);
184 };
185
186 //------------------------------------------------------------------------------
187
188 /** A @b DynamicBuffer with a fixed size internal buffer.
189
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.
193
194 @tparam N The number of bytes in the internal buffer.
195
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.
199
200 @see @ref flat_static_buffer_base
201 */
202 template<std::size_t N>
203 class flat_static_buffer : public flat_static_buffer_base
204 {
205 char buf_[N];
206
207 public:
208 /// Constructor
209 flat_static_buffer(flat_static_buffer const&);
210
211 /// Constructor
212 flat_static_buffer()
213 : flat_static_buffer_base(buf_, N)
214 {
215 }
216
217 /// Assignment
218 flat_static_buffer& operator=(flat_static_buffer const&);
219
220 /// Returns the @ref flat_static_buffer_base portion of this object
221 flat_static_buffer_base&
222 base()
223 {
224 return *this;
225 }
226
227 /// Returns the @ref flat_static_buffer_base portion of this object
228 flat_static_buffer_base const&
229 base() const
230 {
231 return *this;
232 }
233
234 /// Return the maximum sum of the input and output sequence sizes.
235 std::size_t constexpr
236 max_size() const
237 {
238 return N;
239 }
240
241 /// Return the maximum sum of input and output sizes that can be held without an allocation.
242 std::size_t constexpr
243 capacity() const
244 {
245 return N;
246 }
247 };
248
249 } // beast
250 } // boost
251
252 #include <boost/beast/core/impl/flat_static_buffer.ipp>
253
254 #endif