buffers_iterator.hpp 13 KB

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