consuming_buffers.hpp 6.8 KB

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