buffers_iterator.hpp 11 KB

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