5 // Copyright (c) 2003-2019 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_BASIC_IO_OBJECT_HPP
12 #define BOOST_ASIO_BASIC_IO_OBJECT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/io_context.hpp>
21 #include <boost/asio/detail/push_options.hpp>
26 #if defined(BOOST_ASIO_HAS_MOVE)
29 // Type trait used to determine whether a service supports move.
30 template <typename IoObjectService>
31 class service_has_move
34 typedef IoObjectService service_type;
35 typedef typename service_type::implementation_type implementation_type;
37 template <typename T, typename U>
38 static auto asio_service_has_move_eval(T* t, U* u)
39 -> decltype(t->move_construct(*u, *u), char());
40 static char (&asio_service_has_move_eval(...))[2];
43 static const bool value =
44 sizeof(asio_service_has_move_eval(
45 static_cast<service_type*>(0),
46 static_cast<implementation_type*>(0))) == 1;
49 #endif // defined(BOOST_ASIO_HAS_MOVE)
51 /// Base class for all I/O objects.
53 * @note All I/O objects are non-copyable. However, when using C++0x, certain
54 * I/O objects do support move construction and move assignment.
56 #if !defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
57 template <typename IoObjectService>
59 template <typename IoObjectService,
60 bool Movable = detail::service_has_move<IoObjectService>::value>
65 /// The type of the service that will be used to provide I/O operations.
66 typedef IoObjectService service_type;
68 /// The underlying implementation type of I/O object.
69 typedef typename service_type::implementation_type implementation_type;
71 #if !defined(BOOST_ASIO_NO_DEPRECATED)
72 /// (Deprecated: Use get_executor().) Get the io_context associated with the
75 * This function may be used to obtain the io_context object that the I/O
76 * object uses to dispatch handlers for asynchronous operations.
78 * @return A reference to the io_context object that the I/O object will use
79 * to dispatch handlers. Ownership is not transferred to the caller.
81 boost::asio::io_context& get_io_context()
83 return service_.get_io_context();
86 /// (Deprecated: Use get_executor().) Get the io_context associated with the
89 * This function may be used to obtain the io_context object that the I/O
90 * object uses to dispatch handlers for asynchronous operations.
92 * @return A reference to the io_context object that the I/O object will use
93 * to dispatch handlers. Ownership is not transferred to the caller.
95 boost::asio::io_context& get_io_service()
97 return service_.get_io_context();
99 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
101 /// The type of the executor associated with the object.
102 typedef boost::asio::io_context::executor_type executor_type;
104 /// Get the executor associated with the object.
105 executor_type get_executor() BOOST_ASIO_NOEXCEPT
107 return service_.get_io_context().get_executor();
111 /// Construct a basic_io_object.
114 * @code get_service().construct(get_implementation()); @endcode
116 explicit basic_io_object(boost::asio::io_context& io_context)
117 : service_(boost::asio::use_service<IoObjectService>(io_context))
119 service_.construct(implementation_);
122 #if defined(GENERATING_DOCUMENTATION)
123 /// Move-construct a basic_io_object.
126 * @code get_service().move_construct(
127 * get_implementation(), other.get_implementation()); @endcode
129 * @note Available only for services that support movability,
131 basic_io_object(basic_io_object&& other);
133 /// Move-assign a basic_io_object.
136 * @code get_service().move_assign(get_implementation(),
137 * other.get_service(), other.get_implementation()); @endcode
139 * @note Available only for services that support movability,
141 basic_io_object& operator=(basic_io_object&& other);
143 /// Perform a converting move-construction of a basic_io_object.
144 template <typename IoObjectService1>
145 basic_io_object(IoObjectService1& other_service,
146 typename IoObjectService1::implementation_type& other_implementation);
147 #endif // defined(GENERATING_DOCUMENTATION)
149 /// Protected destructor to prevent deletion through this type.
152 * @code get_service().destroy(get_implementation()); @endcode
156 service_.destroy(implementation_);
159 /// Get the service associated with the I/O object.
160 service_type& get_service()
165 /// Get the service associated with the I/O object.
166 const service_type& get_service() const
171 /// Get the underlying implementation of the I/O object.
172 implementation_type& get_implementation()
174 return implementation_;
177 /// Get the underlying implementation of the I/O object.
178 const implementation_type& get_implementation() const
180 return implementation_;
184 basic_io_object(const basic_io_object&);
185 basic_io_object& operator=(const basic_io_object&);
187 // The service associated with the I/O object.
188 service_type& service_;
190 /// The underlying implementation of the I/O object.
191 implementation_type implementation_;
194 #if defined(BOOST_ASIO_HAS_MOVE)
195 // Specialisation for movable objects.
196 template <typename IoObjectService>
197 class basic_io_object<IoObjectService, true>
200 typedef IoObjectService service_type;
201 typedef typename service_type::implementation_type implementation_type;
203 #if !defined(BOOST_ASIO_NO_DEPRECATED)
204 boost::asio::io_context& get_io_context()
206 return service_->get_io_context();
209 boost::asio::io_context& get_io_service()
211 return service_->get_io_context();
213 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
215 typedef boost::asio::io_context::executor_type executor_type;
217 executor_type get_executor() BOOST_ASIO_NOEXCEPT
219 return service_->get_io_context().get_executor();
223 explicit basic_io_object(boost::asio::io_context& io_context)
224 : service_(&boost::asio::use_service<IoObjectService>(io_context))
226 service_->construct(implementation_);
229 basic_io_object(basic_io_object&& other)
230 : service_(&other.get_service())
232 service_->move_construct(implementation_, other.implementation_);
235 template <typename IoObjectService1>
236 basic_io_object(IoObjectService1& other_service,
237 typename IoObjectService1::implementation_type& other_implementation)
238 : service_(&boost::asio::use_service<IoObjectService>(
239 other_service.get_io_context()))
241 service_->converting_move_construct(implementation_,
242 other_service, other_implementation);
247 service_->destroy(implementation_);
250 basic_io_object& operator=(basic_io_object&& other)
252 service_->move_assign(implementation_,
253 *other.service_, other.implementation_);
254 service_ = other.service_;
258 service_type& get_service()
263 const service_type& get_service() const
268 implementation_type& get_implementation()
270 return implementation_;
273 const implementation_type& get_implementation() const
275 return implementation_;
279 basic_io_object(const basic_io_object&);
280 void operator=(const basic_io_object&);
282 IoObjectService* service_;
283 implementation_type implementation_;
285 #endif // defined(BOOST_ASIO_HAS_MOVE)
290 #include <boost/asio/detail/pop_options.hpp>
292 #endif // BOOST_ASIO_BASIC_IO_OBJECT_HPP