overlapped_ptr.hpp 2.8 KB

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