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
6#include <yaclib/config.hpp>
10
11#include <cstddef>
12#include <iterator>
13#include <utility>
14
15namespace yaclib {
16
17/**
18 * Create \ref Future which will be ready when all futures are ready
19 *
20 * \tparam It type of passed iterator
21 * \tparam T type of all passed futures
22 * \param begin , count the range of futures to combine
23 * \return Future<std::vector<future_value_t<T>>, future_error_t<T>>
24 */
26 typename T = typename std::iterator_traits<It>::value_type>
27auto WhenAll(It begin, std::size_t count) {
28 static_assert(F != FailPolicy::LastFail, "TODO(Ri7ay, MBkkt) Add other policy for WhenAll");
29 static_assert(is_future_base_v<T>, "WhenAll function Iterator must be point to some Future");
30 YACLIB_WARN(count < 2, "Don't use combinators for one or zero futures");
31 using V = future_base_value_t<T>;
33 using R = std::conditional_t<F == FailPolicy::None, Result<V, E>, V>;
36 return Future{std::move(future_core)};
37}
38
39/**
40 * Create \ref Future which will be ready when all futures are ready
41 *
42 * \tparam It type of passed iterator
43 * \tparam T type of all passed futures
44 * \param begin , end the range of futures to combine
45 * \return Future<std::vector<future_value_t<T>>, future_error_t<T>>
46 */
48 typename T = typename std::iterator_traits<It>::value_type>
50 static_assert(F != FailPolicy::LastFail, "TODO(Ri7ay, MBkkt) Add other policy for WhenAll");
51 static_assert(is_future_base_v<T>, "WhenAll function Iterator must be point to some Future");
52 // We don't use std::distance because we want to alert the user to the fact that it can be expensive.
53 // Maybe the user has the size of the range, otherwise it is suggested to call WhenAll(begin, distance(begin, end))
54 return WhenAll<F, O>(begin, static_cast<std::size_t>(end - begin));
55}
56
57/**
58 * Create \ref Future which will be ready when all futures are ready
59 *
60 * \tparam V type of value all passed futures
61 * \tparam E type of error all passed futures
62 * \param futures two or more futures to combine
63 * \return Future<std::array<T>>
64 */
65template <FailPolicy F = FailPolicy::FirstFail, OrderPolicy O = OrderPolicy::Fifo, typename V, typename... E>
67 static_assert(F != FailPolicy::LastFail, "TODO(Ri7ay, MBkkt) Add other policy for WhenAll");
68 constexpr std::size_t kSize = sizeof...(E);
69 static_assert(kSize >= 2, "WhenAll wants at least two futures");
70 using Error = head_t<E...>;
71 using R = std::conditional_t<F == FailPolicy::None, Result<V, Error>, V>;
73 detail::WhenImpl(*combinator, std::move(futures)...);
74 return Future{std::move(future_core)};
75}
76
77} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:20
Provides a mechanism to access the result of async operations.
Definition future.hpp:199
static std::pair< ResultPtr, AllCombinator * > Make(std::size_t count)
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
void WhenImpl(Combinator *combinator, It it, std::size_t count) noexcept
Definition when_impl.hpp:13
typename detail::FutureBaseTypes< T >::Value future_base_value_t
typename detail::Head< Args... >::Type head_t
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)
auto WhenAll(It begin, std::size_t count)
Create Future which will be ready when all futures are ready.
Definition when_all.hpp:27
typename detail::FutureBaseTypes< T >::Error future_base_error_t
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
OrderPolicy
This Policy describe how algorithm produce result.