Zserio C++17 runtime library  1.1.0
Built for Zserio 2.18.1
zserio::BasicVariant< ALLOC, INDEX, T > Class Template Reference

#include <Variant.h>

Public Types

using allocator_type = ALLOC
 
using IndexType = INDEX
 

Public Member Functions

 BasicVariant ()
 
 BasicVariant (const ALLOC &allocator)
 
template<INDEX I, typename... ARGS, std::enable_if_t<!is_first_allocator_v< ARGS... >> * = nullptr>
 BasicVariant (in_place_index_t< I >, ARGS &&... args)
 
template<INDEX I, typename... ARGS>
 BasicVariant (in_place_index_t< I >, const ALLOC &allocator, ARGS &&... args)
 
 BasicVariant (const BasicVariant &other)
 
 BasicVariant (const BasicVariant &other, const ALLOC &allocator)
 
BasicVariantoperator= (const BasicVariant &other)
 
 BasicVariant (BasicVariant &&other)
 
 BasicVariant (BasicVariant &&other, const ALLOC &allocator)
 
 ~BasicVariant ()=default
 
BasicVariantoperator= (BasicVariant &&other)
 
bool valueless_by_exception () const noexcept
 
template<INDEX I, typename... ARGS>
decltype(auto) emplace (ARGS &&... args)
 
INDEX index () const noexcept
 
template<INDEX I, typename U = detail::type_at_t<I, T...>>
U * get_if () noexcept
 
template<INDEX I, typename U = detail::type_at_t<I, T...>>
const U * get_if () const noexcept
 
template<INDEX I, typename U = detail::type_at_t<I, T...>>
U & get ()
 
template<INDEX I, typename U = detail::type_at_t<I, T...>>
const U & get () const
 
void swap (BasicVariant &other)
 
template<typename F >
auto visit (F &&fun) -> decltype(fun(std::declval< detail::type_at_t< 0, T... >>()))
 
template<typename F >
auto visit (F &&fun) const -> decltype(fun(std::declval< detail::type_at_t< 0, T... >>()))
 
bool operator== (const BasicVariant &other) const
 
bool operator!= (const BasicVariant &other) const
 
bool operator< (const BasicVariant &other) const
 
bool operator> (const BasicVariant &other) const
 
bool operator<= (const BasicVariant &other) const
 
bool operator>= (const BasicVariant &other) const
 

Detailed Description

template<typename ALLOC, typename INDEX, typename... T>
class zserio::BasicVariant< ALLOC, INDEX, T >

Implementation of variant type using BasicAny which allocates memory when selected type doesn't fit under 3*sizeof(void*)

Largely compatible with std::variant with following differences:

  • access only through index whose type has to be specified (by design)
  • supplied INDEX type needs to be convertible to/from size_t and std::variant_npos
  • holds_alternative() omitted
  • get/get_if() exist as member functions as well

Definition at line 102 of file Variant.h.

Member Typedef Documentation

◆ allocator_type

template<typename ALLOC , typename INDEX , typename... T>
using zserio::BasicVariant< ALLOC, INDEX, T >::allocator_type = ALLOC

Definition at line 107 of file Variant.h.

◆ IndexType

template<typename ALLOC , typename INDEX , typename... T>
using zserio::BasicVariant< ALLOC, INDEX, T >::IndexType = INDEX

Definition at line 108 of file Variant.h.

Constructor & Destructor Documentation

◆ BasicVariant() [1/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( )
inline

Empty constructor.

Definition at line 113 of file Variant.h.

◆ BasicVariant() [2/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( const ALLOC &  allocator)
inlineexplicit

Constructor from given allocator.

Parameters
allocatorAllocator to be used.
Exceptions
canthrow any exception thrown by T[0]

Definition at line 124 of file Variant.h.

◆ BasicVariant() [3/8]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS, std::enable_if_t<!is_first_allocator_v< ARGS... >> * = nullptr>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( in_place_index_t< I >  ,
ARGS &&...  args 
)
inlineexplicit

Constructor with initial emplacement.

Parameters
in_place_indexIndex of the active element.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T[I]

Definition at line 139 of file Variant.h.

◆ BasicVariant() [4/8]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( in_place_index_t< I >  ,
const ALLOC &  allocator,
ARGS &&...  args 
)
inline

Constructor with allocator and initial emplacement.

Parameters
in_place_indexIndex of the active element.
allocatorAllocator to be used.
argsArguments to be forwarded for element construction.
Exceptions
canthrow any exception thrown by T[I]

Definition at line 154 of file Variant.h.

◆ BasicVariant() [5/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( const BasicVariant< ALLOC, INDEX, T > &  other)
inline

Copy constructor.

Parameters
otherVariant to copy.

Definition at line 165 of file Variant.h.

◆ BasicVariant() [6/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( const BasicVariant< ALLOC, INDEX, T > &  other,
const ALLOC &  allocator 
)
inline

Allocator-extended copy constructor.

Parameters
otherVariant to copy.
allocatorAllocator to be used for dynamic memory allocations.
Exceptions
canthrow any exception thrown by the active element in other

Definition at line 178 of file Variant.h.

◆ BasicVariant() [7/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( BasicVariant< ALLOC, INDEX, T > &&  other)
inline

Move constructor.

Parameters
otherVariant to move from.
Exceptions
canthrow any exception thrown by the active element in other

Definition at line 206 of file Variant.h.

◆ BasicVariant() [8/8]

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::BasicVariant ( BasicVariant< ALLOC, INDEX, T > &&  other,
const ALLOC &  allocator 
)
inline

Allocator-extended move constructor.

Parameters
otherVariant to move from.
allocatorAllocator to be used for dynamic memory allocations.
Exceptions
canthrow any exception thrown by the active element in other

Definition at line 219 of file Variant.h.

◆ ~BasicVariant()

template<typename ALLOC , typename INDEX , typename... T>
zserio::BasicVariant< ALLOC, INDEX, T >::~BasicVariant ( )
default

Desctructor.

Member Function Documentation

◆ emplace()

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS>
decltype(auto) zserio::BasicVariant< ALLOC, INDEX, T >::emplace ( ARGS &&...  args)
inline

Constructs a value of type U at index I with given arguments.

Parameters
argsArguments to be forwarded for element construction.

Definition at line 266 of file Variant.h.

◆ get() [1/2]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename U = detail::type_at_t<I, T...>>
U& zserio::BasicVariant< ALLOC, INDEX, T >::get ( )
inline

Returns element at index I. Throws if index doesn't match

Exceptions
BadVariantAccessif the requested index doesn't match.

Definition at line 314 of file Variant.h.

◆ get() [2/2]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename U = detail::type_at_t<I, T...>>
const U& zserio::BasicVariant< ALLOC, INDEX, T >::get ( ) const
inline

Returns element at index I. Throws if index doesn't match

Exceptions
BadVariantAccessif the requested index doesn't match.

Definition at line 331 of file Variant.h.

◆ get_if() [1/2]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename U = detail::type_at_t<I, T...>>
const U* zserio::BasicVariant< ALLOC, INDEX, T >::get_if ( ) const
inlinenoexcept

Returns a pointer to an element at index I or nullptr if index doesn't match.

Definition at line 299 of file Variant.h.

◆ get_if() [2/2]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename U = detail::type_at_t<I, T...>>
U* zserio::BasicVariant< ALLOC, INDEX, T >::get_if ( )
inlinenoexcept

Returns a pointer to an element at index I or nullptr if index doesn't match.

Definition at line 286 of file Variant.h.

◆ index()

template<typename ALLOC , typename INDEX , typename... T>
INDEX zserio::BasicVariant< ALLOC, INDEX, T >::index ( ) const
inlinenoexcept

Returns an index of active element.

Definition at line 277 of file Variant.h.

◆ operator!=()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator!= ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant inequality test.

Parameters
otherVariant to compare with.

Definition at line 439 of file Variant.h.

◆ operator<()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator< ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant less-than test.

Parameters
otherVariant to compare with.

Definition at line 449 of file Variant.h.

◆ operator<=()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator<= ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant less-or-equal test.

Parameters
otherVariant to compare with.

Definition at line 478 of file Variant.h.

◆ operator=() [1/2]

template<typename ALLOC , typename INDEX , typename... T>
BasicVariant& zserio::BasicVariant< ALLOC, INDEX, T >::operator= ( BasicVariant< ALLOC, INDEX, T > &&  other)
inline

Move assignment operator.

Parameters
otherVariant to move from.
Returns
Reference to this.

Definition at line 236 of file Variant.h.

◆ operator=() [2/2]

template<typename ALLOC , typename INDEX , typename... T>
BasicVariant& zserio::BasicVariant< ALLOC, INDEX, T >::operator= ( const BasicVariant< ALLOC, INDEX, T > &  other)
inline

Copy assignment operator.

Parameters
otherVariant to copy.
Returns
Reference to this.
Exceptions
canthrow any exception thrown by the active element in other

Definition at line 192 of file Variant.h.

◆ operator==()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator== ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant equality test.

Parameters
otherVariant to compare with.

Definition at line 420 of file Variant.h.

◆ operator>()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator> ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant greater-than test.

Parameters
otherVariant to compare with.

Definition at line 468 of file Variant.h.

◆ operator>=()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::operator>= ( const BasicVariant< ALLOC, INDEX, T > &  other) const
inline

Variant greater-or-equal test.

Parameters
otherVariant to compare with.

Definition at line 488 of file Variant.h.

◆ swap()

template<typename ALLOC , typename INDEX , typename... T>
void zserio::BasicVariant< ALLOC, INDEX, T >::swap ( BasicVariant< ALLOC, INDEX, T > &  other)
inline

Swaps content of variant with other.

Parameters
otherOther variant to swap content with.

Definition at line 347 of file Variant.h.

◆ valueless_by_exception()

template<typename ALLOC , typename INDEX , typename... T>
bool zserio::BasicVariant< ALLOC, INDEX, T >::valueless_by_exception ( ) const
inlinenoexcept

Reports if variant is in nonstandard empty state. Note: which operations lead to an empty state is not specified by ISO C++

As an zserio extension, the valueless state can arise after move. This happens only When the held alternative is stored on the heap, and the used allocator is the same in both variants. In that case, the underlying pointer is stolen and the moved-out variant remains in the valueless state.

Definition at line 255 of file Variant.h.

◆ visit() [1/2]

template<typename ALLOC , typename INDEX , typename... T>
template<typename F >
auto zserio::BasicVariant< ALLOC, INDEX, T >::visit ( F &&  fun) -> decltype(fun(std::declval<detail::type_at_t<0, T...>>()))
inline

Calls a given functor with active element.

Parameters
funFunctor to be called on the active element.
Exceptions
BadVariantAccessif variant is in valueless state.

Definition at line 361 of file Variant.h.

◆ visit() [2/2]

template<typename ALLOC , typename INDEX , typename... T>
template<typename F >
auto zserio::BasicVariant< ALLOC, INDEX, T >::visit ( F &&  fun) const -> decltype(fun(std::declval<detail::type_at_t<0, T...>>()))
inline

Calls a given functor with active element.

Parameters
funFunctor to be called on the active element.
Exceptions
BadVariantAccessif variant is in valueless state.

Definition at line 392 of file Variant.h.


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