buffers_iterator.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //
  2. // buffers_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_BUFFERS_ITERATOR_HPP
  11. #define ASIO_BUFFERS_ITERATOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/assert.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #include <boost/iterator.hpp>
  22. #include <boost/type_traits/is_convertible.hpp>
  23. #include <boost/type_traits/add_const.hpp>
  24. #include "asio/detail/pop_options.hpp"
  25. #include "asio/buffer.hpp"
  26. namespace asio {
  27. namespace detail
  28. {
  29. template <bool IsMutable>
  30. struct buffers_iterator_types_helper;
  31. template <>
  32. struct buffers_iterator_types_helper<false>
  33. {
  34. typedef const_buffer buffer_type;
  35. template <typename ByteType>
  36. struct byte_type
  37. {
  38. typedef typename boost::add_const<ByteType>::type type;
  39. };
  40. };
  41. template <>
  42. struct buffers_iterator_types_helper<true>
  43. {
  44. typedef mutable_buffer buffer_type;
  45. template <typename ByteType>
  46. struct byte_type
  47. {
  48. typedef ByteType type;
  49. };
  50. };
  51. template <typename BufferSequence, typename ByteType>
  52. struct buffers_iterator_types
  53. {
  54. enum
  55. {
  56. is_mutable = boost::is_convertible<
  57. typename BufferSequence::value_type, mutable_buffer>::value
  58. };
  59. typedef buffers_iterator_types_helper<is_mutable> helper;
  60. typedef typename helper::buffer_type buffer_type;
  61. typedef typename helper::template byte_type<ByteType>::type byte_type;
  62. };
  63. }
  64. /// A random access iterator over the bytes in a buffer sequence.
  65. template <typename BufferSequence, typename ByteType = char>
  66. class buffers_iterator
  67. : public boost::iterator<
  68. std::random_access_iterator_tag,
  69. typename detail::buffers_iterator_types<
  70. BufferSequence, ByteType>::byte_type>
  71. {
  72. private:
  73. typedef typename detail::buffers_iterator_types<
  74. BufferSequence, ByteType>::buffer_type buffer_type;
  75. typedef typename detail::buffers_iterator_types<
  76. BufferSequence, ByteType>::byte_type byte_type;
  77. public:
  78. /// Default constructor. Creates an iterator in an undefined state.
  79. buffers_iterator()
  80. : current_buffer_(),
  81. current_buffer_position_(0),
  82. begin_(),
  83. current_(),
  84. end_(),
  85. position_(0)
  86. {
  87. }
  88. /// Construct an iterator representing the beginning of the buffers' data.
  89. static buffers_iterator begin(const BufferSequence& buffers)
  90. #if BOOST_WORKAROUND(__GNUC__, == 4) && BOOST_WORKAROUND(__GNUC_MINOR__, == 3)
  91. __attribute__ ((noinline))
  92. #endif
  93. {
  94. buffers_iterator new_iter;
  95. new_iter.begin_ = buffers.begin();
  96. new_iter.current_ = buffers.begin();
  97. new_iter.end_ = buffers.end();
  98. while (new_iter.current_ != new_iter.end_)
  99. {
  100. new_iter.current_buffer_ = *new_iter.current_;
  101. if (asio::buffer_size(new_iter.current_buffer_) > 0)
  102. break;
  103. ++new_iter.current_;
  104. }
  105. return new_iter;
  106. }
  107. /// Construct an iterator representing the end of the buffers' data.
  108. static buffers_iterator end(const BufferSequence& buffers)
  109. #if BOOST_WORKAROUND(__GNUC__, == 4) && BOOST_WORKAROUND(__GNUC_MINOR__, == 3)
  110. __attribute__ ((noinline))
  111. #endif
  112. {
  113. buffers_iterator new_iter;
  114. new_iter.begin_ = buffers.begin();
  115. new_iter.current_ = buffers.begin();
  116. new_iter.end_ = buffers.end();
  117. while (new_iter.current_ != new_iter.end_)
  118. {
  119. buffer_type buffer = *new_iter.current_;
  120. new_iter.position_ += asio::buffer_size(buffer);
  121. ++new_iter.current_;
  122. }
  123. return new_iter;
  124. }
  125. /// Dereference an iterator.
  126. byte_type& operator*() const
  127. {
  128. return dereference();
  129. }
  130. /// Dereference an iterator.
  131. byte_type* operator->() const
  132. {
  133. return &dereference();
  134. }
  135. /// Access an individual element.
  136. byte_type& operator[](std::ptrdiff_t difference) const
  137. {
  138. buffers_iterator tmp(*this);
  139. tmp.advance(difference);
  140. return *tmp;
  141. }
  142. /// Increment operator (prefix).
  143. buffers_iterator& operator++()
  144. {
  145. increment();
  146. return *this;
  147. }
  148. /// Increment operator (postfix).
  149. buffers_iterator operator++(int)
  150. {
  151. buffers_iterator tmp(*this);
  152. ++*this;
  153. return tmp;
  154. }
  155. /// Decrement operator (prefix).
  156. buffers_iterator& operator--()
  157. {
  158. decrement();
  159. return *this;
  160. }
  161. /// Decrement operator (postfix).
  162. buffers_iterator operator--(int)
  163. {
  164. buffers_iterator tmp(*this);
  165. --*this;
  166. return tmp;
  167. }
  168. /// Addition operator.
  169. buffers_iterator& operator+=(std::ptrdiff_t difference)
  170. {
  171. advance(difference);
  172. return *this;
  173. }
  174. /// Subtraction operator.
  175. buffers_iterator& operator-=(std::ptrdiff_t difference)
  176. {
  177. advance(-difference);
  178. return *this;
  179. }
  180. /// Addition operator.
  181. friend buffers_iterator operator+(const buffers_iterator& iter,
  182. std::ptrdiff_t difference)
  183. {
  184. buffers_iterator tmp(iter);
  185. tmp.advance(difference);
  186. return tmp;
  187. }
  188. /// Subtraction operator.
  189. friend buffers_iterator operator-(const buffers_iterator& iter,
  190. std::ptrdiff_t difference)
  191. {
  192. buffers_iterator tmp(iter);
  193. tmp.advance(-difference);
  194. return tmp;
  195. }
  196. /// Subtraction operator.
  197. friend std::ptrdiff_t operator-(const buffers_iterator& a,
  198. const buffers_iterator& b)
  199. {
  200. return b.distance_to(a);
  201. }
  202. /// Test two iterators for equality.
  203. friend bool operator==(const buffers_iterator& a, const buffers_iterator& b)
  204. {
  205. return a.equal(b);
  206. }
  207. /// Test two iterators for inequality.
  208. friend bool operator!=(const buffers_iterator& a, const buffers_iterator& b)
  209. {
  210. return !a.equal(b);
  211. }
  212. /// Compare two iterators.
  213. friend bool operator<(const buffers_iterator& a, const buffers_iterator& b)
  214. {
  215. return a.distance_to(b) > 0;
  216. }
  217. /// Compare two iterators.
  218. friend bool operator<=(const buffers_iterator& a, const buffers_iterator& b)
  219. {
  220. return !(b < a);
  221. }
  222. /// Compare two iterators.
  223. friend bool operator>(const buffers_iterator& a, const buffers_iterator& b)
  224. {
  225. return b < a;
  226. }
  227. /// Compare two iterators.
  228. friend bool operator>=(const buffers_iterator& a, const buffers_iterator& b)
  229. {
  230. return !(a < b);
  231. }
  232. private:
  233. // Dereference the iterator.
  234. byte_type& dereference() const
  235. {
  236. return buffer_cast<byte_type*>(current_buffer_)[current_buffer_position_];
  237. }
  238. // Compare two iterators for equality.
  239. bool equal(const buffers_iterator& other) const
  240. {
  241. return position_ == other.position_;
  242. }
  243. // Increment the iterator.
  244. void increment()
  245. {
  246. BOOST_ASSERT(current_ != end_ && "iterator out of bounds");
  247. ++position_;
  248. // Check if the increment can be satisfied by the current buffer.
  249. ++current_buffer_position_;
  250. if (current_buffer_position_ != asio::buffer_size(current_buffer_))
  251. return;
  252. // Find the next non-empty buffer.
  253. ++current_;
  254. current_buffer_position_ = 0;
  255. while (current_ != end_)
  256. {
  257. current_buffer_ = *current_;
  258. if (asio::buffer_size(current_buffer_) > 0)
  259. return;
  260. ++current_;
  261. }
  262. }
  263. // Decrement the iterator.
  264. void decrement()
  265. {
  266. BOOST_ASSERT(position_ > 0 && "iterator out of bounds");
  267. --position_;
  268. // Check if the decrement can be satisfied by the current buffer.
  269. if (current_buffer_position_ != 0)
  270. {
  271. --current_buffer_position_;
  272. return;
  273. }
  274. // Find the previous non-empty buffer.
  275. typename BufferSequence::const_iterator iter = current_;
  276. while (iter != begin_)
  277. {
  278. --iter;
  279. buffer_type buffer = *iter;
  280. std::size_t buffer_size = asio::buffer_size(buffer);
  281. if (buffer_size > 0)
  282. {
  283. current_ = iter;
  284. current_buffer_ = buffer;
  285. current_buffer_position_ = buffer_size - 1;
  286. return;
  287. }
  288. }
  289. }
  290. // Advance the iterator by the specified distance.
  291. void advance(std::ptrdiff_t n)
  292. {
  293. if (n > 0)
  294. {
  295. BOOST_ASSERT(current_ != end_ && "iterator out of bounds");
  296. for (;;)
  297. {
  298. std::ptrdiff_t current_buffer_balance
  299. = asio::buffer_size(current_buffer_)
  300. - current_buffer_position_;
  301. // Check if the advance can be satisfied by the current buffer.
  302. if (current_buffer_balance > n)
  303. {
  304. position_ += n;
  305. current_buffer_position_ += n;
  306. return;
  307. }
  308. // Update position.
  309. n -= current_buffer_balance;
  310. position_ += current_buffer_balance;
  311. // Move to next buffer. If it is empty then it will be skipped on the
  312. // next iteration of this loop.
  313. if (++current_ == end_)
  314. {
  315. BOOST_ASSERT(n == 0 && "iterator out of bounds");
  316. current_buffer_ = buffer_type();
  317. current_buffer_position_ = 0;
  318. return;
  319. }
  320. current_buffer_ = *current_;
  321. current_buffer_position_ = 0;
  322. }
  323. }
  324. else if (n < 0)
  325. {
  326. std::size_t abs_n = -n;
  327. BOOST_ASSERT(position_ >= abs_n && "iterator out of bounds");
  328. for (;;)
  329. {
  330. // Check if the advance can be satisfied by the current buffer.
  331. if (current_buffer_position_ >= abs_n)
  332. {
  333. position_ -= abs_n;
  334. current_buffer_position_ -= abs_n;
  335. return;
  336. }
  337. // Update position.
  338. abs_n -= current_buffer_position_;
  339. position_ -= current_buffer_position_;
  340. // Check if we've reached the beginning of the buffers.
  341. if (current_ == begin_)
  342. {
  343. BOOST_ASSERT(abs_n == 0 && "iterator out of bounds");
  344. current_buffer_position_ = 0;
  345. return;
  346. }
  347. // Find the previous non-empty buffer.
  348. typename BufferSequence::const_iterator iter = current_;
  349. while (iter != begin_)
  350. {
  351. --iter;
  352. buffer_type buffer = *iter;
  353. std::size_t buffer_size = asio::buffer_size(buffer);
  354. if (buffer_size > 0)
  355. {
  356. current_ = iter;
  357. current_buffer_ = buffer;
  358. current_buffer_position_ = buffer_size;
  359. break;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. // Determine the distance between two iterators.
  366. std::ptrdiff_t distance_to(const buffers_iterator& other) const
  367. {
  368. return other.position_ - position_;
  369. }
  370. buffer_type current_buffer_;
  371. std::size_t current_buffer_position_;
  372. typename BufferSequence::const_iterator begin_;
  373. typename BufferSequence::const_iterator current_;
  374. typename BufferSequence::const_iterator end_;
  375. std::size_t position_;
  376. };
  377. /// Construct an iterator representing the beginning of the buffers' data.
  378. template <typename BufferSequence>
  379. inline buffers_iterator<BufferSequence> buffers_begin(
  380. const BufferSequence& buffers)
  381. {
  382. return buffers_iterator<BufferSequence>::begin(buffers);
  383. }
  384. /// Construct an iterator representing the end of the buffers' data.
  385. template <typename BufferSequence>
  386. inline buffers_iterator<BufferSequence> buffers_end(
  387. const BufferSequence& buffers)
  388. {
  389. return buffers_iterator<BufferSequence>::end(buffers);
  390. }
  391. } // namespace asio
  392. #include "asio/detail/pop_options.hpp"
  393. #endif // ASIO_BUFFERS_ITERATOR_HPP