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>,
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 OutputError = typename Head::Error;
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, OutputError>(std::move(futures)...);
30 } else {
31 using OutputValue = std::vector<ContainerElem<Head, F>>;
32 return when::When<when::All, F, OutputValue, OutputError>(std::move(futures)...);
33 }
34 } else {
35 using OutputValue = std::tuple<ContainerElem<typename Futures::Core, F>...>;
36 return when::When<when::AllTuple, F, OutputValue, OutputError>(std::move(futures)...);
37 }
38}
39
41YACLIB_INLINE auto WhenAll(It begin, std::size_t count) {
42 using OutputError = typename T::Core::Error;
43
44 if constexpr (std::is_same_v<typename T::Core::Value, void> && F != FailPolicy::None) {
45 return when::When<when::Join, F, void, OutputError>(begin, count);
46 } else {
47 using OutputValue = std::vector<ContainerElem<typename T::Core, F>>;
48 return when::When<when::All, F, OutputValue, OutputError>(begin, count);
49 }
50}
51
54 // We don't use std::distance because we want to alert the user to the fact that it can be expensive.
55 // Maybe the user has the size of the range, otherwise it is suggested to call WhenAny(begin, distance(begin, end))
56 return WhenAll<F>(begin, static_cast<std::size_t>(end - begin));
57}
58
59} // namespace yaclib
Encapsulated return value from caller.
Definition result.hpp:90
YACLIB_INLINE void CheckSameError()
Definition when.hpp:20
typename detail::Head< Args... >::Type head_t
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
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
std::conditional_t< F==FailPolicy::FirstFail, wrap_void_t< typename Core::Value >, Result< typename Core::Value, typename Core::Error > > ContainerElem
Definition when_all.hpp:16