YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
when_all.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <yaclib/config.hpp>
11
12namespace yaclib {
13
14template <typename Core, FailPolicy F>
15using ContainerElem = std::conditional_t<F == FailPolicy::FirstFail, wrap_void_t<typename Core::Value>,
16 typename Core::Trait::template Result<typename Core::Value>>;
17
18template <FailPolicy F = FailPolicy::FirstFail, typename... Futures,
19 typename = std::enable_if_t<(... && is_combinator_input_v<Futures>)>>
22
23 using Head = typename head_t<Futures...>::Core;
24 using Value = typename Head::Value;
25 using OutputTrait = typename Head::Trait;
26
27 if constexpr ((... && std::is_same_v<Value, typename Futures::Core::Value>)) {
28 if constexpr (std::is_same_v<Value, void> && F != FailPolicy::None) {
29 return when::When<when::Join, F, void, OutputTrait>(std::move(futures)...);
30 } else {
31 using OutputValue = std::vector<ContainerElem<Head, F>>;
32 return when::When<when::All, F, OutputValue, OutputTrait>(std::move(futures)...);
33 }
34 } else {
35 using OutputValue = std::tuple<ContainerElem<typename Futures::Core, F>...>;
36 return when::When<when::AllTuple, F, OutputValue, OutputTrait>(std::move(futures)...);
37 }
38}
39
40template <FailPolicy F = FailPolicy::FirstFail, typename It, typename = std::enable_if_t<is_input_iterator_v<It>>>
41YACLIB_INLINE auto WhenAll(It begin, std::size_t count) {
42 using T = typename std::iterator_traits<It>::value_type;
43 using OutputTrait = typename T::Core::Trait;
44
45 if constexpr (std::is_same_v<typename T::Core::Value, void> && F != FailPolicy::None) {
46 return when::When<when::Join, F, void, OutputTrait>(begin, count);
47 } else {
48 using OutputValue = std::vector<ContainerElem<typename T::Core, F>>;
49 return when::When<when::All, F, OutputValue, OutputTrait>(begin, count);
50 }
51}
52
53template <FailPolicy F = FailPolicy::FirstFail, typename It, typename Sentinel,
54 typename = std::enable_if_t<is_input_range_pair_v<It, Sentinel>>>
56 static_assert(
58 "Use WhenAll(begin, std::distance(begin, end)) instead"); // We don't use std::distance because we want to alert
59 // the user to the fact that it can be expensive.
60
61 return WhenAll<F>(begin, static_cast<std::size_t>(end - begin));
62}
63
64template <FailPolicy F = FailPolicy::FirstFail, typename Range, typename = std::enable_if_t<is_input_range_v<Range>>>
66 return WhenAll<F>(std::begin(range), std::end(range));
67}
68
69} // namespace yaclib
Encapsulated return value from caller.
Definition result.hpp:52
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
std::conditional_t< F==FailPolicy::FirstFail, wrap_void_t< typename Core::Value >, typename Core::Trait::template Result< typename Core::Value > > ContainerElem
Definition when_all.hpp:16
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)
YACLIB_INLINE auto WhenAll(Futures... futures)
Definition when_all.hpp:20