cancellation_condition.hpp 3.9 KB

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