handler_invoke_hook.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // handler_invoke_hook.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 (Deprecated: Use the associated_executor trait.) Default invoke
  22. * function for handlers.
  23. *
  24. * Completion handlers for asynchronous operations are invoked by the
  25. * io_context associated with the corresponding object (e.g. a socket or
  26. * deadline_timer). Certain guarantees are made on when the handler may be
  27. * invoked, in particular that a handler can only be invoked from a thread that
  28. * is currently calling @c run() on the corresponding io_context object.
  29. * Handlers may subsequently be invoked through other objects (such as
  30. * io_context::strand objects) that provide additional guarantees.
  31. *
  32. * When asynchronous operations are composed from other asynchronous
  33. * operations, all intermediate handlers should be invoked using the same
  34. * method as the final handler. This is required to ensure that user-defined
  35. * objects are not accessed in a way that may violate the guarantees. This
  36. * hooking function ensures that the invoked method used for the final handler
  37. * is accessible at each intermediate step.
  38. *
  39. * Implement asio_handler_invoke for your own handlers to specify a custom
  40. * invocation strategy.
  41. *
  42. * This default implementation invokes the function object like so:
  43. * @code function(); @endcode
  44. * If necessary, the default implementation makes a copy of the function object
  45. * so that the non-const operator() can be used.
  46. *
  47. * @par Example
  48. * @code
  49. * class my_handler;
  50. *
  51. * template <typename Function>
  52. * void asio_handler_invoke(Function function, my_handler* context)
  53. * {
  54. * context->strand_.dispatch(function);
  55. * }
  56. * @endcode
  57. */
  58. /*@{*/
  59. #if defined(BOOST_ASIO_NO_DEPRECATED)
  60. // Places in asio that would have previously called the invocation hook to
  61. // execute a handler, now call it only to check whether the result type is this
  62. // type. If the result is not this type, it indicates that the user code still
  63. // has the old hooks in place, and if so we want to trigger a compile error.
  64. enum asio_handler_invoke_is_no_longer_used {};
  65. typedef asio_handler_invoke_is_no_longer_used
  66. asio_handler_invoke_is_deprecated;
  67. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  68. typedef void asio_handler_invoke_is_deprecated;
  69. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  70. /// Default handler invocation hook used for non-const function objects.
  71. template <typename Function>
  72. inline asio_handler_invoke_is_deprecated
  73. asio_handler_invoke(Function& function, ...)
  74. {
  75. function();
  76. #if defined(BOOST_ASIO_NO_DEPRECATED)
  77. return asio_handler_invoke_is_no_longer_used();
  78. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  79. }
  80. /// Default handler invocation hook used for const function objects.
  81. template <typename Function>
  82. inline asio_handler_invoke_is_deprecated
  83. asio_handler_invoke(const Function& function, ...)
  84. {
  85. Function tmp(function);
  86. tmp();
  87. #if defined(BOOST_ASIO_NO_DEPRECATED)
  88. return asio_handler_invoke_is_no_longer_used();
  89. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  90. }
  91. /*@}*/
  92. } // namespace asio
  93. } // namespace boost
  94. #include <boost/asio/detail/pop_options.hpp>
  95. #endif // BOOST_ASIO_HANDLER_INVOKE_HOOK_HPP