]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | // |
2 | // io_object_impl.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2019 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_IO_OBJECT_IMPL_HPP | |
12 | #define BOOST_ASIO_DETAIL_IO_OBJECT_IMPL_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <new> | |
19 | #include <boost/asio/detail/config.hpp> | |
20 | #include <boost/asio/detail/io_object_executor.hpp> | |
21 | #include <boost/asio/detail/type_traits.hpp> | |
22 | #include <boost/asio/io_context.hpp> | |
23 | ||
24 | #include <boost/asio/detail/push_options.hpp> | |
25 | ||
26 | namespace boost { | |
27 | namespace asio { | |
28 | ||
29 | class executor; | |
30 | ||
31 | namespace detail { | |
32 | ||
33 | inline bool is_native_io_executor(const io_context::executor_type&) | |
34 | { | |
35 | return true; | |
36 | } | |
37 | ||
38 | template <typename Executor> | |
39 | inline bool is_native_io_executor(const Executor&, | |
40 | typename enable_if<!is_same<Executor, executor>::value>::type* = 0) | |
41 | { | |
42 | return false; | |
43 | } | |
44 | ||
45 | template <typename Executor> | |
46 | inline bool is_native_io_executor(const Executor& ex, | |
47 | typename enable_if<is_same<Executor, executor>::value>::type* = 0) | |
48 | { | |
49 | #if !defined (BOOST_ASIO_NO_TYPEID) | |
50 | return ex.target_type() == typeid(io_context::executor_type); | |
51 | #else // !defined (BOOST_ASIO_NO_TYPEID) | |
52 | return false; | |
53 | #endif // !defined (BOOST_ASIO_NO_TYPEID) | |
54 | } | |
55 | ||
56 | template <typename IoObjectService, | |
57 | typename Executor = io_context::executor_type> | |
58 | class io_object_impl | |
59 | { | |
60 | public: | |
61 | // The type of the service that will be used to provide I/O operations. | |
62 | typedef IoObjectService service_type; | |
63 | ||
64 | // The underlying implementation type of I/O object. | |
65 | typedef typename service_type::implementation_type implementation_type; | |
66 | ||
67 | // The type of the executor associated with the object. | |
68 | typedef Executor executor_type; | |
69 | ||
70 | // The type of executor to be used when implementing asynchronous operations. | |
71 | typedef io_object_executor<Executor> implementation_executor_type; | |
72 | ||
73 | // Construct an I/O object using an executor. | |
74 | explicit io_object_impl(const executor_type& ex) | |
75 | : service_(&boost::asio::use_service<IoObjectService>(ex.context())), | |
76 | implementation_executor_(ex, (is_native_io_executor)(ex)) | |
77 | { | |
78 | service_->construct(implementation_); | |
79 | } | |
80 | ||
81 | // Construct an I/O object using an execution context. | |
82 | template <typename ExecutionContext> | |
83 | explicit io_object_impl(ExecutionContext& context, | |
84 | typename enable_if<is_convertible< | |
85 | ExecutionContext&, execution_context&>::value>::type* = 0) | |
86 | : service_(&boost::asio::use_service<IoObjectService>(context)), | |
87 | implementation_executor_(context.get_executor(), | |
88 | is_same<ExecutionContext, io_context>::value) | |
89 | { | |
90 | service_->construct(implementation_); | |
91 | } | |
92 | ||
93 | #if defined(BOOST_ASIO_HAS_MOVE) | |
94 | // Move-construct an I/O object. | |
95 | io_object_impl(io_object_impl&& other) | |
96 | : service_(&other.get_service()), | |
97 | implementation_executor_(other.get_implementation_executor()) | |
98 | { | |
99 | service_->move_construct(implementation_, other.implementation_); | |
100 | } | |
101 | ||
102 | // Perform a converting move-construction of an I/O object. | |
103 | template <typename IoObjectService1, typename Executor1> | |
104 | io_object_impl(io_object_impl<IoObjectService1, Executor1>&& other) | |
105 | : service_(&boost::asio::use_service<IoObjectService>( | |
106 | other.get_implementation_executor().context())), | |
107 | implementation_executor_(other.get_implementation_executor()) | |
108 | { | |
109 | service_->converting_move_construct(implementation_, | |
110 | other.get_service(), other.get_implementation()); | |
111 | } | |
112 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
113 | ||
114 | // Destructor. | |
115 | ~io_object_impl() | |
116 | { | |
117 | service_->destroy(implementation_); | |
118 | } | |
119 | ||
120 | #if defined(BOOST_ASIO_HAS_MOVE) | |
121 | // Move-assign an I/O object. | |
122 | io_object_impl& operator=(io_object_impl&& other) | |
123 | { | |
124 | if (this != &other) | |
125 | { | |
126 | service_->move_assign(implementation_, | |
127 | *other.service_, other.implementation_); | |
128 | implementation_executor_.~implementation_executor_type(); | |
129 | new (&implementation_executor_) implementation_executor_type( | |
130 | std::move(other.implementation_executor_)); | |
131 | service_ = other.service_; | |
132 | } | |
133 | return *this; | |
134 | } | |
135 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
136 | ||
137 | // Get the executor associated with the object. | |
138 | executor_type get_executor() BOOST_ASIO_NOEXCEPT | |
139 | { | |
140 | return implementation_executor_.inner_executor(); | |
141 | } | |
142 | ||
143 | // Get the executor to be used when implementing asynchronous operations. | |
144 | const implementation_executor_type& get_implementation_executor() | |
145 | BOOST_ASIO_NOEXCEPT | |
146 | { | |
147 | return implementation_executor_; | |
148 | } | |
149 | ||
150 | // Get the service associated with the I/O object. | |
151 | service_type& get_service() | |
152 | { | |
153 | return *service_; | |
154 | } | |
155 | ||
156 | // Get the service associated with the I/O object. | |
157 | const service_type& get_service() const | |
158 | { | |
159 | return *service_; | |
160 | } | |
161 | ||
162 | // Get the underlying implementation of the I/O object. | |
163 | implementation_type& get_implementation() | |
164 | { | |
165 | return implementation_; | |
166 | } | |
167 | ||
168 | // Get the underlying implementation of the I/O object. | |
169 | const implementation_type& get_implementation() const | |
170 | { | |
171 | return implementation_; | |
172 | } | |
173 | ||
174 | private: | |
175 | // Disallow copying and copy assignment. | |
176 | io_object_impl(const io_object_impl&); | |
177 | io_object_impl& operator=(const io_object_impl&); | |
178 | ||
179 | // The service associated with the I/O object. | |
180 | service_type* service_; | |
181 | ||
182 | // The underlying implementation of the I/O object. | |
183 | implementation_type implementation_; | |
184 | ||
185 | // The associated executor. | |
186 | implementation_executor_type implementation_executor_; | |
187 | }; | |
188 | ||
189 | } // namespace detail | |
190 | } // namespace asio | |
191 | } // namespace boost | |
192 | ||
193 | #include <boost/asio/detail/pop_options.hpp> | |
194 | ||
195 | #endif // BOOST_ASIO_DETAIL_IO_OBJECT_IMPL_HPP |