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
Tis 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.
otheris 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
trueif the Variants are equal;falseotherwise.
-
bool operator!=(const Variant &other) const noexcept
Tests for inequality between two variants.
- Parameters
other – The other Variant.
- Returns
trueif the Variants are not equal;falseotherwise.
-
bool hasValue() const noexcept
Tests if a Variant is empty (i.e.
contains no value).
- Returns
trueif the Variant is empty;falseotherwise.
-
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
optionalcontaining the requested value if conversion succeeds; an emptyoptionalotherwise.
-
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
fallbackif 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.
-
Variant() noexcept