io_object_impl.hpp 4.8 KB

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