handler_invoke_hook.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // handler_invoke_hook.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_HANDLER_INVOKE_HOOK_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. namespace asio {
  18. /** @defgroup asio_handler_invoke asio::asio_handler_invoke
  19. *
  20. * @brief (Deprecated: Use the associated_executor trait.) Default invoke
  21. * 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. #if defined(ASIO_NO_DEPRECATED)
  59. // Places in asio that would have previously called the invocation hook to
  60. // execute a handler, now call it only to check whether the result type is this
  61. // type. If the result is not this type, it indicates that the user code still
  62. // has the old hooks in place, and if so we want to trigger a compile error.
  63. enum asio_handler_invoke_is_no_longer_used {};
  64. typedef asio_handler_invoke_is_no_longer_used
  65. asio_handler_invoke_is_deprecated;
  66. #else // defined(ASIO_NO_DEPRECATED)
  67. typedef void asio_handler_invoke_is_deprecated;
  68. #endif // defined(ASIO_NO_DEPRECATED)
  69. /// Default handler invocation hook used for non-const function objects.
  70. template <typename Function>
  71. inline asio_handler_invoke_is_deprecated
  72. asio_handler_invoke(Function& function, ...)
  73. {
  74. function();
  75. #if defined(ASIO_NO_DEPRECATED)
  76. return asio_handler_invoke_is_no_longer_used();
  77. #endif // defined(ASIO_NO_DEPRECATED)
  78. }
  79. /// Default handler invocation hook used for const function objects.
  80. template <typename Function>
  81. inline asio_handler_invoke_is_deprecated
  82. asio_handler_invoke(const Function& function, ...)
  83. {
  84. Function tmp(function);
  85. tmp();
  86. #if defined(ASIO_NO_DEPRECATED)
  87. return asio_handler_invoke_is_no_longer_used();
  88. #endif // defined(ASIO_NO_DEPRECATED)
  89. }
  90. /*@}*/
  91. } // namespace asio
  92. #include "asio/detail/pop_options.hpp"
  93. #endif // ASIO_HANDLER_INVOKE_HOOK_HPP