fix doc example typo
[boost.git] / boost / asio / completion_condition.hpp
blobc317c02903fec0cea3c2d69bb532c07b1f843f2f
1 //
2 // completion_condition.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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 //
11 #ifndef BOOST_ASIO_COMPLETION_CONDITION_HPP
12 #define BOOST_ASIO_COMPLETION_CONDITION_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <cstddef>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 namespace boost {
26 namespace asio {
28 namespace detail {
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)
45 return result;
48 class transfer_all_t
50 public:
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
62 public:
63 typedef std::size_t result_type;
65 explicit transfer_at_least_t(std::size_t minimum)
66 : minimum_(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;
77 private:
78 std::size_t minimum_;
81 } // namespace detail
83 /**
84 * @defgroup completion_condition Completion Condition Function Objects
86 * Function objects used for determining when a read or write operation should
87 * complete.
89 /*@{*/
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.
94 /**
95 * This function is used to create an object, of unspecified type, that meets
96 * CompletionCondition requirements.
98 * @par Example
99 * Reading until a buffer is full:
100 * @code
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);
106 * if (ec)
108 * // An error occurred.
110 * else
112 * // n == 128
114 * @endcode
116 #if defined(GENERATING_DOCUMENTATION)
117 unspecified transfer_all();
118 #else
119 inline detail::transfer_all_t transfer_all()
121 return detail::transfer_all_t();
123 #endif
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.
132 * @par Example
133 * Reading until a buffer is full or contains at least 64 bytes:
134 * @code
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);
140 * if (ec)
142 * // An error occurred.
144 * else
146 * // n >= 64 && n <= 128
148 * @endcode
150 #if defined(GENERATING_DOCUMENTATION)
151 unspecified transfer_at_least(std::size_t minimum);
152 #else
153 inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
155 return detail::transfer_at_least_t(minimum);
157 #endif
159 /*@}*/
161 } // namespace asio
162 } // namespace boost
164 #include <boost/asio/detail/pop_options.hpp>
166 #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP