iceoryx_doc  1.0.1
Public Member Functions | List of all members
iox::cxx::variant< Types > Class Template Reference

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

 variant ()=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>
 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. More...
 
template<typename T , typename... CTorArguments>
 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. More...
 
 variant (const variant &rhs) noexcept
 if the variant contains an element the elements copy constructor is called otherwise an empty variant is copied More...
 
variantoperator= (const variant &rhs) noexcept
 if the variant contains an element the elements copy assignment operator is called otherwise an empty variant is copied More...
 
 variant (variant &&rhs) noexcept
 if the variant contains an element the elements move constructor is called otherwise an empty variant is moved More...
 
variantoperator= (variant &&rhs) noexcept
 if the variant contains an element the elements move assignment operator is called otherwise an empty variant is moved More...
 
 ~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. More...
 
template<uint64_t TypeIndex, typename... CTorArguments>
bool 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) More...
 
template<typename T , typename... CTorArguments>
bool emplace (CTorArguments &&... args) noexcept
 calls the constructor of the type T and perfectly forwards the arguments to the constructor of T. More...
 
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) More...
 
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) More...
 
template<typename T >
const T * get () const noexcept
 returns a pointer to the type T stored in the variant. (not stl compliant) More...
 
template<typename T >
T * get () noexcept
 returns a pointer to the type T stored in the variant. (not stl compliant) More...
 
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 More...
 
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 More...
 
constexpr size_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 More...
 

Detailed Description

template<typename... Types>
class iox::cxx::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 "iceoryx_utils/cxx/variant.hpp"
#include <iostream>
cxx::variant<int, float, double> someVariant;
// ... 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>();
std::cout << *blubb << std::endl;
auto sameAsBlubb = someVariant.get<float>();
std::cout << *sameAsBlubb << std::endl;
}
// .. do stuff
int defaultValue = 123;
int * fuu = someVariant.get_if<int>(&defaultValue);
std::cout << *fuu << std::endl;

Constructor & Destructor Documentation

◆ variant() [1/4]

template<typename... Types>
template<uint64_t N, typename... CTorArguments>
iox::cxx::variant< Types >::variant ( const in_place_index< N > &  index,
CTorArguments &&...  args 
)
inlinenoexcept

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.

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

◆ variant() [2/4]

template<typename... Types>
template<typename T , typename... CTorArguments>
iox::cxx::variant< Types >::variant ( const in_place_type< T > &  type,
CTorArguments &&...  args 
)
inlinenoexcept

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.

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

◆ variant() [3/4]

template<typename... Types>
iox::cxx::variant< Types >::variant ( const variant< Types > &  rhs)
inlinenoexcept

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

Parameters
[in]rhssource of the copy

◆ variant() [4/4]

template<typename... Types>
iox::cxx::variant< Types >::variant ( variant< Types > &&  rhs)
noexcept

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

Parameters
[in]rhssource of the move

Member Function Documentation

◆ emplace()

template<typename... Types>
template<typename T , typename... CTorArguments>
bool iox::cxx::variant< Types >::emplace ( CTorArguments &&...  args)
inlinenoexcept

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

Returns
if the variant already contains a different type it returns false, if the construction was successful it returns true

◆ emplace_at_index()

template<typename... Types>
template<uint64_t TypeIndex, typename... CTorArguments>
bool iox::cxx::variant< Types >::emplace_at_index ( CTorArguments &&...  args)
inlinenoexcept

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

Parameters
TypeIndexindex of the type which will be created
[in]argsarguments which will be forwarded to the constructor to the type at TypeIndex
Returns
if the variant already contains a different type it returns false, if the construction was successful it returns true

◆ get() [1/2]

template<typename... Types>
template<typename T >
const T * iox::cxx::variant< Types >::get
inlinenoexcept

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

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

◆ get() [2/2]

template<typename... Types>
template<typename T >
T * iox::cxx::variant< Types >::get
inlinenoexcept

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

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

◆ get_at_index() [1/2]

template<typename... Types>
template<uint64_t TypeIndex>
const internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox::cxx::variant< Types >::get_at_index
inlinenoexcept

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

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.
cxx::variant<int, float> someVariant(cxx::in_place_type<int>(), 12);
int * someNumber = someVariant.template get_at_index<0>();

◆ get_at_index() [2/2]

template<typename... Types>
template<uint64_t TypeIndex>
internal::get_type_at_index< 0, TypeIndex, Types... >::type * iox::cxx::variant< Types >::get_at_index
inlinenoexcept

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

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.
cxx::variant<int, float> someVariant(cxx::in_place_type<int>(), 12);
int * someNumber = someVariant.template get_at_index<0>();

◆ get_if() [1/2]

template<typename... Types>
template<typename T >
const T * iox::cxx::variant< Types >::get_if ( const T *  defaultValue) const
inlinenoexcept

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

◆ get_if() [2/2]

template<typename... Types>
template<typename T >
T * iox::cxx::variant< Types >::get_if ( T *  defaultValue)
inlinenoexcept

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

◆ index()

template<typename... Types>
constexpr size_t iox::cxx::variant< Types >::index
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

◆ operator=() [1/3]

template<typename... Types>
variant< Types... > & iox::cxx::variant< Types >::operator= ( const variant< Types > &  rhs)
noexcept

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

◆ operator=() [2/3]

template<typename... Types>
template<typename T >
std::enable_if<!std::is_same< T, variant< Types... > & >::value, variant< Types... > >::type & iox::cxx::variant< Types >::operator= ( T &&  rhs)
inlinenoexcept

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.

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

◆ operator=() [3/3]

template<typename... Types>
variant< Types... > & iox::cxx::variant< Types >::operator= ( variant< Types > &&  rhs)
noexcept

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

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