io_object_impl.hpp 5.6 KB

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