cancellation_condition.hpp 4.0 KB

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