detached.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // detached.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_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. /// Class used to specify that an asynchronous operation is detached.
  21. /**
  22. * The detached_t class is used to indicate that an asynchronous operation is
  23. * detached. That is, there is no completion handler waiting for the
  24. * operation's result. A detached_t object may be passed as a handler to an
  25. * asynchronous operation, typically using the special value
  26. * @c asio::detached. For example:
  27. * @code my_socket.async_send(my_buffer, asio::detached);
  28. * @endcode
  29. */
  30. class detached_t
  31. {
  32. public:
  33. /// Constructor.
  34. ASIO_CONSTEXPR detached_t()
  35. {
  36. }
  37. /// Adapts an executor to add the @c detached_t completion token as the
  38. /// default.
  39. template <typename InnerExecutor>
  40. struct executor_with_default : InnerExecutor
  41. {
  42. /// Specify @c detached_t as the default completion token type.
  43. typedef detached_t default_completion_token_type;
  44. /// Construct the adapted executor from the inner executor type.
  45. executor_with_default(const InnerExecutor& ex) ASIO_NOEXCEPT
  46. : InnerExecutor(ex)
  47. {
  48. }
  49. /// Convert the specified executor to the inner executor type, then use
  50. /// that to construct the adapted executor.
  51. template <typename OtherExecutor>
  52. executor_with_default(const OtherExecutor& ex,
  53. typename constraint<
  54. is_convertible<OtherExecutor, InnerExecutor>::value
  55. >::type = 0) ASIO_NOEXCEPT
  56. : InnerExecutor(ex)
  57. {
  58. }
  59. };
  60. /// Type alias to adapt an I/O object to use @c detached_t as its
  61. /// default completion token type.
  62. #if defined(ASIO_HAS_ALIAS_TEMPLATES) \
  63. || defined(GENERATING_DOCUMENTATION)
  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. #endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
  68. // || defined(GENERATING_DOCUMENTATION)
  69. /// Function helper to adapt an I/O object to use @c detached_t as its
  70. /// default completion token type.
  71. template <typename T>
  72. static typename decay<T>::type::template rebind_executor<
  73. executor_with_default<typename decay<T>::type::executor_type>
  74. >::other
  75. as_default_on(ASIO_MOVE_ARG(T) object)
  76. {
  77. return typename decay<T>::type::template rebind_executor<
  78. executor_with_default<typename decay<T>::type::executor_type>
  79. >::other(ASIO_MOVE_CAST(T)(object));
  80. }
  81. };
  82. /// A special value, similar to std::nothrow.
  83. /**
  84. * See the documentation for asio::detached_t for a usage example.
  85. */
  86. #if defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  87. constexpr detached_t detached;
  88. #elif defined(ASIO_MSVC)
  89. __declspec(selectany) detached_t detached;
  90. #endif
  91. } // namespace asio
  92. #include "asio/detail/pop_options.hpp"
  93. #include "asio/impl/detached.hpp"
  94. #endif // ASIO_DETACHED_HPP