buffers_adaptor.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_ADAPTOR_HPP
  10. #define BOOST_BEAST_BUFFERS_ADAPTOR_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/optional.hpp>
  14. #include <type_traits>
  15. namespace boost {
  16. namespace beast {
  17. /** Adapts a <em>MutableBufferSequence</em> into a <em>DynamicBuffer</em>.
  18. This class wraps a <em>MutableBufferSequence</em> to meet the requirements
  19. of <em>DynamicBuffer</em>. Upon construction the input and output sequences
  20. are empty. A copy of the mutable buffer sequence object is stored; however,
  21. ownership of the underlying memory is not transferred. The caller is
  22. responsible for making sure that referenced memory remains valid
  23. for the duration of any operations.
  24. The size of the mutable buffer sequence determines the maximum
  25. number of bytes which may be prepared and committed.
  26. @tparam MutableBufferSequence The type of mutable buffer sequence to adapt.
  27. */
  28. template<class MutableBufferSequence>
  29. class buffers_adaptor
  30. {
  31. static_assert(net::is_mutable_buffer_sequence<
  32. MutableBufferSequence>::value,
  33. "MutableBufferSequence type requirements not met");
  34. using iter_type =
  35. buffers_iterator_type<MutableBufferSequence>;
  36. template<bool>
  37. class subrange;
  38. MutableBufferSequence bs_;
  39. iter_type begin_;
  40. iter_type out_;
  41. iter_type end_;
  42. std::size_t max_size_;
  43. std::size_t in_pos_ = 0; // offset in *begin_
  44. std::size_t in_size_ = 0; // size of input sequence
  45. std::size_t out_pos_ = 0; // offset in *out_
  46. std::size_t out_end_ = 0; // output end offset
  47. iter_type end_impl() const;
  48. buffers_adaptor(
  49. buffers_adaptor const& other,
  50. std::size_t nbegin,
  51. std::size_t nout,
  52. std::size_t nend);
  53. public:
  54. /// The type of the underlying mutable buffer sequence
  55. using value_type = MutableBufferSequence;
  56. /** Construct a buffers adaptor.
  57. @param buffers The mutable buffer sequence to wrap. A copy of
  58. the object will be made, but ownership of the memory is not
  59. transferred.
  60. */
  61. explicit
  62. buffers_adaptor(MutableBufferSequence const& buffers);
  63. /** Constructor
  64. This constructs the buffer adaptor in-place from
  65. a list of arguments.
  66. @param args Arguments forwarded to the buffers constructor.
  67. */
  68. template<class... Args>
  69. explicit
  70. buffers_adaptor(boost::in_place_init_t, Args&&... args);
  71. /// Copy Constructor
  72. buffers_adaptor(buffers_adaptor const& other);
  73. /// Copy Assignment
  74. buffers_adaptor& operator=(buffers_adaptor const&);
  75. /// Returns the original mutable buffer sequence
  76. value_type const&
  77. value() const
  78. {
  79. return bs_;
  80. }
  81. //--------------------------------------------------------------------------
  82. #if BOOST_BEAST_DOXYGEN
  83. /// The ConstBufferSequence used to represent the readable bytes.
  84. using const_buffers_type = __implementation_defined__;
  85. /// The MutableBufferSequence used to represent the writable bytes.
  86. using mutable_buffers_type = __implementation_defined__;
  87. #else
  88. using const_buffers_type = subrange<false>;
  89. using mutable_buffers_type = subrange<true>;
  90. #endif
  91. /// Returns the number of readable bytes.
  92. std::size_t
  93. size() const noexcept
  94. {
  95. return in_size_;
  96. }
  97. /// Return the maximum number of bytes, both readable and writable, that can ever be held.
  98. std::size_t
  99. max_size() const noexcept
  100. {
  101. return max_size_;
  102. }
  103. /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
  104. std::size_t
  105. capacity() const noexcept
  106. {
  107. return max_size_;
  108. }
  109. /// Returns a constant buffer sequence representing the readable bytes
  110. const_buffers_type
  111. data() const noexcept;
  112. /// Returns a constant buffer sequence representing the readable bytes
  113. const_buffers_type
  114. cdata() const noexcept
  115. {
  116. return data();
  117. }
  118. /// Returns a mutable buffer sequence representing the readable bytes.
  119. mutable_buffers_type
  120. data() noexcept;
  121. /** Returns a mutable buffer sequence representing writable bytes.
  122. Returns a mutable buffer sequence representing the writable
  123. bytes containing exactly `n` bytes of storage. This function
  124. does not allocate memory. Instead, the storage comes from
  125. the underlying mutable buffer sequence.
  126. All buffer sequences previously obtained using @ref prepare are
  127. invalidated. Buffer sequences previously obtained using @ref data
  128. remain valid.
  129. @param n The desired number of bytes in the returned buffer
  130. sequence.
  131. @throws std::length_error if `size() + n` exceeds `max_size()`.
  132. @esafe
  133. Strong guarantee.
  134. */
  135. mutable_buffers_type
  136. prepare(std::size_t n);
  137. /** Append writable bytes to the readable bytes.
  138. Appends n bytes from the start of the writable bytes to the
  139. end of the readable bytes. The remainder of the writable bytes
  140. are discarded. If n is greater than the number of writable
  141. bytes, all writable bytes are appended to the readable bytes.
  142. All buffer sequences previously obtained using @ref prepare are
  143. invalidated. Buffer sequences previously obtained using @ref data
  144. remain valid.
  145. @param n The number of bytes to append. If this number
  146. is greater than the number of writable bytes, all
  147. writable bytes are appended.
  148. @esafe
  149. No-throw guarantee.
  150. */
  151. void
  152. commit(std::size_t n) noexcept;
  153. /** Remove bytes from beginning of the readable bytes.
  154. Removes n bytes from the beginning of the readable bytes.
  155. All buffers sequences previously obtained using
  156. @ref data or @ref prepare are invalidated.
  157. @param n The number of bytes to remove. If this number
  158. is greater than the number of readable bytes, all
  159. readable bytes are removed.
  160. @esafe
  161. No-throw guarantee.
  162. */
  163. void
  164. consume(std::size_t n) noexcept;
  165. private:
  166. subrange<true>
  167. make_subrange(std::size_t pos, std::size_t n);
  168. subrange<false>
  169. make_subrange(std::size_t pos, std::size_t n) const;
  170. #ifndef BOOST_BEAST_DOXYGEN
  171. friend struct buffers_adaptor_test_hook;
  172. #endif
  173. };
  174. } // beast
  175. } // boost
  176. #include <boost/beast/core/impl/buffers_adaptor.hpp>
  177. #endif