C/C++ API Reference
Loading...
Searching...
No Matches
pw::OptionalTuple< Types > Class Template Reference

Overview

template<typename... Types>
class pw::OptionalTuple< Types >

Tuple class with optional elements. Equivalent to std::tuple<std::optional<Types>...>, but much more efficient. Field presence is tracked by a single pw::BitSet, so the tuple elements are packed equivalently to std::tuple<Types...>.

Tuple elements are specified by their index. Elements may also be referenced by type, if there is only one instance of that type in the tuple.

Compatible with std::tuple_element and std::tuple_size.

Note
Like std::optional, moving an element out of an OptionalTuple leaves the element in an active but moved state (has_value<>() returns true). Call reset() to remove the element.

Public Member Functions

constexpr OptionalTuple ()
 Default constructs an OptionalTuple with all elements unset.
 
template<typename... Args, typename = std::enable_if_t< (std::is_constructible_v<containers::internal::Maybe<Types>, Args> && ...) && (sizeof...(Types) != 1 || (!containers::internal::IsOptionalTuple< cpp20::remove_cvref_t<Args>>::value && ...))>>
constexpr OptionalTuple (Args &&... args)
 
constexpr OptionalTuple (const OptionalTuple &other)
 Copy constructs from another OptionalTuple.
 
constexpr OptionalTupleoperator= (const OptionalTuple &other)
 Copy assigns this from another OptionalTuple.
 
constexpr OptionalTuple (OptionalTuple &&other)
 Move constructs from another OptionalTuple.
 
constexpr OptionalTupleoperator= (OptionalTuple &&other)
 Move assigns this from another OptionalTuple.
 
 ~OptionalTuple ()
 Destroys the OptionalTuple and all its active elements.
 
constexpr bool empty () const
 Checks if the OptionalTuple contains no active elements.
 
constexpr size_t count () const
 Returns the number of active elements in the OptionalTuple.
 
constexpr size_t size () const
 
template<size_t kIndex>
constexpr bool has_value () const
 Checks if the element at kIndex has a value.
 
template<typename T >
constexpr bool has_value () const
 Checks if the element of type T has a value.
 
template<size_t kIndex>
constexpr Element< kIndex > & value () &
 
template<size_t kIndex>
constexpr const Element< kIndex > & value () const &
 
template<size_t kIndex>
constexpr Element< kIndex > && value () &&
 
template<size_t kIndex>
constexpr const Element< kIndex > && value () const &&
 
template<typename T >
constexpr ElementByType< T > & value () &
 
template<typename T >
constexpr const ElementByType< T > & value () const &
 
template<typename T >
constexpr ElementByType< T > && value () &&
 
template<typename T >
constexpr const ElementByType< T > && value () const &&
 
template<size_t kIndex, int... kExplicitGuard, typename U >
constexpr Element< kIndex > value_or (U &&default_value) const &
 
template<typename T , int... kExplicitGuard, typename U >
constexpr ElementByType< T > value_or (U &&default_value) const &
 
template<size_t kIndex, int... kExplicitGuard, typename U >
constexpr Element< kIndex > value_or (U &&default_value) &&
 
template<typename T , int... kExplicitGuard, typename U >
constexpr ElementByType< T > value_or (U &&default_value) &&
 
template<size_t kIndex, int... kExplicitGuard, typename... Args>
constexpr Element< kIndex > & emplace (Args &&... args)
 
template<typename T , int... kExplicitGuard, typename... Args>
constexpr ElementByType< T > & emplace (Args &&... args)
 
template<size_t kIndex>
constexpr void reset ()
 Resets (clears) the value at the specified index, if any.
 
template<typename T >
constexpr void reset ()
 Resets (clears) the value of type T, if any.
 

Constructor & Destructor Documentation

◆ OptionalTuple()

template<typename... Types>
template<typename... Args, typename = std::enable_if_t< (std::is_constructible_v<containers::internal::Maybe<Types>, Args> && ...) && (sizeof...(Types) != 1 || (!containers::internal::IsOptionalTuple< cpp20::remove_cvref_t<Args>>::value && ...))>>
constexpr pw::OptionalTuple< Types >::OptionalTuple ( Args &&...  args)
inlineconstexpr

Constructs an OptionalTuple, forwarding each argument to its corresponding element. Pass pw::kTupleNull to skip initializing an element.

Member Function Documentation

◆ emplace() [1/2]

template<typename... Types>
template<size_t kIndex, int... kExplicitGuard, typename... Args>
constexpr Element< kIndex > & pw::OptionalTuple< Types >::emplace ( Args &&...  args)
inlineconstexpr

Constructs an element in place and marks it active. Destroys the previous value, if any.

Returns
A reference to the newly initialized item.

◆ emplace() [2/2]

template<typename... Types>
template<typename T , int... kExplicitGuard, typename... Args>
constexpr ElementByType< T > & pw::OptionalTuple< Types >::emplace ( Args &&...  args)
inlineconstexpr

Constructs an element of type T in place and marks it active. Destroys the previous value, if any.

Returns
A reference to the newly initialized item.

◆ size()

template<typename... Types>
constexpr size_t pw::OptionalTuple< Types >::size ( ) const
inlineconstexpr

Returns the total number elements in the OptionalTuple. Equivalent to std::tuple_size<>.

◆ value() [1/8]

template<typename... Types>
template<size_t kIndex>
constexpr Element< kIndex > & pw::OptionalTuple< Types >::value ( ) &
inlineconstexpr

Returns a reference to the element at kIndex.

Precondition
has_value<kIndex> must be true.

◆ value() [2/8]

template<typename... Types>
template<typename T >
constexpr ElementByType< T > & pw::OptionalTuple< Types >::value ( ) &
inlineconstexpr

Returns a reference to the element of type T.

Precondition
has_value<T> must be true.

◆ value() [3/8]

template<typename... Types>
template<size_t kIndex>
constexpr Element< kIndex > && pw::OptionalTuple< Types >::value ( ) &&
inlineconstexpr

Returns an rvalue reference to the element at kIndex.

Precondition
has_value<kIndex> must be true.

◆ value() [4/8]

template<typename... Types>
template<typename T >
constexpr ElementByType< T > && pw::OptionalTuple< Types >::value ( ) &&
inlineconstexpr

Returns an rvalue reference to the element of type T.

Precondition
has_value<T> must be true.

◆ value() [5/8]

template<typename... Types>
template<size_t kIndex>
constexpr const Element< kIndex > & pw::OptionalTuple< Types >::value ( ) const &
inlineconstexpr

Returns a const reference to the element at kIndex.

Precondition
has_value<kIndex> must be true.

◆ value() [6/8]

template<typename... Types>
template<typename T >
constexpr const ElementByType< T > & pw::OptionalTuple< Types >::value ( ) const &
inlineconstexpr

Returns a const reference to the element of type T.

Precondition
has_value<T> must be true.

◆ value() [7/8]

template<typename... Types>
template<size_t kIndex>
constexpr const Element< kIndex > && pw::OptionalTuple< Types >::value ( ) const &&
inlineconstexpr

Returns a const rvalue reference to the element at kIndex.

Precondition
has_value<kIndex> must be true.

◆ value() [8/8]

template<typename... Types>
template<typename T >
constexpr const ElementByType< T > && pw::OptionalTuple< Types >::value ( ) const &&
inlineconstexpr

Returns a const rvalue reference to the element of type T.

Precondition
has_value<T> must be true.

◆ value_or() [1/4]

template<typename... Types>
template<size_t kIndex, int... kExplicitGuard, typename U >
constexpr Element< kIndex > pw::OptionalTuple< Types >::value_or ( U &&  default_value) &&
inlineconstexpr

Moves and returns the element at the specified index, if present. Otherwise, returns default_value.

◆ value_or() [2/4]

template<typename... Types>
template<typename T , int... kExplicitGuard, typename U >
constexpr ElementByType< T > pw::OptionalTuple< Types >::value_or ( U &&  default_value) &&
inlineconstexpr

Moves and returns the element with the specified type, if present. Otherwise, returns default_value.

◆ value_or() [3/4]

template<typename... Types>
template<size_t kIndex, int... kExplicitGuard, typename U >
constexpr Element< kIndex > pw::OptionalTuple< Types >::value_or ( U &&  default_value) const &
inlineconstexpr

Returns the element at the specified index, if present. Otherwise, returns default_value.

◆ value_or() [4/4]

template<typename... Types>
template<typename T , int... kExplicitGuard, typename U >
constexpr ElementByType< T > pw::OptionalTuple< Types >::value_or ( U &&  default_value) const &
inlineconstexpr

Returns the element with the specified type, if present. Otherwise, returns default_value.


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