io_object_impl.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // detail/io_object_impl.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/type_traits.hpp>
  18. #include <boost/asio/execution/executor.hpp>
  19. #include <boost/asio/execution/context.hpp>
  20. #include <boost/asio/io_context.hpp>
  21. #include <boost/asio/query.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. template <typename IoObjectService,
  27. typename Executor = io_context::executor_type>
  28. class io_object_impl
  29. {
  30. public:
  31. // The type of the service that will be used to provide I/O operations.
  32. typedef IoObjectService service_type;
  33. // The underlying implementation type of I/O object.
  34. typedef typename service_type::implementation_type implementation_type;
  35. // The type of the executor associated with the object.
  36. typedef Executor executor_type;
  37. // Construct an I/O object using an executor.
  38. explicit io_object_impl(int, const executor_type& ex)
  39. : service_(&boost::asio::use_service<IoObjectService>(
  40. io_object_impl::get_context(ex))),
  41. executor_(ex)
  42. {
  43. service_->construct(implementation_);
  44. }
  45. // Construct an I/O object using an execution context.
  46. template <typename ExecutionContext>
  47. explicit io_object_impl(int, int, ExecutionContext& context)
  48. : service_(&boost::asio::use_service<IoObjectService>(context)),
  49. executor_(context.get_executor())
  50. {
  51. service_->construct(implementation_);
  52. }
  53. // Move-construct an I/O object.
  54. io_object_impl(io_object_impl&& other)
  55. : service_(&other.get_service()),
  56. executor_(other.get_executor())
  57. {
  58. service_->move_construct(implementation_, other.implementation_);
  59. }
  60. // Perform converting move-construction of an I/O object on the same service.
  61. template <typename Executor1>
  62. io_object_impl(io_object_impl<IoObjectService, Executor1>&& other)
  63. : service_(&other.get_service()),
  64. executor_(other.get_executor())
  65. {
  66. service_->move_construct(implementation_, other.get_implementation());
  67. }
  68. // Perform converting move-construction of an I/O object on another service.
  69. template <typename IoObjectService1, typename Executor1>
  70. io_object_impl(io_object_impl<IoObjectService1, Executor1>&& other)
  71. : service_(&boost::asio::use_service<IoObjectService>(
  72. io_object_impl::get_context(other.get_executor()))),
  73. executor_(other.get_executor())
  74. {
  75. service_->converting_move_construct(implementation_,
  76. other.get_service(), other.get_implementation());
  77. }
  78. // Destructor.
  79. ~io_object_impl()
  80. {
  81. service_->destroy(implementation_);
  82. }
  83. // Move-assign an I/O object.
  84. io_object_impl& operator=(io_object_impl&& other)
  85. {
  86. if (this != &other)
  87. {
  88. service_->move_assign(implementation_,
  89. *other.service_, other.implementation_);
  90. executor_.~executor_type();
  91. new (&executor_) executor_type(other.executor_);
  92. service_ = other.service_;
  93. }
  94. return *this;
  95. }
  96. // Get the executor associated with the object.
  97. const executor_type& get_executor() noexcept
  98. {
  99. return executor_;
  100. }
  101. // Get the service associated with the I/O object.
  102. service_type& get_service()
  103. {
  104. return *service_;
  105. }
  106. // Get the service associated with the I/O object.
  107. const service_type& get_service() const
  108. {
  109. return *service_;
  110. }
  111. // Get the underlying implementation of the I/O object.
  112. implementation_type& get_implementation()
  113. {
  114. return implementation_;
  115. }
  116. // Get the underlying implementation of the I/O object.
  117. const implementation_type& get_implementation() const
  118. {
  119. return implementation_;
  120. }
  121. private:
  122. // Helper function to get an executor's context.
  123. template <typename T>
  124. static execution_context& get_context(const T& t,
  125. enable_if_t<execution::is_executor<T>::value>* = 0)
  126. {
  127. return boost::asio::query(t, execution::context);
  128. }
  129. // Helper function to get an executor's context.
  130. template <typename T>
  131. static execution_context& get_context(const T& t,
  132. enable_if_t<!execution::is_executor<T>::value>* = 0)
  133. {
  134. return t.context();
  135. }
  136. // Disallow copying and copy assignment.
  137. io_object_impl(const io_object_impl&);
  138. io_object_impl& operator=(const io_object_impl&);
  139. // The service associated with the I/O object.
  140. service_type* service_;
  141. // The underlying implementation of the I/O object.
  142. implementation_type implementation_;
  143. // The associated executor.
  144. executor_type executor_;
  145. };
  146. } // namespace detail
  147. } // namespace asio
  148. } // namespace boost
  149. #include <boost/asio/detail/pop_options.hpp>
  150. #endif // BOOST_ASIO_DETAIL_IO_OBJECT_IMPL_HPP