completion_condition.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // completion_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_COMPLETION_CONDITION_HPP
  11. #define BOOST_ASIO_COMPLETION_CONDITION_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. // The default maximum number of bytes to transfer in a single operation.
  24. enum default_max_transfer_size_t { default_max_transfer_size = 65536 };
  25. // Adapt result of old-style completion conditions (which had a bool result
  26. // where true indicated that the operation was complete).
  27. inline std::size_t adapt_completion_condition_result(bool result)
  28. {
  29. return result ? 0 : default_max_transfer_size;
  30. }
  31. // Adapt result of current completion conditions (which have a size_t result
  32. // where 0 means the operation is complete, and otherwise the result is the
  33. // maximum number of bytes to transfer on the next underlying operation).
  34. inline std::size_t adapt_completion_condition_result(std::size_t result)
  35. {
  36. return result;
  37. }
  38. class transfer_all_t
  39. {
  40. public:
  41. typedef std::size_t result_type;
  42. template <typename Error>
  43. std::size_t operator()(const Error& err, std::size_t)
  44. {
  45. return !!err ? 0 : default_max_transfer_size;
  46. }
  47. };
  48. class transfer_at_least_t
  49. {
  50. public:
  51. typedef std::size_t result_type;
  52. explicit transfer_at_least_t(std::size_t minimum)
  53. : minimum_(minimum)
  54. {
  55. }
  56. template <typename Error>
  57. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  58. {
  59. return (!!err || bytes_transferred >= minimum_)
  60. ? 0 : default_max_transfer_size;
  61. }
  62. private:
  63. std::size_t minimum_;
  64. };
  65. class transfer_exactly_t
  66. {
  67. public:
  68. typedef std::size_t result_type;
  69. explicit transfer_exactly_t(std::size_t size)
  70. : size_(size)
  71. {
  72. }
  73. template <typename Error>
  74. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  75. {
  76. return (!!err || bytes_transferred >= size_) ? 0 :
  77. (size_ - bytes_transferred < default_max_transfer_size
  78. ? size_ - bytes_transferred : std::size_t(default_max_transfer_size));
  79. }
  80. private:
  81. std::size_t size_;
  82. };
  83. template <typename T, typename = void>
  84. struct is_completion_condition_helper : false_type
  85. {
  86. };
  87. template <typename T>
  88. struct is_completion_condition_helper<T,
  89. enable_if_t<
  90. is_same<
  91. result_of_t<T(boost::system::error_code, std::size_t)>,
  92. bool
  93. >::value
  94. >
  95. > : true_type
  96. {
  97. };
  98. template <typename T>
  99. struct is_completion_condition_helper<T,
  100. enable_if_t<
  101. is_same<
  102. result_of_t<T(boost::system::error_code, std::size_t)>,
  103. std::size_t
  104. >::value
  105. >
  106. > : true_type
  107. {
  108. };
  109. } // namespace detail
  110. #if defined(GENERATING_DOCUMENTATION)
  111. /// Trait for determining whether a function object is a completion condition.
  112. template <typename T>
  113. struct is_completion_condition
  114. {
  115. static constexpr bool value = automatically_determined;
  116. };
  117. #else // defined(GENERATING_DOCUMENTATION)
  118. template <typename T>
  119. struct is_completion_condition : detail::is_completion_condition_helper<T>
  120. {
  121. };
  122. #endif // defined(GENERATING_DOCUMENTATION)
  123. /**
  124. * @defgroup completion_condition Completion Condition Function Objects
  125. *
  126. * Function objects used for determining when a read or write operation should
  127. * complete.
  128. */
  129. /*@{*/
  130. /// Return a completion condition function object that indicates that a read or
  131. /// write operation should continue until all of the data has been transferred,
  132. /// or until an error occurs.
  133. /**
  134. * This function is used to create an object, of unspecified type, that meets
  135. * CompletionCondition requirements.
  136. *
  137. * @par Example
  138. * Reading until a buffer is full:
  139. * @code
  140. * boost::array<char, 128> buf;
  141. * boost::system::error_code ec;
  142. * std::size_t n = boost::asio::read(
  143. * sock, boost::asio::buffer(buf),
  144. * boost::asio::transfer_all(), ec);
  145. * if (ec)
  146. * {
  147. * // An error occurred.
  148. * }
  149. * else
  150. * {
  151. * // n == 128
  152. * }
  153. * @endcode
  154. */
  155. #if defined(GENERATING_DOCUMENTATION)
  156. unspecified transfer_all();
  157. #else
  158. inline detail::transfer_all_t transfer_all()
  159. {
  160. return detail::transfer_all_t();
  161. }
  162. #endif
  163. /// Return a completion condition function object that indicates that a read or
  164. /// write operation should continue until a minimum number of bytes has been
  165. /// transferred, or until an error occurs.
  166. /**
  167. * This function is used to create an object, of unspecified type, that meets
  168. * CompletionCondition requirements.
  169. *
  170. * @par Example
  171. * Reading until a buffer is full or contains at least 64 bytes:
  172. * @code
  173. * boost::array<char, 128> buf;
  174. * boost::system::error_code ec;
  175. * std::size_t n = boost::asio::read(
  176. * sock, boost::asio::buffer(buf),
  177. * boost::asio::transfer_at_least(64), ec);
  178. * if (ec)
  179. * {
  180. * // An error occurred.
  181. * }
  182. * else
  183. * {
  184. * // n >= 64 && n <= 128
  185. * }
  186. * @endcode
  187. */
  188. #if defined(GENERATING_DOCUMENTATION)
  189. unspecified transfer_at_least(std::size_t minimum);
  190. #else
  191. inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
  192. {
  193. return detail::transfer_at_least_t(minimum);
  194. }
  195. #endif
  196. /// Return a completion condition function object that indicates that a read or
  197. /// write operation should continue until an exact number of bytes has been
  198. /// transferred, or until an error occurs.
  199. /**
  200. * This function is used to create an object, of unspecified type, that meets
  201. * CompletionCondition requirements.
  202. *
  203. * @par Example
  204. * Reading until a buffer is full or contains exactly 64 bytes:
  205. * @code
  206. * boost::array<char, 128> buf;
  207. * boost::system::error_code ec;
  208. * std::size_t n = boost::asio::read(
  209. * sock, boost::asio::buffer(buf),
  210. * boost::asio::transfer_exactly(64), ec);
  211. * if (ec)
  212. * {
  213. * // An error occurred.
  214. * }
  215. * else
  216. * {
  217. * // n == 64
  218. * }
  219. * @endcode
  220. */
  221. #if defined(GENERATING_DOCUMENTATION)
  222. unspecified transfer_exactly(std::size_t size);
  223. #else
  224. inline detail::transfer_exactly_t transfer_exactly(std::size_t size)
  225. {
  226. return detail::transfer_exactly_t(size);
  227. }
  228. #endif
  229. /*@}*/
  230. } // namespace asio
  231. } // namespace boost
  232. #include <boost/asio/detail/pop_options.hpp>
  233. #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP