overlapped_ptr.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // windows/overlapped_ptr.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 Handler>
  45. explicit overlapped_ptr(boost::asio::io_context& io_context,
  46. BOOST_ASIO_MOVE_ARG(Handler) handler)
  47. : impl_(io_context, BOOST_ASIO_MOVE_CAST(Handler)(handler))
  48. {
  49. }
  50. /// Destructor automatically frees the OVERLAPPED object unless released.
  51. ~overlapped_ptr()
  52. {
  53. }
  54. /// Reset to empty.
  55. void reset()
  56. {
  57. impl_.reset();
  58. }
  59. /// Reset to contain the specified handler, freeing any current OVERLAPPED
  60. /// object.
  61. template <typename Handler>
  62. void reset(boost::asio::io_context& io_context,
  63. BOOST_ASIO_MOVE_ARG(Handler) handler)
  64. {
  65. impl_.reset(io_context, BOOST_ASIO_MOVE_CAST(Handler)(handler));
  66. }
  67. /// Get the contained OVERLAPPED object.
  68. OVERLAPPED* get()
  69. {
  70. return impl_.get();
  71. }
  72. /// Get the contained OVERLAPPED object.
  73. const OVERLAPPED* get() const
  74. {
  75. return impl_.get();
  76. }
  77. /// Release ownership of the OVERLAPPED object.
  78. OVERLAPPED* release()
  79. {
  80. return impl_.release();
  81. }
  82. /// Post completion notification for overlapped operation. Releases ownership.
  83. void complete(const boost::system::error_code& ec,
  84. std::size_t bytes_transferred)
  85. {
  86. impl_.complete(ec, bytes_transferred);
  87. }
  88. private:
  89. detail::win_iocp_overlapped_ptr impl_;
  90. };
  91. } // namespace windows
  92. } // namespace asio
  93. } // namespace boost
  94. #include <boost/asio/detail/pop_options.hpp>
  95. #endif // defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
  96. // || defined(GENERATING_DOCUMENTATION)
  97. #endif // BOOST_ASIO_WINDOWS_OVERLAPPED_PTR_HPP