completion_condition.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // completion_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 ASIO_COMPLETION_CONDITION_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. namespace detail {
  20. // The default maximum number of bytes to transfer in a single operation.
  21. enum { default_max_transfer_size = 65536 };
  22. // Adapt result of old-style completion conditions (which had a bool result
  23. // where true indicated that the operation was complete).
  24. inline std::size_t adapt_completion_condition_result(bool result)
  25. {
  26. return result ? 0 : default_max_transfer_size;
  27. }
  28. // Adapt result of current completion conditions (which have a size_t result
  29. // where 0 means the operation is complete, and otherwise the result is the
  30. // maximum number of bytes to transfer on the next underlying operation).
  31. inline std::size_t adapt_completion_condition_result(std::size_t result)
  32. {
  33. return result;
  34. }
  35. class transfer_all_t
  36. {
  37. public:
  38. typedef std::size_t result_type;
  39. template <typename Error>
  40. std::size_t operator()(const Error& err, std::size_t)
  41. {
  42. return !!err ? 0 : default_max_transfer_size;
  43. }
  44. };
  45. class transfer_at_least_t
  46. {
  47. public:
  48. typedef std::size_t result_type;
  49. explicit transfer_at_least_t(std::size_t minimum)
  50. : minimum_(minimum)
  51. {
  52. }
  53. template <typename Error>
  54. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  55. {
  56. return (!!err || bytes_transferred >= minimum_)
  57. ? 0 : default_max_transfer_size;
  58. }
  59. private:
  60. std::size_t minimum_;
  61. };
  62. } // namespace detail
  63. /**
  64. * @defgroup completion_condition Completion Condition Function Objects
  65. *
  66. * Function objects used for determining when a read or write operation should
  67. * complete.
  68. */
  69. /*@{*/
  70. /// Return a completion condition function object that indicates that a read or
  71. /// write operation should continue until all of the data has been transferred,
  72. /// or until an error occurs.
  73. /**
  74. * This function is used to create an object, of unspecified type, that meets
  75. * CompletionCondition requirements.
  76. *
  77. * @par Example
  78. * Reading until a buffer is full:
  79. * @code
  80. * boost::array<char, 128> buf;
  81. * asio::error_code ec;
  82. * std::size_t n = asio::read(
  83. * sock, asio::buffer(buf),
  84. * asio::transfer_all(), ec);
  85. * if (ec)
  86. * {
  87. * // An error occurred.
  88. * }
  89. * else
  90. * {
  91. * // n == 128
  92. * }
  93. * @endcode
  94. */
  95. #if defined(GENERATING_DOCUMENTATION)
  96. unspecified transfer_all();
  97. #else
  98. inline detail::transfer_all_t transfer_all()
  99. {
  100. return detail::transfer_all_t();
  101. }
  102. #endif
  103. /// Return a completion condition function object that indicates that a read or
  104. /// write operation should continue until a minimum number of bytes has been
  105. /// transferred, or until an error occurs.
  106. /**
  107. * This function is used to create an object, of unspecified type, that meets
  108. * CompletionCondition requirements.
  109. *
  110. * @par Example
  111. * Reading until a buffer is full or contains at least 64 bytes:
  112. * @code
  113. * boost::array<char, 128> buf;
  114. * asio::error_code ec;
  115. * std::size_t n = asio::read(
  116. * sock, asio::buffer(buf),
  117. * asio::transfer_at_least(64), ec);
  118. * if (ec)
  119. * {
  120. * // An error occurred.
  121. * }
  122. * else
  123. * {
  124. * // n >= 64 && n <= 128
  125. * }
  126. * @endcode
  127. */
  128. #if defined(GENERATING_DOCUMENTATION)
  129. unspecified transfer_at_least(std::size_t minimum);
  130. #else
  131. inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
  132. {
  133. return detail::transfer_at_least_t(minimum);
  134. }
  135. #endif
  136. /*@}*/
  137. } // namespace asio
  138. #include "asio/detail/pop_options.hpp"
  139. #endif // ASIO_COMPLETION_CONDITION_HPP