]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/impl/buffers_suffix.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / impl / buffers_suffix.ipp
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_IMPL_BUFFERS_SUFFIX_IPP
11 #define BOOST_BEAST_IMPL_BUFFERS_SUFFIX_IPP
12
13 #include <boost/beast/core/type_traits.hpp>
14 #include <algorithm>
15 #include <cstdint>
16 #include <iterator>
17 #include <type_traits>
18 #include <utility>
19
20 namespace boost {
21 namespace beast {
22
23 template<class Buffers>
24 class buffers_suffix<Buffers>::const_iterator
25 {
26 friend class buffers_suffix<Buffers>;
27
28 using iter_type = typename
29 detail::buffer_sequence_iterator<Buffers>::type;
30
31 iter_type it_;
32 buffers_suffix const* b_ = nullptr;
33
34 public:
35 using value_type = typename std::conditional<
36 std::is_convertible<typename
37 std::iterator_traits<iter_type>::value_type,
38 boost::asio::mutable_buffer>::value,
39 boost::asio::mutable_buffer,
40 boost::asio::const_buffer>::type;
41 using pointer = value_type const*;
42 using reference = value_type;
43 using difference_type = std::ptrdiff_t;
44 using iterator_category =
45 std::bidirectional_iterator_tag;
46
47 const_iterator() = default;
48 const_iterator(const_iterator&& other) = default;
49 const_iterator(const_iterator const& other) = default;
50 const_iterator& operator=(const_iterator&& other) = default;
51 const_iterator& operator=(const_iterator const& other) = default;
52
53 bool
54 operator==(const_iterator const& other) const
55 {
56 return b_ == other.b_ && it_ == other.it_;
57 }
58
59 bool
60 operator!=(const_iterator const& other) const
61 {
62 return !(*this == other);
63 }
64
65 reference
66 operator*() const
67 {
68 return it_ == b_->begin_
69 ? value_type{*it_} + b_->skip_
70 : *it_;
71 }
72
73 pointer
74 operator->() const = delete;
75
76 const_iterator&
77 operator++()
78 {
79 ++it_;
80 return *this;
81 }
82
83 const_iterator
84 operator++(int)
85 {
86 auto temp = *this;
87 ++(*this);
88 return temp;
89 }
90
91 const_iterator&
92 operator--()
93 {
94 --it_;
95 return *this;
96 }
97
98 const_iterator
99 operator--(int)
100 {
101 auto temp = *this;
102 --(*this);
103 return temp;
104 }
105
106 private:
107 const_iterator(buffers_suffix const& b,
108 iter_type it)
109 : it_(it)
110 , b_(&b)
111 {
112 }
113 };
114
115 //------------------------------------------------------------------------------
116
117 template<class Buffers>
118 buffers_suffix<Buffers>::
119 buffers_suffix()
120 : begin_(boost::asio::buffer_sequence_begin(bs_))
121 {
122 }
123
124 template<class Buffers>
125 buffers_suffix<Buffers>::
126 buffers_suffix(buffers_suffix&& other)
127 : buffers_suffix(std::move(other),
128 std::distance<iter_type>(
129 boost::asio::buffer_sequence_begin(
130 other.bs_), other.begin_))
131 {
132 }
133
134 template<class Buffers>
135 buffers_suffix<Buffers>::
136 buffers_suffix(buffers_suffix const& other)
137 : buffers_suffix(other,
138 std::distance<iter_type>(
139 boost::asio::buffer_sequence_begin(
140 other.bs_), other.begin_))
141 {
142 }
143
144 template<class Buffers>
145 buffers_suffix<Buffers>::
146 buffers_suffix(Buffers const& bs)
147 : bs_(bs)
148 , begin_(boost::asio::buffer_sequence_begin(bs_))
149 {
150 static_assert(
151 boost::asio::is_const_buffer_sequence<Buffers>::value||
152 boost::asio::is_mutable_buffer_sequence<Buffers>::value,
153 "BufferSequence requirements not met");
154 }
155
156 template<class Buffers>
157 template<class... Args>
158 buffers_suffix<Buffers>::
159 buffers_suffix(boost::in_place_init_t, Args&&... args)
160 : bs_(std::forward<Args>(args)...)
161 , begin_(boost::asio::buffer_sequence_begin(bs_))
162 {
163 static_assert(sizeof...(Args) > 0,
164 "Missing constructor arguments");
165 static_assert(
166 std::is_constructible<Buffers, Args...>::value,
167 "Buffers not constructible from arguments");
168 }
169
170 template<class Buffers>
171 auto
172 buffers_suffix<Buffers>::
173 operator=(buffers_suffix&& other) ->
174 buffers_suffix&
175 {
176 auto const dist = std::distance<iter_type>(
177 boost::asio::buffer_sequence_begin(other.bs_),
178 other.begin_);
179 bs_ = std::move(other.bs_);
180 begin_ = std::next(
181 boost::asio::buffer_sequence_begin(bs_),
182 dist);
183 skip_ = other.skip_;
184 return *this;
185 }
186
187 template<class Buffers>
188 auto
189 buffers_suffix<Buffers>::
190 operator=(buffers_suffix const& other) ->
191 buffers_suffix&
192 {
193 auto const dist = std::distance<iter_type>(
194 boost::asio::buffer_sequence_begin(other.bs_),
195 other.begin_);
196 bs_ = other.bs_;
197 begin_ = std::next(
198 boost::asio::buffer_sequence_begin(bs_), dist);
199 skip_ = other.skip_;
200 return *this;
201 }
202
203 template<class Buffers>
204 inline
205 auto
206 buffers_suffix<Buffers>::
207 begin() const ->
208 const_iterator
209 {
210 return const_iterator{*this, begin_};
211 }
212
213 template<class Buffers>
214 inline
215 auto
216 buffers_suffix<Buffers>::
217 end() const ->
218 const_iterator
219 {
220 return const_iterator{*this,
221 boost::asio::buffer_sequence_end(bs_)};
222 }
223
224 template<class Buffers>
225 void
226 buffers_suffix<Buffers>::
227 consume(std::size_t amount)
228 {
229 using boost::asio::buffer_size;
230 auto const end =
231 boost::asio::buffer_sequence_end(bs_);
232 for(;amount > 0 && begin_ != end; ++begin_)
233 {
234 auto const len =
235 buffer_size(*begin_) - skip_;
236 if(amount < len)
237 {
238 skip_ += amount;
239 break;
240 }
241 amount -= len;
242 skip_ = 0;
243 }
244 }
245
246 } // beast
247 } // boost
248
249 #endif