buffers_range_adaptor.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_DETAIL_BUFFERS_RANGE_ADAPTOR_HPP
  10. #define BOOST_BEAST_DETAIL_BUFFERS_RANGE_ADAPTOR_HPP
  11. #include <boost/beast/core/buffer_traits.hpp>
  12. #include <iterator>
  13. #include <type_traits>
  14. namespace boost {
  15. namespace beast {
  16. namespace detail {
  17. template<class BufferSequence>
  18. class buffers_range_adaptor
  19. {
  20. BufferSequence b_;
  21. public:
  22. #if BOOST_BEAST_DOXYGEN
  23. using value_type = __see_below__;
  24. #else
  25. using value_type = buffers_type<BufferSequence>;
  26. #endif
  27. class const_iterator
  28. {
  29. friend class buffers_range_adaptor;
  30. using iter_type =
  31. buffers_iterator_type<BufferSequence>;
  32. iter_type it_{};
  33. const_iterator(iter_type const& it)
  34. : it_(it)
  35. {
  36. }
  37. public:
  38. using value_type = typename
  39. buffers_range_adaptor::value_type;
  40. using pointer = value_type const*;
  41. using reference = value_type;
  42. using difference_type = std::ptrdiff_t;
  43. using iterator_category =
  44. std::bidirectional_iterator_tag;
  45. const_iterator() = default;
  46. bool
  47. operator==(const_iterator const& other) const
  48. {
  49. return it_ == other.it_;
  50. }
  51. bool
  52. operator!=(const_iterator const& other) const
  53. {
  54. return !(*this == other);
  55. }
  56. reference
  57. operator*() const
  58. {
  59. return *it_;
  60. }
  61. pointer
  62. operator->() const = delete;
  63. const_iterator&
  64. operator++()
  65. {
  66. ++it_;
  67. return *this;
  68. }
  69. const_iterator
  70. operator++(int)
  71. {
  72. auto temp = *this;
  73. ++(*this);
  74. return temp;
  75. }
  76. const_iterator&
  77. operator--()
  78. {
  79. --it_;
  80. return *this;
  81. }
  82. const_iterator
  83. operator--(int)
  84. {
  85. auto temp = *this;
  86. --(*this);
  87. return temp;
  88. }
  89. };
  90. explicit
  91. buffers_range_adaptor(BufferSequence const& b)
  92. : b_(b)
  93. {
  94. }
  95. const_iterator
  96. begin() const noexcept
  97. {
  98. return {net::buffer_sequence_begin(b_)};
  99. }
  100. const_iterator
  101. end() const noexcept
  102. {
  103. return {net::buffer_sequence_end(b_)};
  104. }
  105. };
  106. } // detail
  107. } // beast
  108. } // boost
  109. #endif