Google Cloud C++ Client  0.4.0
C++ Client Library for Google Cloud Platform
future_void.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_VOID_H_
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_VOID_H_
17 /**
18  * @file
19  *
20  * Fully specialize `future<void>` and `promise<R>` for void.
21  */
22 
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 
28 namespace google {
29 namespace cloud {
30 inline namespace GOOGLE_CLOUD_CPP_NS {
31 /**
32  * Implement ISO/IEC TS 19571:2016 future for void.
33  */
34 template <>
35 class future<void> final : private internal::future_base<void> {
36  public:
37  using shared_state_type =
38  typename internal::future_base<void>::shared_state_type;
39 
40  // workaround Apple Clang-7xx series bug, if we use `= default` here, the
41  // compiler believes there is no default constructor defined. :shrug:
42  future() noexcept {}
43 
44  /**
45  * Creates a new future that unwraps @p rhs.
46  *
47  * This constructor creates a new shared state that becomes satisfied when
48  * both `rhs` and `rhs.get()` become satisfied. If `rhs` is satisfied, but
49  * `rhs.get()` returns an invalid future then the newly created future becomes
50  * satisfied with a `std::future_error` exception, and the exception error
51  * code is `std::future_errc::broken_promise`.
52  *
53  * @note The technical specification requires this to be a `noexcept`
54  * constructor I (coryan) believe this is a defect in the technical
55  * specification, as this *creates* a new shared state: shared states are
56  * dynamically allocated, and the allocator (which might be the default
57  * `operator new`) may raise.
58  */
59  future(future<future<void>>&& rhs) noexcept(false);
60 
61  /**
62  * Waits until the shared state becomes ready, then retrieves the value stored
63  * in the shared state.
64  *
65  * @throws any exceptions stored in the shared state.
66  * @throws std::future_error with std::no_state if the future does not have
67  * a shared state.
68  */
69  void get() {
70  check_valid();
71  std::shared_ptr<shared_state_type> tmp;
72  tmp.swap(shared_state_);
73  return tmp->get();
74  }
75 
76  using future_base::is_ready;
77  using future_base::valid;
78  using future_base::wait;
79  using future_base::wait_for;
80  using future_base::wait_until;
81 
82  /**
83  * Attach a continuation to the future.
84  *
85  * Attach a callable @a func to be invoked when the future is
86  * ready. The return type is a future wrapping the return type of
87  * @a func.
88  *
89  * @return future<T> where T is std::result_of_t<F, R> (basically).
90  * If T matches future<U> then it returns future<U>. The returned
91  * future will contain the result of @a func.
92  * @param func a Callable to be invoked when the future is ready.
93  * The function might be called immediately, e.g., if the future is
94  * ready.
95  *
96  * Side effects: valid() == false if the operation is successful.
97  */
98  template <typename F>
99  typename internal::then_helper<F, void>::future_t then(F&& func) {
100  check_valid();
101  using requires_unwrap_t =
102  typename internal::then_helper<F, void>::requires_unwrap_t;
103  return then_impl(std::forward<F>(func), requires_unwrap_t{});
104  }
105 
106  explicit future(std::shared_ptr<shared_state_type> state)
107  : future_base<void>(std::move(state)) {}
108 
109  private:
110  /// Implement `then()` if the result does not require unwrapping.
111  template <typename F>
112  typename internal::then_helper<F, void>::future_t then_impl(F&& functor,
113  std::false_type);
114 
115  /// Implement `then()` if the result requires unwrapping.
116  template <typename F>
117  typename internal::then_helper<F, void>::future_t then_impl(F&& functor,
118  std::true_type);
119 
120  template <typename U>
121  friend class future;
122 };
123 
124 /**
125  * Specialize promise as defined in ISO/IEC TS 19571:2016 for void.
126  */
127 template <>
128 class promise<void> final : private internal::promise_base<void> {
129  public:
130  /// Creates a promise with an unsatisfied shared state.
131  promise() = default;
132 
133  /// Constructs a new promise and transfer any shared state from @p rhs.
134  promise(promise&& rhs) noexcept = default;
135 
136  /// Abandons the shared state in `*this`, if any, and transfers the shared
137  /// state from @p rhs.
138  promise& operator=(promise&& rhs) noexcept {
139  promise tmp(std::move(rhs));
140  tmp.swap(*this);
141  return *this;
142  }
143 
144  /**
145  * Abandons any shared state.
146  *
147  * If the shared state was not already satisfied it becomes satisfied with
148  * a `std::future_error` exception. The error code in this exception is
149  * `std::future_errc::broken_promise`.
150  */
151  ~promise() = default;
152 
153  promise(promise const&) = delete;
154  promise& operator=(promise const&) = delete;
155 
156  /// Swaps the shared state in `*this` with @p rhs.
157  void swap(promise& other) noexcept {
158  std::swap(shared_state_, other.shared_state_);
159  }
160 
161  /**
162  * Creates the `future<void>` using the same shared state as `*this`.
163  */
164  future<void> get_future() {
165  shared_state_type::mark_retrieved(shared_state_);
166  return future<void>(shared_state_);
167  }
168 
169  /**
170  * Satisfies the shared state.
171  *
172  * @throws std::future_error with std::future_errc::promise_already_satisfied
173  * if the shared state is already satisfied.
174  * @throws std::future_error with std::no_state if the promise does not have
175  * a shared state.
176  */
177  void set_value() {
178  if (!shared_state_) {
179  internal::ThrowFutureError(std::future_errc::no_state, __func__);
180  }
181  shared_state_->set_value();
182  }
183 
184  using promise_base<void>::set_exception;
185 };
186 
187 } // namespace GOOGLE_CLOUD_CPP_NS
188 } // namespace cloud
189 } // namespace google
190 
191 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_FUTURE_VOID_H_
#define GOOGLE_CLOUD_CPP_NS
Definition: version.h:24
Contains all the Google Cloud C++ Library APIs.
Definition: iam_bindings.cc:21
promise(promise &&rhs) noexcept=default
Constructs a new promise and transfer any shared state from rhs.
void get()
Waits until the shared state becomes ready, then retrieves the value stored in the shared state...
Definition: future_void.h:69
void swap(promise &other) noexcept
Swaps the shared state in *this with rhs.
Definition: future_void.h:157
promise & operator=(promise &&rhs) noexcept
Abandons the shared state in *this, if any, and transfers the shared state from rhs.
Definition: future_void.h:138
promise()=default
Creates a promise with an unsatisfied shared state.
internal::then_helper< F, void >::future_t then(F &&func)
Attach a continuation to the future.
Definition: future_void.h:99
promise(promise const &)=delete
void set_value()
Satisfies the shared state.
Definition: future_void.h:177
future< void > get_future()
Creates the future<void> using the same shared state as *this.
Definition: future_void.h:164
future(std::shared_ptr< shared_state_type > state)
Definition: future_void.h:106
promise & operator=(promise const &)=delete
future(future< future< void >> &&rhs) noexcept(false)
Creates a new future that unwraps rhs.
~promise()=default
Abandons any shared state.