Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

completion_condition.hpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // completion_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_t { 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. class transfer_exactly_t
  63. {
  64. public:
  65. typedef std::size_t result_type;
  66. explicit transfer_exactly_t(std::size_t size)
  67. : size_(size)
  68. {
  69. }
  70. template <typename Error>
  71. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  72. {
  73. return (!!err || bytes_transferred >= size_) ? 0 :
  74. (size_ - bytes_transferred < default_max_transfer_size
  75. ? size_ - bytes_transferred : std::size_t(default_max_transfer_size));
  76. }
  77. private:
  78. std::size_t size_;
  79. };
  80. } // namespace detail
  81. /**
  82. * @defgroup completion_condition Completion Condition Function Objects
  83. *
  84. * Function objects used for determining when a read or write operation should
  85. * complete.
  86. */
  87. /*@{*/
  88. /// Return a completion condition function object that indicates that a read or
  89. /// write operation should continue until all of the data has been transferred,
  90. /// or until an error occurs.
  91. /**
  92. * This function is used to create an object, of unspecified type, that meets
  93. * CompletionCondition requirements.
  94. *
  95. * @par Example
  96. * Reading until a buffer is full:
  97. * @code
  98. * boost::array<char, 128> buf;
  99. * asio::error_code ec;
  100. * std::size_t n = asio::read(
  101. * sock, asio::buffer(buf),
  102. * asio::transfer_all(), ec);
  103. * if (ec)
  104. * {
  105. * // An error occurred.
  106. * }
  107. * else
  108. * {
  109. * // n == 128
  110. * }
  111. * @endcode
  112. */
  113. #if defined(GENERATING_DOCUMENTATION)
  114. unspecified transfer_all();
  115. #else
  116. inline detail::transfer_all_t transfer_all()
  117. {
  118. return detail::transfer_all_t();
  119. }
  120. #endif
  121. /// Return a completion condition function object that indicates that a read or
  122. /// write operation should continue until a minimum number of bytes has been
  123. /// transferred, or until an error occurs.
  124. /**
  125. * This function is used to create an object, of unspecified type, that meets
  126. * CompletionCondition requirements.
  127. *
  128. * @par Example
  129. * Reading until a buffer is full or contains at least 64 bytes:
  130. * @code
  131. * boost::array<char, 128> buf;
  132. * asio::error_code ec;
  133. * std::size_t n = asio::read(
  134. * sock, asio::buffer(buf),
  135. * asio::transfer_at_least(64), ec);
  136. * if (ec)
  137. * {
  138. * // An error occurred.
  139. * }
  140. * else
  141. * {
  142. * // n >= 64 && n <= 128
  143. * }
  144. * @endcode
  145. */
  146. #if defined(GENERATING_DOCUMENTATION)
  147. unspecified transfer_at_least(std::size_t minimum);
  148. #else
  149. inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
  150. {
  151. return detail::transfer_at_least_t(minimum);
  152. }
  153. #endif
  154. /// Return a completion condition function object that indicates that a read or
  155. /// write operation should continue until an exact number of bytes has been
  156. /// transferred, or until an error occurs.
  157. /**
  158. * This function is used to create an object, of unspecified type, that meets
  159. * CompletionCondition requirements.
  160. *
  161. * @par Example
  162. * Reading until a buffer is full or contains exactly 64 bytes:
  163. * @code
  164. * boost::array<char, 128> buf;
  165. * asio::error_code ec;
  166. * std::size_t n = asio::read(
  167. * sock, asio::buffer(buf),
  168. * asio::transfer_exactly(64), ec);
  169. * if (ec)
  170. * {
  171. * // An error occurred.
  172. * }
  173. * else
  174. * {
  175. * // n == 64
  176. * }
  177. * @endcode
  178. */
  179. #if defined(GENERATING_DOCUMENTATION)
  180. unspecified transfer_exactly(std::size_t size);
  181. #else
  182. inline detail::transfer_exactly_t transfer_exactly(std::size_t size)
  183. {
  184. return detail::transfer_exactly_t(size);
  185. }
  186. #endif
  187. /*@}*/
  188. } // namespace asio
  189. #include "asio/detail/pop_options.hpp"
  190. #endif // ASIO_COMPLETION_CONDITION_HPP