Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:33:43

0001 //
0002 // completion_condition.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_COMPLETION_CONDITION_HPP
0012 #define BOOST_ASIO_COMPLETION_CONDITION_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <cstddef>
0020 
0021 #include <boost/asio/detail/push_options.hpp>
0022 
0023 namespace boost {
0024 namespace asio {
0025 
0026 namespace detail {
0027 
0028 // The default maximum number of bytes to transfer in a single operation.
0029 enum default_max_transfer_size_t { default_max_transfer_size = 65536 };
0030 
0031 // Adapt result of old-style completion conditions (which had a bool result
0032 // where true indicated that the operation was complete).
0033 inline std::size_t adapt_completion_condition_result(bool result)
0034 {
0035   return result ? 0 : default_max_transfer_size;
0036 }
0037 
0038 // Adapt result of current completion conditions (which have a size_t result
0039 // where 0 means the operation is complete, and otherwise the result is the
0040 // maximum number of bytes to transfer on the next underlying operation).
0041 inline std::size_t adapt_completion_condition_result(std::size_t result)
0042 {
0043   return result;
0044 }
0045 
0046 class transfer_all_t
0047 {
0048 public:
0049   typedef std::size_t result_type;
0050 
0051   template <typename Error>
0052   std::size_t operator()(const Error& err, std::size_t)
0053   {
0054     return !!err ? 0 : default_max_transfer_size;
0055   }
0056 };
0057 
0058 class transfer_at_least_t
0059 {
0060 public:
0061   typedef std::size_t result_type;
0062 
0063   explicit transfer_at_least_t(std::size_t minimum)
0064     : minimum_(minimum)
0065   {
0066   }
0067 
0068   template <typename Error>
0069   std::size_t operator()(const Error& err, std::size_t bytes_transferred)
0070   {
0071     return (!!err || bytes_transferred >= minimum_)
0072       ? 0 : default_max_transfer_size;
0073   }
0074 
0075 private:
0076   std::size_t minimum_;
0077 };
0078 
0079 class transfer_exactly_t
0080 {
0081 public:
0082   typedef std::size_t result_type;
0083 
0084   explicit transfer_exactly_t(std::size_t size)
0085     : size_(size)
0086   {
0087   }
0088 
0089   template <typename Error>
0090   std::size_t operator()(const Error& err, std::size_t bytes_transferred)
0091   {
0092     return (!!err || bytes_transferred >= size_) ? 0 :
0093       (size_ - bytes_transferred < default_max_transfer_size
0094         ? size_ - bytes_transferred : std::size_t(default_max_transfer_size));
0095   }
0096 
0097 private:
0098   std::size_t size_;
0099 };
0100 
0101 } // namespace detail
0102 
0103 /**
0104  * @defgroup completion_condition Completion Condition Function Objects
0105  *
0106  * Function objects used for determining when a read or write operation should
0107  * complete.
0108  */
0109 /*@{*/
0110 
0111 /// Return a completion condition function object that indicates that a read or
0112 /// write operation should continue until all of the data has been transferred,
0113 /// or until an error occurs.
0114 /**
0115  * This function is used to create an object, of unspecified type, that meets
0116  * CompletionCondition requirements.
0117  *
0118  * @par Example
0119  * Reading until a buffer is full:
0120  * @code
0121  * boost::array<char, 128> buf;
0122  * boost::system::error_code ec;
0123  * std::size_t n = boost::asio::read(
0124  *     sock, boost::asio::buffer(buf),
0125  *     boost::asio::transfer_all(), ec);
0126  * if (ec)
0127  * {
0128  *   // An error occurred.
0129  * }
0130  * else
0131  * {
0132  *   // n == 128
0133  * }
0134  * @endcode
0135  */
0136 #if defined(GENERATING_DOCUMENTATION)
0137 unspecified transfer_all();
0138 #else
0139 inline detail::transfer_all_t transfer_all()
0140 {
0141   return detail::transfer_all_t();
0142 }
0143 #endif
0144 
0145 /// Return a completion condition function object that indicates that a read or
0146 /// write operation should continue until a minimum number of bytes has been
0147 /// transferred, or until an error occurs.
0148 /**
0149  * This function is used to create an object, of unspecified type, that meets
0150  * CompletionCondition requirements.
0151  *
0152  * @par Example
0153  * Reading until a buffer is full or contains at least 64 bytes:
0154  * @code
0155  * boost::array<char, 128> buf;
0156  * boost::system::error_code ec;
0157  * std::size_t n = boost::asio::read(
0158  *     sock, boost::asio::buffer(buf),
0159  *     boost::asio::transfer_at_least(64), ec);
0160  * if (ec)
0161  * {
0162  *   // An error occurred.
0163  * }
0164  * else
0165  * {
0166  *   // n >= 64 && n <= 128
0167  * }
0168  * @endcode
0169  */
0170 #if defined(GENERATING_DOCUMENTATION)
0171 unspecified transfer_at_least(std::size_t minimum);
0172 #else
0173 inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
0174 {
0175   return detail::transfer_at_least_t(minimum);
0176 }
0177 #endif
0178 
0179 /// Return a completion condition function object that indicates that a read or
0180 /// write operation should continue until an exact number of bytes has been
0181 /// transferred, or until an error occurs.
0182 /**
0183  * This function is used to create an object, of unspecified type, that meets
0184  * CompletionCondition requirements.
0185  *
0186  * @par Example
0187  * Reading until a buffer is full or contains exactly 64 bytes:
0188  * @code
0189  * boost::array<char, 128> buf;
0190  * boost::system::error_code ec;
0191  * std::size_t n = boost::asio::read(
0192  *     sock, boost::asio::buffer(buf),
0193  *     boost::asio::transfer_exactly(64), ec);
0194  * if (ec)
0195  * {
0196  *   // An error occurred.
0197  * }
0198  * else
0199  * {
0200  *   // n == 64
0201  * }
0202  * @endcode
0203  */
0204 #if defined(GENERATING_DOCUMENTATION)
0205 unspecified transfer_exactly(std::size_t size);
0206 #else
0207 inline detail::transfer_exactly_t transfer_exactly(std::size_t size)
0208 {
0209   return detail::transfer_exactly_t(size);
0210 }
0211 #endif
0212 
0213 /*@}*/
0214 
0215 } // namespace asio
0216 } // namespace boost
0217 
0218 #include <boost/asio/detail/pop_options.hpp>
0219 
0220 #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP