diff options
Diffstat (limited to 'boost/asio/impl/write_at.hpp')
-rw-r--r-- | boost/asio/impl/write_at.hpp | 146 |
1 files changed, 76 insertions, 70 deletions
diff --git a/boost/asio/impl/write_at.hpp b/boost/asio/impl/write_at.hpp index 6b597d2085..3cad9c5b50 100644 --- a/boost/asio/impl/write_at.hpp +++ b/boost/asio/impl/write_at.hpp @@ -2,7 +2,7 @@ // impl/write_at.hpp // ~~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -28,6 +28,7 @@ #include <boost/asio/detail/handler_cont_helpers.hpp> #include <boost/asio/detail/handler_invoke_helpers.hpp> #include <boost/asio/detail/handler_type_requirements.hpp> +#include <boost/asio/detail/non_const_lvalue.hpp> #include <boost/asio/detail/throw_error.hpp> #include <boost/asio/detail/push_options.hpp> @@ -69,7 +70,8 @@ std::size_t write_at(SyncRandomAccessWriteDevice& d, CompletionCondition completion_condition, boost::system::error_code& ec) { return detail::write_at_buffer_sequence(d, offset, buffers, - boost::asio::buffer_sequence_begin(buffers), completion_condition, ec); + boost::asio::buffer_sequence_begin(buffers), + BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec); } template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence> @@ -98,8 +100,8 @@ inline std::size_t write_at(SyncRandomAccessWriteDevice& d, CompletionCondition completion_condition) { boost::system::error_code ec; - std::size_t bytes_transferred = write_at( - d, offset, buffers, completion_condition, ec); + std::size_t bytes_transferred = write_at(d, offset, buffers, + BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec); boost::asio::detail::throw_error(ec, "write_at"); return bytes_transferred; } @@ -113,8 +115,8 @@ std::size_t write_at(SyncRandomAccessWriteDevice& d, uint64_t offset, boost::asio::basic_streambuf<Allocator>& b, CompletionCondition completion_condition, boost::system::error_code& ec) { - std::size_t bytes_transferred = write_at( - d, offset, b.data(), completion_condition, ec); + std::size_t bytes_transferred = write_at(d, offset, b.data(), + BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec); b.consume(bytes_transferred); return bytes_transferred; } @@ -144,8 +146,8 @@ inline std::size_t write_at(SyncRandomAccessWriteDevice& d, CompletionCondition completion_condition) { boost::system::error_code ec; - std::size_t bytes_transferred = write_at( - d, offset, b, completion_condition, ec); + std::size_t bytes_transferred = write_at(d, offset, b, + BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec); boost::asio::detail::throw_error(ec, "write_at"); return bytes_transferred; } @@ -164,7 +166,7 @@ namespace detail public: write_at_op(AsyncRandomAccessWriteDevice& device, uint64_t offset, const ConstBufferSequence& buffers, - CompletionCondition completion_condition, WriteHandler& handler) + CompletionCondition& completion_condition, WriteHandler& handler) : detail::base_from_completion_cond< CompletionCondition>(completion_condition), device_(device), @@ -187,10 +189,12 @@ namespace detail } write_at_op(write_at_op&& other) - : detail::base_from_completion_cond<CompletionCondition>(other), + : detail::base_from_completion_cond<CompletionCondition>( + BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond< + CompletionCondition>)(other)), device_(other.device_), offset_(other.offset_), - buffers_(other.buffers_), + buffers_(BOOST_ASIO_MOVE_CAST(buffers_type)(other.buffers_)), start_(other.start_), handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_)) { @@ -222,10 +226,12 @@ namespace detail } //private: + typedef boost::asio::detail::consuming_buffers<const_buffer, + ConstBufferSequence, ConstBufferIterator> buffers_type; + AsyncRandomAccessWriteDevice& device_; uint64_t offset_; - boost::asio::detail::consuming_buffers<const_buffer, - ConstBufferSequence, ConstBufferIterator> buffers_; + buffers_type buffers_; int start_; WriteHandler handler_; }; @@ -291,7 +297,7 @@ namespace detail typename CompletionCondition, typename WriteHandler> inline void start_write_at_buffer_sequence_op(AsyncRandomAccessWriteDevice& d, uint64_t offset, const ConstBufferSequence& buffers, - const ConstBufferIterator&, CompletionCondition completion_condition, + const ConstBufferIterator&, CompletionCondition& completion_condition, WriteHandler& handler) { detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence, @@ -299,6 +305,27 @@ namespace detail d, offset, buffers, completion_condition, handler)( boost::system::error_code(), 0, 1); } + + struct initiate_async_write_at_buffer_sequence + { + template <typename WriteHandler, typename AsyncRandomAccessWriteDevice, + typename ConstBufferSequence, typename CompletionCondition> + void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler, + AsyncRandomAccessWriteDevice* d, uint64_t offset, + const ConstBufferSequence& buffers, + BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const + { + // If you get an error on the following line it means that your handler + // does not meet the documented type requirements for a WriteHandler. + BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; + + non_const_lvalue<WriteHandler> handler2(handler); + non_const_lvalue<CompletionCondition> completion_cond2(completion_cond); + start_write_at_buffer_sequence_op(*d, offset, buffers, + boost::asio::buffer_sequence_begin(buffers), + completion_cond2.value, handler2.value); + } + }; } // namespace detail #if !defined(GENERATING_DOCUMENTATION) @@ -354,18 +381,10 @@ async_write_at(AsyncRandomAccessWriteDevice& d, CompletionCondition completion_condition, BOOST_ASIO_MOVE_ARG(WriteHandler) handler) { - // If you get an error on the following line it means that your handler does - // not meet the documented type requirements for a WriteHandler. - BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; - - async_completion<WriteHandler, - void (boost::system::error_code, std::size_t)> init(handler); - - detail::start_write_at_buffer_sequence_op(d, offset, buffers, - boost::asio::buffer_sequence_begin(buffers), completion_condition, - init.completion_handler); - - return init.result.get(); + return async_initiate<WriteHandler, + void (boost::system::error_code, std::size_t)>( + detail::initiate_async_write_at_buffer_sequence(), handler, &d, offset, + buffers, BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition)); } template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence, @@ -376,18 +395,10 @@ async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset, const ConstBufferSequence& buffers, BOOST_ASIO_MOVE_ARG(WriteHandler) handler) { - // If you get an error on the following line it means that your handler does - // not meet the documented type requirements for a WriteHandler. - BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; - - async_completion<WriteHandler, - void (boost::system::error_code, std::size_t)> init(handler); - - detail::start_write_at_buffer_sequence_op(d, offset, buffers, - boost::asio::buffer_sequence_begin(buffers), transfer_all(), - init.completion_handler); - - return init.result.get(); + return async_initiate<WriteHandler, + void (boost::system::error_code, std::size_t)>( + detail::initiate_async_write_at_buffer_sequence(), + handler, &d, offset, buffers, transfer_all()); } #if !defined(BOOST_ASIO_NO_EXTENSIONS) @@ -473,13 +484,26 @@ namespace detail function, this_handler->handler_); } - template <typename Allocator, typename WriteHandler> - inline write_at_streambuf_op<Allocator, WriteHandler> - make_write_at_streambuf_op( - boost::asio::basic_streambuf<Allocator>& b, WriteHandler handler) + struct initiate_async_write_at_streambuf { - return write_at_streambuf_op<Allocator, WriteHandler>(b, handler); - } + template <typename WriteHandler, typename AsyncRandomAccessWriteDevice, + typename Allocator, typename CompletionCondition> + void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler, + AsyncRandomAccessWriteDevice* d, uint64_t offset, + basic_streambuf<Allocator>* b, + BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_condition) const + { + // If you get an error on the following line it means that your handler + // does not meet the documented type requirements for a WriteHandler. + BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; + + non_const_lvalue<WriteHandler> handler2(handler); + async_write_at(*d, offset, b->data(), + BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), + write_at_streambuf_op<Allocator, typename decay<WriteHandler>::type>( + *b, handler2.value)); + } + }; } // namespace detail #if !defined(GENERATING_DOCUMENTATION) @@ -525,19 +549,10 @@ async_write_at(AsyncRandomAccessWriteDevice& d, CompletionCondition completion_condition, BOOST_ASIO_MOVE_ARG(WriteHandler) handler) { - // If you get an error on the following line it means that your handler does - // not meet the documented type requirements for a WriteHandler. - BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; - - async_completion<WriteHandler, - void (boost::system::error_code, std::size_t)> init(handler); - - async_write_at(d, offset, b.data(), completion_condition, - detail::write_at_streambuf_op<Allocator, BOOST_ASIO_HANDLER_TYPE( - WriteHandler, void (boost::system::error_code, std::size_t))>( - b, init.completion_handler)); - - return init.result.get(); + return async_initiate<WriteHandler, + void (boost::system::error_code, std::size_t)>( + detail::initiate_async_write_at_streambuf(), handler, &d, offset, + &b, BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition)); } template <typename AsyncRandomAccessWriteDevice, typename Allocator, @@ -548,19 +563,10 @@ async_write_at(AsyncRandomAccessWriteDevice& d, uint64_t offset, boost::asio::basic_streambuf<Allocator>& b, BOOST_ASIO_MOVE_ARG(WriteHandler) handler) { - // If you get an error on the following line it means that your handler does - // not meet the documented type requirements for a WriteHandler. - BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; - - async_completion<WriteHandler, - void (boost::system::error_code, std::size_t)> init(handler); - - async_write_at(d, offset, b.data(), transfer_all(), - detail::write_at_streambuf_op<Allocator, BOOST_ASIO_HANDLER_TYPE( - WriteHandler, void (boost::system::error_code, std::size_t))>( - b, init.completion_handler)); - - return init.result.get(); + return async_initiate<WriteHandler, + void (boost::system::error_code, std::size_t)>( + detail::initiate_async_write_at_streambuf(), + handler, &d, offset, &b, transfer_all()); } #endif // !defined(BOOST_ASIO_NO_IOSTREAM) |