StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
StormByte::Safe::Variant< Ts > Class Template Referencefinal

std::variant stored on Base's heap. More...

#include <StormByte/safe/variant.hxx>

Public Member Functions

 Variant ()
 Default-construct the first alternative.
template<class T>
requires (Variant::template AlternativeIndex<T>() != npos)
 Variant (T &&value)
 Copy an alternative into Base storage.
template<std::size_t I, class... Args>
 Variant (std::in_place_index_t< I > index, Args &&... args)
 Construct the alternative at I.
template<class T, class... Args>
 Variant (std::in_place_type_t< T > tag, Args &&... args)
 Construct the alternative T.
 Variant (const std::variant< Ts... > &value)
 Copy a caller-owned std::variant.
 Variant (std::variant< Ts... > &&value)
 Move a caller-owned std::variant by copying the alternative into Base storage.
 Variant (const Variant &other)
 Copy the active alternative into a new Base block.
 Variant (Variant &&other) noexcept
 Take the active block.
 ~Variant () noexcept
 Destroy the active alternative and release its block.
Variant & operator= (const Variant &other)
 Copy-assign.
Variant & operator= (Variant &&other) noexcept
 Move-assign.
template<class T>
requires (Variant::template AlternativeIndex<T>() != npos)
Variant & operator= (T &&value)
 Assign an alternative.
std::size_t index () const noexcept
 Index of the active alternative, or npos when valueless.
bool valueless_by_exception () const noexcept
 Whether a throwing operation left this variant without an alternative.
template<std::size_t I, class... Args>
auto & emplace (Args &&... args)
 Construct the alternative at I, replacing the current one only after construction.
template<class T, class... Args>
T & emplace (Args &&... args)
 Construct T, replacing the current alternative only after construction.
void swap (Variant &other) noexcept
 Exchange storage.
bool operator== (const Variant &other) const
 Content equality.
STORMBYTE_FORCE_INLINE std::strong_ordering operator<=> (const Variant &other) const
 Index order, then alternative order when the indices match.
STORMBYTE_FORCE_INLINE operator std::variant< Ts... > () const
 Copy the active alternative into caller-owned STL storage.

Static Public Member Functions

template<class T>
static constexpr std::size_t AlternativeIndex () noexcept
 Index of the single alternative constructible from T, or npos.

Static Public Attributes

static constexpr std::size_t npos = static_cast<std::size_t>(-1)
 No alternative.

Friends

template<std::size_t I, Type::SafeValue... Us>
auto * get_if (Variant< Us... > *value) noexcept
template<std::size_t I, Type::SafeValue... Us>
const auto * get_if (const Variant< Us... > *value) noexcept
template<class T, Type::SafeValue... Us>
T * get_if (Variant< Us... > *value) noexcept
template<class T, Type::SafeValue... Us>
const T * get_if (const Variant< Us... > *value) noexcept
template<class Visitor, Type::SafeValue... Us>
decltype(auto) visit (Visitor &&visitor, Variant< Us... > &value)
template<class Visitor, Type::SafeValue... Us>
decltype(auto) visit (Visitor &&visitor, const Variant< Us... > &value)

Detailed Description

template<Type::SafeValue... Ts>
requires (sizeof...(Ts) > 0)
class StormByte::Safe::Variant< Ts >

std::variant stored on Base's heap.

Template Parameters
TsAlternatives. Each one is a StormByte::Type::SafeValue.

Not a std::variant. The active alternative is constructed in a block from Heap::Allocate and released with Heap::Free. A move of this variant leaves the source valueless. A move from std::variant does not change its index. emplace builds the replacement first, so a throw keeps the previous alternative. get and visit are found by argument lookup; std::get and std::visit do not accept this type.

Constructor & Destructor Documentation

◆ Variant() [1/8]

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::Variant ( )

Default-construct the first alternative.

Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [2/8]

template<Type::SafeValue... Ts>
template<class T>
requires (Variant::template AlternativeIndex<T>() != npos)
StormByte::Safe::Variant< Ts >::Variant ( T && value)
inline

Copy an alternative into Base storage.

Template Parameters
TAlternative. Exactly one alternative is constructible from value.
Parameters
valueSource.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [3/8]

template<Type::SafeValue... Ts>
template<std::size_t I, class... Args>
StormByte::Safe::Variant< Ts >::Variant ( std::in_place_index_t< I > index,
Args &&... args )
explicit

Construct the alternative at I.

Template Parameters
IAlternative index.
ArgsConstructor argument types.
Parameters
indexIn-place index tag.
argsArguments forwarded to the alternative.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [4/8]

template<Type::SafeValue... Ts>
template<class T, class... Args>
StormByte::Safe::Variant< Ts >::Variant ( std::in_place_type_t< T > tag,
Args &&... args )
explicit

Construct the alternative T.

Template Parameters
TAlternative. It occurs once.
ArgsConstructor argument types.
Parameters
tagIn-place type tag.
argsArguments forwarded to T.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [5/8]

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::Variant ( const std::variant< Ts... > & value)
explicit

Copy a caller-owned std::variant.

A valueless source stays valueless.

Parameters
valueSource. Its index is unchanged.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [6/8]

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::Variant ( std::variant< Ts... > && value)
explicit

Move a caller-owned std::variant by copying the alternative into Base storage.

Parameters
valueSource. Its index is unchanged; a held alternative is moved-from.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [7/8]

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::Variant ( const Variant< Ts > & other)

Copy the active alternative into a new Base block.

Parameters
otherSource.
Exceptions
AllocationErrorThe block could not be allocated.

◆ Variant() [8/8]

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::Variant ( Variant< Ts > && other)
noexcept

Take the active block.

other becomes valueless.

