2 // completion_condition.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2008 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/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
30 // The default maximum number of bytes to transfer in a single operation.
31 enum { default_max_transfer_size
= 65536 };
33 // Adapt result of old-style completion conditions (which had a bool result
34 // where true indicated that the operation was complete).
35 inline std::size_t adapt_completion_condition_result(bool result
)
37 return result
? 0 : default_max_transfer_size
;
40 // Adapt result of current completion conditions (which have a size_t result
41 // where 0 means the operation is complete, and otherwise the result is the
42 // maximum number of bytes to transfer on the next underlying operation).
43 inline std::size_t adapt_completion_condition_result(std::size_t result
)
51 typedef std::size_t result_type
;
53 template <typename Error
>
54 std::size_t operator()(const Error
& err
, std::size_t)
56 return !!err
? 0 : default_max_transfer_size
;
60 class transfer_at_least_t
63 typedef std::size_t result_type
;
65 explicit transfer_at_least_t(std::size_t minimum
)
70 template <typename Error
>
71 std::size_t operator()(const Error
& err
, std::size_t bytes_transferred
)
73 return (!!err
|| bytes_transferred
>= minimum_
)
74 ? 0 : default_max_transfer_size
;
84 * @defgroup completion_condition Completion Condition Function Objects
86 * Function objects used for determining when a read or write operation should
91 /// Return a completion condition function object that indicates that a read or
92 /// write operation should continue until all of the data has been transferred,
93 /// or until an error occurs.
95 * This function is used to create an object, of unspecified type, that meets
96 * CompletionCondition requirements.
99 * Reading until a buffer is full:
101 * boost::array<char, 128> buf;
102 * boost::system::error_code ec;
103 * std::size_t n = boost::asio::read(
104 * sock, boost::asio::buffer(buf),
105 * boost::asio::transfer_all(), ec);
108 * // An error occurred.
116 #if defined(GENERATING_DOCUMENTATION)
117 unspecified
transfer_all();
119 inline detail::transfer_all_t
transfer_all()
121 return detail::transfer_all_t();
125 /// Return a completion condition function object that indicates that a read or
126 /// write operation should continue until a minimum number of bytes has been
127 /// transferred, or until an error occurs.
129 * This function is used to create an object, of unspecified type, that meets
130 * CompletionCondition requirements.
133 * Reading until a buffer is full or contains at least 64 bytes:
135 * boost::array<char, 128> buf;
136 * boost::system::error_code ec;
137 * std::size_t n = boost::asio::read(
138 * sock, boost::asio::buffer(buf),
139 * boost::asio::transfer_at_least(64), ec);
142 * // An error occurred.
146 * // n >= 64 && n <= 128
150 #if defined(GENERATING_DOCUMENTATION)
151 unspecified
transfer_at_least(std::size_t minimum
);
153 inline detail::transfer_at_least_t
transfer_at_least(std::size_t minimum
)
155 return detail::transfer_at_least_t(minimum
);
164 #include <boost/asio/detail/pop_options.hpp>
166 #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP