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)
233 : state_(std::move(state))
236 state_->OnFutureCreated();
240void Future<T>::CheckValid()
const {
242 throw std::future_error(std::future_errc::no_state);
248 : state_(std::make_shared<impl::FutureState<T>>())
252Promise<T>& Promise<T>::operator=(Promise<T>&& other)
noexcept {
253 if (
this == &other) {
257 [[maybe_unused]]
const auto for_destruction = std::move(*
this);
259 state_ = std::move(other.state_);
264Promise<T>::~Promise() {
265 if (state_ && !state_->IsReady() && state_->IsFutureCreated()) {
267 state_->SetException(std::make_exception_ptr(std::future_error(std::future_errc::broken_promise)));
268 }
catch (
const std::future_error&) {
276 return Future<T>(state_);
281 state_->SetValue(value);
286 state_->SetValue(std::move(value));
291 state_->SetException(std::move(ex));
295 : state_(std::make_shared<impl::FutureState<
void>>())
298inline Promise<
void>& Promise<
void>::operator=(Promise<
void>&& other)
noexcept {
299 if (
this == &other) {
303 [[maybe_unused]]
const auto for_destruction = std::move(*
this);
305 state_ = std::move(other.state_);
309inline Promise<
void>::~Promise() {
310 if (state_ && !state_->IsReady() && state_->IsFutureCreated()) {
312 state_->SetException(std::make_exception_ptr(std::future_error(std::future_errc::broken_promise)));
313 }
catch (
const std::future_error&) {
319inline Future<
void> Promise<
void>::
get_future() {
return Future<
void>(state_); }
321inline void Promise<
void>::
set_value() { state_->SetValue(); }
323inline void Promise<
void>::
set_exception(std::exception_ptr ex) { state_->SetException(std::move(ex)); }