YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
when_any.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <yaclib/config.hpp>
8
9namespace yaclib {
10
11template <FailPolicy F = FailPolicy::LastFail, typename... Futures,
12 typename = std::enable_if_t<(... && is_combinator_input_v<Futures>)>>
15
16 using OutputValue = typename MaybeVariant<typename Unique<std::tuple<typename Futures::Core::Value...>>::Type>::Type;
17 using OutputError = typename head_t<Futures...>::Core::Error;
18
19 return when::When<when::Any, F, OutputValue, OutputError>(std::move(futures)...);
20}
21
23YACLIB_INLINE auto WhenAny(It begin, std::size_t count) {
24 if constexpr (is_future_base_v<T>) {
25 if (count == 1) {
26 using V = async_value_t<T>;
27 using E = async_error_t<T>;
28 return Future<V, E>{std::exchange(begin->GetCore(), nullptr)};
29 }
30 }
31
32 return when::When<when::Any, F, typename T::Core::Value, typename T::Core::Error>(begin, count);
33}
34
37 // We don't use std::distance because we want to alert the user to the fact that it can be expensive.
38 // Maybe the user has the size of the range, otherwise it is suggested to call WhenAny(begin, distance(begin, end))
39 return WhenAny<F>(begin, static_cast<std::size_t>(end - begin));
40}
41
42} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:211
YACLIB_INLINE void CheckSameError()
Definition when.hpp:20
typename detail::AsyncTypes< T >::Error async_error_t
typename detail::Head< Args... >::Type head_t
typename detail::AsyncTypes< T >::Value async_value_t
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_INLINE auto WhenAny(Futures... futures)
Definition when_any.hpp:13