YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
all_tuple.hpp
Go to the documentation of this file.
1#pragma once
2
4
10
11namespace yaclib::when {
12
13template <FailPolicy F, typename OutputValue, typename Trait, typename InputCore>
14struct AllTuple {
15 static_assert(F != FailPolicy::LastFail, "LastFail policy is not supported by AllTuple");
16};
17
18template <typename OutputValue, typename Trait, typename InputCore>
19struct AllTuple<FailPolicy::None, OutputValue, Trait, InputCore> {
21
22 static constexpr ConsumePolicy kConsumePolicy = ConsumePolicy::Static;
23 static constexpr CorePolicy kCorePolicy = CorePolicy::Managed;
24
25 AllTuple(std::size_t count, PromiseType p) : _p{std::move(p)} {
26 }
27
28 template <std::size_t Index, typename R>
29 void Consume(R&& result) {
30 std::get<Index>(_tuple) = std::forward<R>(result);
31 }
32
34 std::move(_p).Set(std::move(_tuple));
35 }
36
37 private:
38 OutputValue _tuple;
39 PromiseType _p;
40};
41
42template <typename OutputValue, typename Trait, typename InputCore>
43struct AllTuple<FailPolicy::FirstFail, OutputValue, Trait, InputCore> {
45
46 static constexpr ConsumePolicy kConsumePolicy = ConsumePolicy::Static;
47 static constexpr CorePolicy kCorePolicy = CorePolicy::Managed;
48
49 AllTuple(std::size_t count, PromiseType p) : _p{std::move(p)} {
50 }
51
52 template <std::size_t Index, typename R>
53 void Consume(R&& result) {
54 if (Trait::Ok(result)) {
55 std::get<Index>(_tuple) = Trait::GetValue(std::forward<R>(result));
56 } else if (!_done.load(std::memory_order_relaxed) && !_done.exchange(true, std::memory_order_acq_rel)) {
57 std::move(_p).Set(Trait::GetError(std::forward<R>(result)));
58 }
59 // An error that lost the _done race is dropped, the promise is already set
60 }
61
63 if (_p.Valid()) {
64 std::move(_p).Set(std::move(_tuple));
65 }
66 }
67
68 private:
69 yaclib_std::atomic_bool _done = false;
70 OutputValue _tuple;
71 PromiseType _p;
72};
73
74} // namespace yaclib::when
atomic< bool > atomic_bool
Definition atomic.hpp:41
Contract< V, T > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)