detached.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // detached.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_DETACHED_HPP
  11. #define ASIO_DETACHED_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 <memory>
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. /// A @ref completion_token type used to specify that an asynchronous operation
  21. /// is detached.
  22. /**
  23. * The detached_t class is used to indicate that an asynchronous operation is
  24. * detached. That is, there is no completion handler waiting for the
  25. * operation's result. A detached_t object may be passed as a handler to an
  26. * asynchronous operation, typically using the special value
  27. * @c asio::detached. For example:
  28. *
  29. * @code my_socket.async_send(my_buffer, asio::detached);
  30. * @endcode
  31. */
  32. class detached_t
  33. {
  34. public:
  35. /// Constructor.
  36. constexpr detached_t()
  37. {
  38. }
  39. /// Adapts an executor to add the @c detached_t completion token as the
  40. /// default.
  41. template <typename InnerExecutor>
  42. struct executor_with_default : InnerExecutor
  43. {
  44. /// Specify @c detached_t as the default completion token type.
  45. typedef detached_t default_completion_token_type;
  46. /// Construct the adapted executor from the inner executor type.
  47. executor_with_default(const InnerExecutor& ex) noexcept
  48. : InnerExecutor(ex)
  49. {
  50. }
  51. /// Convert the specified executor to the inner executor type, then use
  52. /// that to construct the adapted executor.
  53. template <typename OtherExecutor>
  54. executor_with_default(const OtherExecutor& ex,
  55. constraint_t<
  56. is_convertible<OtherExecutor, InnerExecutor>::value
  57. > = 0) noexcept
  58. : InnerExecutor(ex)
  59. {
  60. }
  61. };
  62. /// Type alias to adapt an I/O object to use @c detached_t as its
  63. /// default completion token type.
  64. template <typename T>
  65. using as_default_on_t = typename T::template rebind_executor<
  66. executor_with_default<typename T::executor_type>>::other;
  67. /// Function helper to adapt an I/O object to use @c detached_t as its
  68. /// default completion token type.
  69. template <typename T>
  70. static typename decay_t<T>::template rebind_executor<
  71. executor_with_default<typename decay_t<T>::executor_type>
  72. >::other
  73. as_default_on(T&& object)
  74. {
  75. return typename decay_t<T>::template rebind_executor<
  76. executor_with_default<typename decay_t<T>::executor_type>
  77. >::other(static_cast<T&&>(object));
  78. }
  79. };
  80. /// A @ref completion_token object used to specify that an asynchronous
  81. /// operation is detached.
  82. /**
  83. * See the documentation for asio::detached_t for a usage example.
  84. */
  85. constexpr detached_t detached;
  86. } // namespace asio
  87. #include "asio/detail/pop_options.hpp"
  88. #include "asio/impl/detached.hpp"
  89. #endif // ASIO_DETACHED_HPP