detached.hpp 3.1 KB

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