15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_GENERIC_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_GENERIC_H
23 #include "google/cloud/internal/future_base.h"
24 #include "google/cloud/internal/future_fwd.h"
25 #include "google/cloud/internal/future_impl.h"
26 #include "google/cloud/internal/future_then_meta.h"
27 #include "google/cloud/version.h"
28 #include "absl/meta/type_traits.h"
37 class future final :
private internal::future_base<T> {
39 using shared_state_type =
40 typename internal::future_base<T>::shared_state_type;
68 template <
class U,
typename Enable =
69 absl::enable_if_t<std::is_constructible<T, U>::value>>
71 :
future<T>(rhs.then([](
future<U> other) {
return T(other.get()); })) {}
86 std::shared_ptr<shared_state_type> tmp;
87 tmp.swap(
this->shared_state_);
91 using internal::future_base<T>::cancel;
92 using internal::future_base<T>::is_ready;
93 using internal::future_base<T>::valid;
94 using internal::future_base<T>::wait;
95 using internal::future_base<T>::wait_for;
96 using internal::future_base<T>::wait_until;
114 template <
typename F>
117 using requires_unwrap_t =
118 typename internal::then_helper<F, T>::requires_unwrap_t;
119 return then_impl(std::forward<F>(func), requires_unwrap_t{});
122 explicit future(std::shared_ptr<shared_state_type> state)
123 : internal::future_base<T>(std::move(state)) {}
127 template <
typename F>
128 typename internal::then_helper<F, T>::future_t then_impl(F&& functor,
132 template <
typename F>
133 typename internal::then_helper<F, T>::future_t then_impl(F&& functor,
136 template <
typename U>
138 friend class future<
void>;
140 friend struct internal::CoroutineSupport;
146 template <
typename T>
147 class promise final :
private internal::promise_base<T> {
150 promise() : internal::promise_base<T>([] {}) {}
153 explicit promise(std::function<
void()> cancellation_callback)
154 : internal::promise_base<T>(std::move(cancellation_callback)) {}
158 : internal::promise_base<T>(std::move(x)) {}
167 promise tmp(std::move(rhs));
185 void swap(promise& other)
noexcept {
186 std::swap(
this->shared_state_, other.shared_state_);
193 internal::future_shared_state<T>::mark_retrieved(
this->shared_state_);
194 return future<T>(
this->shared_state_);
206 if (!
this->shared_state_) {
207 internal::ThrowFutureError(std::future_errc::no_state,
__func__);
209 this->shared_state_->set_value(std::move(value));
212 using internal::promise_base<T>::set_exception;
216 template <
typename T>
219 using V =
typename internal::make_ready_return<T>::type;
221 static_assert(!std::is_reference<V>::value,
"future<R&> is not implemented");
223 p.set_value(std::forward<T>(t));
224 return p.get_future();