2 // detail/win_iocp_overlapped_ptr.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_service.hpp>
23 #include <boost/asio/detail/addressof.hpp>
24 #include <boost/asio/detail/handler_alloc_helpers.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_service.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_service& io_service, BOOST_ASIO_MOVE_ARG(Handler) handler)
54 this->reset(io_service, 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_service& io_service, Handler handler)
80 typedef win_iocp_overlapped_op<Handler> op;
81 typename op::ptr p = { boost::asio::detail::addressof(handler),
82 boost_asio_handler_alloc_helpers::allocate(
83 sizeof(op), handler), 0 };
84 p.p = new (p.v) op(handler);
86 BOOST_ASIO_HANDLER_CREATION((p.p, "io_service",
87 &io_service.impl_, "overlapped"));
89 io_service.impl_.work_started();
93 iocp_service_ = &io_service.impl_;
96 // Get the contained OVERLAPPED object.
102 // Get the contained OVERLAPPED object.
103 const OVERLAPPED* get() const
108 // Release ownership of the OVERLAPPED object.
109 OVERLAPPED* release()
112 iocp_service_->on_pending(ptr_);
114 OVERLAPPED* tmp = ptr_;
120 // Post completion notification for overlapped operation. Releases ownership.
121 void complete(const boost::system::error_code& ec,
122 std::size_t bytes_transferred)
126 iocp_service_->on_completion(ptr_, ec,
127 static_cast<DWORD>(bytes_transferred));
134 win_iocp_operation* ptr_;
135 win_iocp_io_service* iocp_service_;
138 } // namespace detail
142 #include <boost/asio/detail/pop_options.hpp>
144 #endif // defined(BOOST_ASIO_HAS_IOCP)
146 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP