2 // completion_condition.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_COMPLETION_CONDITION_HPP
12 #define BOOST_ASIO_COMPLETION_CONDITION_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/push_options.hpp>
28 // The default maximum number of bytes to transfer in a single operation.
29 enum default_max_transfer_size_t { default_max_transfer_size = 65536 };
31 // Adapt result of old-style completion conditions (which had a bool result
32 // where true indicated that the operation was complete).
33 inline std::size_t adapt_completion_condition_result(bool result)
35 return result ? 0 : default_max_transfer_size;
38 // Adapt result of current completion conditions (which have a size_t result
39 // where 0 means the operation is complete, and otherwise the result is the
40 // maximum number of bytes to transfer on the next underlying operation).
41 inline std::size_t adapt_completion_condition_result(std::size_t result)
49 typedef std::size_t result_type;
51 template <typename Error>
52 std::size_t operator()(const Error& err, std::size_t)
54 return !!err ? 0 : default_max_transfer_size;
58 class transfer_at_least_t
61 typedef std::size_t result_type;
63 explicit transfer_at_least_t(std::size_t minimum)
68 template <typename Error>
69 std::size_t operator()(const Error& err, std::size_t bytes_transferred)
71 return (!!err || bytes_transferred >= minimum_)
72 ? 0 : default_max_transfer_size;
79 class transfer_exactly_t
82 typedef std::size_t result_type;
84 explicit transfer_exactly_t(std::size_t size)
89 template <typename Error>
90 std::size_t operator()(const Error& err, std::size_t bytes_transferred)
92 return (!!err || bytes_transferred >= size_) ? 0 :
93 (size_ - bytes_transferred < default_max_transfer_size
94 ? size_ - bytes_transferred : std::size_t(default_max_transfer_size));
101 } // namespace detail
104 * @defgroup completion_condition Completion Condition Function Objects
106 * Function objects used for determining when a read or write operation should
111 /// Return a completion condition function object that indicates that a read or
112 /// write operation should continue until all of the data has been transferred,
113 /// or until an error occurs.
115 * This function is used to create an object, of unspecified type, that meets
116 * CompletionCondition requirements.
119 * Reading until a buffer is full:
121 * boost::array<char, 128> buf;
122 * boost::system::error_code ec;
123 * std::size_t n = boost::asio::read(
124 * sock, boost::asio::buffer(buf),
125 * boost::asio::transfer_all(), ec);
128 * // An error occurred.
136 #if defined(GENERATING_DOCUMENTATION)
137 unspecified transfer_all();
139 inline detail::transfer_all_t transfer_all()
141 return detail::transfer_all_t();
145 /// Return a completion condition function object that indicates that a read or
146 /// write operation should continue until a minimum number of bytes has been
147 /// transferred, or until an error occurs.
149 * This function is used to create an object, of unspecified type, that meets
150 * CompletionCondition requirements.
153 * Reading until a buffer is full or contains at least 64 bytes:
155 * boost::array<char, 128> buf;
156 * boost::system::error_code ec;
157 * std::size_t n = boost::asio::read(
158 * sock, boost::asio::buffer(buf),
159 * boost::asio::transfer_at_least(64), ec);
162 * // An error occurred.
166 * // n >= 64 && n <= 128
170 #if defined(GENERATING_DOCUMENTATION)
171 unspecified transfer_at_least(std::size_t minimum);
173 inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
175 return detail::transfer_at_least_t(minimum);
179 /// Return a completion condition function object that indicates that a read or
180 /// write operation should continue until an exact number of bytes has been
181 /// transferred, or until an error occurs.
183 * This function is used to create an object, of unspecified type, that meets
184 * CompletionCondition requirements.
187 * Reading until a buffer is full or contains exactly 64 bytes:
189 * boost::array<char, 128> buf;
190 * boost::system::error_code ec;
191 * std::size_t n = boost::asio::read(
192 * sock, boost::asio::buffer(buf),
193 * boost::asio::transfer_exactly(64), ec);
196 * // An error occurred.
204 #if defined(GENERATING_DOCUMENTATION)
205 unspecified transfer_exactly(std::size_t size);
207 inline detail::transfer_exactly_t transfer_exactly(std::size_t size)
209 return detail::transfer_exactly_t(size);
218 #include <boost/asio/detail/pop_options.hpp>
220 #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP