|
| 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_INTERNAL_FUTURE_VOID_H_ |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_FUTURE_VOID_H_ |
| 17 | +/** |
| 18 | + * @file |
| 19 | + * |
| 20 | + * Fully specialize `future<void>` and `promise<R>` for void. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "google/cloud/internal/port_platform.h" |
| 24 | + |
| 25 | +// C++ futures only make sense when exceptions are enabled. |
| 26 | +#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS |
| 27 | +#include "google/cloud/internal/future_fwd.h" |
| 28 | +#include "google/cloud/internal/future_impl.h" |
| 29 | + |
| 30 | +namespace google { |
| 31 | +namespace cloud { |
| 32 | +inline namespace GOOGLE_CLOUD_CPP_NS { |
| 33 | +/** |
| 34 | + * Implement ISO/IEC TS 19571:2016 future for void. |
| 35 | + */ |
| 36 | +template <> |
| 37 | +class future<void> { |
| 38 | + public: |
| 39 | + future() noexcept : shared_state_() {} |
| 40 | + future(future<void>&& f) noexcept |
| 41 | + : shared_state_(std::move(f.shared_state_)) {} |
| 42 | + |
| 43 | + // TODO(#1345) - implement the unwrapping constructor. |
| 44 | + // future(future<future<void>>&& f) noexcept; |
| 45 | + // future& operator=(future<future<void>>&& f) noexcept; |
| 46 | + future(future<void> const&) = delete; |
| 47 | + |
| 48 | + future& operator=(future<void>&& f) noexcept { |
| 49 | + future tmp(std::move(f)); |
| 50 | + std::swap(tmp.shared_state_, shared_state_); |
| 51 | + return *this; |
| 52 | + } |
| 53 | + future& operator=(future const&) = delete; |
| 54 | + |
| 55 | + void get() { |
| 56 | + check_valid(); |
| 57 | + std::shared_ptr<shared_state_type> tmp; |
| 58 | + tmp.swap(shared_state_); |
| 59 | + return tmp->get(); |
| 60 | + } |
| 61 | + |
| 62 | + bool valid() const noexcept { return static_cast<bool>(shared_state_); } |
| 63 | + void wait() const { |
| 64 | + check_valid(); |
| 65 | + shared_state_->wait(); |
| 66 | + } |
| 67 | + |
| 68 | + template <typename Rep, typename Period> |
| 69 | + std::future_status wait_for( |
| 70 | + std::chrono::duration<Rep, Period> const& rel_time) const { |
| 71 | + check_valid(); |
| 72 | + return shared_state_->wait_for(rel_time); |
| 73 | + } |
| 74 | + |
| 75 | + template <typename Rep, typename Period> |
| 76 | + std::future_status wait_until( |
| 77 | + std::chrono::time_point<Rep, Period> const& abs_time) const { |
| 78 | + check_valid(); |
| 79 | + return shared_state_->wait_until(abs_time); |
| 80 | + } |
| 81 | + |
| 82 | + private: |
| 83 | + /// Shorthand to refer to the shared state type. |
| 84 | + using shared_state_type = internal::future_shared_state<void>; |
| 85 | + |
| 86 | + friend class promise<void>; |
| 87 | + explicit future(std::shared_ptr<shared_state_type> state) |
| 88 | + : shared_state_(std::move(state)) {} |
| 89 | + |
| 90 | + void check_valid() const { |
| 91 | + if (not shared_state_) { |
| 92 | + throw std::future_error(std::future_errc::no_state); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + std::shared_ptr<shared_state_type> shared_state_; |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Specialize promise as defined in ISO/IEC TS 19571:2016 for void. |
| 101 | + */ |
| 102 | +template <> |
| 103 | +class promise<void> { |
| 104 | + public: |
| 105 | + /// Create a promise |
| 106 | + promise() : shared_state_(new shared_state) {} |
| 107 | + |
| 108 | + promise(promise&& rhs) noexcept |
| 109 | + : shared_state_(std::move(rhs.shared_state_)) { |
| 110 | + rhs.shared_state_.reset(); |
| 111 | + } |
| 112 | + |
| 113 | + promise(promise const&) = delete; |
| 114 | + |
| 115 | + ~promise() { |
| 116 | + if (shared_state_) { |
| 117 | + shared_state_->abandon(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + promise& operator=(promise&& rhs) noexcept { |
| 122 | + promise tmp(std::move(rhs)); |
| 123 | + this->swap(tmp); |
| 124 | + return *this; |
| 125 | + } |
| 126 | + |
| 127 | + promise& operator=(promise const&) = delete; |
| 128 | + |
| 129 | + void swap(promise& other) noexcept { |
| 130 | + std::swap(shared_state_, other.shared_state_); |
| 131 | + } |
| 132 | + |
| 133 | + future<void> get_future() { |
| 134 | + shared_state::mark_retrieved(shared_state_); |
| 135 | + return future<void>(shared_state_); |
| 136 | + } |
| 137 | + |
| 138 | + void set_value() { |
| 139 | + if (not shared_state_) { |
| 140 | + throw std::future_error(std::future_errc::no_state); |
| 141 | + } |
| 142 | + shared_state_->set_value(); |
| 143 | + } |
| 144 | + |
| 145 | + void set_exception(std::exception_ptr p) { |
| 146 | + if (not shared_state_) { |
| 147 | + throw std::future_error(std::future_errc::no_state); |
| 148 | + } |
| 149 | + shared_state_->set_exception(std::move(p)); |
| 150 | + } |
| 151 | + |
| 152 | + private: |
| 153 | + /// Shorthand to refer to the shared state type. |
| 154 | + using shared_state = internal::future_shared_state<void>; |
| 155 | + std::shared_ptr<shared_state> shared_state_; |
| 156 | +}; |
| 157 | + |
| 158 | +} // namespace GOOGLE_CLOUD_CPP_NS |
| 159 | +} // namespace cloud |
| 160 | +} // namespace google |
| 161 | + |
| 162 | +#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS |
| 163 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_FUTURE_VOID_H_ |
0 commit comments