wait_handler.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // detail/wait_handler.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_DETAIL_WAIT_HANDLER_HPP
  11. #define ASIO_DETAIL_WAIT_HANDLER_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. #include "asio/detail/fenced_block.hpp"
  17. #include "asio/detail/handler_alloc_helpers.hpp"
  18. #include "asio/detail/handler_invoke_helpers.hpp"
  19. #include "asio/detail/timer_op.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. template <typename Handler>
  24. class wait_handler : public timer_op
  25. {
  26. public:
  27. ASIO_DEFINE_HANDLER_PTR(wait_handler);
  28. wait_handler(Handler h)
  29. : timer_op(&wait_handler::do_complete),
  30. handler_(h)
  31. {
  32. }
  33. static void do_complete(io_service_impl* owner, operation* base,
  34. asio::error_code /*ec*/, std::size_t /*bytes_transferred*/)
  35. {
  36. // Take ownership of the handler object.
  37. wait_handler* h(static_cast<wait_handler*>(base));
  38. ptr p = { boost::addressof(h->handler_), h, h };
  39. // Make a copy of the handler so that the memory can be deallocated before
  40. // the upcall is made. Even if we're not about to make an upcall, a
  41. // sub-object of the handler may be the true owner of the memory associated
  42. // with the handler. Consequently, a local copy of the handler is required
  43. // to ensure that any owning sub-object remains valid until after we have
  44. // deallocated the memory here.
  45. detail::binder1<Handler, asio::error_code>
  46. handler(h->handler_, h->ec_);
  47. p.h = boost::addressof(handler.handler_);
  48. p.reset();
  49. // Make the upcall if required.
  50. if (owner)
  51. {
  52. asio::detail::fenced_block b;
  53. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  54. }
  55. }
  56. private:
  57. Handler handler_;
  58. };
  59. } // namespace detail
  60. } // namespace asio
  61. #include "asio/detail/pop_options.hpp"
  62. #endif // ASIO_DETAIL_WAIT_HANDLER_HPP