overlapped_ptr.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // windows/overlapped_ptr.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_WINDOWS_OVERLAPPED_PTR_HPP
  11. #define ASIO_WINDOWS_OVERLAPPED_PTR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_WINDOWS_OVERLAPPED_PTR) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include "asio/detail/noncopyable.hpp"
  19. #include "asio/detail/win_iocp_overlapped_ptr.hpp"
  20. #include "asio/io_context.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace windows {
  24. /// Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
  25. /**
  26. * A special-purpose smart pointer used to wrap an application handler so that
  27. * it can be passed as the LPOVERLAPPED argument to overlapped I/O functions.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. */
  33. class overlapped_ptr
  34. : private noncopyable
  35. {
  36. public:
  37. /// Construct an empty overlapped_ptr.
  38. overlapped_ptr()
  39. : impl_()
  40. {
  41. }
  42. /// Construct an overlapped_ptr to contain the specified handler.
  43. template <typename ExecutionContext, typename Handler>
  44. explicit overlapped_ptr(ExecutionContext& context,
  45. Handler&& handler,
  46. constraint_t<
  47. is_convertible<ExecutionContext&, execution_context&>::value
  48. > = 0)
  49. : impl_(context.get_executor(), static_cast<Handler&&>(handler))
  50. {
  51. }
  52. /// Construct an overlapped_ptr to contain the specified handler.
  53. template <typename Executor, typename Handler>
  54. explicit overlapped_ptr(const Executor& ex,
  55. Handler&& handler,
  56. constraint_t<
  57. execution::is_executor<Executor>::value
  58. || is_executor<Executor>::value
  59. > = 0)
  60. : impl_(ex, static_cast<Handler&&>(handler))
  61. {
  62. }
  63. /// Destructor automatically frees the OVERLAPPED object unless released.
  64. ~overlapped_ptr()
  65. {
  66. }
  67. /// Reset to empty.
  68. void reset()
  69. {
  70. impl_.reset();
  71. }
  72. /// Reset to contain the specified handler, freeing any current OVERLAPPED
  73. /// object.
  74. template <typename ExecutionContext, typename Handler>
  75. void reset(ExecutionContext& context, Handler&& handler,
  76. constraint_t<
  77. is_convertible<ExecutionContext&, execution_context&>::value
  78. > = 0)
  79. {
  80. impl_.reset(context.get_executor(), static_cast<Handler&&>(handler));
  81. }
  82. /// Reset to contain the specified handler, freeing any current OVERLAPPED
  83. /// object.
  84. template <typename Executor, typename Handler>
  85. void reset(const Executor& ex, Handler&& handler,
  86. constraint_t<
  87. execution::is_executor<Executor>::value
  88. || is_executor<Executor>::value
  89. > = 0)
  90. {
  91. impl_.reset(ex, static_cast<Handler&&>(handler));
  92. }
  93. /// Get the contained OVERLAPPED object.
  94. OVERLAPPED* get()
  95. {
  96. return impl_.get();
  97. }
  98. /// Get the contained OVERLAPPED object.
  99. const OVERLAPPED* get() const
  100. {
  101. return impl_.get();
  102. }
  103. /// Release ownership of the OVERLAPPED object.
  104. OVERLAPPED* release()
  105. {
  106. return impl_.release();
  107. }
  108. /// Post completion notification for overlapped operation. Releases ownership.
  109. void complete(const asio::error_code& ec,
  110. std::size_t bytes_transferred)
  111. {
  112. impl_.complete(ec, bytes_transferred);
  113. }
  114. private:
  115. detail::win_iocp_overlapped_ptr impl_;
  116. };
  117. } // namespace windows
  118. } // namespace asio
  119. #include "asio/detail/pop_options.hpp"
  120. #endif // defined(ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
  121. // || defined(GENERATING_DOCUMENTATION)
  122. #endif // ASIO_WINDOWS_OVERLAPPED_PTR_HPP