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
6#include <yaclib/config.hpp>
9
10#include <cstddef>
11#include <iterator>
12#include <utility>
13
14namespace yaclib {
15
16/**
17 * \brief Create \ref Future that is ready when any of futures is ready
18 *
19 * \tparam P policy WhenAny errors
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<T>
24 */
26auto WhenAny(It begin, std::size_t count) {
27 static_assert(is_future_base_v<T>, "WhenAny function Iterator must be point to some Future");
28 using V = future_base_value_t<T>;
30 YACLIB_WARN(count < 2, "Don't use combinators for zero or one futures");
31 if (count == 0) {
32 return Future<V, E>{};
33 }
34 if (count == 1) {
35 return Future<V, E>{std::exchange(begin->GetCore(), nullptr)};
36 }
39 return Future{std::move(future_core)};
40}
41
42/**
43 * \brief Create \ref Future that is ready when any of futures is ready
44 *
45 * \tparam P policy WhenAny errors
46 * \tparam It type of passed iterator
47 * \tparam T type of all passed futures
48 * \param begin , end the range of futures to combine
49 * \return Future<T>
50 */
53 static_assert(is_future_base_v<T>, "WhenAny function Iterator must be point to some Future");
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 WhenAny<P>(begin, static_cast<std::size_t>(end - begin));
57}
58
59/**
60 * \brief Create \ref Future that is ready when any of futures is ready
61 *
62 * \tparam P policy WhenAny errors
63 * \tparam V type of value all passed futures
64 * \tparam E type of error all passed futures
65 * \param futures two or more futures to combine
66 * \return Future<T>
67 */
68template <FailPolicy P = FailPolicy::LastFail, typename V, typename... E>
70 constexpr std::size_t kSize = sizeof...(E);
71 static_assert(kSize >= 2, "WhenAny wants at least two futures");
72 auto [future_core, combinator] = detail::AnyCombinator<V, head_t<E...>, P>::Make(kSize);
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 auto 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)
typename detail::FutureBaseTypes< T >::Error future_base_error_t
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
auto WhenAny(It begin, std::size_t count)
Create Future that is ready when any of futures is ready.
Definition when_any.hpp:26