overlapped_ptr.hpp 4.0 KB

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