buffers_prefix.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_IMPL_BUFFERS_PREFIX_HPP
  10. #define BOOST_BEAST_IMPL_BUFFERS_PREFIX_HPP
  11. #include <boost/beast/core/buffer_traits.hpp>
  12. #include <boost/config/workaround.hpp>
  13. #include <algorithm>
  14. #include <cstdint>
  15. #include <iterator>
  16. #include <stdexcept>
  17. #include <type_traits>
  18. #include <utility>
  19. namespace boost {
  20. namespace beast {
  21. template<class Buffers>
  22. class buffers_prefix_view<Buffers>::const_iterator
  23. {
  24. friend class buffers_prefix_view<Buffers>;
  25. buffers_prefix_view const* b_ = nullptr;
  26. std::size_t remain_ = 0;
  27. iter_type it_{};
  28. public:
  29. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  30. using value_type = typename std::conditional<
  31. boost::is_convertible<typename
  32. std::iterator_traits<iter_type>::value_type,
  33. net::mutable_buffer>::value,
  34. net::mutable_buffer,
  35. net::const_buffer>::type;
  36. #else
  37. using value_type = buffers_type<Buffers>;
  38. #endif
  39. BOOST_STATIC_ASSERT(std::is_same<
  40. typename const_iterator::value_type,
  41. typename buffers_prefix_view::value_type>::value);
  42. using pointer = value_type const*;
  43. using reference = value_type;
  44. using difference_type = std::ptrdiff_t;
  45. using iterator_category =
  46. std::bidirectional_iterator_tag;
  47. const_iterator() = default;
  48. const_iterator(
  49. const_iterator const& other) = default;
  50. const_iterator& operator=(
  51. const_iterator const& other) = default;
  52. bool
  53. operator==(const_iterator const& other) const
  54. {
  55. return b_ == other.b_ && it_ == other.it_;
  56. }
  57. bool
  58. operator!=(const_iterator const& other) const
  59. {
  60. return !(*this == other);
  61. }
  62. reference
  63. operator*() const
  64. {
  65. value_type v(*it_);
  66. if(remain_ < v.size())
  67. return {v.data(), remain_};
  68. return v;
  69. }
  70. pointer
  71. operator->() const = delete;
  72. const_iterator&
  73. operator++()
  74. {
  75. value_type const v = *it_++;
  76. remain_ -= v.size();
  77. return *this;
  78. }
  79. const_iterator
  80. operator++(int)
  81. {
  82. auto temp = *this;
  83. value_type const v = *it_++;
  84. remain_ -= v.size();
  85. return temp;
  86. }
  87. const_iterator&
  88. operator--()
  89. {
  90. value_type const v = *--it_;
  91. remain_ += v.size();
  92. return *this;
  93. }
  94. const_iterator
  95. operator--(int)
  96. {
  97. auto temp = *this;
  98. value_type const v = *--it_;
  99. remain_ += v.size();
  100. return temp;
  101. }
  102. private:
  103. const_iterator(
  104. buffers_prefix_view const& b,
  105. std::true_type)
  106. : b_(&b)
  107. , remain_(b.remain_)
  108. , it_(b_->end_)
  109. {
  110. }
  111. const_iterator(
  112. buffers_prefix_view const& b,
  113. std::false_type)
  114. : b_(&b)
  115. , remain_(b_->size_)
  116. , it_(net::buffer_sequence_begin(b_->bs_))
  117. {
  118. }
  119. };
  120. //------------------------------------------------------------------------------
  121. template<class Buffers>
  122. void
  123. buffers_prefix_view<Buffers>::
  124. setup(std::size_t size)
  125. {
  126. size_ = 0;
  127. remain_ = 0;
  128. end_ = net::buffer_sequence_begin(bs_);
  129. auto const last = bs_.end();
  130. while(end_ != last)
  131. {
  132. auto const len = buffer_bytes(*end_++);
  133. if(len >= size)
  134. {
  135. size_ += size;
  136. // by design, this subtraction can wrap
  137. BOOST_STATIC_ASSERT(std::is_unsigned<
  138. decltype(remain_)>::value);
  139. remain_ = size - len;
  140. break;
  141. }
  142. size -= len;
  143. size_ += len;
  144. }
  145. }
  146. template<class Buffers>
  147. buffers_prefix_view<Buffers>::
  148. buffers_prefix_view(
  149. buffers_prefix_view const& other,
  150. std::size_t dist)
  151. : bs_(other.bs_)
  152. , size_(other.size_)
  153. , remain_(other.remain_)
  154. , end_(std::next(bs_.begin(), dist))
  155. {
  156. }
  157. template<class Buffers>
  158. buffers_prefix_view<Buffers>::
  159. buffers_prefix_view(buffers_prefix_view const& other)
  160. : buffers_prefix_view(other,
  161. std::distance<iter_type>(
  162. net::buffer_sequence_begin(other.bs_),
  163. other.end_))
  164. {
  165. }
  166. template<class Buffers>
  167. auto
  168. buffers_prefix_view<Buffers>::
  169. operator=(buffers_prefix_view const& other) ->
  170. buffers_prefix_view&
  171. {
  172. auto const dist = std::distance<iter_type>(
  173. net::buffer_sequence_begin(other.bs_),
  174. other.end_);
  175. bs_ = other.bs_;
  176. size_ = other.size_;
  177. remain_ = other.remain_;
  178. end_ = std::next(
  179. net::buffer_sequence_begin(bs_),
  180. dist);
  181. return *this;
  182. }
  183. template<class Buffers>
  184. buffers_prefix_view<Buffers>::
  185. buffers_prefix_view(
  186. std::size_t size,
  187. Buffers const& bs)
  188. : bs_(bs)
  189. {
  190. setup(size);
  191. }
  192. template<class Buffers>
  193. template<class... Args>
  194. buffers_prefix_view<Buffers>::
  195. buffers_prefix_view(
  196. std::size_t size,
  197. boost::in_place_init_t,
  198. Args&&... args)
  199. : bs_(std::forward<Args>(args)...)
  200. {
  201. setup(size);
  202. }
  203. template<class Buffers>
  204. auto
  205. buffers_prefix_view<Buffers>::
  206. begin() const ->
  207. const_iterator
  208. {
  209. return const_iterator{
  210. *this, std::false_type{}};
  211. }
  212. template<class Buffers>
  213. auto
  214. buffers_prefix_view<Buffers>::
  215. end() const ->
  216. const_iterator
  217. {
  218. return const_iterator{
  219. *this, std::true_type{}};
  220. }
  221. //------------------------------------------------------------------------------
  222. template<>
  223. class buffers_prefix_view<net::const_buffer>
  224. : public net::const_buffer
  225. {
  226. public:
  227. using net::const_buffer::const_buffer;
  228. buffers_prefix_view(buffers_prefix_view const&) = default;
  229. buffers_prefix_view& operator=(buffers_prefix_view const&) = default;
  230. buffers_prefix_view(
  231. std::size_t size,
  232. net::const_buffer buffer)
  233. : net::const_buffer(
  234. buffer.data(),
  235. std::min<std::size_t>(size, buffer.size())
  236. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  237. , buffer.get_debug_check()
  238. #endif
  239. )
  240. {
  241. }
  242. template<class... Args>
  243. buffers_prefix_view(
  244. std::size_t size,
  245. boost::in_place_init_t,
  246. Args&&... args)
  247. : buffers_prefix_view(size,
  248. net::const_buffer(
  249. std::forward<Args>(args)...))
  250. {
  251. }
  252. };
  253. //------------------------------------------------------------------------------
  254. template<>
  255. class buffers_prefix_view<net::mutable_buffer>
  256. : public net::mutable_buffer
  257. {
  258. public:
  259. using net::mutable_buffer::mutable_buffer;
  260. buffers_prefix_view(buffers_prefix_view const&) = default;
  261. buffers_prefix_view& operator=(buffers_prefix_view const&) = default;
  262. buffers_prefix_view(
  263. std::size_t size,
  264. net::mutable_buffer buffer)
  265. : net::mutable_buffer(
  266. buffer.data(),
  267. std::min<std::size_t>(size, buffer.size())
  268. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  269. , buffer.get_debug_check()
  270. #endif
  271. )
  272. {
  273. }
  274. template<class... Args>
  275. buffers_prefix_view(
  276. std::size_t size,
  277. boost::in_place_init_t,
  278. Args&&... args)
  279. : buffers_prefix_view(size,
  280. net::mutable_buffer(
  281. std::forward<Args>(args)...))
  282. {
  283. }
  284. };
  285. } // beast
  286. } // boost
  287. #endif