consuming_buffers.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // detail/consuming_buffers.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 ASIO_DETAIL_CONSUMING_BUFFERS_HPP
  11. #define ASIO_DETAIL_CONSUMING_BUFFERS_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/limits.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. // A proxy iterator for a sub-range in a list of buffers.
  24. template <typename Buffer, typename Buffer_Iterator>
  25. class consuming_buffers_iterator
  26. {
  27. public:
  28. /// The type used for the distance between two iterators.
  29. typedef std::ptrdiff_t difference_type;
  30. /// The type of the value pointed to by the iterator.
  31. typedef Buffer value_type;
  32. /// The type of the result of applying operator->() to the iterator.
  33. typedef const Buffer* pointer;
  34. /// The type of the result of applying operator*() to the iterator.
  35. typedef const Buffer& reference;
  36. /// The iterator category.
  37. typedef std::forward_iterator_tag iterator_category;
  38. // Default constructor creates an end iterator.
  39. consuming_buffers_iterator()
  40. : at_end_(true)
  41. {
  42. }
  43. // Construct with a buffer for the first entry and an iterator
  44. // range for the remaining entries.
  45. consuming_buffers_iterator(bool at_end, const Buffer& first,
  46. Buffer_Iterator begin_remainder, Buffer_Iterator end_remainder,
  47. std::size_t max_size)
  48. : at_end_(max_size > 0 ? at_end : true),
  49. first_(buffer(first, max_size)),
  50. begin_remainder_(begin_remainder),
  51. end_remainder_(end_remainder),
  52. offset_(0),
  53. max_size_(max_size)
  54. {
  55. }
  56. // Dereference an iterator.
  57. const Buffer& operator*() const
  58. {
  59. return dereference();
  60. }
  61. // Dereference an iterator.
  62. const Buffer* operator->() const
  63. {
  64. return &dereference();
  65. }
  66. // Increment operator (prefix).
  67. consuming_buffers_iterator& operator++()
  68. {
  69. increment();
  70. return *this;
  71. }
  72. // Increment operator (postfix).
  73. consuming_buffers_iterator operator++(int)
  74. {
  75. consuming_buffers_iterator tmp(*this);
  76. ++*this;
  77. return tmp;
  78. }
  79. // Test two iterators for equality.
  80. friend bool operator==(const consuming_buffers_iterator& a,
  81. const consuming_buffers_iterator& b)
  82. {
  83. return a.equal(b);
  84. }
  85. // Test two iterators for inequality.
  86. friend bool operator!=(const consuming_buffers_iterator& a,
  87. const consuming_buffers_iterator& b)
  88. {
  89. return !a.equal(b);
  90. }
  91. private:
  92. void increment()
  93. {
  94. if (!at_end_)
  95. {
  96. if (begin_remainder_ == end_remainder_
  97. || offset_ + buffer_size(first_) >= max_size_)
  98. {
  99. at_end_ = true;
  100. }
  101. else
  102. {
  103. offset_ += buffer_size(first_);
  104. first_ = buffer(*begin_remainder_++, max_size_ - offset_);
  105. }
  106. }
  107. }
  108. bool equal(const consuming_buffers_iterator& other) const
  109. {
  110. if (at_end_ && other.at_end_)
  111. return true;
  112. return !at_end_ && !other.at_end_
  113. && buffer_cast<const void*>(first_)
  114. == buffer_cast<const void*>(other.first_)
  115. && buffer_size(first_) == buffer_size(other.first_)
  116. && begin_remainder_ == other.begin_remainder_
  117. && end_remainder_ == other.end_remainder_;
  118. }
  119. const Buffer& dereference() const
  120. {
  121. return first_;
  122. }
  123. bool at_end_;
  124. Buffer first_;
  125. Buffer_Iterator begin_remainder_;
  126. Buffer_Iterator end_remainder_;
  127. std::size_t offset_;
  128. std::size_t max_size_;
  129. };
  130. // A proxy for a sub-range in a list of buffers.
  131. template <typename Buffer, typename Buffers>
  132. class consuming_buffers
  133. {
  134. public:
  135. // The type for each element in the list of buffers.
  136. typedef Buffer value_type;
  137. // A forward-only iterator type that may be used to read elements.
  138. typedef consuming_buffers_iterator<Buffer, typename Buffers::const_iterator>
  139. const_iterator;
  140. // Construct to represent the entire list of buffers.
  141. consuming_buffers(const Buffers& buffers)
  142. : buffers_(buffers),
  143. at_end_(buffers_.begin() == buffers_.end()),
  144. begin_remainder_(buffers_.begin()),
  145. max_size_((std::numeric_limits<std::size_t>::max)())
  146. {
  147. if (!at_end_)
  148. {
  149. first_ = *buffers_.begin();
  150. ++begin_remainder_;
  151. }
  152. }
  153. // Copy constructor.
  154. consuming_buffers(const consuming_buffers& other)
  155. : buffers_(other.buffers_),
  156. at_end_(other.at_end_),
  157. first_(other.first_),
  158. begin_remainder_(buffers_.begin()),
  159. max_size_(other.max_size_)
  160. {
  161. typename Buffers::const_iterator first = other.buffers_.begin();
  162. typename Buffers::const_iterator second = other.begin_remainder_;
  163. std::advance(begin_remainder_, std::distance(first, second));
  164. }
  165. // Assignment operator.
  166. consuming_buffers& operator=(const consuming_buffers& other)
  167. {
  168. buffers_ = other.buffers_;
  169. at_end_ = other.at_end_;
  170. first_ = other.first_;
  171. begin_remainder_ = buffers_.begin();
  172. typename Buffers::const_iterator first = other.buffers_.begin();
  173. typename Buffers::const_iterator second = other.begin_remainder_;
  174. std::advance(begin_remainder_, std::distance(first, second));
  175. max_size_ = other.max_size_;
  176. return *this;
  177. }
  178. // Get a forward-only iterator to the first element.
  179. const_iterator begin() const
  180. {
  181. return const_iterator(at_end_, first_,
  182. begin_remainder_, buffers_.end(), max_size_);
  183. }
  184. // Get a forward-only iterator for one past the last element.
  185. const_iterator end() const
  186. {
  187. return const_iterator();
  188. }
  189. // Set the maximum size for a single transfer.
  190. void prepare(std::size_t max_size)
  191. {
  192. max_size_ = max_size;
  193. }
  194. // Consume the specified number of bytes from the buffers.
  195. void consume(std::size_t size)
  196. {
  197. // Remove buffers from the start until the specified size is reached.
  198. while (size > 0 && !at_end_)
  199. {
  200. if (buffer_size(first_) <= size)
  201. {
  202. size -= buffer_size(first_);
  203. if (begin_remainder_ == buffers_.end())
  204. at_end_ = true;
  205. else
  206. first_ = *begin_remainder_++;
  207. }
  208. else
  209. {
  210. first_ = first_ + size;
  211. size = 0;
  212. }
  213. }
  214. // Remove any more empty buffers at the start.
  215. while (!at_end_ && buffer_size(first_) == 0)
  216. {
  217. if (begin_remainder_ == buffers_.end())
  218. at_end_ = true;
  219. else
  220. first_ = *begin_remainder_++;
  221. }
  222. }
  223. private:
  224. Buffers buffers_;
  225. bool at_end_;
  226. Buffer first_;
  227. typename Buffers::const_iterator begin_remainder_;
  228. std::size_t max_size_;
  229. };
  230. // Specialisation for null_buffers to ensure that the null_buffers type is
  231. // always passed through to the underlying read or write operation.
  232. template <typename Buffer>
  233. class consuming_buffers<Buffer, asio::null_buffers>
  234. : public asio::null_buffers
  235. {
  236. public:
  237. consuming_buffers(const asio::null_buffers&)
  238. {
  239. // No-op.
  240. }
  241. void prepare(std::size_t)
  242. {
  243. // No-op.
  244. }
  245. void consume(std::size_t)
  246. {
  247. // No-op.
  248. }
  249. };
  250. } // namespace detail
  251. } // namespace asio
  252. #include "asio/detail/pop_options.hpp"
  253. #endif // ASIO_DETAIL_CONSUMING_BUFFERS_HPP