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
-
| T | type of the optional value |
| kHasValueState | State value that represents |
|
|
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 Optional & | operator= (const Optional &)=default |
| |
|
constexpr | Optional (Optional &&)=default |
| |
|
constexpr Optional & | operator= (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 Optional & | operator= (const value_type &value) |
| | Assigns a new value to the optional.
|
| |
|
constexpr Optional & | operator= (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.
|
| |