io_object_impl.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // io_object_impl.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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. #if defined(ASIO_HAS_MOVE)
  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 a converting move-construction of an I/O object.
  61. template <typename IoObjectService1, typename Executor1>
  62. io_object_impl(io_object_impl<IoObjectService1, Executor1>&& other)
  63. : service_(&asio::use_service<IoObjectService>(
  64. io_object_impl::get_context(other.get_executor()))),
  65. executor_(other.get_executor())
  66. {
  67. service_->converting_move_construct(implementation_,
  68. other.get_service(), other.get_implementation());
  69. }
  70. #endif // defined(ASIO_HAS_MOVE)
  71. // Destructor.
  72. ~io_object_impl()
  73. {
  74. service_->destroy(implementation_);
  75. }
  76. #if defined(ASIO_HAS_MOVE)
  77. // Move-assign an I/O object.
  78. io_object_impl& operator=(io_object_impl&& other)
  79. {
  80. if (this != &other)
  81. {
  82. service_->move_assign(implementation_,
  83. *other.service_, other.implementation_);
  84. executor_.~executor_type();
  85. new (&executor_) executor_type(other.executor_);
  86. service_ = other.service_;
  87. }
  88. return *this;
  89. }
  90. #endif // defined(ASIO_HAS_MOVE)
  91. // Get the executor associated with the object.
  92. const executor_type& get_executor() ASIO_NOEXCEPT
  93. {
  94. return executor_;
  95. }
  96. // Get the service associated with the I/O object.
  97. service_type& get_service()
  98. {
  99. return *service_;
  100. }
  101. // Get the service associated with the I/O object.
  102. const service_type& get_service() const
  103. {
  104. return *service_;
  105. }
  106. // Get the underlying implementation of the I/O object.
  107. implementation_type& get_implementation()
  108. {
  109. return implementation_;
  110. }
  111. // Get the underlying implementation of the I/O object.
  112. const implementation_type& get_implementation() const
  113. {
  114. return implementation_;
  115. }
  116. private:
  117. // Helper function to get an executor's context.
  118. template <typename T>
  119. static execution_context& get_context(const T& t,
  120. typename enable_if<execution::is_executor<T>::value>::type* = 0)
  121. {
  122. return asio::query(t, execution::context);
  123. }
  124. // Helper function to get an executor's context.
  125. template <typename T>
  126. static execution_context& get_context(const T& t,
  127. typename enable_if<!execution::is_executor<T>::value>::type* = 0)
  128. {
  129. return t.context();
  130. }
  131. // Disallow copying and copy assignment.
  132. io_object_impl(const io_object_impl&);
  133. io_object_impl& operator=(const io_object_impl&);
  134. // The service associated with the I/O object.
  135. service_type* service_;
  136. // The underlying implementation of the I/O object.
  137. implementation_type implementation_;
  138. // The associated executor.
  139. executor_type executor_;
  140. };
  141. } // namespace detail
  142. } // namespace asio
  143. #include "asio/detail/pop_options.hpp"
  144. #endif // ASIO_DETAIL_IO_OBJECT_IMPL_HPP