completion_condition.hpp 3.9 KB

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