SPH
Public Member Functions | Static Public Member Functions | List of all members
Variant< TArgs > Class Template Reference

Variant, an implementation of type-safe union, similar to std::variant or boost::variant. More...

#include <Variant.h>

Public Member Functions

 Variant ()
 Default constructor, constructs the object of type listed first in the type list using its default constructor. More...
 
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
 Variant (T &&value)
 Construct variant from value of stored type. More...
 
 Variant (const ConstructTypeIdxTag, const Size typeIdx)
 Default-constructs a type with given type index. More...
 
 Variant (const Variant &other)
 
 Variant (Variant &&other)
 
 ~Variant ()
 
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
Variantoperator= (T &&value)
 Universal copy/move operator with type of rhs being one of stored types. More...
 
Variantoperator= (const Variant &other)
 Assigns another variant into this. More...
 
Variantoperator= (Variant &&other)
 Moves another variant into this. More...
 
void swap (Variant &other)
 Swaps value stored in the variant with value of different variant. More...
 
template<typename T , typename... Ts>
void emplace (Ts &&... args)
 Creates a value of type T in place. More...
 
INLINE Size getTypeIdx () const
 Returns index of type currently stored in variant. More...
 
template<typename T >
INLINE bool has () const
 Checks if the variant currently hold value of given type. More...
 
template<typename T >
INLINE T & get ()
 Returns the stored value. More...
 
template<typename T >
INLINE const T & get () const
 Returns the stored value, const version. More...
 
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
 operator T& ()
 Implicit conversion to one of stored values. More...
 
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
 operator const T & () const
 Const version of conversion operator. More...
 
template<typename T >
Optional< T > tryGet () const
 Returns the stored value in the variant. More...
 

Static Public Member Functions

template<typename T >
static constexpr INLINE bool canHold ()
 Checks if the given type is one of the listed ones. More...
 

Detailed Description

template<typename... TArgs>
class Variant< TArgs >

Variant, an implementation of type-safe union, similar to std::variant or boost::variant.

Definition at line 171 of file Variant.h.

Constructor & Destructor Documentation

◆ Variant() [1/5]

template<typename... TArgs>
Variant< TArgs >::Variant ( )
inline

Default constructor, constructs the object of type listed first in the type list using its default constructor.

The default constructor must exist.

Definition at line 188 of file Variant.h.

◆ Variant() [2/5]

template<typename... TArgs>
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
Variant< TArgs >::Variant ( T &&  value)
inline

Construct variant from value of stored type.

Definition at line 196 of file Variant.h.

◆ Variant() [3/5]

template<typename... TArgs>
Variant< TArgs >::Variant ( const  ConstructTypeIdxTag,
const Size  typeIdx 
)
inline

Default-constructs a type with given type index.

Useful for (de)serialization of variant.

Definition at line 207 of file Variant.h.

◆ Variant() [4/5]

template<typename... TArgs>
Variant< TArgs >::Variant ( const Variant< TArgs > &  other)
inline

Definition at line 214 of file Variant.h.

◆ Variant() [5/5]

template<typename... TArgs>
Variant< TArgs >::Variant ( Variant< TArgs > &&  other)
inline

Definition at line 220 of file Variant.h.

◆ ~Variant()

template<typename... TArgs>
Variant< TArgs >::~Variant ( )
inline

Definition at line 226 of file Variant.h.

Member Function Documentation

◆ canHold()

template<typename... TArgs>
template<typename T >
static constexpr INLINE bool Variant< TArgs >::canHold ( )
inlinestaticconstexpr

Checks if the given type is one of the listed ones.

Returns true even for references and const/volatile-qualified types.

Definition at line 325 of file Variant.h.

◆ emplace()

template<typename... TArgs>
template<typename T , typename... Ts>
void Variant< TArgs >::emplace ( Ts &&...  args)
inline

Creates a value of type T in place.

Type T must be one of variant types. Any previously stored value is destroyed.

Definition at line 300 of file Variant.h.

◆ get() [1/2]

template<typename... TArgs>
template<typename T >
INLINE T& Variant< TArgs >::get ( )
inline

Returns the stored value.

Performs a compile-time check that the type is contained in Variant, and runtime check that the variant currently holds value of given type.

Definition at line 335 of file Variant.h.

◆ get() [2/2]

template<typename... TArgs>
template<typename T >
INLINE const T& Variant< TArgs >::get ( ) const
inline

Returns the stored value, const version.

Definition at line 344 of file Variant.h.

◆ getTypeIdx()

template<typename... TArgs>
INLINE Size Variant< TArgs >::getTypeIdx ( ) const
inline

Returns index of type currently stored in variant.

Definition at line 310 of file Variant.h.

◆ has()

template<typename... TArgs>
template<typename T >
INLINE bool Variant< TArgs >::has ( ) const
inline

Checks if the variant currently hold value of given type.

Definition at line 316 of file Variant.h.

◆ operator const T &()

template<typename... TArgs>
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
Variant< TArgs >::operator const T & ( ) const
inline

Const version of conversion operator.

Definition at line 362 of file Variant.h.

◆ operator T&()

template<typename... TArgs>
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
Variant< TArgs >::operator T& ( )
inline

Implicit conversion to one of stored values.

Performs a compile-time check that the type is contained in Variant, and runtime check that the variant currently holds value of given type.

Definition at line 356 of file Variant.h.

◆ operator=() [1/3]

template<typename... TArgs>
Variant& Variant< TArgs >::operator= ( const Variant< TArgs > &  other)
inline

Assigns another variant into this.

If the type of the currently stored value is the same as type in rhs, the copy operator is utilized, otherwise the current value (if there is one) is destroyed and a new value is created using copy constructor.

Definition at line 257 of file Variant.h.

◆ operator=() [2/3]

template<typename... TArgs>
template<typename T , typename = std::enable_if_t<!std::is_same<std::decay_t<T>, Variant>::value>>
Variant& Variant< TArgs >::operator= ( T &&  value)
inline

Universal copy/move operator with type of rhs being one of stored types.

Valid type is checked by static assert. If the type of rhs is the same as the type currently stored in variant, the value is copy/move assigned, otherwise the current value is destroyed and a copy/move constructor is used.

Definition at line 236 of file Variant.h.

◆ operator=() [3/3]

template<typename... TArgs>
Variant& Variant< TArgs >::operator= ( Variant< TArgs > &&  other)
inline

Moves another variant into this.

If the type of the currently stored value is the same as type in rhs, the move operator is utilized, otherwise the current value is destroyed and a new value is created using move constructor.

Definition at line 274 of file Variant.h.

◆ swap()

template<typename... TArgs>
void Variant< TArgs >::swap ( Variant< TArgs > &  other)
inline

Swaps value stored in the variant with value of different variant.

Both variants must hold the value of the same type, checked by assert.

Definition at line 290 of file Variant.h.

◆ tryGet()

template<typename... TArgs>
template<typename T >
Optional<T> Variant< TArgs >::tryGet ( ) const
inline

Returns the stored value in the variant.

Safer than implicit conversion as it returns NOTHING in case the value is currently not stored in variant.

Definition at line 371 of file Variant.h.


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