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_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_service.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 /// Get the io_service associated with the object.
73 * This function may be used to obtain the io_service object that the I/O
74 * object uses to dispatch handlers for asynchronous operations.
76 * @return A reference to the io_service object that the I/O object will use
77 * to dispatch handlers. Ownership is not transferred to the caller.
79 boost::asio::io_service& get_io_service()
81 return service.get_io_service();
85 /// Construct a basic_io_object.
88 * @code get_service().construct(get_implementation()); @endcode
90 explicit basic_io_object(boost::asio::io_service& io_service)
91 : service(boost::asio::use_service<IoObjectService>(io_service))
93 service.construct(implementation);
96 #if defined(GENERATING_DOCUMENTATION)
97 /// Move-construct a basic_io_object.
100 * @code get_service().move_construct(
101 * get_implementation(), other.get_implementation()); @endcode
103 * @note Available only for services that support movability,
105 basic_io_object(basic_io_object&& other);
107 /// Move-assign a basic_io_object.
110 * @code get_service().move_assign(get_implementation(),
111 * other.get_service(), other.get_implementation()); @endcode
113 * @note Available only for services that support movability,
115 basic_io_object& operator=(basic_io_object&& other);
116 #endif // defined(GENERATING_DOCUMENTATION)
118 /// Protected destructor to prevent deletion through this type.
121 * @code get_service().destroy(get_implementation()); @endcode
125 service.destroy(implementation);
128 /// Get the service associated with the I/O object.
129 service_type& get_service()
134 /// Get the service associated with the I/O object.
135 const service_type& get_service() const
140 /// (Deprecated: Use get_service().) The service associated with the I/O
143 * @note Available only for services that do not support movability.
145 service_type& service;
147 /// Get the underlying implementation of the I/O object.
148 implementation_type& get_implementation()
150 return implementation;
153 /// Get the underlying implementation of the I/O object.
154 const implementation_type& get_implementation() const
156 return implementation;
159 /// (Deprecated: Use get_implementation().) The underlying implementation of
161 implementation_type implementation;
164 basic_io_object(const basic_io_object&);
165 basic_io_object& operator=(const basic_io_object&);
168 #if defined(BOOST_ASIO_HAS_MOVE)
169 // Specialisation for movable objects.
170 template <typename IoObjectService>
171 class basic_io_object<IoObjectService, true>
174 typedef IoObjectService service_type;
175 typedef typename service_type::implementation_type implementation_type;
177 boost::asio::io_service& get_io_service()
179 return service_->get_io_service();
183 explicit basic_io_object(boost::asio::io_service& io_service)
184 : service_(&boost::asio::use_service<IoObjectService>(io_service))
186 service_->construct(implementation);
189 basic_io_object(basic_io_object&& other)
190 : service_(&other.get_service())
192 service_->move_construct(implementation, other.implementation);
197 service_->destroy(implementation);
200 basic_io_object& operator=(basic_io_object&& other)
202 service_->move_assign(implementation,
203 *other.service_, other.implementation);
204 service_ = other.service_;
208 service_type& get_service()
213 const service_type& get_service() const
218 implementation_type& get_implementation()
220 return implementation;
223 const implementation_type& get_implementation() const
225 return implementation;
228 implementation_type implementation;
231 basic_io_object(const basic_io_object&);
232 void operator=(const basic_io_object&);
234 IoObjectService* service_;
236 #endif // defined(BOOST_ASIO_HAS_MOVE)
241 #include <boost/asio/detail/pop_options.hpp>
243 #endif // BOOST_ASIO_BASIC_IO_OBJECT_HPP