cancellation_condition.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // experimental/cancellation_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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 <boost/asio/cancellation_type.hpp>
  17. #include <boost/asio/disposition.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  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<!is_disposition<E>::value, cancellation_type_t>
  70. operator()(const E&, Args&&...) const noexcept
  71. {
  72. return cancel_type_;
  73. }
  74. template <typename E, typename... Args>
  75. constexpr constraint_t<is_disposition<E>::value, cancellation_type_t>
  76. operator()(const E& e, Args&&...) const noexcept
  77. {
  78. return e != no_error ? cancellation_type::none : cancel_type_;
  79. }
  80. private:
  81. cancellation_type_t cancel_type_;
  82. };
  83. /// Wait until an operation completes with an error, then cancel the others.
  84. /**
  85. * If no operation completes with an error, waits for completion of all
  86. * operations.
  87. */
  88. class wait_for_one_error
  89. {
  90. public:
  91. constexpr explicit wait_for_one_error(
  92. cancellation_type_t cancel_type = cancellation_type::all)
  93. : cancel_type_(cancel_type)
  94. {
  95. }
  96. constexpr cancellation_type_t operator()() const noexcept
  97. {
  98. return cancellation_type::none;
  99. }
  100. template <typename E, typename... Args>
  101. constexpr constraint_t<!is_disposition<E>::value, cancellation_type_t>
  102. operator()(const E&, Args&&...) const noexcept
  103. {
  104. return cancellation_type::none;
  105. }
  106. template <typename E, typename... Args>
  107. constexpr constraint_t<is_disposition<E>::value, cancellation_type_t>
  108. operator()(const E& e, Args&&...) const noexcept
  109. {
  110. return e != no_error ? cancel_type_ : cancellation_type::none;
  111. }
  112. private:
  113. cancellation_type_t cancel_type_;
  114. };
  115. } // namespace experimental
  116. } // namespace asio
  117. } // namespace boost
  118. #include <boost/asio/detail/pop_options.hpp>
  119. #endif // BOOST_ASIO_EXPERIMENTAL_CANCELLATION_CONDITION_HPP