buffers_iterator.hpp 13 KB

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