11#include <userver/engine/deadline.hpp>
12#include <userver/engine/future_status.hpp>
13#include <userver/engine/impl/future_state.hpp>
16#include <userver/utils/assert.hpp>
18USERVER_NAMESPACE_BEGIN
56 Future(
const Future&) =
delete;
57 Future(Future&&)
noexcept =
default;
58 Future& operator=(
const Future&) =
delete;
59 Future& operator=(Future&&)
noexcept =
default;
91 template <
typename Rep,
typename Period>
100 template <
typename Clock,
typename Duration>
113 impl::ContextAccessor* TryGetContextAccessor()
noexcept {
114 return state_ ? state_->TryGetContextAccessor() :
nullptr;
119 friend class Promise<T>;
121 explicit Future(std::shared_ptr<impl::FutureState<T>> state);
123 void CheckValid()
const;
125 std::shared_ptr<impl::FutureState<T>> state_;
136 Promise(
const Promise&) =
delete;
137 Promise(Promise&&)
noexcept =
default;
138 Promise& operator=(
const Promise&) =
delete;
139 Promise& operator=(Promise&&)
noexcept;
158 std::shared_ptr<impl::FutureState<T>> state_;
162class Promise<
void>
final {
169 Promise(
const Promise&) =
delete;
170 Promise(Promise&&)
noexcept =
default;
171 Promise& operator=(
const Promise&) =
delete;
172 Promise& operator=(Promise&&)
noexcept;
187 std::shared_ptr<impl::FutureState<
void>> state_;
191bool Future<T>::
valid()
const noexcept {
198 return state_->IsReady();
204 return std::exchange(state_,
nullptr)->Get();
210 return state_->WaitUntil({});
214template <
typename Rep,
typename Period>
216 return wait_until(Deadline::FromDuration(timeout));
220template <
typename Clock,
typename Duration>
222 return wait_until(Deadline::FromTimePoint(until));
228 return state_->WaitUntil(deadline);
232Future<T>::Future(std::shared_ptr<impl::FutureState<T>> state) : state_(std::move(state)) {
234 state_->OnFutureCreated();
238void Future<T>::CheckValid()
const {
240 throw std::future_error(std::future_errc::no_state);
245Promise<T>::
Promise() : state_(std::make_shared<impl::FutureState<T>>()) {}
248Promise<T>& Promise<T>::operator=(Promise<T>&& other)
noexcept {
249 if (
this == &other)
return *
this;
250 { [[maybe_unused]]
const auto for_destruction = std::move(*
this); }
251 state_ = std::move(other.state_);
256Promise<T>::~Promise() {
257 if (state_ && !state_->IsReady() && state_->IsFutureCreated()) {
259 state_->SetException(std::make_exception_ptr(std::future_error(std::future_errc::broken_promise)));
260 }
catch (
const std::future_error&) {
268 return Future<T>(state_);
273 state_->SetValue(value);
278 state_->SetValue(std::move(value));
283 state_->SetException(std::move(ex));
288inline Promise<
void>& Promise<
void>::operator=(Promise<
void>&& other)
noexcept {
289 if (
this == &other)
return *
this;
290 { [[maybe_unused]]
const auto for_destruction = std::move(*
this); }
291 state_ = std::move(other.state_);
295inline Promise<
void>::~Promise() {
296 if (state_ && !state_->IsReady() && state_->IsFutureCreated()) {
298 state_->SetException(std::make_exception_ptr(std::future_error(std::future_errc::broken_promise)));
299 }
catch (
const std::future_error&) {
305inline Future<
void> Promise<
void>::
get_future() {
return Future<
void>(state_); }
307inline void Promise<
void>::
set_value() { state_->SetValue(); }
309inline void Promise<
void>::
set_exception(std::exception_ptr ex) { state_->SetException(std::move(ex)); }