handler_invoke_hook.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // handler_invoke_hook.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_HANDLER_INVOKE_HOOK_HPP
  11. #define BOOST_ASIO_HANDLER_INVOKE_HOOK_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/push_options.hpp>
  17. namespace boost {
  18. namespace asio {
  19. /** @defgroup asio_handler_invoke boost::asio::asio_handler_invoke
  20. *
  21. * @brief Default invoke function for handlers.
  22. *
  23. * Completion handlers for asynchronous operations are invoked by the
  24. * io_context associated with the corresponding object (e.g. a socket or
  25. * deadline_timer). Certain guarantees are made on when the handler may be
  26. * invoked, in particular that a handler can only be invoked from a thread that
  27. * is currently calling @c run() on the corresponding io_context object.
  28. * Handlers may subsequently be invoked through other objects (such as
  29. * io_context::strand objects) that provide additional guarantees.
  30. *
  31. * When asynchronous operations are composed from other asynchronous
  32. * operations, all intermediate handlers should be invoked using the same
  33. * method as the final handler. This is required to ensure that user-defined
  34. * objects are not accessed in a way that may violate the guarantees. This
  35. * hooking function ensures that the invoked method used for the final handler
  36. * is accessible at each intermediate step.
  37. *
  38. * Implement asio_handler_invoke for your own handlers to specify a custom
  39. * invocation strategy.
  40. *
  41. * This default implementation invokes the function object like so:
  42. * @code function(); @endcode
  43. * If necessary, the default implementation makes a copy of the function object
  44. * so that the non-const operator() can be used.
  45. *
  46. * @par Example
  47. * @code
  48. * class my_handler;
  49. *
  50. * template <typename Function>
  51. * void asio_handler_invoke(Function function, my_handler* context)
  52. * {
  53. * context->strand_.dispatch(function);
  54. * }
  55. * @endcode
  56. */
  57. /*@{*/
  58. /// Default handler invocation hook used for non-const function objects.
  59. template <typename Function>
  60. inline void asio_handler_invoke(Function& function, ...)
  61. {
  62. function();
  63. }
  64. /// Default handler invocation hook used for const function objects.
  65. template <typename Function>
  66. inline void asio_handler_invoke(const Function& function, ...)
  67. {
  68. Function tmp(function);
  69. tmp();
  70. }
  71. /*@}*/
  72. } // namespace asio
  73. } // namespace boost
  74. #include <boost/asio/detail/pop_options.hpp>
  75. #endif // BOOST_ASIO_HANDLER_INVOKE_HOOK_HPP