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 &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 rawconst char*
acts as a view on the original value and does not manage the memory for that type. Using the raw pointer after allowing theVariant
to be assigned a different value or destroyed will lead to undefined behavior.Warning
A temporary copy of the VariantData is created to perform the conversion, the return value is captured, and the temporary copy is destroyed. If the return value would be referencing dangling memory due to the destruction of the temporary, use convertTo() instead.
- Returns:
A
optional
containing the requested value if conversion succeeds; an emptyoptional
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 rawconst char*
acts as a view on the original value and does not manage the memory for that type. Using the raw pointer after allowing theVariant
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.
-
inline const VariantData &data() const noexcept#
Access the underlying VariantData.
- Returns:
The underlying VariantData.
-
Variant() noexcept#