cancellation_condition.hpp 3.7 KB

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