2017-09-19 22:54:40 +02:00
|
|
|
// Copyright 2017 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2019-03-08 16:27:53 +01:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// optional.h
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2017-09-24 17:20:48 +02:00
|
|
|
// This header file defines the `absl::optional` type for holding a value which
|
2017-09-19 22:54:40 +02:00
|
|
|
// may or may not be present. This type is useful for providing value semantics
|
|
|
|
// for operations that may either wish to return or hold "something-or-nothing".
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// // A common way to signal operation failure is to provide an output
|
|
|
|
// // parameter and a bool return type:
|
|
|
|
// bool AcquireResource(const Input&, Resource * out);
|
|
|
|
//
|
|
|
|
// // Providing an absl::optional return type provides a cleaner API:
|
|
|
|
// absl::optional<Resource> AcquireResource(const Input&);
|
|
|
|
//
|
|
|
|
// `absl::optional` is a C++11 compatible version of the C++17 `std::optional`
|
|
|
|
// abstraction and is designed to be a drop-in replacement for code compliant
|
|
|
|
// with C++17.
|
|
|
|
#ifndef ABSL_TYPES_OPTIONAL_H_
|
|
|
|
#define ABSL_TYPES_OPTIONAL_H_
|
|
|
|
|
2019-04-04 17:13:57 +02:00
|
|
|
#include "absl/base/config.h" // TODO(calabrese) IWYU removal?
|
2017-09-19 22:54:40 +02:00
|
|
|
#include "absl/utility/utility.h"
|
|
|
|
|
|
|
|
#ifdef ABSL_HAVE_STD_OPTIONAL
|
|
|
|
|
2019-03-12 19:39:15 +01:00
|
|
|
#include <optional> // IWYU pragma: export
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
using std::bad_optional_access;
|
|
|
|
using std::optional;
|
|
|
|
using std::make_optional;
|
|
|
|
using std::nullopt_t;
|
|
|
|
using std::nullopt;
|
2018-06-18 22:18:53 +02:00
|
|
|
} // namespace absl
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
#else // ABSL_HAVE_STD_OPTIONAL
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-08-28 10:05:21 +02:00
|
|
|
#include "absl/base/attributes.h"
|
2019-03-27 16:05:41 +01:00
|
|
|
#include "absl/base/internal/inline_variable.h"
|
2017-09-19 22:54:40 +02:00
|
|
|
#include "absl/meta/type_traits.h"
|
|
|
|
#include "absl/types/bad_optional_access.h"
|
2019-04-04 17:13:57 +02:00
|
|
|
#include "absl/types/internal/optional.h"
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
|
2019-04-04 17:13:57 +02:00
|
|
|
// nullopt_t
|
|
|
|
//
|
|
|
|
// Class type for `absl::nullopt` used to indicate an `absl::optional<T>` type
|
|
|
|
// that does not contain a value.
|
|
|
|
struct nullopt_t {
|
|
|
|
// It must not be default-constructible to avoid ambiguity for opt = {}.
|
|
|
|
explicit constexpr nullopt_t(optional_internal::init_t) noexcept {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// nullopt
|
|
|
|
//
|
|
|
|
// A tag constant of type `absl::nullopt_t` used to indicate an empty
|
|
|
|
// `absl::optional` in certain functions, such as construction or assignment.
|
|
|
|
ABSL_INTERNAL_INLINE_CONSTEXPR(nullopt_t, nullopt,
|
|
|
|
nullopt_t(optional_internal::init_t()));
|
|
|
|
|
2018-04-20 18:16:52 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// absl::optional
|
|
|
|
// -----------------------------------------------------------------------------
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
// A value of type `absl::optional<T>` holds either a value of `T` or an
|
|
|
|
// "empty" value. When it holds a value of `T`, it stores it as a direct
|
|
|
|
// sub-object, so `sizeof(optional<T>)` is approximately
|
|
|
|
// `sizeof(T) + sizeof(bool)`.
|
|
|
|
//
|
|
|
|
// This implementation is based on the specification in the latest draft of the
|
|
|
|
// C++17 `std::optional` specification as of May 2017, section 20.6.
|
|
|
|
//
|
|
|
|
// Differences between `absl::optional<T>` and `std::optional<T>` include:
|
|
|
|
//
|
|
|
|
// * `constexpr` is not used for non-const member functions.
|
|
|
|
// (dependency on some differences between C++11 and C++14.)
|
|
|
|
// * `absl::nullopt` and `absl::in_place` are not declared `constexpr`. We
|
|
|
|
// need the inline variable support in C++17 for external linkage.
|
|
|
|
// * Throws `absl::bad_optional_access` instead of
|
|
|
|
// `std::bad_optional_access`.
|
|
|
|
// * `make_optional()` cannot be declared `constexpr` due to the absence of
|
|
|
|
// guaranteed copy elision.
|
2017-10-05 22:33:44 +02:00
|
|
|
// * The move constructor's `noexcept` specification is stronger, i.e. if the
|
|
|
|
// default allocator is non-throwing (via setting
|
|
|
|
// `ABSL_ALLOCATOR_NOTHROW`), it evaluates to `noexcept(true)`, because
|
|
|
|
// we assume
|
|
|
|
// a) move constructors should only throw due to allocation failure and
|
|
|
|
// b) if T's move constructor allocates, it uses the same allocation
|
|
|
|
// function as the default allocator.
|
2017-09-19 22:54:40 +02:00
|
|
|
//
|
|
|
|
template <typename T>
|
|
|
|
class optional : private optional_internal::optional_data<T>,
|
|
|
|
private optional_internal::optional_ctor_base<
|
2019-03-20 17:34:39 +01:00
|
|
|
optional_internal::ctor_copy_traits<T>::traits>,
|
2017-09-19 22:54:40 +02:00
|
|
|
private optional_internal::optional_assign_base<
|
2019-03-20 17:34:39 +01:00
|
|
|
optional_internal::assign_copy_traits<T>::traits> {
|
2017-09-19 22:54:40 +02:00
|
|
|
using data_base = optional_internal::optional_data<T>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
|
2018-02-01 05:34:59 +01:00
|
|
|
// Constructs an `optional` holding an empty value, NOT a default constructed
|
|
|
|
// `T`.
|
2017-09-19 22:54:40 +02:00
|
|
|
constexpr optional() noexcept {}
|
|
|
|
|
2018-02-01 05:34:59 +01:00
|
|
|
// Constructs an `optional` initialized with `nullopt` to hold an empty value.
|
2017-09-19 22:54:40 +02:00
|
|
|
constexpr optional(nullopt_t) noexcept {} // NOLINT(runtime/explicit)
|
|
|
|
|
|
|
|
// Copy constructor, standard semantics
|
|
|
|
optional(const optional& src) = default;
|
|
|
|
|
|
|
|
// Move constructor, standard semantics
|
|
|
|
optional(optional&& src) = default;
|
|
|
|
|
|
|
|
// Constructs a non-empty `optional` direct-initialized value of type `T` from
|
|
|
|
// the arguments `std::forward<Args>(args)...` within the `optional`.
|
|
|
|
// (The `in_place_t` is a tag used to indicate that the contained object
|
|
|
|
// should be constructed in-place.)
|
2019-03-13 22:59:43 +01:00
|
|
|
template <typename InPlaceT, typename... Args,
|
|
|
|
absl::enable_if_t<absl::conjunction<
|
|
|
|
std::is_same<InPlaceT, in_place_t>,
|
|
|
|
std::is_constructible<T, Args&&...> >::value>* = nullptr>
|
|
|
|
constexpr explicit optional(InPlaceT, Args&&... args)
|
2017-09-19 22:54:40 +02:00
|
|
|
: data_base(in_place_t(), absl::forward<Args>(args)...) {}
|
|
|
|
|
2018-01-17 18:53:47 +01:00
|
|
|
// Constructs a non-empty `optional` direct-initialized value of type `T` from
|
2017-09-19 22:54:40 +02:00
|
|
|
// the arguments of an initializer_list and `std::forward<Args>(args)...`.
|
|
|
|
// (The `in_place_t` is a tag used to indicate that the contained object
|
|
|
|
// should be constructed in-place.)
|
|
|
|
template <typename U, typename... Args,
|
|
|
|
typename = typename std::enable_if<std::is_constructible<
|
|
|
|
T, std::initializer_list<U>&, Args&&...>::value>::type>
|
|
|
|
constexpr explicit optional(in_place_t, std::initializer_list<U> il,
|
|
|
|
Args&&... args)
|
|
|
|
: data_base(in_place_t(), il, absl::forward<Args>(args)...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value constructor (implicit)
|
|
|
|
template <
|
|
|
|
typename U = T,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<absl::negation<std::is_same<
|
|
|
|
in_place_t, typename std::decay<U>::type> >,
|
|
|
|
absl::negation<std::is_same<
|
|
|
|
optional<T>, typename std::decay<U>::type> >,
|
|
|
|
std::is_convertible<U&&, T>,
|
|
|
|
std::is_constructible<T, U&&> >::value,
|
|
|
|
bool>::type = false>
|
|
|
|
constexpr optional(U&& v) : data_base(in_place_t(), absl::forward<U>(v)) {}
|
|
|
|
|
|
|
|
// Value constructor (explicit)
|
|
|
|
template <
|
|
|
|
typename U = T,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<absl::negation<std::is_same<
|
|
|
|
in_place_t, typename std::decay<U>::type>>,
|
|
|
|
absl::negation<std::is_same<
|
|
|
|
optional<T>, typename std::decay<U>::type>>,
|
|
|
|
absl::negation<std::is_convertible<U&&, T>>,
|
|
|
|
std::is_constructible<T, U&&>>::value,
|
|
|
|
bool>::type = false>
|
|
|
|
explicit constexpr optional(U&& v)
|
|
|
|
: data_base(in_place_t(), absl::forward<U>(v)) {}
|
|
|
|
|
|
|
|
// Converting copy constructor (implicit)
|
|
|
|
template <typename U,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U> >,
|
|
|
|
std::is_constructible<T, const U&>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::
|
|
|
|
is_constructible_convertible_from_optional<T, U> >,
|
|
|
|
std::is_convertible<const U&, T> >::value,
|
|
|
|
bool>::type = false>
|
|
|
|
optional(const optional<U>& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->construct(*rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converting copy constructor (explicit)
|
|
|
|
template <typename U,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U>>,
|
|
|
|
std::is_constructible<T, const U&>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::
|
|
|
|
is_constructible_convertible_from_optional<T, U>>,
|
|
|
|
absl::negation<std::is_convertible<const U&, T>>>::value,
|
|
|
|
bool>::type = false>
|
|
|
|
explicit optional(const optional<U>& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->construct(*rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converting move constructor (implicit)
|
|
|
|
template <typename U,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U> >,
|
|
|
|
std::is_constructible<T, U&&>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::
|
|
|
|
is_constructible_convertible_from_optional<T, U> >,
|
|
|
|
std::is_convertible<U&&, T> >::value,
|
|
|
|
bool>::type = false>
|
|
|
|
optional(optional<U>&& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->construct(std::move(*rhs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converting move constructor (explicit)
|
|
|
|
template <
|
|
|
|
typename U,
|
|
|
|
typename std::enable_if<
|
|
|
|
absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::is_constructible_convertible_from_optional<
|
|
|
|
T, U>>,
|
|
|
|
absl::negation<std::is_convertible<U&&, T>>>::value,
|
|
|
|
bool>::type = false>
|
|
|
|
explicit optional(optional<U>&& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->construct(std::move(*rhs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor. Trivial if `T` is trivially destructible.
|
|
|
|
~optional() = default;
|
|
|
|
|
|
|
|
// Assignment Operators
|
|
|
|
|
|
|
|
// Assignment from `nullopt`
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// struct S { int value; };
|
|
|
|
// optional<S> opt = absl::nullopt; // Could also use opt = { };
|
|
|
|
optional& operator=(nullopt_t) noexcept {
|
|
|
|
this->destruct();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy assignment operator, standard semantics
|
|
|
|
optional& operator=(const optional& src) = default;
|
|
|
|
|
|
|
|
// Move assignment operator, standard semantics
|
|
|
|
optional& operator=(optional&& src) = default;
|
|
|
|
|
|
|
|
// Value assignment operators
|
|
|
|
template <
|
|
|
|
typename U = T,
|
|
|
|
typename = typename std::enable_if<absl::conjunction<
|
|
|
|
absl::negation<
|
|
|
|
std::is_same<optional<T>, typename std::decay<U>::type>>,
|
|
|
|
absl::negation<
|
|
|
|
absl::conjunction<std::is_scalar<T>,
|
|
|
|
std::is_same<T, typename std::decay<U>::type>>>,
|
|
|
|
std::is_constructible<T, U>, std::is_assignable<T&, U>>::value>::type>
|
|
|
|
optional& operator=(U&& v) {
|
|
|
|
this->assign(std::forward<U>(v));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename U,
|
|
|
|
typename = typename std::enable_if<absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U>>,
|
|
|
|
std::is_constructible<T, const U&>, std::is_assignable<T&, const U&>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::
|
|
|
|
is_constructible_convertible_assignable_from_optional<
|
|
|
|
T, U>>>::value>::type>
|
|
|
|
optional& operator=(const optional<U>& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->assign(*rhs);
|
|
|
|
} else {
|
|
|
|
this->destruct();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U,
|
|
|
|
typename = typename std::enable_if<absl::conjunction<
|
|
|
|
absl::negation<std::is_same<T, U>>, std::is_constructible<T, U>,
|
|
|
|
std::is_assignable<T&, U>,
|
|
|
|
absl::negation<
|
|
|
|
optional_internal::
|
|
|
|
is_constructible_convertible_assignable_from_optional<
|
|
|
|
T, U>>>::value>::type>
|
|
|
|
optional& operator=(optional<U>&& rhs) {
|
|
|
|
if (rhs) {
|
|
|
|
this->assign(std::move(*rhs));
|
|
|
|
} else {
|
|
|
|
this->destruct();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modifiers
|
|
|
|
|
|
|
|
// optional::reset()
|
|
|
|
//
|
|
|
|
// Destroys the inner `T` value of an `absl::optional` if one is present.
|
2018-08-28 10:05:21 +02:00
|
|
|
ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept { this->destruct(); }
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
// optional::emplace()
|
|
|
|
//
|
|
|
|
// (Re)constructs the underlying `T` in-place with the given forwarded
|
|
|
|
// arguments.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// optional<Foo> opt;
|
|
|
|
// opt.emplace(arg1,arg2,arg3); // Constructs Foo(arg1,arg2,arg3)
|
|
|
|
//
|
|
|
|
// If the optional is non-empty, and the `args` refer to subobjects of the
|
|
|
|
// current object, then behaviour is undefined, because the current object
|
|
|
|
// will be destructed before the new object is constructed with `args`.
|
|
|
|
template <typename... Args,
|
|
|
|
typename = typename std::enable_if<
|
|
|
|
std::is_constructible<T, Args&&...>::value>::type>
|
|
|
|
T& emplace(Args&&... args) {
|
|
|
|
this->destruct();
|
|
|
|
this->construct(std::forward<Args>(args)...);
|
|
|
|
return reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emplace reconstruction overload for an initializer list and the given
|
|
|
|
// forwarded arguments.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// struct Foo {
|
|
|
|
// Foo(std::initializer_list<int>);
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// optional<Foo> opt;
|
|
|
|
// opt.emplace({1,2,3}); // Constructs Foo({1,2,3})
|
|
|
|
template <typename U, typename... Args,
|
|
|
|
typename = typename std::enable_if<std::is_constructible<
|
|
|
|
T, std::initializer_list<U>&, Args&&...>::value>::type>
|
|
|
|
T& emplace(std::initializer_list<U> il, Args&&... args) {
|
|
|
|
this->destruct();
|
|
|
|
this->construct(il, std::forward<Args>(args)...);
|
|
|
|
return reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swaps
|
|
|
|
|
|
|
|
// Swap, standard semantics
|
|
|
|
void swap(optional& rhs) noexcept(
|
|
|
|
std::is_nothrow_move_constructible<T>::value&&
|
2019-03-19 19:14:01 +01:00
|
|
|
type_traits_internal::IsNothrowSwappable<T>::value) {
|
2017-09-19 22:54:40 +02:00
|
|
|
if (*this) {
|
|
|
|
if (rhs) {
|
2019-03-19 19:14:01 +01:00
|
|
|
type_traits_internal::Swap(**this, *rhs);
|
2017-09-19 22:54:40 +02:00
|
|
|
} else {
|
|
|
|
rhs.construct(std::move(**this));
|
|
|
|
this->destruct();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rhs) {
|
|
|
|
this->construct(std::move(*rhs));
|
|
|
|
rhs.destruct();
|
|
|
|
} else {
|
|
|
|
// No effect (swap(disengaged, disengaged)).
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Observers
|
|
|
|
|
|
|
|
// optional::operator->()
|
|
|
|
//
|
|
|
|
// Accesses the underlying `T` value's member `m` of an `optional`. If the
|
|
|
|
// `optional` is empty, behavior is undefined.
|
2018-04-23 17:17:58 +02:00
|
|
|
//
|
|
|
|
// If you need myOpt->foo in constexpr, use (*myOpt).foo instead.
|
2018-05-11 14:42:49 +02:00
|
|
|
const T* operator->() const {
|
|
|
|
assert(this->engaged_);
|
|
|
|
return std::addressof(this->data_);
|
|
|
|
}
|
2017-09-19 22:54:40 +02:00
|
|
|
T* operator->() {
|
|
|
|
assert(this->engaged_);
|
2018-05-11 14:42:49 +02:00
|
|
|
return std::addressof(this->data_);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// optional::operator*()
|
|
|
|
//
|
2017-11-02 18:46:34 +01:00
|
|
|
// Accesses the underlying `T` value of an `optional`. If the `optional` is
|
2017-09-19 22:54:40 +02:00
|
|
|
// empty, behavior is undefined.
|
|
|
|
constexpr const T& operator*() const & { return reference(); }
|
|
|
|
T& operator*() & {
|
|
|
|
assert(this->engaged_);
|
|
|
|
return reference();
|
|
|
|
}
|
|
|
|
constexpr const T&& operator*() const && {
|
|
|
|
return absl::move(reference());
|
|
|
|
}
|
|
|
|
T&& operator*() && {
|
|
|
|
assert(this->engaged_);
|
|
|
|
return std::move(reference());
|
|
|
|
}
|
|
|
|
|
|
|
|
// optional::operator bool()
|
|
|
|
//
|
|
|
|
// Returns false if and only if the `optional` is empty.
|
|
|
|
//
|
|
|
|
// if (opt) {
|
|
|
|
// // do something with opt.value();
|
|
|
|
// } else {
|
|
|
|
// // opt is empty.
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
constexpr explicit operator bool() const noexcept { return this->engaged_; }
|
|
|
|
|
|
|
|
// optional::has_value()
|
|
|
|
//
|
|
|
|
// Determines whether the `optional` contains a value. Returns `false` if and
|
|
|
|
// only if `*this` is empty.
|
|
|
|
constexpr bool has_value() const noexcept { return this->engaged_; }
|
|
|
|
|
2018-05-17 18:05:57 +02:00
|
|
|
// Suppress bogus warning on MSVC: MSVC complains call to reference() after
|
|
|
|
// throw_bad_optional_access() is unreachable.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4702)
|
|
|
|
#endif // _MSC_VER
|
2017-09-19 22:54:40 +02:00
|
|
|
// optional::value()
|
|
|
|
//
|
|
|
|
// Returns a reference to an `optional`s underlying value. The constness
|
|
|
|
// and lvalue/rvalue-ness of the `optional` is preserved to the view of
|
|
|
|
// the `T` sub-object. Throws `absl::bad_optional_access` when the `optional`
|
|
|
|
// is empty.
|
|
|
|
constexpr const T& value() const & {
|
|
|
|
return static_cast<bool>(*this)
|
|
|
|
? reference()
|
|
|
|
: (optional_internal::throw_bad_optional_access(), reference());
|
|
|
|
}
|
|
|
|
T& value() & {
|
|
|
|
return static_cast<bool>(*this)
|
|
|
|
? reference()
|
|
|
|
: (optional_internal::throw_bad_optional_access(), reference());
|
|
|
|
}
|
|
|
|
T&& value() && { // NOLINT(build/c++11)
|
|
|
|
return std::move(
|
|
|
|
static_cast<bool>(*this)
|
|
|
|
? reference()
|
|
|
|
: (optional_internal::throw_bad_optional_access(), reference()));
|
|
|
|
}
|
|
|
|
constexpr const T&& value() const && { // NOLINT(build/c++11)
|
|
|
|
return absl::move(
|
|
|
|
static_cast<bool>(*this)
|
|
|
|
? reference()
|
|
|
|
: (optional_internal::throw_bad_optional_access(), reference()));
|
|
|
|
}
|
2018-05-17 18:05:57 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif // _MSC_VER
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
// optional::value_or()
|
|
|
|
//
|
2018-02-02 19:36:07 +01:00
|
|
|
// Returns either the value of `T` or a passed default `v` if the `optional`
|
2017-09-19 22:54:40 +02:00
|
|
|
// is empty.
|
|
|
|
template <typename U>
|
|
|
|
constexpr T value_or(U&& v) const& {
|
2018-01-23 18:07:44 +01:00
|
|
|
static_assert(std::is_copy_constructible<value_type>::value,
|
|
|
|
"optional<T>::value_or: T must by copy constructible");
|
|
|
|
static_assert(std::is_convertible<U&&, value_type>::value,
|
|
|
|
"optional<T>::value_or: U must be convertible to T");
|
2017-09-19 22:54:40 +02:00
|
|
|
return static_cast<bool>(*this)
|
|
|
|
? **this
|
|
|
|
: static_cast<T>(absl::forward<U>(v));
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
T value_or(U&& v) && { // NOLINT(build/c++11)
|
2018-01-23 18:07:44 +01:00
|
|
|
static_assert(std::is_move_constructible<value_type>::value,
|
|
|
|
"optional<T>::value_or: T must by copy constructible");
|
|
|
|
static_assert(std::is_convertible<U&&, value_type>::value,
|
|
|
|
"optional<T>::value_or: U must be convertible to T");
|
2017-09-19 22:54:40 +02:00
|
|
|
return static_cast<bool>(*this) ? std::move(**this)
|
|
|
|
: static_cast<T>(std::forward<U>(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private accessors for internal storage viewed as reference to T.
|
2018-04-23 17:17:58 +02:00
|
|
|
constexpr const T& reference() const { return this->data_; }
|
|
|
|
T& reference() { return this->data_; }
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
// T constraint checks. You can't have an optional of nullopt_t, in_place_t
|
|
|
|
// or a reference.
|
|
|
|
static_assert(
|
|
|
|
!std::is_same<nullopt_t, typename std::remove_cv<T>::type>::value,
|
|
|
|
"optional<nullopt_t> is not allowed.");
|
|
|
|
static_assert(
|
|
|
|
!std::is_same<in_place_t, typename std::remove_cv<T>::type>::value,
|
|
|
|
"optional<in_place_t> is not allowed.");
|
|
|
|
static_assert(!std::is_reference<T>::value,
|
|
|
|
"optional<reference> is not allowed.");
|
|
|
|
};
|
|
|
|
|
|
|
|
// Non-member functions
|
|
|
|
|
|
|
|
// swap()
|
|
|
|
//
|
|
|
|
// Performs a swap between two `absl::optional` objects, using standard
|
|
|
|
// semantics.
|
2019-03-19 19:14:01 +01:00
|
|
|
template <typename T, typename std::enable_if<
|
|
|
|
std::is_move_constructible<T>::value &&
|
|
|
|
type_traits_internal::IsSwappable<T>::value,
|
|
|
|
bool>::type = false>
|
2017-09-19 22:54:40 +02:00
|
|
|
void swap(optional<T>& a, optional<T>& b) noexcept(noexcept(a.swap(b))) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make_optional()
|
|
|
|
//
|
|
|
|
// Creates a non-empty `optional<T>` where the type of `T` is deduced. An
|
|
|
|
// `absl::optional` can also be explicitly instantiated with
|
|
|
|
// `make_optional<T>(v)`.
|
|
|
|
//
|
|
|
|
// Note: `make_optional()` constructions may be declared `constexpr` for
|
|
|
|
// trivially copyable types `T`. Non-trivial types require copy elision
|
|
|
|
// support in C++17 for `make_optional` to support `constexpr` on such
|
|
|
|
// non-trivial types.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// constexpr absl::optional<int> opt = absl::make_optional(1);
|
|
|
|
// static_assert(opt.value() == 1, "");
|
|
|
|
template <typename T>
|
|
|
|
constexpr optional<typename std::decay<T>::type> make_optional(T&& v) {
|
|
|
|
return optional<typename std::decay<T>::type>(absl::forward<T>(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
constexpr optional<T> make_optional(Args&&... args) {
|
|
|
|
return optional<T>(in_place_t(), absl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U, typename... Args>
|
|
|
|
constexpr optional<T> make_optional(std::initializer_list<U> il,
|
|
|
|
Args&&... args) {
|
|
|
|
return optional<T>(in_place_t(), il,
|
|
|
|
absl::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relational operators [optional.relops]
|
|
|
|
|
|
|
|
// Empty optionals are considered equal to each other and less than non-empty
|
|
|
|
// optionals. Supports relations between optional<T> and optional<U>, between
|
|
|
|
// optional<T> and U, and between optional<T> and nullopt.
|
|
|
|
//
|
|
|
|
// Note: We're careful to support T having non-bool relationals.
|
|
|
|
|
|
|
|
// Requires: The expression, e.g. "*x == *y" shall be well-formed and its result
|
|
|
|
// shall be convertible to bool.
|
|
|
|
// The C++17 (N4606) "Returns:" statements are translated into
|
|
|
|
// code in an obvious way here, and the original text retained as function docs.
|
|
|
|
// Returns: If bool(x) != bool(y), false; otherwise if bool(x) == false, true;
|
|
|
|
// otherwise *x == *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator==(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x == *y)) {
|
|
|
|
return static_cast<bool>(x) != static_cast<bool>(y)
|
|
|
|
? false
|
2018-05-04 18:58:56 +02:00
|
|
|
: static_cast<bool>(x) == false ? true
|
|
|
|
: static_cast<bool>(*x == *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns: If bool(x) != bool(y), true; otherwise, if bool(x) == false, false;
|
|
|
|
// otherwise *x != *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator!=(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x != *y)) {
|
|
|
|
return static_cast<bool>(x) != static_cast<bool>(y)
|
|
|
|
? true
|
2018-05-04 18:58:56 +02:00
|
|
|
: static_cast<bool>(x) == false ? false
|
|
|
|
: static_cast<bool>(*x != *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
// Returns: If !y, false; otherwise, if !x, true; otherwise *x < *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x < *y)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return !y ? false : !x ? true : static_cast<bool>(*x < *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
// Returns: If !x, false; otherwise, if !y, true; otherwise *x > *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x > *y)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return !x ? false : !y ? true : static_cast<bool>(*x > *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
// Returns: If !x, true; otherwise, if !y, false; otherwise *x <= *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<=(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x <= *y)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return !x ? true : !y ? false : static_cast<bool>(*x <= *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
// Returns: If !y, true; otherwise, if !x, false; otherwise *x >= *y.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>=(const optional<T>& x, const optional<U>& y)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x >= *y)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return !y ? true : !x ? false : static_cast<bool>(*x >= *y);
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison with nullopt [optional.nullops]
|
|
|
|
// The C++17 (N4606) "Returns:" statements are used directly here.
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
|
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
|
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
|
|
|
|
return static_cast<bool>(x);
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
|
|
|
|
return static_cast<bool>(x);
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
|
|
|
|
return static_cast<bool>(x);
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
|
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
|
|
|
|
return static_cast<bool>(x);
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
|
|
|
|
return !x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison with T [optional.comp_with_t]
|
|
|
|
|
|
|
|
// Requires: The expression, e.g. "*x == v" shall be well-formed and its result
|
|
|
|
// shall be convertible to bool.
|
|
|
|
// The C++17 (N4606) "Equivalent to:" statements are used directly here.
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator==(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x == v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x == v) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator==(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v == *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v == *x) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator!=(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x != v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x != v) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator!=(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v != *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v != *x) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x < v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x < v) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v < *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v < *x) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<=(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x <= v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x <= v) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator<=(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v <= *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v <= *x) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x > v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x > v) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v > *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v > *x) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>=(const optional<T>& x, const U& v)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(*x >= v)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(*x >= v) : false;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
|
|
constexpr auto operator>=(const U& v, const optional<T>& x)
|
|
|
|
-> decltype(optional_internal::convertible_to_bool(v >= *x)) {
|
2018-05-04 18:58:56 +02:00
|
|
|
return static_cast<bool>(x) ? static_cast<bool>(v >= *x) : true;
|
2017-09-19 22:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace absl
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
// std::hash specialization for absl::optional.
|
|
|
|
template <typename T>
|
2017-09-20 02:15:26 +02:00
|
|
|
struct hash<absl::optional<T> >
|
|
|
|
: absl::optional_internal::optional_hash_base<T> {};
|
2017-09-19 22:54:40 +02:00
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
#undef ABSL_MSVC_CONSTEXPR_BUG_IN_UNION_LIKE_CLASS
|
|
|
|
|
|
|
|
#endif // ABSL_HAVE_STD_OPTIONAL
|
|
|
|
|
|
|
|
#endif // ABSL_TYPES_OPTIONAL_H_
|