| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | #ifndef BOOST_ASIO_BUFFERS_ITERATOR_HPP |
|---|
| 12 | #define BOOST_ASIO_BUFFERS_ITERATOR_HPP |
|---|
| 13 | |
|---|
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
|---|
| 15 | # pragma once |
|---|
| 16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
|---|
| 17 | |
|---|
| 18 | #include <boost/asio/detail/push_options.hpp> |
|---|
| 19 | |
|---|
| 20 | #include <boost/asio/detail/push_options.hpp> |
|---|
| 21 | #include <cstddef> |
|---|
| 22 | #include <boost/assert.hpp> |
|---|
| 23 | #include <boost/config.hpp> |
|---|
| 24 | #include <boost/detail/workaround.hpp> |
|---|
| 25 | #include <boost/iterator/iterator_facade.hpp> |
|---|
| 26 | #include <boost/type_traits/is_convertible.hpp> |
|---|
| 27 | #include <boost/type_traits/add_const.hpp> |
|---|
| 28 | #include <boost/asio/detail/pop_options.hpp> |
|---|
| 29 | |
|---|
| 30 | #include <boost/asio/buffer.hpp> |
|---|
| 31 | |
|---|
| 32 | namespace boost { |
|---|
| 33 | namespace asio { |
|---|
| 34 | |
|---|
| 35 | namespace detail |
|---|
| 36 | { |
|---|
| 37 | template <bool IsMutable> |
|---|
| 38 | struct buffers_iterator_types_helper; |
|---|
| 39 | |
|---|
| 40 | template <> |
|---|
| 41 | struct buffers_iterator_types_helper<false> |
|---|
| 42 | { |
|---|
| 43 | typedef const_buffer buffer_type; |
|---|
| 44 | template <typename ByteType> |
|---|
| 45 | struct byte_type |
|---|
| 46 | { |
|---|
| 47 | typedef typename boost::add_const<ByteType>::type type; |
|---|
| 48 | }; |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | template <> |
|---|
| 52 | struct buffers_iterator_types_helper<true> |
|---|
| 53 | { |
|---|
| 54 | typedef mutable_buffer buffer_type; |
|---|
| 55 | template <typename ByteType> |
|---|
| 56 | struct byte_type |
|---|
| 57 | { |
|---|
| 58 | typedef ByteType type; |
|---|
| 59 | }; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | template <typename BufferSequence, typename ByteType> |
|---|
| 63 | struct buffers_iterator_types |
|---|
| 64 | { |
|---|
| 65 | enum |
|---|
| 66 | { |
|---|
| 67 | is_mutable = boost::is_convertible< |
|---|
| 68 | typename BufferSequence::value_type, mutable_buffer>::value |
|---|
| 69 | }; |
|---|
| 70 | typedef buffers_iterator_types_helper<is_mutable> helper; |
|---|
| 71 | typedef typename helper::buffer_type buffer_type; |
|---|
| 72 | typedef typename helper::template byte_type<ByteType>::type byte_type; |
|---|
| 73 | }; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | template <typename BufferSequence, typename ByteType = char> |
|---|
| 78 | class buffers_iterator |
|---|
| 79 | : public boost::iterator_facade< |
|---|
| 80 | buffers_iterator<BufferSequence, ByteType>, |
|---|
| 81 | typename detail::buffers_iterator_types< |
|---|
| 82 | BufferSequence, ByteType>::byte_type, |
|---|
| 83 | boost::random_access_traversal_tag> |
|---|
| 84 | { |
|---|
| 85 | private: |
|---|
| 86 | typedef typename detail::buffers_iterator_types< |
|---|
| 87 | BufferSequence, ByteType>::buffer_type buffer_type; |
|---|
| 88 | typedef typename detail::buffers_iterator_types< |
|---|
| 89 | BufferSequence, ByteType>::byte_type byte_type; |
|---|
| 90 | |
|---|
| 91 | public: |
|---|
| 92 | |
|---|
| 93 | buffers_iterator() |
|---|
| 94 | : current_buffer_(), |
|---|
| 95 | current_buffer_position_(0), |
|---|
| 96 | begin_(), |
|---|
| 97 | current_(), |
|---|
| 98 | end_(), |
|---|
| 99 | position_(0) |
|---|
| 100 | { |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | static buffers_iterator begin(const BufferSequence& buffers) |
|---|
| 105 | #if BOOST_WORKAROUND(__GNUC__, == 4) && BOOST_WORKAROUND(__GNUC_MINOR__, == 3) |
|---|
| 106 | __attribute__ ((noinline)) |
|---|
| 107 | #endif |
|---|
| 108 | { |
|---|
| 109 | buffers_iterator new_iter; |
|---|
| 110 | new_iter.begin_ = buffers.begin(); |
|---|
| 111 | new_iter.current_ = buffers.begin(); |
|---|
| 112 | new_iter.end_ = buffers.end(); |
|---|
| 113 | while (new_iter.current_ != new_iter.end_) |
|---|
| 114 | { |
|---|
| 115 | new_iter.current_buffer_ = *new_iter.current_; |
|---|
| 116 | if (boost::asio::buffer_size(new_iter.current_buffer_) > 0) |
|---|
| 117 | break; |
|---|
| 118 | ++new_iter.current_; |
|---|
| 119 | } |
|---|
| 120 | return new_iter; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | static buffers_iterator end(const BufferSequence& buffers) |
|---|
| 125 | #if BOOST_WORKAROUND(__GNUC__, == 4) && BOOST_WORKAROUND(__GNUC_MINOR__, == 3) |
|---|
| 126 | __attribute__ ((noinline)) |
|---|
| 127 | #endif |
|---|
| 128 | { |
|---|
| 129 | buffers_iterator new_iter; |
|---|
| 130 | new_iter.begin_ = buffers.begin(); |
|---|
| 131 | new_iter.current_ = buffers.begin(); |
|---|
| 132 | new_iter.end_ = buffers.end(); |
|---|
| 133 | while (new_iter.current_ != new_iter.end_) |
|---|
| 134 | { |
|---|
| 135 | buffer_type buffer = *new_iter.current_; |
|---|
| 136 | new_iter.position_ += boost::asio::buffer_size(buffer); |
|---|
| 137 | ++new_iter.current_; |
|---|
| 138 | } |
|---|
| 139 | return new_iter; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | private: |
|---|
| 143 | friend class boost::iterator_core_access; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | byte_type& dereference() const |
|---|
| 147 | { |
|---|
| 148 | return buffer_cast<byte_type*>(current_buffer_)[current_buffer_position_]; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | bool equal(const buffers_iterator& other) const |
|---|
| 153 | { |
|---|
| 154 | return position_ == other.position_; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | void increment() |
|---|
| 159 | { |
|---|
| 160 | BOOST_ASSERT(current_ != end_ && "iterator out of bounds"); |
|---|
| 161 | ++position_; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | ++current_buffer_position_; |
|---|
| 165 | if (current_buffer_position_ != boost::asio::buffer_size(current_buffer_)) |
|---|
| 166 | return; |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | ++current_; |
|---|
| 170 | current_buffer_position_ = 0; |
|---|
| 171 | while (current_ != end_) |
|---|
| 172 | { |
|---|
| 173 | current_buffer_ = *current_; |
|---|
| 174 | if (boost::asio::buffer_size(current_buffer_) > 0) |
|---|
| 175 | return; |
|---|
| 176 | ++current_; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | void decrement() |
|---|
| 182 | { |
|---|
| 183 | BOOST_ASSERT(position_ > 0 && "iterator out of bounds"); |
|---|
| 184 | --position_; |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | if (current_buffer_position_ != 0) |
|---|
| 188 | { |
|---|
| 189 | --current_buffer_position_; |
|---|
| 190 | return; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | typename BufferSequence::const_iterator iter = current_; |
|---|
| 195 | while (iter != begin_) |
|---|
| 196 | { |
|---|
| 197 | --iter; |
|---|
| 198 | buffer_type buffer = *iter; |
|---|
| 199 | std::size_t buffer_size = boost::asio::buffer_size(buffer); |
|---|
| 200 | if (buffer_size > 0) |
|---|
| 201 | { |
|---|
| 202 | current_ = iter; |
|---|
| 203 | current_buffer_ = buffer; |
|---|
| 204 | current_buffer_position_ = buffer_size - 1; |
|---|
| 205 | return; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | void advance(std::ptrdiff_t n) |
|---|
| 212 | { |
|---|
| 213 | if (n > 0) |
|---|
| 214 | { |
|---|
| 215 | BOOST_ASSERT(current_ != end_ && "iterator out of bounds"); |
|---|
| 216 | for (;;) |
|---|
| 217 | { |
|---|
| 218 | std::ptrdiff_t current_buffer_balance |
|---|
| 219 | = boost::asio::buffer_size(current_buffer_) |
|---|
| 220 | - current_buffer_position_; |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | if (current_buffer_balance > n) |
|---|
| 224 | { |
|---|
| 225 | position_ += n; |
|---|
| 226 | current_buffer_position_ += n; |
|---|
| 227 | return; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | n -= current_buffer_balance; |
|---|
| 232 | position_ += current_buffer_balance; |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | if (++current_ == end_) |
|---|
| 237 | { |
|---|
| 238 | BOOST_ASSERT(n == 0 && "iterator out of bounds"); |
|---|
| 239 | current_buffer_ = buffer_type(); |
|---|
| 240 | current_buffer_position_ = 0; |
|---|
| 241 | return; |
|---|
| 242 | } |
|---|
| 243 | current_buffer_ = *current_; |
|---|
| 244 | current_buffer_position_ = 0; |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | else if (n < 0) |
|---|
| 248 | { |
|---|
| 249 | std::size_t abs_n = -n; |
|---|
| 250 | BOOST_ASSERT(position_ >= abs_n && "iterator out of bounds"); |
|---|
| 251 | for (;;) |
|---|
| 252 | { |
|---|
| 253 | |
|---|
| 254 | if (current_buffer_position_ >= abs_n) |
|---|
| 255 | { |
|---|
| 256 | position_ -= abs_n; |
|---|
| 257 | current_buffer_position_ -= abs_n; |
|---|
| 258 | return; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | abs_n -= current_buffer_position_; |
|---|
| 263 | position_ -= current_buffer_position_; |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | if (current_ == begin_) |
|---|
| 267 | { |
|---|
| 268 | BOOST_ASSERT(abs_n == 0 && "iterator out of bounds"); |
|---|
| 269 | current_buffer_position_ = 0; |
|---|
| 270 | return; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | typename BufferSequence::const_iterator iter = current_; |
|---|
| 275 | while (iter != begin_) |
|---|
| 276 | { |
|---|
| 277 | --iter; |
|---|
| 278 | buffer_type buffer = *iter; |
|---|
| 279 | std::size_t buffer_size = boost::asio::buffer_size(buffer); |
|---|
| 280 | if (buffer_size > 0) |
|---|
| 281 | { |
|---|
| 282 | current_ = iter; |
|---|
| 283 | current_buffer_ = buffer; |
|---|
| 284 | current_buffer_position_ = buffer_size; |
|---|
| 285 | break; |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | std::ptrdiff_t distance_to(const buffers_iterator& other) const |
|---|
| 294 | { |
|---|
| 295 | return other.position_ - position_; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | buffer_type current_buffer_; |
|---|
| 299 | std::size_t current_buffer_position_; |
|---|
| 300 | typename BufferSequence::const_iterator begin_; |
|---|
| 301 | typename BufferSequence::const_iterator current_; |
|---|
| 302 | typename BufferSequence::const_iterator end_; |
|---|
| 303 | std::size_t position_; |
|---|
| 304 | }; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | template <typename BufferSequence> |
|---|
| 308 | inline buffers_iterator<BufferSequence> buffers_begin( |
|---|
| 309 | const BufferSequence& buffers) |
|---|
| 310 | { |
|---|
| 311 | return buffers_iterator<BufferSequence>::begin(buffers); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | template <typename BufferSequence> |
|---|
| 316 | inline buffers_iterator<BufferSequence> buffers_end( |
|---|
| 317 | const BufferSequence& buffers) |
|---|
| 318 | { |
|---|
| 319 | return buffers_iterator<BufferSequence>::end(buffers); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | #include <boost/asio/detail/pop_options.hpp> |
|---|
| 326 | |
|---|
| 327 | #endif // BOOST_ASIO_BUFFERS_ITERATOR_HPP |
|---|