/Src/Dependencies/Boost/boost/asio/detail/completion_handler.hpp

http://hadesmem.googlecode.com/ · C++ Header · 81 lines · 49 code · 15 blank · 17 comment · 2 complexity · c2c7f39e733a6fdab36704dbde2b8723 MD5 · raw file

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