buffers_suffix.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_BUFFERS_SUFFIX_HPP
  10. #define BOOST_BEAST_BUFFERS_SUFFIX_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/optional.hpp>
  14. #include <cstdint>
  15. #include <iterator>
  16. #include <utility>
  17. namespace boost {
  18. namespace beast {
  19. /** Adaptor to progressively trim the front of a <em>BufferSequence</em>.
  20. This adaptor wraps a buffer sequence to create a new sequence
  21. which may be incrementally consumed. Bytes consumed are removed
  22. from the front of the buffer. The underlying memory is not changed,
  23. instead the adaptor efficiently iterates through a subset of
  24. the buffers wrapped.
  25. The wrapped buffer is not modified, a copy is made instead.
  26. Ownership of the underlying memory is not transferred, the application
  27. is still responsible for managing its lifetime.
  28. @tparam BufferSequence The buffer sequence to wrap.
  29. @par Example
  30. This function writes the entire contents of a buffer sequence
  31. to the specified stream.
  32. @code
  33. template<class SyncWriteStream, class ConstBufferSequence>
  34. void send(SyncWriteStream& stream, ConstBufferSequence const& buffers)
  35. {
  36. buffers_suffix<ConstBufferSequence> bs{buffers};
  37. while(buffer_bytes(bs) > 0)
  38. bs.consume(stream.write_some(bs));
  39. }
  40. @endcode
  41. */
  42. template<class BufferSequence>
  43. class buffers_suffix
  44. {
  45. using iter_type =
  46. buffers_iterator_type<BufferSequence>;
  47. BufferSequence bs_;
  48. iter_type begin_{};
  49. std::size_t skip_ = 0;
  50. template<class Deduced>
  51. buffers_suffix(Deduced&& other, std::size_t dist)
  52. : bs_(std::forward<Deduced>(other).bs_)
  53. , begin_(std::next(
  54. net::buffer_sequence_begin(bs_),
  55. dist))
  56. , skip_(other.skip_)
  57. {
  58. }
  59. public:
  60. /** The type for each element in the list of buffers.
  61. If <em>BufferSequence</em> meets the requirements of
  62. <em>MutableBufferSequence</em>, then this type will be
  63. `net::mutable_buffer`, otherwise this type will be
  64. `net::const_buffer`.
  65. */
  66. #if BOOST_BEAST_DOXYGEN
  67. using value_type = __see_below__;
  68. #else
  69. using value_type = buffers_type<BufferSequence>;
  70. #endif
  71. #if BOOST_BEAST_DOXYGEN
  72. /// A bidirectional iterator type that may be used to read elements.
  73. using const_iterator = __implementation_defined__;
  74. #else
  75. class const_iterator;
  76. #endif
  77. /// Constructor
  78. buffers_suffix();
  79. /// Copy Constructor
  80. buffers_suffix(buffers_suffix const&);
  81. /** Constructor
  82. A copy of the buffer sequence is made. Ownership of the
  83. underlying memory is not transferred or copied.
  84. */
  85. explicit
  86. buffers_suffix(BufferSequence const& buffers);
  87. /** Constructor
  88. This constructs the buffer sequence in-place from
  89. a list of arguments.
  90. @param args Arguments forwarded to the buffers constructor.
  91. */
  92. template<class... Args>
  93. explicit
  94. buffers_suffix(boost::in_place_init_t, Args&&... args);
  95. /// Copy Assignment
  96. buffers_suffix& operator=(buffers_suffix const&);
  97. /// Get a bidirectional iterator to the first element.
  98. const_iterator
  99. begin() const;
  100. /// Get a bidirectional iterator to one past the last element.
  101. const_iterator
  102. end() const;
  103. /** Remove bytes from the beginning of the sequence.
  104. @param amount The number of bytes to remove. If this is
  105. larger than the number of bytes remaining, all the
  106. bytes remaining are removed.
  107. */
  108. void
  109. consume(std::size_t amount);
  110. };
  111. } // beast
  112. } // boost
  113. #include <boost/beast/core/impl/buffers_suffix.hpp>
  114. #endif