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_DETAIL_IO_OBJECT_IMPL_HPP
12 #define BOOST_ASIO_DETAIL_IO_OBJECT_IMPL_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
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>
24 #include <boost/asio/detail/push_options.hpp>
33 inline bool is_native_io_executor(const io_context::executor_type&)
38 template <typename Executor>
39 inline bool is_native_io_executor(const Executor&,
40 typename enable_if<!is_same<Executor, executor>::value>::type* = 0)
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)
49 #if !defined (BOOST_ASIO_NO_TYPEID)
50 return ex.target_type() == typeid(io_context::executor_type);
51 #else // !defined (BOOST_ASIO_NO_TYPEID)
53 #endif // !defined (BOOST_ASIO_NO_TYPEID)
56 template <typename IoObjectService,
57 typename Executor = io_context::executor_type>
61 // The type of the service that will be used to provide I/O operations.
62 typedef IoObjectService service_type;
64 // The underlying implementation type of I/O object.
65 typedef typename service_type::implementation_type implementation_type;
67 // The type of the executor associated with the object.
68 typedef Executor executor_type;
70 // The type of executor to be used when implementing asynchronous operations.
71 typedef io_object_executor<Executor> implementation_executor_type;
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))
78 service_->construct(implementation_);
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)
90 service_->construct(implementation_);
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())
99 service_->move_construct(implementation_, other.implementation_);
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())
109 service_->converting_move_construct(implementation_,
110 other.get_service(), other.get_implementation());
112 #endif // defined(BOOST_ASIO_HAS_MOVE)
117 service_->destroy(implementation_);
120 #if defined(BOOST_ASIO_HAS_MOVE)
121 // Move-assign an I/O object.
122 io_object_impl& operator=(io_object_impl&& other)
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_;
135 #endif // defined(BOOST_ASIO_HAS_MOVE)
137 // Get the executor associated with the object.
138 executor_type get_executor() BOOST_ASIO_NOEXCEPT
140 return implementation_executor_.inner_executor();
143 // Get the executor to be used when implementing asynchronous operations.
144 const implementation_executor_type& get_implementation_executor()
147 return implementation_executor_;
150 // Get the service associated with the I/O object.
151 service_type& get_service()
156 // Get the service associated with the I/O object.
157 const service_type& get_service() const
162 // Get the underlying implementation of the I/O object.
163 implementation_type& get_implementation()
165 return implementation_;
168 // Get the underlying implementation of the I/O object.
169 const implementation_type& get_implementation() const
171 return implementation_;
175 // Disallow copying and copy assignment.
176 io_object_impl(const io_object_impl&);
177 io_object_impl& operator=(const io_object_impl&);
179 // The service associated with the I/O object.
180 service_type* service_;
182 // The underlying implementation of the I/O object.
183 implementation_type implementation_;
185 // The associated executor.
186 implementation_executor_type implementation_executor_;
189 } // namespace detail
193 #include <boost/asio/detail/pop_options.hpp>
195 #endif // BOOST_ASIO_DETAIL_IO_OBJECT_IMPL_HPP