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

#include <Variant.h>

Inheritance diagram for zserio::BasicVariant< ALLOC, INDEX, T >:
Collaboration diagram for zserio::BasicVariant< ALLOC, INDEX, T >:

Public Types

using allocator_type = ALLOC
 
using IndexType = INDEX
 
using VariantType = std::variant< typename detail::variant_element< T >::type... >
 
- Public Types inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
using allocator_type = ALLOC
 

Public Member Functions

 BasicVariant ()
 
 BasicVariant (const ALLOC &allocator)
 
template<INDEX I, typename... ARGS, std::enable_if_t<!detail::is_variant_heap_allocated_v< I, T... >> * = nullptr, std::enable_if_t<!is_first_allocator_v< ARGS... >> * = nullptr>
 BasicVariant (in_place_index_t< I >, ARGS &&... args)
 
template<INDEX I, typename... ARGS, std::enable_if_t<!detail::is_variant_heap_allocated_v< I, T... >> * = nullptr>
 BasicVariant (in_place_index_t< I >, const ALLOC &allocator, ARGS &&... args)
 
template<INDEX I, typename... ARGS, typename U = detail::type_at_t<static_cast<size_t>(I), T...>, std::enable_if_t< detail::is_variant_heap_allocated_v< I, T... >> * = nullptr>
 BasicVariant (in_place_index_t< I >, const ALLOC &allocator, ARGS &&... args)
 
template<INDEX I, typename... ARGS, typename U = detail::type_at_t<static_cast<size_t>(I), T...>, std::enable_if_t< detail::is_variant_heap_allocated_v< I, T... >> * = nullptr, std::enable_if_t<!is_first_allocator_v< ARGS... >> * = nullptr>
 BasicVariant (in_place_index_t< I >, ARGS &&... args)
 
 ~BasicVariant ()
 
 BasicVariant (const BasicVariant &other)
 
 BasicVariant (const BasicVariant &other, const ALLOC &allocator)
 
BasicVariantoperator= (const BasicVariant &other)
 
 BasicVariant (BasicVariant &&other)
 
 BasicVariant (BasicVariant &&other, const ALLOC &allocator)
 
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
 
- Public Member Functions inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
 AllocatorHolder ()
 
 AllocatorHolder (const allocator_type &allocator)
 
 AllocatorHolder (allocator_type &&allocator)
 
allocator_type get_allocator () const
 
 ~AllocatorHolder ()=default
 
 AllocatorHolder (const AllocatorHolder &other)=default
 
AllocatorHolderoperator= (const AllocatorHolder &other)=default
 
 AllocatorHolder (AllocatorHolder &&other)=default
 
AllocatorHolderoperator= (AllocatorHolder &&other)=default
 

Additional Inherited Members

- Protected Member Functions inherited from zserio::AllocatorHolder< ALLOC, EMPTY >
void set_allocator (const allocator_type &allocator)
 
void set_allocator (allocator_type &&allocator)
 
allocator_typeget_allocator_ref ()
 
const allocator_typeget_allocator_ref () const
 

Detailed Description

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

Implementation of variant type 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
  • supplied INDEX type needs to be convertible to/from size_t
  • holds_alternative() omitted
  • variant_npos omitted
  • get/get_if() exists as member functions as well

Definition at line 93 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 101 of file Variant.h.

◆ IndexType

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

Definition at line 102 of file Variant.h.

◆ VariantType

template<typename ALLOC , typename INDEX , typename... T>
using zserio::BasicVariant< ALLOC, INDEX, T >::VariantType = std::variant<typename detail::variant_element<T>::type...>

Definition at line 103 of file Variant.h.

Constructor & Destructor Documentation

◆ BasicVariant() [1/10]

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

Empty constructor.

Definition at line 108 of file Variant.h.

◆ BasicVariant() [2/10]

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 119 of file Variant.h.

◆ BasicVariant() [3/10]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS, std::enable_if_t<!detail::is_variant_heap_allocated_v< I, T... >> * = nullptr, 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/10]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS, std::enable_if_t<!detail::is_variant_heap_allocated_v< I, T... >> * = nullptr>
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/10]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS, typename U = detail::type_at_t<static_cast<size_t>(I), T...>, std::enable_if_t< detail::is_variant_heap_allocated_v< I, T... >> * = nullptr>
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 170 of file Variant.h.

◆ BasicVariant() [6/10]

template<typename ALLOC , typename INDEX , typename... T>
template<INDEX I, typename... ARGS, typename U = detail::type_at_t<static_cast<size_t>(I), T...>, std::enable_if_t< detail::is_variant_heap_allocated_v< I, T... >> * = nullptr, 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 186 of file Variant.h.

◆ ~BasicVariant()

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

Destructor.

Definition at line 193 of file Variant.h.

◆ BasicVariant() [7/10]

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 203 of file Variant.h.

◆ BasicVariant() [8/10]

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 218 of file Variant.h.

◆ BasicVariant() [9/10]

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 255 of file Variant.h.

◆ BasicVariant() [10/10]

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 269 of file Variant.h.

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

Sets any value to variant.

Parameters
argsArguments to be forwarded for element construction.

Definition at line 324 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 402 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 419 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 380 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 360 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 342 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 520 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 530 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 554 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 282 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 233 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 506 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 544 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 564 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 435 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 305 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 453 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 481 of file Variant.h.


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