15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_GENERIC_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_GENERIC_H
18
19
20
21
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"
34
35
37class future final :
private internal::future_base<T> {
39 using shared_state_type =
40 typename internal::future_base<T>::shared_state_type;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
65
66
67
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()); })) {}
74
75
76
77
78
79
80
81
82
83
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;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 template <
typename F>
115 typename internal::then_helper<F, T>::
future_t then(F&& func) {
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;
144
145
147class 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)) {}
157 explicit promise(null_promise_t x)
158 : internal::promise_base<T>(std::move(x)) {}
166 promise&
operator=(promise&& rhs)
noexcept {
167 promise tmp(std::move(rhs));
173
174
175
176
177
178
181 promise(promise
const&) =
delete;
182 promise&
operator=(promise
const&) =
delete;
185 void swap(promise& other)
noexcept {
186 std::swap(
this->shared_state_, other.shared_state_);
190
191
193 internal::future_shared_state<T>::mark_retrieved(
this->shared_state_);
194 return future<T>(
this->shared_state_);
198
199
200
201
202
203
204
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;
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();
future(future< U > &&rhs)
Creates a future from a future whose result type is convertible to this future's result type.
Definition: future_generic.h:70
T get()
Waits until the shared state becomes ready, then retrieves the value stored in the shared state.
Definition: future_generic.h:84
internal::then_helper< F, T >::future_t then(F &&func)
Attach a continuation to the future.
Definition: future_generic.h:115
future(std::shared_ptr< shared_state_type > state)
Definition: future_generic.h:122
friend class future
Definition: future_generic.h:137
future() noexcept
Definition: future_generic.h:44
future(future< future< T > > &&rhs) noexcept(false)
Creates a new future that unwraps rhs.
promise(promise &&)=default
Constructs a new promise and transfer any shared state from rhs.
void swap(promise &other) noexcept
Swaps the shared state in *this with rhs.
Definition: future_generic.h:185
promise()
Creates a promise with an unsatisfied shared state.
Definition: future_generic.h:150
promise(null_promise_t x)
Creates a promise without a shared state.
Definition: future_generic.h:157
promise & operator=(promise &&rhs) noexcept
Abandons the shared state in *this, if any, and transfers the shared state from rhs.
Definition: future_generic.h:166
promise & operator=(promise const &)=delete
~promise()=default
Abandons any shared state.
future< T > get_future()
Creates the future<T> using the same shared state as *this.
Definition: future_generic.h:192
promise(promise const &)=delete
promise(std::function< void()> cancellation_callback)
Creates a promise with an unsatisfied shared state.
Definition: future_generic.h:153
void set_value(T value)
Satisfies the shared state.
Definition: future_generic.h:205
Contains all the Google Cloud C++ Library APIs.
Definition: async_operation.h:23
future< typename internal::make_ready_return< T >::type > make_ready_future(T &&t)
Create a future<void> that is immediately ready.
Definition: future_generic.h:217
Definition: async_operation.h:22
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Definition: version.h:45
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
Definition: version.h:43