]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/win_iocp_overlapped_ptr.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / win_iocp_overlapped_ptr.hpp
1 //
2 // detail/win_iocp_overlapped_ptr.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_HAS_IOCP)
21
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>
28
29 #include <boost/asio/detail/push_options.hpp>
30
31 namespace boost {
32 namespace asio {
33 namespace detail {
34
35 // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
36 class win_iocp_overlapped_ptr
37 : private noncopyable
38 {
39 public:
40 // Construct an empty win_iocp_overlapped_ptr.
41 win_iocp_overlapped_ptr()
42 : ptr_(0),
43 iocp_service_(0)
44 {
45 }
46
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)
51 : ptr_(0),
52 iocp_service_(0)
53 {
54 this->reset(io_context, BOOST_ASIO_MOVE_CAST(Handler)(handler));
55 }
56
57 // Destructor automatically frees the OVERLAPPED object unless released.
58 ~win_iocp_overlapped_ptr()
59 {
60 reset();
61 }
62
63 // Reset to empty.
64 void reset()
65 {
66 if (ptr_)
67 {
68 ptr_->destroy();
69 ptr_ = 0;
70 iocp_service_->work_finished();
71 iocp_service_ = 0;
72 }
73 }
74
75 // Reset to contain the specified handler, freeing any current OVERLAPPED
76 // object.
77 template <typename Handler>
78 void reset(boost::asio::io_context& io_context, Handler handler)
79 {
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);
84
85 BOOST_ASIO_HANDLER_CREATION((io_context, *p.p,
86 "io_context", &io_context.impl_, 0, "overlapped"));
87
88 io_context.impl_.work_started();
89 reset();
90 ptr_ = p.p;
91 p.v = p.p = 0;
92 iocp_service_ = &io_context.impl_;
93 }
94
95 // Get the contained OVERLAPPED object.
96 OVERLAPPED* get()
97 {
98 return ptr_;
99 }
100
101 // Get the contained OVERLAPPED object.
102 const OVERLAPPED* get() const
103 {
104 return ptr_;
105 }
106
107 // Release ownership of the OVERLAPPED object.
108 OVERLAPPED* release()
109 {
110 if (ptr_)
111 iocp_service_->on_pending(ptr_);
112
113 OVERLAPPED* tmp = ptr_;
114 ptr_ = 0;
115 iocp_service_ = 0;
116 return tmp;
117 }
118
119 // Post completion notification for overlapped operation. Releases ownership.
120 void complete(const boost::system::error_code& ec,
121 std::size_t bytes_transferred)
122 {
123 if (ptr_)
124 {
125 iocp_service_->on_completion(ptr_, ec,
126 static_cast<DWORD>(bytes_transferred));
127 ptr_ = 0;
128 iocp_service_ = 0;
129 }
130 }
131
132 private:
133 win_iocp_operation* ptr_;
134 win_iocp_io_context* iocp_service_;
135 };
136
137 } // namespace detail
138 } // namespace asio
139 } // namespace boost
140
141 #include <boost/asio/detail/pop_options.hpp>
142
143 #endif // defined(BOOST_ASIO_HAS_IOCP)
144
145 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP