cancellation_condition.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // experimental/cancellation_condition.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_EXPERIMENTAL_CANCELLATION_CONDITION_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_CANCELLATION_CONDITION_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 <exception>
  17. #include <boost/asio/cancellation_type.hpp>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/asio/detail/type_traits.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace experimental {
  24. /// Wait for all operations to complete.
  25. class wait_for_all
  26. {
  27. public:
  28. template <typename... Args>
  29. BOOST_ASIO_CONSTEXPR cancellation_type_t operator()(
  30. Args&&...) const BOOST_ASIO_NOEXCEPT
  31. {
  32. return cancellation_type::none;
  33. }
  34. };
  35. /// Wait until an operation completes, then cancel the others.
  36. class wait_for_one
  37. {
  38. public:
  39. BOOST_ASIO_CONSTEXPR explicit wait_for_one(
  40. cancellation_type_t cancel_type = cancellation_type::all)
  41. : cancel_type_(cancel_type)
  42. {
  43. }
  44. template <typename... Args>
  45. BOOST_ASIO_CONSTEXPR cancellation_type_t operator()(
  46. Args&&...) const BOOST_ASIO_NOEXCEPT
  47. {
  48. return cancel_type_;
  49. }
  50. private:
  51. cancellation_type_t cancel_type_;
  52. };
  53. /// Wait until an operation completes without an error, then cancel the others.
  54. /**
  55. * If no operation completes without an error, waits for completion of all
  56. * operations.
  57. */
  58. class wait_for_one_success
  59. {
  60. public:
  61. BOOST_ASIO_CONSTEXPR explicit wait_for_one_success(
  62. cancellation_type_t cancel_type = cancellation_type::all)
  63. : cancel_type_(cancel_type)
  64. {
  65. }
  66. BOOST_ASIO_CONSTEXPR cancellation_type_t
  67. operator()() const BOOST_ASIO_NOEXCEPT
  68. {
  69. return cancel_type_;
  70. }
  71. template <typename E, typename... Args>
  72. BOOST_ASIO_CONSTEXPR typename constraint<
  73. !is_same<typename decay<E>::type, boost::system::error_code>::value
  74. && !is_same<typename decay<E>::type, std::exception_ptr>::value,
  75. cancellation_type_t
  76. >::type operator()(const E&, Args&&...) const BOOST_ASIO_NOEXCEPT
  77. {
  78. return cancel_type_;
  79. }
  80. template <typename E, typename... Args>
  81. BOOST_ASIO_CONSTEXPR typename constraint<
  82. is_same<typename decay<E>::type, boost::system::error_code>::value
  83. || is_same<typename decay<E>::type, std::exception_ptr>::value,
  84. cancellation_type_t
  85. >::type operator()(const E& e, Args&&...) const BOOST_ASIO_NOEXCEPT
  86. {
  87. return !!e ? cancellation_type::none : cancel_type_;
  88. }
  89. private:
  90. cancellation_type_t cancel_type_;
  91. };
  92. /// Wait until an operation completes with an error, then cancel the others.
  93. /**
  94. * If no operation completes with an error, waits for completion of all
  95. * operations.
  96. */
  97. class wait_for_one_error
  98. {
  99. public:
  100. BOOST_ASIO_CONSTEXPR explicit wait_for_one_error(
  101. cancellation_type_t cancel_type = cancellation_type::all)
  102. : cancel_type_(cancel_type)
  103. {
  104. }
  105. BOOST_ASIO_CONSTEXPR cancellation_type_t
  106. operator()() const BOOST_ASIO_NOEXCEPT
  107. {
  108. return cancellation_type::none;
  109. }
  110. template <typename E, typename... Args>
  111. BOOST_ASIO_CONSTEXPR typename constraint<
  112. !is_same<typename decay<E>::type, boost::system::error_code>::value
  113. && !is_same<typename decay<E>::type, std::exception_ptr>::value,
  114. cancellation_type_t
  115. >::type operator()(const E&, Args&&...) const BOOST_ASIO_NOEXCEPT
  116. {
  117. return cancellation_type::none;
  118. }
  119. template <typename E, typename... Args>
  120. BOOST_ASIO_CONSTEXPR typename constraint<
  121. is_same<typename decay<E>::type, boost::system::error_code>::value
  122. || is_same<typename decay<E>::type, std::exception_ptr>::value,
  123. cancellation_type_t
  124. >::type operator()(const E& e, Args&&...) const BOOST_ASIO_NOEXCEPT
  125. {
  126. return !!e ? cancel_type_ : cancellation_type::none;
  127. }
  128. private:
  129. cancellation_type_t cancel_type_;
  130. };
  131. } // namespace experimental
  132. } // namespace asio
  133. } // namespace boost
  134. #include <boost/asio/detail/pop_options.hpp>
  135. #endif // BOOST_ASIO_EXPERIMENTAL_CANCELLATION_CONDITION_HPP