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
9#include <algorithm>
10
11namespace yaclib {
12
13template <FailPolicy F = FailPolicy::LastFail, typename... Futures,
14 typename = std::enable_if_t<(... && is_combinator_input_v<Futures>)>>
17
18 using OutputValue = typename MaybeVariant<typename Unique<std::tuple<typename Futures::Core::Value...>>::Type>::Type;
19 using OutputTrait = typename head_t<Futures...>::Core::Trait;
20
21 return when::When<when::Any, F, OutputValue, OutputTrait>(std::move(futures)...);
22}
23
24template <FailPolicy F = FailPolicy::LastFail, typename It, typename = std::enable_if_t<is_input_iterator_v<It>>>
25YACLIB_INLINE auto WhenAny(It begin, std::size_t count) {
26 using T = typename std::iterator_traits<It>::value_type;
27
28 if constexpr (is_future_base_v<T>) {
29 if (count == 1) {
30 using V = async_value_t<T>;
31 using Trait = async_trait_t<T>;
32 return Future<V, Trait>{std::exchange(begin->GetCore(), nullptr)};
33 }
34 }
35
36 return when::When<when::Any, F, typename T::Core::Value, typename T::Core::Trait>(begin, count);
37}
38
39template <FailPolicy F = FailPolicy::LastFail, typename It, typename Sentinel,
40 typename = std::enable_if_t<is_input_range_pair_v<It, Sentinel>>>
42 static_assert(
44 "Use WhenAny(begin, std::distance(begin, end)) instead"); // We don't use std::distance because we want to alert
45 // the user to the fact that it can be expensive.
46
47 return WhenAny<F>(begin, static_cast<std::size_t>(end - begin));
48}
49
50template <FailPolicy F = FailPolicy::LastFail, typename Range, typename = std::enable_if_t<is_input_range_v<Range>>>
52 return WhenAny<F>(std::begin(range), std::end(range));
53}
54
55} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:214
YACLIB_INLINE void CheckSameTrait()
Definition when.hpp:20
Contract< V, T > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
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)
typename detail::AsyncTypes< T >::Trait async_trait_t
YACLIB_INLINE auto WhenAny(Futures... futures)
Definition when_any.hpp:15