overlapped_ptr.hpp 3.0 KB

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