buffers_adaptor.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 readable_bytes;
  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 readable bytes.
  86. using mutable_data_type = __implementation_defined__;
  87. /// The MutableBufferSequence used to represent the writable bytes.
  88. using mutable_buffers_type = __implementation_defined__;
  89. #else
  90. using const_buffers_type = readable_bytes<false>;
  91. using mutable_data_type = readable_bytes<true>;
  92. class mutable_buffers_type;
  93. #endif
  94. /// Returns the number of readable bytes.
  95. std::size_t
  96. size() const noexcept
  97. {
  98. return in_size_;
  99. }
  100. /// Return the maximum number of bytes, both readable and writable, that can ever be held.
  101. std::size_t
  102. max_size() const noexcept
  103. {
  104. return max_size_;
  105. }
  106. /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
  107. std::size_t
  108. capacity() const noexcept
  109. {
  110. return max_size_;
  111. }
  112. /// Returns a constant buffer sequence representing the readable bytes
  113. const_buffers_type
  114. data() const noexcept;
  115. /// Returns a constant buffer sequence representing the readable bytes
  116. const_buffers_type
  117. cdata() const noexcept
  118. {
  119. return data();
  120. }
  121. /// Returns a mutable buffer sequence representing the readable bytes.
  122. mutable_data_type
  123. data() noexcept;
  124. /** Returns a mutable buffer sequence representing writable bytes.
  125. Returns a mutable buffer sequence representing the writable
  126. bytes containing exactly `n` bytes of storage. This function
  127. does not allocate memory. Instead, the storage comes from
  128. the underlying mutable buffer sequence.
  129. All buffer sequences previously obtained using @ref prepare are
  130. invalidated. Buffer sequences previously obtained using @ref data
  131. remain valid.
  132. @param n The desired number of bytes in the returned buffer
  133. sequence.
  134. @throws std::length_error if `size() + n` exceeds `max_size()`.
  135. @esafe
  136. Strong guarantee.
  137. */
  138. mutable_buffers_type
  139. prepare(std::size_t n);
  140. /** Append writable bytes to the readable bytes.
  141. Appends n bytes from the start of the writable bytes to the
  142. end of the readable bytes. The remainder of the writable bytes
  143. are discarded. If n is greater than the number of writable
  144. bytes, all writable bytes are appended to the readable bytes.
  145. All buffer sequences previously obtained using @ref prepare are
  146. invalidated. Buffer sequences previously obtained using @ref data
  147. remain valid.
  148. @param n The number of bytes to append. If this number
  149. is greater than the number of writable bytes, all
  150. writable bytes are appended.
  151. @esafe
  152. No-throw guarantee.
  153. */
  154. void
  155. commit(std::size_t n) noexcept;
  156. /** Remove bytes from beginning of the readable bytes.
  157. Removes n bytes from the beginning of the readable bytes.
  158. All buffers sequences previously obtained using
  159. @ref data or @ref prepare are invalidated.
  160. @param n The number of bytes to remove. If this number
  161. is greater than the number of readable bytes, all
  162. readable bytes are removed.
  163. @esafe
  164. No-throw guarantee.
  165. */
  166. void
  167. consume(std::size_t n) noexcept;
  168. };
  169. } // beast
  170. } // boost
  171. #include <boost/beast/core/impl/buffers_adaptor.hpp>
  172. #endif