Skip to content

iox::cxx::variant🔗

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 Functions🔗

Name
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)
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>
variant(const in_place_type< T > & type, CTorArguments &&... args)
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.
variant(const variant & rhs)
if the variant contains an element the elements copy constructor is called otherwise an empty variant is copied
variant & operator=(const variant & rhs)
if the variant contains an element the elements copy assignment operator is called otherwise an empty variant is copied
variant(variant && rhs)
if the variant contains an element the elements move constructor is called otherwise an empty variant is moved
variant & operator=(variant && rhs)
if the variant contains an element the elements move assignment operator is called otherwise an empty variant is moved
~variant()
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)
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>
bool
emplace_at_index(CTorArguments &&... args)
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>
bool
emplace(CTorArguments &&... args)
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()
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
returns a pointer to the type stored at index TypeIndex. (not stl compliant)
template \<typename T >
const T *
get() const
returns a pointer to the type T stored in the variant. (not stl compliant)
template \<typename T >
T *
get()
returns a pointer to the type T stored in the variant. (not stl compliant)
template \<typename T >
T *
get_if(T * defaultValue)
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
returns a pointer to the type T if its stored in the variant otherwise it returns the provided defaultValue
constexpr size_t index() const
returns the index of the stored type in the variant. if the variant does not contain any type it returns INVALID_VARIANT_INDEX

Detailed Description🔗

template <typename... Types>
class iox::cxx::variant;

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;

Public Functions Documentation🔗

function variant🔗

variant() =default

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

function variant🔗

template <uint64_t N,
typename... CTorArguments>
inline variant(
    const in_place_index< N > & index,
    CTorArguments &&... args
)

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:

  • index index of the type which should be constructed
  • args variadic list of arguments which will be forwarded to the constructor to the type at index

function variant🔗

template <typename T ,
typename... CTorArguments>
inline variant(
    const in_place_type< T > & type,
    CTorArguments &&... args
)

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:

  • type type which should be created inside the variant
  • args variadic list of arguments which will be forwarded to the constructor to the type

function variant🔗

inline variant(
    const variant & rhs
)

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

Parameters:

  • rhs source of the copy

function operator=🔗

variant & operator=(
    const variant & rhs
)

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

Parameters:

  • rhs source of the copy assignment

Return: reference to the variant itself

function variant🔗

variant(
    variant && rhs
)

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

Parameters:

  • rhs source of the move

function operator=🔗

variant & operator=(
    variant && rhs
)

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

Parameters:

  • rhs source of the move assignment

Return: reference to the variant itself

function ~variant🔗

inline ~variant()

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

function operator=🔗

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

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:

  • rhs source object for the underlying move assignment

Return: reference to the variant itself

function emplace_at_index🔗

template <uint64_t TypeIndex,
typename... CTorArguments>
inline bool emplace_at_index(
    CTorArguments &&... args
)

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

Parameters:

  • TypeIndex index of the type which will be created
  • args arguments which will be forwarded to the constructor to the type at TypeIndex

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

function emplace🔗

template <typename T ,
typename... CTorArguments>
inline bool emplace(
    CTorArguments &&... args
)

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

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

function get_at_index🔗

template <uint64_t TypeIndex>
inline internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index()

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

Return: 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>();

function get_at_index🔗

template <uint64_t TypeIndex>
inline const internal::get_type_at_index< 0, TypeIndex, Types... >::type * get_at_index() const

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

Return: 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>();

function get🔗

template <typename T >
inline const T * get() const

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

Return: 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

function get🔗

template <typename T >
inline T * get()

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

Return: 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

function get_if🔗

template <typename T >
inline T * get_if(
    T * defaultValue
)

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

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

function get_if🔗

template <typename T >
inline const T * get_if(
    const T * defaultValue
) const

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

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

function index🔗

constexpr size_t index() const

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

Return: index of the stored type


Updated on 31 May 2022 at 15:29:15 CEST