detached.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 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. BOOST_ASIO_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) BOOST_ASIO_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. typename constraint<
  57. is_convertible<OtherExecutor, InnerExecutor>::value
  58. >::type = 0) BOOST_ASIO_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. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
  66. || defined(GENERATING_DOCUMENTATION)
  67. template <typename T>
  68. using as_default_on_t = typename T::template rebind_executor<
  69. executor_with_default<typename T::executor_type> >::other;
  70. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  71. // || defined(GENERATING_DOCUMENTATION)
  72. /// Function helper to adapt an I/O object to use @c detached_t as its
  73. /// default completion token type.
  74. template <typename T>
  75. static typename decay<T>::type::template rebind_executor<
  76. executor_with_default<typename decay<T>::type::executor_type>
  77. >::other
  78. as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
  79. {
  80. return typename decay<T>::type::template rebind_executor<
  81. executor_with_default<typename decay<T>::type::executor_type>
  82. >::other(BOOST_ASIO_MOVE_CAST(T)(object));
  83. }
  84. };
  85. /// A @ref completion_token object used to specify that an asynchronous
  86. /// operation is detached.
  87. /**
  88. * See the documentation for boost::asio::detached_t for a usage example.
  89. */
  90. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  91. constexpr detached_t detached;
  92. #elif defined(BOOST_ASIO_MSVC)
  93. __declspec(selectany) detached_t detached;
  94. #endif
  95. } // namespace asio
  96. } // namespace boost
  97. #include <boost/asio/detail/pop_options.hpp>
  98. #include <boost/asio/impl/detached.hpp>
  99. #endif // BOOST_ASIO_DETACHED_HPP