2 // win_iocp_overlapped_ptr.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2008 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/push_options.hpp>
20 #include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
22 #if defined(BOOST_ASIO_HAS_IOCP)
24 #include <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/detail/win_iocp_io_service.hpp>
31 // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
32 class win_iocp_overlapped_ptr
36 // Construct an empty win_iocp_overlapped_ptr.
37 win_iocp_overlapped_ptr()
42 // Construct an win_iocp_overlapped_ptr to contain the specified handler.
43 template <typename Handler
>
44 explicit win_iocp_overlapped_ptr(
45 boost::asio::io_service
& io_service
, Handler handler
)
48 this->reset(io_service
, handler
);
51 // Destructor automatically frees the OVERLAPPED object unless released.
52 ~win_iocp_overlapped_ptr()
67 // Reset to contain the specified handler, freeing any current OVERLAPPED
69 template <typename Handler
>
70 void reset(boost::asio::io_service
& io_service
, Handler handler
)
72 typedef overlapped_operation
<Handler
> value_type
;
73 typedef handler_alloc_traits
<Handler
, value_type
> alloc_traits
;
74 raw_handler_ptr
<alloc_traits
> raw_ptr(handler
);
75 handler_ptr
<alloc_traits
> ptr(raw_ptr
, io_service
.impl_
, handler
);
80 // Get the contained OVERLAPPED object.
86 // Get the contained OVERLAPPED object.
87 const OVERLAPPED
* get() const
92 // Release ownership of the OVERLAPPED object.
95 OVERLAPPED
* tmp
= ptr_
;
100 // Post completion notification for overlapped operation. Releases ownership.
101 void complete(const boost::system::error_code
& ec
,
102 std::size_t bytes_transferred
)
107 ptr_
->io_service_
.post_completion(ptr_
, 0,
108 static_cast<DWORD
>(bytes_transferred
));
114 struct overlapped_operation_base
115 : public win_iocp_io_service::operation
117 overlapped_operation_base(win_iocp_io_service
& io_service
,
118 invoke_func_type invoke_func
, destroy_func_type destroy_func
)
119 : win_iocp_io_service::operation(io_service
, invoke_func
, destroy_func
),
120 io_service_(io_service
)
122 io_service_
.work_started();
125 ~overlapped_operation_base()
127 io_service_
.work_finished();
130 win_iocp_io_service
& io_service_
;
131 boost::system::error_code ec_
;
134 template <typename Handler
>
135 struct overlapped_operation
136 : public overlapped_operation_base
138 overlapped_operation(win_iocp_io_service
& io_service
,
140 : overlapped_operation_base(io_service
,
141 &overlapped_operation
<Handler
>::do_completion_impl
,
142 &overlapped_operation
<Handler
>::destroy_impl
),
148 // Prevent copying and assignment.
149 overlapped_operation(const overlapped_operation
&);
150 void operator=(const overlapped_operation
&);
152 static void do_completion_impl(win_iocp_io_service::operation
* op
,
153 DWORD last_error
, size_t bytes_transferred
)
155 // Take ownership of the operation object.
156 typedef overlapped_operation
<Handler
> op_type
;
157 op_type
* handler_op(static_cast<op_type
*>(op
));
158 typedef handler_alloc_traits
<Handler
, op_type
> alloc_traits
;
159 handler_ptr
<alloc_traits
> ptr(handler_op
->handler_
, handler_op
);
161 // Make a copy of the handler and error_code so that the memory can be
162 // deallocated before the upcall is made.
163 Handler
handler(handler_op
->handler_
);
164 boost::system::error_code
ec(handler_op
->ec_
);
166 ec
= boost::system::error_code(last_error
,
167 boost::asio::error::get_system_category());
169 // Free the memory associated with the handler.
173 boost_asio_handler_invoke_helpers::invoke(
174 bind_handler(handler
, ec
, bytes_transferred
), &handler
);
177 static void destroy_impl(win_iocp_io_service::operation
* op
)
179 // Take ownership of the operation object.
180 typedef overlapped_operation
<Handler
> op_type
;
181 op_type
* handler_op(static_cast<op_type
*>(op
));
182 typedef handler_alloc_traits
<Handler
, op_type
> alloc_traits
;
183 handler_ptr
<alloc_traits
> ptr(handler_op
->handler_
, handler_op
);
185 // A sub-object of the handler may be the true owner of the memory
186 // associated with the handler. Consequently, a local copy of the handler
187 // is required to ensure that any owning sub-object remains valid until
188 // after we have deallocated the memory here.
189 Handler
handler(handler_op
->handler_
);
192 // Free the memory associated with the handler.
199 overlapped_operation_base
* ptr_
;
202 } // namespace detail
206 #endif // defined(BOOST_ASIO_HAS_IOCP)
208 #include <boost/asio/detail/pop_options.hpp>
210 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP