overlapped_ptr.hpp 2.7 KB

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