detached.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // detached.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_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. /// Class used to specify that an asynchronous operation 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 boost::asio::detached. For example:
  28. * @code my_socket.async_send(my_buffer, boost::asio::detached);
  29. * @endcode
  30. */
  31. class detached_t
  32. {
  33. public:
  34. /// Constructor.
  35. BOOST_ASIO_CONSTEXPR detached_t()
  36. {
  37. }
  38. /// Adapts an executor to add the @c detached_t completion token as the
  39. /// default.
  40. template <typename InnerExecutor>
  41. struct executor_with_default : InnerExecutor
  42. {
  43. /// Specify @c detached_t as the default completion token type.
  44. typedef detached_t default_completion_token_type;
  45. /// Construct the adapted executor from the inner executor type.
  46. executor_with_default(const InnerExecutor& ex) BOOST_ASIO_NOEXCEPT
  47. : InnerExecutor(ex)
  48. {
  49. }
  50. /// Convert the specified executor to the inner executor type, then use
  51. /// that to construct the adapted executor.
  52. template <typename OtherExecutor>
  53. executor_with_default(const OtherExecutor& ex,
  54. typename constraint<
  55. is_convertible<OtherExecutor, InnerExecutor>::value
  56. >::type = 0) BOOST_ASIO_NOEXCEPT
  57. : InnerExecutor(ex)
  58. {
  59. }
  60. };
  61. /// Type alias to adapt an I/O object to use @c detached_t as its
  62. /// default completion token type.
  63. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
  64. || defined(GENERATING_DOCUMENTATION)
  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. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  69. // || defined(GENERATING_DOCUMENTATION)
  70. /// Function helper to adapt an I/O object to use @c detached_t as its
  71. /// default completion token type.
  72. template <typename T>
  73. static typename decay<T>::type::template rebind_executor<
  74. executor_with_default<typename decay<T>::type::executor_type>
  75. >::other
  76. as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
  77. {
  78. return typename decay<T>::type::template rebind_executor<
  79. executor_with_default<typename decay<T>::type::executor_type>
  80. >::other(BOOST_ASIO_MOVE_CAST(T)(object));
  81. }
  82. };
  83. /// A special value, similar to std::nothrow.
  84. /**
  85. * See the documentation for boost::asio::detached_t for a usage example.
  86. */
  87. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  88. constexpr detached_t detached;
  89. #elif defined(BOOST_ASIO_MSVC)
  90. __declspec(selectany) detached_t detached;
  91. #endif
  92. } // namespace asio
  93. } // namespace boost
  94. #include <boost/asio/detail/pop_options.hpp>
  95. #include <boost/asio/impl/detached.hpp>
  96. #endif // BOOST_ASIO_DETACHED_HPP