Variant#

Fully qualified name: carb::variant::Variant

Defined in carb/variant/VariantUtils.h

class Variant : protected carb::variant::VariantData#

A wrapper class for managing the lifetime of VariantData and converting the contained value to C++ types.

Public Functions

Variant() noexcept#

Default constructor.

Produces an empty Variant, that is, hasValue() will return false. Any attempt to getValue() will fail and convertTo() will produce an empty Variant. Empty Variants are only equal to other empty Variants.

template<class T>
inline explicit Variant(T &&val) noexcept#

Construct based on given type.

To allow copy/move constructors to work properly, this constructor participates in overload resolution only if T is not Variant.

Warning

This function will fail to compile if a Translator cannot be found for T.

Parameters:

val – The value to store in the variant.

~Variant() noexcept#

Destructor.

Variant(const Variant &other) noexcept#

Copy constructor.

Parameters:

other – The Variant to copy.

Variant &operator=(const Variant &other) noexcept#

Copy-assign operator.

Parameters:

other – The Variant to copy.

Returns:

*this

Variant(Variant &&other) noexcept#

Move constructor.

Parameters:

other – The Variant to move from. other is left in an empty state.

Variant &operator=(Variant &&other) noexcept#

Move-assign operator.

Parameters:

other – The Variant to move from.

Returns:

*this

bool operator==(const Variant &other) const noexcept#

Tests for equality between two variants.

Parameters:

other – The other Variant.

Returns:

true if the Variants are equal; false otherwise.

bool operator!=(const Variant &other) const noexcept#

Tests for inequality between two variants.

Parameters:

other – The other Variant.

Returns:

true if the Variants are not equal; false otherwise.

bool hasValue() const noexcept#

Tests if a Variant is empty (i.e.

contains no value).

Returns:

true if the Variant is empty; false otherwise.

omni::string toString() const noexcept#

Renders the Variant as a string for debugging purposes.

Returns:

The string value of the variant.

size_t getHash() const noexcept#

Obtains the hash value of the variant.

Returns:

The hash value of the variant.

template<class T>
cpp::optional<T> getValue() const noexcept#

Attempts to convert the Variant to the given type.

Returns:

A optional containing the requested value if conversion succeeds; an empty optional otherwise.

template<class T>
T getValueOr(T fallback) const noexcept#

Attempts to convert the Variant to the given type with a fallback value if conversion fails.

Parameters:

fallback – The default value to return if conversion fails.

Returns:

The contained value if conversion succeeds, or fallback if conversion fails.

template<class T>
Variant convertTo() const noexcept#

Attempts to convert to a Variant of a different type.

Returns:

A Variant representing a different C++ type if conversion succeeds, otherwise returns an empty Variant.

inline const VariantData &data() const noexcept#

Access the underlying VariantData.

Returns:

The underlying VariantData.

Protected Attributes

const VTable *vtable#

The v-table for this variant. Only empty variants are allowed a nullptr v-table. The v-table is used to provide functions for variant behavior and can be used as a type-identifier of sorts.