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