completion_condition.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // completion_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/asio/detail/pop_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 = 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. } // namespace detail
  66. /**
  67. * @defgroup completion_condition Completion Condition Function Objects
  68. *
  69. * Function objects used for determining when a read or write operation should
  70. * complete.
  71. */
  72. /*@{*/
  73. /// Return a completion condition function object that indicates that a read or
  74. /// write operation should continue until all of the data has been transferred,
  75. /// or until an error occurs.
  76. /**
  77. * This function is used to create an object, of unspecified type, that meets
  78. * CompletionCondition requirements.
  79. *
  80. * @par Example
  81. * Reading until a buffer is full:
  82. * @code
  83. * boost::array<char, 128> buf;
  84. * boost::system::error_code ec;
  85. * std::size_t n = boost::asio::read(
  86. * sock, boost::asio::buffer(buf),
  87. * boost::asio::transfer_all(), ec);
  88. * if (ec)
  89. * {
  90. * // An error occurred.
  91. * }
  92. * else
  93. * {
  94. * // n == 128
  95. * }
  96. * @endcode
  97. */
  98. #if defined(GENERATING_DOCUMENTATION)
  99. unspecified transfer_all();
  100. #else
  101. inline detail::transfer_all_t transfer_all()
  102. {
  103. return detail::transfer_all_t();
  104. }
  105. #endif
  106. /// Return a completion condition function object that indicates that a read or
  107. /// write operation should continue until a minimum number of bytes has been
  108. /// transferred, or until an error occurs.
  109. /**
  110. * This function is used to create an object, of unspecified type, that meets
  111. * CompletionCondition requirements.
  112. *
  113. * @par Example
  114. * Reading until a buffer is full or contains at least 64 bytes:
  115. * @code
  116. * boost::array<char, 128> buf;
  117. * boost::system::error_code ec;
  118. * std::size_t n = boost::asio::read(
  119. * sock, boost::asio::buffer(buf),
  120. * boost::asio::transfer_at_least(64), ec);
  121. * if (ec)
  122. * {
  123. * // An error occurred.
  124. * }
  125. * else
  126. * {
  127. * // n >= 64 && n <= 128
  128. * }
  129. * @endcode
  130. */
  131. #if defined(GENERATING_DOCUMENTATION)
  132. unspecified transfer_at_least(std::size_t minimum);
  133. #else
  134. inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
  135. {
  136. return detail::transfer_at_least_t(minimum);
  137. }
  138. #endif
  139. /*@}*/
  140. } // namespace asio
  141. } // namespace boost
  142. #include <boost/asio/detail/pop_options.hpp>
  143. #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP