Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffers_iterator.hpp 14KB

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