C/C++ API Reference
Loading...
Searching...
No Matches
pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible > Class Template Referencefinal

Overview

template<typename T, OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
class pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >

Optional is similar to std::optional and pw::Result, but uses a user-specified state type for object presence. One state value is reserved to indicate that the object is present.

Optional is essentially a customizable pw::Result, supporting custom enums instead of only pw::Status. pw::Result cannot easily wrap a pw::Status, so is not suitable as a general T wrapper. Construction or assignment from a Status is ambiguous, since it is unclear if the Status is a value or an error. These issues apply to absl::StatusOr as well (see b/280392796). Optional resolves this by prohibiting conversions between the value and state type and with a simpler construction/assignment API.

To simplify its use in templates, Optional is specialized for void.

Template Parameters
Ttype of the optional value
kHasValueStateState value that represents
Inheritance diagram for pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >:

Public Types

using state_type = decltype(kHasValueState)
 
using value_type = T
 

Public Member Functions

constexpr Optional (state_type state)
 Constructs an optional without a value using the provided state.
 
constexpr Optional (const value_type &value)
 Constructs an optional containing a value.
 
constexpr Optional (value_type &&value)
 
template<typename... Args>
constexpr Optional (std::in_place_t, Args &&... args)
 Constructs an optional in-place containing a value.
 
constexpr Optional (const Optional &)=default
 
constexpr Optionaloperator= (const Optional &)=default
 
constexpr Optional (Optional &&)=default
 
constexpr Optionaloperator= (Optional &&)=default
 
template<typename U , std::enable_if_t< std::conjunction_v< std::negation< std::is_same< T, U > >, std::is_constructible< T, const U & >, std::is_convertible< const U &, T > >, int > = 0>
constexpr Optional (const Optional< U, kHasValueState > &other)
 Constructs an optional from another optional with a compatible value type.
 
template<typename U , std::enable_if_t< std::conjunction_v< std::negation< std::is_same< T, U > >, std::is_constructible< T, const U & >, std::negation< std::is_convertible< const U &, T > > >, int > = 0>
constexpr Optional (const Optional< U, kHasValueState > &other)
 
template<typename U , std::enable_if_t< std::conjunction_v< std::negation< std::is_same< T, U > >, std::is_constructible< T, U && >, std::is_convertible< U &&, T > >, int > = 0>
constexpr Optional (Optional< U, kHasValueState > &&other)
 
template<typename U , std::enable_if_t< std::conjunction_v< std::negation< std::is_same< T, U > >, std::is_constructible< T, U && >, std::negation< std::is_convertible< U &&, T > > >, int > = 0>
constexpr Optional (Optional< U, kHasValueState > &&other)
 
constexpr Optionaloperator= (const value_type &value)
 Assigns a new value to the optional.
 
constexpr Optionaloperator= (value_type &&value)
 Move assigns a new value to the optional.
 
template<typename... Args>
constexpr void emplace (Args &&... args)
 Constructs a new value in-place.
 
constexpr void reset (state_type state)
 
template<state_type kState>
constexpr void reset ()
 
constexpr bool has_value () const
 Returns true if the optional contains a value.
 
constexpr T & operator* () &
 
constexpr const T & operator* () const &
 
constexpr T && operator* () &&
 
constexpr const T && operator* () const &&
 
constexpr T * operator-> ()
 
constexpr const T * operator-> () const
 
constexpr T & value () &
 Returns the contained value. Asserts if the value is not present.
 
constexpr const T & value () const &
 Returns the contained value. Asserts if the value is not present.
 
constexpr T && value () &&
 Returns the contained value. Asserts if the value is not present.
 
constexpr const T && value () const &&
 Returns the contained value. Asserts if the value is not present.
 
constexpr state_type state () const
 Returns the state. Always valid, whether has_value is true or false.
 

Friends

constexpr bool operator== (const Optional &lhs, const Optional &rhs)
 
constexpr bool operator!= (const Optional &lhs, const Optional &rhs)
 
constexpr bool operator== (const Optional &lhs, const T &rhs)
 
constexpr bool operator== (const T &lhs, const Optional &rhs)
 
constexpr bool operator!= (const Optional &lhs, const T &rhs)
 
constexpr bool operator!= (const T &lhs, const Optional &rhs)
 

Member Function Documentation

◆ operator*() [1/4]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr T & pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator* ( ) &
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ operator*() [2/4]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr T && pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator* ( ) &&
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ operator*() [3/4]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr const T & pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator* ( ) const &
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ operator*() [4/4]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr const T && pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator* ( ) const &&
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ operator->() [1/2]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr T * pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator-> ( )
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ operator->() [2/2]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr const T * pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::operator-> ( ) const
inlineconstexpr

Accesses the contained value. Does NOT check if the value is present!

Precondition
has_value() MUST be true.

◆ reset() [1/2]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
template<state_type kState>
constexpr void pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::reset ( )
inlineconstexpr

Destroys the contained value, if any, and sets the state. The state must not be kHasValueState. To avoid a runtime check, use the reset<kState>() function template whenever possible.

◆ reset() [2/2]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr void pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::reset ( state_type  state)
inlineconstexpr

Destroys the contained value, if any, and sets the state. The state must not be kHasValueState. To avoid a runtime check, use the reset<kState>() function template whenever possible.

◆ value() [1/3]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr T && pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::value ( ) &&
inlineconstexpr

Returns the contained value. Asserts if the value is not present.

◆ value() [2/3]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr const T & pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::value ( ) const &
inlineconstexpr

Returns the contained value. Asserts if the value is not present.

◆ value() [3/3]

template<typename T , OptionalState auto kHasValueState, bool IsTriviallyDestructible = std::is_trivially_destructible_v<T>>
constexpr const T && pw::containers::internal::Optional< T, kHasValueState, IsTriviallyDestructible >::value ( ) const &&
inlineconstexpr

Returns the contained value. Asserts if the value is not present.


The documentation for this class was generated from the following file: