buffers_generator.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Copyright (c) 2022 Seth Heeren (sgheeren 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_CORE_BUFFERS_GENERATOR_HPP
  10. #define BOOST_BEAST_CORE_BUFFERS_GENERATOR_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/type_traits.hpp>
  13. #include <boost/beast/core/error.hpp>
  14. #include <boost/beast/core/stream_traits.hpp>
  15. #include <boost/asio/async_result.hpp>
  16. #include <type_traits>
  17. namespace boost {
  18. namespace beast {
  19. /** Determine if type satisfies the <em>BuffersGenerator</em> requirements.
  20. This metafunction is used to determine if the specified type meets the
  21. requirements for a buffers generator.
  22. The static member `value` will evaluate to `true` if so, `false` otherwise.
  23. @tparam T a type to check
  24. */
  25. #ifdef BOOST_BEAST_DOXYGEN
  26. template <class T>
  27. struct is_buffers_generator
  28. : integral_constant<bool, automatically_determined>
  29. {
  30. };
  31. #else
  32. template<class T, class = void>
  33. struct is_buffers_generator
  34. : std::false_type
  35. {
  36. };
  37. template<class T>
  38. struct is_buffers_generator<
  39. T, detail::void_t<decltype(
  40. bool(std::declval<T const&>().is_done()),
  41. typename T::const_buffers_type(
  42. std::declval<T&>().prepare(
  43. std::declval<error_code&>())),
  44. std::declval<T&>().consume(
  45. std::size_t{})
  46. )>> : std::true_type
  47. {
  48. };
  49. #endif
  50. /** Write all output from a BuffersGenerator to a stream.
  51. This function is used to write all of the buffers generated
  52. by a caller-provided BuffersGenerator to a stream. The call
  53. will block until one of the following conditions is true:
  54. @li A call to the generator's `is_done` returns `false`.
  55. @li An error occurs.
  56. This operation is implemented in terms of one or more calls
  57. to the stream's `write_some` function.
  58. @param stream The stream to which the data is to be written.
  59. The type must support the <em>SyncWriteStream</em> concept.
  60. @param generator The generator to use.
  61. @param ec Set to the error, if any occurred.
  62. @return The number of bytes written to the stream.
  63. @see BuffersGenerator
  64. */
  65. template<
  66. class SyncWriteStream,
  67. class BuffersGenerator
  68. #if ! BOOST_BEAST_DOXYGEN
  69. , typename std::enable_if<is_buffers_generator<
  70. typename std::decay<BuffersGenerator>::
  71. type>::value>::type* = nullptr
  72. #endif
  73. >
  74. std::size_t
  75. write(
  76. SyncWriteStream& stream,
  77. BuffersGenerator&& generator,
  78. beast::error_code& ec);
  79. /** Write all output from a BuffersGenerator to a stream.
  80. This function is used to write all of the buffers generated
  81. by a caller-provided BuffersGenerator to a stream. The call
  82. will block until one of the following conditions is true:
  83. @li A call to the generator's `is_done` returns `false`.
  84. @li An error occurs.
  85. This operation is implemented in terms of one or more calls
  86. to the stream's `write_some` function.
  87. @param stream The stream to which the data is to be written.
  88. The type must support the <em>SyncWriteStream</em> concept.
  89. @param generator The generator to use.
  90. @return The number of bytes written to the stream.
  91. @throws system_error Thrown on failure.
  92. @see BuffersGenerator
  93. */
  94. template<
  95. class SyncWriteStream,
  96. class BuffersGenerator
  97. #if ! BOOST_BEAST_DOXYGEN
  98. , typename std::enable_if<is_buffers_generator<
  99. typename std::decay<BuffersGenerator>::
  100. type>::value>::type* = nullptr
  101. #endif
  102. >
  103. std::size_t
  104. write(
  105. SyncWriteStream& stream,
  106. BuffersGenerator&& generator);
  107. /** Write all output from a BuffersGenerator asynchronously to a
  108. stream.
  109. This function is used to write all of the buffers generated
  110. by a caller-provided `BuffersGenerator` to a stream. The
  111. function call always returns immediately. The asynchronous
  112. operation will continue until one of the following
  113. conditions is true:
  114. @li A call to the generator's `is_done` returns `false`.
  115. @li An error occurs.
  116. This operation is implemented in terms of zero or more calls
  117. to the stream's `async_write_some` function, and is known as
  118. a <em>composed operation</em>. The program must ensure that
  119. the stream performs no other writes until this operation
  120. completes.
  121. @param stream The stream to which the data is to be written.
  122. The type must support the <em>SyncWriteStream</em> concept.
  123. @param generator The generator to use.
  124. @param token The completion handler to invoke when the
  125. operation completes. The implementation takes ownership of
  126. the handler by performing a decay-copy. The equivalent
  127. function signature of the handler must be:
  128. @code
  129. void handler(
  130. error_code const& error, // result of operation
  131. std::size_t bytes_transferred // the number of bytes written to the stream
  132. );
  133. @endcode
  134. If the handler has an associated immediate executor,
  135. an immediate completion will be dispatched to it.
  136. Otherwise, the handler will not be invoked from within
  137. this function. Invocation of the handler will be performed in a
  138. manner equivalent to using `net::post`.
  139. @see BuffersGenerator
  140. */
  141. template<
  142. class AsyncWriteStream,
  143. class BuffersGenerator,
  144. BOOST_BEAST_ASYNC_TPARAM2 CompletionToken
  145. = net::default_completion_token_t<executor_type<AsyncWriteStream>>
  146. #if !BOOST_BEAST_DOXYGEN
  147. , typename std::enable_if<is_buffers_generator<
  148. BuffersGenerator>::value>::type* = nullptr
  149. #endif
  150. >
  151. BOOST_BEAST_ASYNC_RESULT2(CompletionToken)
  152. async_write(
  153. AsyncWriteStream& stream,
  154. BuffersGenerator generator,
  155. CompletionToken&& token
  156. = net::default_completion_token_t<executor_type<AsyncWriteStream>>{});
  157. } // beast
  158. } // boost
  159. #include <boost/beast/core/impl/buffers_generator.hpp>
  160. #endif