YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
shared_future.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <yaclib/fwd.hpp>
10
11namespace yaclib {
12
13template <typename V, typename E>
16
17 public:
18 static_assert(Check<V>(), "V should be valid");
19 static_assert(Check<E>(), "E should be valid");
20 static_assert(!std::is_same_v<V, E>, "SharedFuture cannot be instantiated with same V and E, because it's ambiguous");
21 static_assert(std::is_copy_constructible_v<Result<V, E>>, "Result should be copyable");
22
23 SharedFutureBase() = default;
24
26 return _core != nullptr;
27 }
28
31 return !_core->Empty();
32 }
33
34 [[nodiscard]] Result<V, E> Get() && noexcept {
36 Wait(*this);
37 if (_core->GetRef() == 1) {
38 return std::move(_core->Get());
39 } else {
40 return _core->Get();
41 }
42 }
43
44 void Get() const&& = delete;
45
48 Wait(*this);
49 return _core->Get();
50 }
51
52 [[nodiscard]] Result<V, E> Touch() && noexcept {
55 if (_core->GetRef() == 1) {
56 return std::move(_core->Get());
57 } else {
58 return _core->Get();
59 }
60 }
61
62 void Touch() const&& = delete;
63
67 return _core->Get();
68 }
69
70 template <typename Func>
71 [[nodiscard]] /*FutureOn*/ auto Then(IExecutor& e, Func&& f) const {
73 "better way is use ThenInline(...) instead of Then(MakeInline(), ...)");
74 static constexpr auto CoreT = CoreType::ToUnique | CoreType::Call;
75 return detail::SetCallback<CoreT, true>(_core, &e, std::forward<Func>(f));
76 }
77
78 void Detach() && noexcept {
79 _core = nullptr;
80 }
81
82 template <typename Func>
83 void SubscribeInline(Func&& f) const {
84 static constexpr auto CoreT = CoreType::Detach;
85 detail::SetCallback<CoreT, false>(_core, nullptr, std::forward<Func>(f));
86 }
87
88 template <typename Func>
89 void Subscribe(IExecutor& e, Func&& f) const {
91 "better way is use SubscribeInline(...) instead of Subscribe(MakeInline(), ...)");
92 static constexpr auto CoreT = CoreType::Detach | CoreType::Call;
93 detail::SetCallback<CoreT, true>(_core, &e, std::forward<Func>(f));
94 }
95
99
103
105
109
110 protected:
111 explicit SharedFutureBase(detail::SharedCorePtr<V, E> core) noexcept : _core{std::move(core)} {
112 }
113
115};
116
117extern template class SharedFutureBase<void, StopError>;
118
119template <typename V, typename E>
123
124 public:
125 using Base::Base;
126
128 }
129
130 template <typename Func>
131 [[nodiscard]] /*Future*/ auto ThenInline(Func&& f) const {
132 static constexpr auto CoreT = CoreType::ToUnique;
133 return detail::SetCallback<CoreT, false>(this->_core, nullptr, std::forward<Func>(f));
134 }
135};
136
137extern template class SharedFuture<>;
138
139template <typename V, typename E>
143
144 public:
145 using Base::Base;
146 using Base::Detach;
147 using Base::Then;
148
150 }
151
152 [[nodiscard]] SharedFuture<V, E> On(std::nullptr_t) && noexcept {
153 return {std::move(this->_core)};
154 }
155
156 template <typename Func>
157 [[nodiscard]] /*FutureOn*/ auto ThenInline(Func&& f) const {
158 static constexpr auto CoreT = CoreType::ToUnique;
159 return detail::SetCallback<CoreT, true>(this->_core, nullptr, std::forward<Func>(f));
160 }
161
162 template <typename Func>
163 [[nodiscard]] /*FutureOn*/ auto Then(Func&& f) const {
164 static constexpr auto CoreT = CoreType::ToUnique | CoreType::Call;
165 return detail::SetCallback<CoreT, true>(this->_core, nullptr, std::forward<Func>(f));
166 }
167
168 template <typename Func>
169 void Subscribe(Func&& f) const {
170 static constexpr auto CoreT = CoreType::Detach | CoreType::Call;
171 detail::SetCallback<CoreT, false>(this->_core, nullptr, std::forward<Func>(f));
172 }
173};
174
175extern template class SharedFutureOn<>;
176
177} // namespace yaclib
A intrusive pointer to objects with an embedded reference count.
Encapsulated return value from caller.
Definition result.hpp:90
void Subscribe(IExecutor &e, Func &&f) const
void Detach() &&noexcept
void SubscribeInline(Func &&f) const
detail::SharedHandle GetHandle() const noexcept
SharedFutureBase(detail::SharedCorePtr< V, E > core) noexcept
Result< V, E > Get() &&noexcept
auto Then(IExecutor &e, Func &&f) const
detail::SharedCorePtr< V, E > _core
const detail::SharedCorePtr< V, E > & GetCore() const noexcept
detail::SharedCorePtr< V, E > & GetCore() noexcept
void Touch() const &&=delete
bool Ready() const noexcept
bool Valid() const noexcept
Result< V, E > Touch() &&noexcept
void Get() const &&=delete
auto ThenInline(Func &&f) const
auto Then(Func &&f) const
SharedFutureOn(detail::SharedCorePtr< V, E > core) noexcept
void Subscribe(Func &&f) const
SharedFuture< V, E > On(std::nullptr_t) &&noexcept
auto ThenInline(Func &&f) const
SharedFuture(detail::SharedCorePtr< V, E > core) noexcept
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_INLINE std::enable_if_t<(... &&is_waitable_v< Waited >), void > Wait(Waited &... fs) noexcept
Wait until Ready becomes true.
Definition wait.hpp:18