Parameters
otherSource.

◆ ~Variant()

template<Type::SafeValue... Ts>
StormByte::Safe::Variant< Ts >::~Variant ( )
noexcept

Destroy the active alternative and release its block.

Member Function Documentation

◆ AlternativeIndex()

template<Type::SafeValue... Ts>
template<class T>
constexpr std::size_t StormByte::Safe::Variant< Ts >::AlternativeIndex ( )
staticconstexprnoexcept

Index of the single alternative constructible from T, or npos.

Template Parameters
TSource type.
Returns
Index, or npos when none or more than one matches.

◆ emplace() [1/2]

template<Type::SafeValue... Ts>
template<class T, class... Args>
T & StormByte::Safe::Variant< Ts >::emplace ( Args &&... args)

Construct T, replacing the current alternative only after construction.

Template Parameters
TAlternative. It occurs once.
ArgsConstructor argument types.
Parameters
argsArguments forwarded to T.
Returns
The new alternative.
Exceptions
AllocationErrorThe block could not be allocated. The previous alternative is kept.

◆ emplace() [2/2]

template<Type::SafeValue... Ts>
template<std::size_t I, class... Args>
auto & StormByte::Safe::Variant< Ts >::emplace ( Args &&... args)

Construct the alternative at I, replacing the current one only after construction.

Template Parameters
IAlternative index.
ArgsConstructor argument types.
Parameters
argsArguments forwarded to the alternative.
Returns
The new alternative.
Exceptions
AllocationErrorThe block could not be allocated. The previous alternative is kept.

◆ index()

template<Type::SafeValue... Ts>
std::size_t StormByte::Safe::Variant< Ts >::index ( ) const
noexcept

Index of the active alternative, or npos when valueless.

Returns
Index.

◆ operator std::variant< Ts... >()

template<Type::SafeValue... Ts>
STORMBYTE_FORCE_INLINE StormByte::Safe::Variant< Ts >::operator std::variant< Ts... > ( ) const
inlineexplicit

Copy the active alternative into caller-owned STL storage.

Returns
A std::variant owned by the caller.
Exceptions
BadVariantAccessThis variant is valueless.

◆ operator<=>()

template<Type::SafeValue... Ts>
STORMBYTE_FORCE_INLINE std::strong_ordering StormByte::Safe::Variant< Ts >::operator<=> ( const Variant< Ts > & other) const

Index order, then alternative order when the indices match.

Parameters
otherOther variant.
Returns
Ordering.

◆ operator=() [1/3]

template<Type::SafeValue... Ts>
Variant & StormByte::Safe::Variant< Ts >::operator= ( const Variant< Ts > & other)

Copy-assign.

The replacement is built first.

Parameters
otherSource.
Returns
This variant.
Exceptions
AllocationErrorThe block could not be allocated. The previous alternative is kept.

◆ operator=() [2/3]

template<Type::SafeValue... Ts>
template<class T>
requires (Variant::template AlternativeIndex<T>() != npos)
Variant & StormByte::Safe::Variant< Ts >::operator= ( T && value)
inline

Assign an alternative.

The replacement is built first.

Template Parameters
TAlternative.
Parameters
valueSource.
Returns
This variant.
Exceptions
AllocationErrorThe block could not be allocated. The previous alternative is kept.

◆ operator=() [3/3]

template<Type::SafeValue... Ts>
Variant & StormByte::Safe::Variant< Ts >::operator= ( Variant< Ts > && other)
noexcept

Move-assign.

The block is taken. other becomes valueless.

Parameters
otherSource.
Returns
This variant.

◆ operator==()

template<Type::SafeValue... Ts>
bool StormByte::Safe::Variant< Ts >::operator== ( const Variant< Ts > & other) const

Content equality.

Two valueless variants are equal.

Parameters
otherOther variant.
Returns
Whether the active alternatives are equal.

◆ swap()

template<Type::SafeValue... Ts>
void StormByte::Safe::Variant< Ts >::swap ( Variant< Ts > & other)
noexcept

Exchange storage.

Both blocks stay on Base's heap.

Parameters
otherOther variant.

◆ valueless_by_exception()

template<Type::SafeValue... Ts>
bool StormByte::Safe::Variant< Ts >::valueless_by_exception ( ) const
noexcept

Whether a throwing operation left this variant without an alternative.

Returns
Valueless state. emplace does not enter it.

◆ get_if [1/4]

template<Type::SafeValue... Ts>
template<class T, Type::SafeValue... Us>
const T * get_if ( const Variant< Us... > * value)
friend

◆ get_if [2/4]

template<Type::SafeValue... Ts>
template<std::size_t I, Type::SafeValue... Us>
const auto * get_if ( const Variant< Us... > * value)
friend

◆ get_if [3/4]

template<Type::SafeValue... Ts>
template<class T, Type::SafeValue... Us>
T * get_if ( Variant< Us... > * value)
friend

◆ get_if [4/4]

template<Type::SafeValue... Ts>
template<std::size_t I, Type::SafeValue... Us>
auto * get_if ( Variant< Us... > * value)
friend

◆ visit [1/2]

template<Type::SafeValue... Ts>
template<class Visitor, Type::SafeValue... Us>
decltype(auto) visit ( Visitor && visitor,
const Variant< Us... > & value )
friend

◆ visit [2/2]

template<Type::SafeValue... Ts>
template<class Visitor, Type::SafeValue... Us>
decltype(auto) visit ( Visitor && visitor,
Variant< Us... > & value )
friend

Member Data Documentation

◆ npos

template<Type::SafeValue... Ts>
std::size_t StormByte::Safe::Variant< Ts >::npos = static_cast<std::size_t>(-1)
staticconstexpr

No alternative.


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