15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPTIONAL_H_ 16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPTIONAL_H_ 18 #include "google/cloud/internal/throw_delegate.h" 19 #include <type_traits> 56 explicit optional(T
const& x) : has_value_(
true) {
57 new (
reinterpret_cast<T*>(&buffer_)) T(x);
59 explicit optional(T&& x)
noexcept : has_value_(
true) {
60 new (
reinterpret_cast<T*>(&buffer_)) T(std::move(x));
64 new (
reinterpret_cast<T*>(&buffer_)) T(std::move(*rhs));
69 new (
reinterpret_cast<T*>(&buffer_)) T(*rhs);
79 if (!rhs.has_value_) {
82 new (
reinterpret_cast<T*>(&buffer_)) T(*rhs);
86 if (!rhs.has_value_) {
100 if (!rhs.has_value_) {
103 new (
reinterpret_cast<T*>(&buffer_)) T(std::move(*rhs));
107 if (!rhs.has_value_) {
111 **
this = std::move(*rhs);
119 template <
typename U = T>
120 typename std::enable_if<
121 not std::is_same<
optional,
typename std::decay<U>::type>::value,
128 new (
reinterpret_cast<T*>(&buffer_)) T(std::forward<U>(rhs));
132 **
this = std::forward<U>(rhs);
138 return reinterpret_cast<T
const*>(&buffer_);
140 T*
operator->() {
return reinterpret_cast<T*>(&buffer_); }
142 return *
reinterpret_cast<T
const*>(&buffer_);
144 T&
operator*() & {
return *
reinterpret_cast<T*>(&buffer_); }
146 #if GOOGLE_CLOUD_CPP_HAVE_CONST_REF_REF
147 T
const&& operator*()
const&& {
148 return std::move(*
reinterpret_cast<T
const*>(&buffer_));
152 return std::move(*
reinterpret_cast<T*>(&buffer_));
165 return std::move(**
this);
173 template <
typename U>
175 return bool(*
this) ? **
this 176 :
static_cast<T>(std::forward<U>(default_value));
179 template <
typename U>
181 return bool(*
this) ? std::move(**
this)
182 :
static_cast<T>(std::forward<U>(default_value));
190 reinterpret_cast<T*>(&buffer_)->~T();
196 new (
reinterpret_cast<T*>(&buffer_)) T(std::move(value));
208 return **
this == *rhs;
212 return std::rel_ops::operator!=(*
this, rhs);
217 if (!rhs.has_value()) {
221 return **
this < *rhs;
226 return rhs.has_value();
230 return std::rel_ops::operator>(*
this, rhs);
234 return std::rel_ops::operator>=(*
this, rhs);
238 return std::rel_ops::operator<=(*
this, rhs);
242 void check_access()
const {
246 google::
cloud::internal::ThrowLogicError(
"access unset optional");
252 using aligned_storage_t = std::aligned_storage<
sizeof(T),
alignof(T)>;
253 typename aligned_storage_t::type buffer_;
257 template <
typename T>
259 return optional<T>(std::forward<T>(t));
constexpr T const & operator*() const &
#define GOOGLE_CLOUD_CPP_NS
bool operator==(optional const &rhs) const
Contains all the Google Cloud C++ Library APIs.
bool operator>(optional const &rhs) const
bool operator<=(optional const &rhs) const
std::enable_if< not std::is_same< optional, typename std::decay< U >::type >::value, optional >::type & operator=(U &&rhs)
A poor's man version of std::optional<T>.
T const && value() const &&
optional & operator=(optional< T > &&rhs) noexcept
bool operator>=(optional const &rhs) const
T value_or(U &&default_value) &&
optional< T > make_optional(T &&t)
bool operator!=(optional const &rhs) const
optional & operator=(optional< T > const &rhs)
optional(optional< T > const &rhs)
T const & value() const &
constexpr T const * operator->() const
constexpr T value_or(U &&default_value) const &
bool operator<(optional const &rhs) const
optional(optional< T > &&rhs) noexcept