]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/static_buffer.hpp
update sources to ceph Nautilus 14.2.1
[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::const_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 mutable list of buffers that represent the input sequence.
98 */
99 mutable_buffers_type
100 mutable_data();
101
102 /** Get a list of buffers that represent the output sequence, with the given size.
103
104 @param size The number of bytes to request.
105
106 @throws std::length_error if the size would exceed the capacity.
107 */
108 mutable_buffers_type
109 prepare(std::size_t size);
110
111 /** Move bytes from the output sequence to the input sequence.
112
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.
116 */
117 void
118 commit(std::size_t size);
119
120 /** Remove bytes from the input sequence.
121
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
124 is consumed.
125 */
126 void
127 consume(std::size_t size);
128
129 protected:
130 /** Constructor
131
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.
135 */
136 static_buffer_base() = default;
137
138 /** Reset the pointed-to buffer.
139
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.
144
145 @param p A pointer to valid storage of at least `n` bytes.
146
147 @param size The number of valid bytes pointed to by `p`.
148 */
149 void
150 reset(void* p, std::size_t size);
151 };
152
153 //------------------------------------------------------------------------------
154
155 /** A circular @b DynamicBuffer with a fixed size internal buffer.
156
157 This implements a circular dynamic buffer. Calls to @ref prepare
158 never require moving memory. The buffer sequences returned may
159 be up to length two.
160 Ownership of the underlying storage belongs to the derived class.
161
162 @tparam N The number of bytes in the internal buffer.
163
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.
167
168 @see @ref static_buffer_base
169 */
170 template<std::size_t N>
171 class static_buffer : public static_buffer_base
172 {
173 char buf_[N];
174
175 public:
176 /// Constructor
177 static_buffer(static_buffer const&);
178
179 /// Constructor
180 static_buffer()
181 : static_buffer_base(buf_, N)
182 {
183 }
184
185 /// Assignment
186 static_buffer& operator=(static_buffer const&);
187
188 /// Returns the @ref static_buffer_base portion of this object
189 static_buffer_base&
190 base()
191 {
192 return *this;
193 }
194
195 /// Returns the @ref static_buffer_base portion of this object
196 static_buffer_base const&
197 base() const
198 {
199 return *this;
200 }
201
202 /// Return the maximum sum of the input and output sequence sizes.
203 std::size_t constexpr
204 max_size() const
205 {
206 return N;
207 }
208
209 /// Return the maximum sum of input and output sizes that can be held without an allocation.
210 std::size_t constexpr
211 capacity() const
212 {
213 return N;
214 }
215 };
216
217 } // beast
218 } // boost
219
220 #include <boost/beast/core/impl/static_buffer.ipp>
221
222 #endif