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.

See Translator for information on supported types. Additional types can be supported by providing a Translator specialization as well as registering a VTable with IVariant::registerType.

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 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.

Warning

Converting a Python variant value or omni::string to raw const char* acts as a view on the original value and does not manage the memory for that type. Using the raw pointer after allowing the Variant to be assigned a different value or destroyed will lead to undefined behavior.

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.

Warning

Converting a Python variant value or omni::string to raw const char* acts as a view on the original value and does not manage the memory for that type. Using the raw pointer after allowing the Variant to be assigned a different value or destroyed will lead to undefined behavior.

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.