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