2 // detail/win_iocp_overlapped_ptr.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_IOCP)
22 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/detail/handler_alloc_helpers.hpp>
24 #include <boost/asio/detail/memory.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/win_iocp_overlapped_op.hpp>
27 #include <boost/asio/detail/win_iocp_io_context.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
36 class win_iocp_overlapped_ptr
40 // Construct an empty win_iocp_overlapped_ptr.
41 win_iocp_overlapped_ptr()
47 // Construct an win_iocp_overlapped_ptr to contain the specified handler.
48 template <typename Handler>
49 explicit win_iocp_overlapped_ptr(
50 boost::asio::io_context& io_context, BOOST_ASIO_MOVE_ARG(Handler) handler)
54 this->reset(io_context, BOOST_ASIO_MOVE_CAST(Handler)(handler));
57 // Destructor automatically frees the OVERLAPPED object unless released.
58 ~win_iocp_overlapped_ptr()
70 iocp_service_->work_finished();
75 // Reset to contain the specified handler, freeing any current OVERLAPPED
77 template <typename Handler>
78 void reset(boost::asio::io_context& io_context, Handler handler)
80 typedef win_iocp_overlapped_op<Handler> op;
81 typename op::ptr p = { boost::asio::detail::addressof(handler),
82 op::ptr::allocate(handler), 0 };
83 p.p = new (p.v) op(handler);
85 BOOST_ASIO_HANDLER_CREATION((io_context, *p.p,
86 "io_context", &io_context.impl_, 0, "overlapped"));
88 io_context.impl_.work_started();
92 iocp_service_ = &io_context.impl_;
95 // Get the contained OVERLAPPED object.
101 // Get the contained OVERLAPPED object.
102 const OVERLAPPED* get() const
107 // Release ownership of the OVERLAPPED object.
108 OVERLAPPED* release()
111 iocp_service_->on_pending(ptr_);
113 OVERLAPPED* tmp = ptr_;
119 // Post completion notification for overlapped operation. Releases ownership.
120 void complete(const boost::system::error_code& ec,
121 std::size_t bytes_transferred)
125 iocp_service_->on_completion(ptr_, ec,
126 static_cast<DWORD>(bytes_transferred));
133 win_iocp_operation* ptr_;
134 win_iocp_io_context* iocp_service_;
137 } // namespace detail
141 #include <boost/asio/detail/pop_options.hpp>
143 #endif // defined(BOOST_ASIO_HAS_IOCP)
145 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP