iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
iox2::legacy::variant< Types > Class Template Referencefinal

Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 standard but it has changes in get and emplace since we are not allowed to throw exceptions. More...

#include <variant.hpp>

Public Member Functions

constexpr variant () noexcept=default
 the default constructor constructs a variant which does not contain an element and returns INVALID_VARIANT_INDEX when .index() is called
template<uint64_t N, typename... CTorArguments>
constexpr variant (const in_place_index< N > &index, CTorArguments &&... args) noexcept
 creates a variant and perform an in place construction of the type stored at index N. If the index N is out of bounds you get a compiler error.
template<typename T, typename... CTorArguments>
constexpr variant (const in_place_type< T > &type, CTorArguments &&... args) noexcept
 creates a variant and perform an in place construction of the type T. If T is not part of the variant you get a compiler error.
template<typename T, typename = std::enable_if_t<!std::is_same<std::decay_t<T>, variant>::value>, typename std::enable_if_t<!internal::is_in_place_index< std::decay_t< T > >::value, bool > = false, typename std::enable_if_t<!internal::is_in_place_type< std::decay_t< T > >::value, bool > = false>
constexpr variant (T &&arg) noexcept
 creates a variant from a user supplied value
constexpr variant (const variant &rhs) noexcept
 if the variant contains an element the elements copy constructor is called otherwise an empty variant is copied
constexpr variant & operator= (const variant &rhs) noexcept
 if the variant contains an element the elements copy assignment operator is called otherwise an empty variant is copied
constexpr variant (variant &&rhs) noexcept
 if the variant contains an element the elements move constructor is called otherwise an empty variant is moved
constexpr variant & operator= (variant &&rhs) noexcept
 if the variant contains an element the elements move assignment operator is called otherwise an empty variant is moved
 ~variant () noexcept
 if the variant contains an element the elements destructor is called otherwise nothing happens
template<typename T>
std::enable_if<!std::is_same< T, variant< Types... > & >::value, variant< Types... > >::type & operator= (T &&rhs) noexcept
 if the variant contains an element the elements assignment operator is called otherwise we have undefined behavior. It is important that you make sure that the variant really contains that type T.
template<uint64_t TypeIndex, typename... CTorArguments>
void emplace_at_index (CTorArguments &&... args) noexcept
 calls the constructor of the type at index TypeIndex and perfectly forwards the arguments to this constructor. (not stl compliant)
template<typename T, typename... CTorArguments>
void emplace (CTorArguments &&... args) noexcept
 calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.
template<uint64_t TypeIndex>
internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index () noexcept
 returns a pointer to the type stored at index TypeIndex. (not stl compliant)
template<uint64_t TypeIndex>
internal::get_type_at_index< 0, TypeIndex, Types... >::type * unsafe_get_at_index_unchecked () noexcept
 returns a pointer to the type stored at index TypeIndex. (not stl compliant)
template<uint64_t TypeIndex>
const internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index () const noexcept
 returns a pointer to the type stored at index TypeIndex. (not stl compliant)
template<uint64_t TypeIndex>
const internal::get_type_at_index< 0, TypeIndex, Types... >::type * unsafe_get_at_index_unchecked () const noexcept
 returns a pointer to the type stored at index TypeIndex. (not stl compliant)
template<typename T>
const T * get () const noexcept
 returns a pointer to the type T stored in the variant. (not stl compliant)
template<typename T>
T * get () noexcept
 returns a pointer to the type T stored in the variant. (not stl compliant)
template<typename T>
T * get_if (T *defaultValue) noexcept
 returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaultValue
template<typename T>
const T * get_if (const T *defaultValue) const noexcept
 returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaultValue
constexpr uint64_t index () const noexcept
 returns the index of the stored type in the variant. if the variant does not contain any type it returns INVALID_VARIANT_INDEX

Friends

template<typename... Ts>
constexpr bool operator== (const variant< Ts... > &lhs, const variant< Ts... > &rhs) noexcept
template<typename... Ts>
constexpr bool operator!= (const variant< Ts... > &lhs, const variant< Ts... > &rhs) noexcept

Detailed Description

template<typename... Types>
class iox2::legacy::variant< Types >

Variant implementation from the C++17 standard with C++11. The interface is inspired by the C++17 standard but it has changes in get and emplace since we are not allowed to throw exceptions.

Parameters
Types...variadic list of types which the variant should be able to store
#include <iostream>
// ... do stuff
if ( someVariant.index() == INVALID_VARIANT_INDEX )
{
someVariant.emplace<float>(123.456f);
}
else if ( someVariant.index() == 1)
{
auto blubb = someVariant.template get_at_index<1>();
IOX2_LOG(Info, *blubb);
auto sameAsBlubb = someVariant.get<float>();
IOX2_LOG(Info, *sameAsBlubb);
}
// .. do stuff
int defaultValue = 123;
int * fuu = someVariant.get_if<int>(&defaultValue);
IOX2_LOG(Info, *fuu);
const T * get() const noexcept
returns a pointer to the type T stored in the variant. (not stl compliant)
void emplace(CTorArguments &&... args) noexcept
calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.
T * get_if(T *defaultValue) noexcept
returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaul...
constexpr uint64_t index() const noexcept
returns the index of the stored type in the variant. if the variant does not contain any type it retu...
internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index() noexcept
returns a pointer to the type stored at index TypeIndex. (not stl compliant)
constexpr variant() noexcept=default
the default constructor constructs a variant which does not contain an element and returns INVALID_VA...
#define IOX2_LOG(level, msg_stream)
Macro for logging.
Definition logging.hpp:63
static constexpr uint64_t INVALID_VARIANT_INDEX
value which an invalid variant index occupies
Definition variant.hpp:65

Definition at line 101 of file variant.hpp.

Constructor & Destructor Documentation

◆ variant() [1/6]

template<typename... Types>
iox2::legacy::variant< Types >::variant ( )
constexprdefaultnoexcept

the default constructor constructs a variant which does not contain an element and returns INVALID_VARIANT_INDEX when .index() is called

Referenced by operator!=, operator=(), operator=(), operator==, variant(), variant(), variant(), variant(), variant(), and ~variant().

◆ variant() [2/6]

template<typename... Types>
template<uint64_t N, typename... CTorArguments>
iox2::legacy::variant< Types >::variant ( const in_place_index< N > & index,
CTorArguments &&... args )
explicitconstexprnoexcept

creates a variant and perform an in place construction of the type stored at index N. If the index N is out of bounds you get a compiler error.

Template Parameters

in] N index where to perform the placement new

Template Parameters

in] CTorArguments variadic types of the c'tor arguments

Parameters
[in]indexindex of the type which should be constructed
[in]argsvariadic list of arguments which will be forwarded to the constructor to the type at index

References index(), and variant().

◆ variant() [3/6]

template<typename... Types>
template<typename T, typename... CTorArguments>
iox2::legacy::variant< Types >::variant ( const in_place_type< T > & type,
CTorArguments &&... args )
explicitconstexprnoexcept

creates a variant and perform an in place construction of the type T. If T is not part of the variant you get a compiler error.

Template Parameters

in] T type which should be created inside the variant

Template Parameters

in] CTorArguments variadic types of the c'tor arguments

Parameters
[in]typetype which should be created inside the variant
[in]argsvariadic list of arguments which will be forwarded to the constructor to the type

References variant().

◆ variant() [4/6]

template<typename... Types>
template<typename T, typename = std::enable_if_t<!std::is_same<std::decay_t<T>, variant>::value>, typename std::enable_if_t<!internal::is_in_place_index< std::decay_t< T > >::value, bool > = false, typename std::enable_if_t<!internal::is_in_place_type< std::decay_t< T > >::value, bool > = false>
iox2::legacy::variant< Types >::variant ( T && arg)
explicitconstexprnoexcept

creates a variant from a user supplied value

Template Parameters

in] T type of the value to be stored in the variant

Parameters
[in]argarg to be forwared to the c'tor of T

References variant().

◆ variant() [5/6]

template<typename... Types>
iox2::legacy::variant< Types >::variant ( const variant< Types > & rhs)
constexprnoexcept

if the variant contains an element the elements copy constructor is called otherwise an empty variant is copied

Parameters
[in]rhssource of the copy

References variant().

◆ variant() [6/6]

template<typename... Types>
iox2::legacy::variant< Types >::variant ( variant< Types > && rhs)
constexprnoexcept

if the variant contains an element the elements move constructor is called otherwise an empty variant is moved

Parameters
[in]rhssource of the move
Note
The move c'tor does not explicitly invalidate the moved-from object but relies on the move c'tor of Types to correctly invalidate the stored object

References variant().

◆ ~variant()

template<typename... Types>
iox2::legacy::variant< Types >::~variant ( )
noexcept

if the variant contains an element the elements destructor is called otherwise nothing happens

References variant().

Member Function Documentation

◆ emplace()

template<typename... Types>
template<typename T, typename... CTorArguments>
void iox2::legacy::variant< Types >::emplace ( CTorArguments &&... args)
noexcept

calls the constructor of the type T and perfectly forwards the arguments to the constructor of T.

Template Parameters

in] T type which is created inside the variant

Template Parameters

in] CTorArguments variadic types of the c'tor arguments

References emplace().

Referenced by emplace().

◆ emplace_at_index()

template<typename... Types>
template<uint64_t TypeIndex, typename... CTorArguments>
void iox2::legacy::variant< Types >::emplace_at_index ( CTorArguments &&... args)
noexcept

calls the constructor of the type at index TypeIndex and perfectly forwards the arguments to this constructor. (not stl compliant)

Template Parameters
TypeIndexindex of the type which will be created
CTorArgumentsvariadic types of the c'tor arguments
Parameters
[in]argsarguments which will be forwarded to the constructor to the type at TypeIndex

References emplace_at_index().

Referenced by emplace_at_index().

◆ get() [1/2]

template<typename... Types>
template<typename T>
const T * iox2::legacy::variant< Types >::get ( ) const
noexcept

returns a pointer to the type T stored in the variant. (not stl compliant)

Template Parameters

in] T type of the returned pointer

Returns
if the variant does contain the type T it returns a valid pointer otherwise if the variant does contain no type at all or a different type it returns nullptr

References get().

Referenced by get(), and get().

◆ get() [2/2]

template<typename... Types>
template<typename T>
T * iox2::legacy::variant< Types >::get ( )
noexcept

returns a pointer to the type T stored in the variant. (not stl compliant)

Template Parameters

in] T type of the returned pointer

Returns
if the variant does contain the type T it returns a valid pointer otherwise if the variant does contain no type at all or a different type it returns nullptr

References get().

◆ get_at_index() [1/2]

template<typename... Types>
template<uint64_t TypeIndex>
const internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox2::legacy::variant< Types >::get_at_index ( ) const
noexcept

returns a pointer to the type stored at index TypeIndex. (not stl compliant)

Template Parameters

in] TypeIndex index of the stored type

Returns
if the variant does contain the type at index TypeIndex it returns a valid pointer, if it does contain no type at all or a different type it returns nullptr.
int * someNumber = someVariant.template get_at_index<0>();
helper struct to perform an emplacement of a predefined type in in the constructor of a variant
Definition variant.hpp:49

References get_at_index().

◆ get_at_index() [2/2]

template<typename... Types>
template<uint64_t TypeIndex>
internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox2::legacy::variant< Types >::get_at_index ( )
noexcept

returns a pointer to the type stored at index TypeIndex. (not stl compliant)

Template Parameters

in] TypeIndex index of the stored type

Returns
if the variant does contain the type at index TypeIndex it returns a valid pointer, if it does contain no type at all or a different type it returns nullptr.
int * someNumber = someVariant.template get_at_index<0>();

References get_at_index().

Referenced by get_at_index(), and get_at_index().

◆ get_if() [1/2]

template<typename... Types>
template<typename T>
const T * iox2::legacy::variant< Types >::get_if ( const T * defaultValue) const
noexcept

returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaultValue

Template Parameters

in] T type of the returned pointer

Parameters
[in]defaultValuevalue which is returned in case of empty or different type in variant
Returns
pointer to the stored value if it is of type T, otherwise defaultValue

References get_if().

◆ get_if() [2/2]

template<typename... Types>
template<typename T>
T * iox2::legacy::variant< Types >::get_if ( T * defaultValue)
noexcept

returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaultValue

Returns
pointer to the stored value if it is of type T, otherwise defaultValue

References get_if().

Referenced by get_if(), and get_if().

◆ index()

template<typename... Types>
uint64_t iox2::legacy::variant< Types >::index ( ) const
constexprnoexcept

returns the index of the stored type in the variant. if the variant does not contain any type it returns INVALID_VARIANT_INDEX

Returns
index of the stored type

References index().

Referenced by index(), and variant().

◆ operator=() [1/3]

template<typename... Types>
variant & iox2::legacy::variant< Types >::operator= ( const variant< Types > & rhs)
constexprnoexcept

if the variant contains an element the elements copy assignment operator is called otherwise an empty variant is copied

Parameters
[in]rhssource of the copy assignment
Returns
reference to the variant itself

References variant().

◆ operator=() [2/3]

template<typename... Types>
template<typename T>
std::enable_if<!std::is_same< T, variant< Types... > & >::value, variant< Types... > >::type & iox2::legacy::variant< Types >::operator= ( T && rhs)
noexcept

if the variant contains an element the elements assignment operator is called otherwise we have undefined behavior. It is important that you make sure that the variant really contains that type T.

Template Parameters

in] T Type of the rhs

Parameters
[in]rhssource object for the underlying move assignment
Returns
reference to the variant itself

◆ operator=() [3/3]

template<typename... Types>
variant & iox2::legacy::variant< Types >::operator= ( variant< Types > && rhs)
constexprnoexcept

if the variant contains an element the elements move assignment operator is called otherwise an empty variant is moved

Parameters
[in]rhssource of the move assignment
Returns
reference to the variant itself
Note
The move assignment operator does not explicitly invalidate the moved-from object but relies on the move assignment operator of Types to correctly invalidate the stored object

References variant().

◆ unsafe_get_at_index_unchecked() [1/2]

template<typename... Types>
template<uint64_t TypeIndex>
const internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox2::legacy::variant< Types >::unsafe_get_at_index_unchecked ( ) const
noexcept

returns a pointer to the type stored at index TypeIndex. (not stl compliant)

Template Parameters

in] TypeIndex index of the stored type

Returns
a const pointer to the type at index
Attention
this function is unsafe and does not check if the type at the index is the current active one; only use this function when it is ensured that the type at index is indeed the current active one; it is undefined behavior to call this on an index which is not the active type

References unsafe_get_at_index_unchecked().

◆ unsafe_get_at_index_unchecked() [2/2]

template<typename... Types>
template<uint64_t TypeIndex>
internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox2::legacy::variant< Types >::unsafe_get_at_index_unchecked ( )
noexcept

returns a pointer to the type stored at index TypeIndex. (not stl compliant)

Template Parameters

in] TypeIndex index of the stored type

Returns
a pointer to the type at index
Attention
this function is unsafe and does not check if the type at the index is the current active one; only use this function when it is ensured that the type at index is indeed the current active one; it is undefined behavior to call this on an index which is not the active type

References unsafe_get_at_index_unchecked().

Referenced by unsafe_get_at_index_unchecked(), and unsafe_get_at_index_unchecked().

◆ operator!=

template<typename... Types>
template<typename... Ts>
bool operator!= ( const variant< Ts... > & lhs,
const variant< Ts... > & rhs )
friend

References variant().

◆ operator==

template<typename... Types>
template<typename... Ts>
bool operator== ( const variant< Ts... > & lhs,
const variant< Ts... > & rhs )
friend

References variant().


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