]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/include/beast/core/impl/static_streambuf.ipp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / include / beast / core / impl / static_streambuf.ipp
1 //
2 // Copyright (c) 2013-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
8 #ifndef BEAST_IMPL_STATIC_STREAMBUF_IPP
9 #define BEAST_IMPL_STATIC_STREAMBUF_IPP
10
11 #include <beast/core/detail/type_traits.hpp>
12 #include <boost/asio/buffer.hpp>
13 #include <algorithm>
14 #include <cstring>
15 #include <iterator>
16 #include <stdexcept>
17
18 namespace beast {
19
20 class static_streambuf::const_buffers_type
21 {
22 std::size_t n_;
23 std::uint8_t const* p_;
24
25 public:
26 using value_type = boost::asio::const_buffer;
27
28 class const_iterator;
29
30 const_buffers_type() = delete;
31 const_buffers_type(
32 const_buffers_type const&) = default;
33 const_buffers_type& operator=(
34 const_buffers_type const&) = default;
35
36 const_iterator
37 begin() const;
38
39 const_iterator
40 end() const;
41
42 private:
43 friend class static_streambuf;
44
45 const_buffers_type(
46 std::uint8_t const* p, std::size_t n)
47 : n_(n)
48 , p_(p)
49 {
50 }
51 };
52
53 class static_streambuf::const_buffers_type::const_iterator
54 {
55 std::size_t n_ = 0;
56 std::uint8_t const* p_ = nullptr;
57
58 public:
59 using value_type = boost::asio::const_buffer;
60 using pointer = value_type const*;
61 using reference = value_type;
62 using difference_type = std::ptrdiff_t;
63 using iterator_category =
64 std::bidirectional_iterator_tag;
65
66 const_iterator() = default;
67 const_iterator(const_iterator&& other) = default;
68 const_iterator(const_iterator const& other) = default;
69 const_iterator& operator=(const_iterator&& other) = default;
70 const_iterator& operator=(const_iterator const& other) = default;
71
72 bool
73 operator==(const_iterator const& other) const
74 {
75 return p_ == other.p_;
76 }
77
78 bool
79 operator!=(const_iterator const& other) const
80 {
81 return !(*this == other);
82 }
83
84 reference
85 operator*() const
86 {
87 return value_type{p_, n_};
88 }
89
90 pointer
91 operator->() const = delete;
92
93 const_iterator&
94 operator++()
95 {
96 p_ += n_;
97 return *this;
98 }
99
100 const_iterator
101 operator++(int)
102 {
103 auto temp = *this;
104 ++(*this);
105 return temp;
106 }
107
108 const_iterator&
109 operator--()
110 {
111 p_ -= n_;
112 return *this;
113 }
114
115 const_iterator
116 operator--(int)
117 {
118 auto temp = *this;
119 --(*this);
120 return temp;
121 }
122
123 private:
124 friend class const_buffers_type;
125
126 const_iterator(
127 std::uint8_t const* p, std::size_t n)
128 : n_(n)
129 , p_(p)
130 {
131 }
132 };
133
134 inline
135 auto
136 static_streambuf::const_buffers_type::begin() const ->
137 const_iterator
138 {
139 return const_iterator{p_, n_};
140 }
141
142 inline
143 auto
144 static_streambuf::const_buffers_type::end() const ->
145 const_iterator
146 {
147 return const_iterator{p_ + n_, n_};
148 }
149
150 //------------------------------------------------------------------------------
151
152 class static_streambuf::mutable_buffers_type
153 {
154 std::size_t n_;
155 std::uint8_t* p_;
156
157 public:
158 using value_type = boost::asio::mutable_buffer;
159
160 class const_iterator;
161
162 mutable_buffers_type() = delete;
163 mutable_buffers_type(
164 mutable_buffers_type const&) = default;
165 mutable_buffers_type& operator=(
166 mutable_buffers_type const&) = default;
167
168 const_iterator
169 begin() const;
170
171 const_iterator
172 end() const;
173
174 private:
175 friend class static_streambuf;
176
177 mutable_buffers_type(
178 std::uint8_t* p, std::size_t n)
179 : n_(n)
180 , p_(p)
181 {
182 }
183 };
184
185 class static_streambuf::mutable_buffers_type::const_iterator
186 {
187 std::size_t n_ = 0;
188 std::uint8_t* p_ = nullptr;
189
190 public:
191 using value_type = boost::asio::mutable_buffer;
192 using pointer = value_type const*;
193 using reference = value_type;
194 using difference_type = std::ptrdiff_t;
195 using iterator_category =
196 std::bidirectional_iterator_tag;
197
198 const_iterator() = default;
199 const_iterator(const_iterator&& other) = default;
200 const_iterator(const_iterator const& other) = default;
201 const_iterator& operator=(const_iterator&& other) = default;
202 const_iterator& operator=(const_iterator const& other) = default;
203
204 bool
205 operator==(const_iterator const& other) const
206 {
207 return p_ == other.p_;
208 }
209
210 bool
211 operator!=(const_iterator const& other) const
212 {
213 return !(*this == other);
214 }
215
216 reference
217 operator*() const
218 {
219 return value_type{p_, n_};
220 }
221
222 pointer
223 operator->() const = delete;
224
225 const_iterator&
226 operator++()
227 {
228 p_ += n_;
229 return *this;
230 }
231
232 const_iterator
233 operator++(int)
234 {
235 auto temp = *this;
236 ++(*this);
237 return temp;
238 }
239
240 const_iterator&
241 operator--()
242 {
243 p_ -= n_;
244 return *this;
245 }
246
247 const_iterator
248 operator--(int)
249 {
250 auto temp = *this;
251 --(*this);
252 return temp;
253 }
254
255 private:
256 friend class mutable_buffers_type;
257
258 const_iterator(std::uint8_t* p, std::size_t n)
259 : n_(n)
260 , p_(p)
261 {
262 }
263 };
264
265 inline
266 auto
267 static_streambuf::mutable_buffers_type::begin() const ->
268 const_iterator
269 {
270 return const_iterator{p_, n_};
271 }
272
273 inline
274 auto
275 static_streambuf::mutable_buffers_type::end() const ->
276 const_iterator
277 {
278 return const_iterator{p_ + n_, n_};
279 }
280
281 //------------------------------------------------------------------------------
282
283
284 inline
285 auto
286 static_streambuf::data() const ->
287 const_buffers_type
288 {
289 return const_buffers_type{in_,
290 static_cast<std::size_t>(out_ - in_)};
291 }
292
293 inline
294 auto
295 static_streambuf::prepare(std::size_t n) ->
296 mutable_buffers_type
297 {
298 if(n > static_cast<std::size_t>(end_ - out_))
299 throw detail::make_exception<std::length_error>(
300 "no space in streambuf", __FILE__, __LINE__);
301 last_ = out_ + n;
302 return mutable_buffers_type{out_, n};
303 }
304
305 } // beast
306
307 #endif