Class Variant
Defined in File variant.hpp
Content
Public Types
enum | Type |
Describes types that can be stored inside Variant container. | |
Public Member Functions
Variant () | |
Default constructor. | |
Variant (const Variant &v) | |
Copy constructor for Variant object. | |
Variant (Variant &&v) | |
Move constructor for Variant object. | |
~Variant () | |
Destructor for Variant object. | |
explicit | Variant (const int8_t v) |
Constructs a new variant with an int8_t value. | |
explicit | Variant (const int16_t v) |
Constructs a new variant with an int16_t value. | |
explicit | Variant (const int32_t v) |
Constructs a new variant with an int32_t value. | |
explicit | Variant (const int64_t v) |
Constructs a new variant with an int64_t value. | |
explicit | Variant (const uint8_t v) |
Constructs a new variant with an uint8_t value. | |
explicit | Variant (const uint16_t v) |
Constructs a new variant with an uint16_t value. | |
explicit | Variant (const uint32_t v) |
Constructs a new variant with an uint32_t value. | |
explicit | Variant (const uint64_t v) |
Constructs a new variant with an uint64_t value. | |
explicit | Variant (const double v) |
Constructs a new variant with an double value. | |
explicit | Variant (const bool v) |
Constructs a new variant with an bool value. | |
explicit | Variant (const std::string &v) |
Constructs a new variant with an std::string& value. | |
explicit | Variant (const char *v) |
Constructs a new variant object of type Type::STRING with an const char* value. | |
explicit | Variant (const ByteArray_t &v) |
Constructs a new variant with an ByteArray_t& value. | |
explicit | Variant (const char *binaryData, const size_t bytesCount) |
Constructs a new variant of type Type::BYTEARRAY. Copies bytesCount bytes from binaryData buffer. | |
explicit | Variant (const VariantVector_t &v) |
Constructs a new variant with an VariantVector_t& value. | |
explicit | Variant (const VariantList_t &v) |
Constructs a new variant with an VariantList_t& value. | |
explicit | Variant (const VariantMap_t &v) |
Constructs a new variant with an VariantMap_t& value. | |
explicit | Variant (const VariantPair_t &v) |
Constructs a new variant with an VariantPair_t& value. | |
explicit | Variant (const std::vector<T> &v) |
Constructs a new variant with an std::vector | |
explicit | Variant (const std::list<T> &v) |
Constructs a new variant with an std::list | |
explicit | Variant (const std::map<K, V> &v) |
explicit | Variant (const TFirst &first, const TSecond &second) |
Constructs a new variant object of type Type::PAIR. | |
explicit | Variant (const T &v) |
Constructs a new variant object of type Type::CUSTOM using custom types. | |
Variant & | operator= (const Variant &v) |
Assigns the value of the v to this variant. | |
Variant & | operator= (Variant &&v) |
Move-assigns the value of the v to this variant. | |
Variant & | operator= (const int8_t v) |
Assigns int8_t value to current variant object and changing it’s type to Type::BYTE_1 . | |
Variant & | operator= (const int16_t v) |
Assigns int16_t value to current variant object and changing it’s type to Type::BYTE_2 . | |
Variant & | operator= (const int32_t v) |
Assigns int32_t value to current variant object and changing it’s type to Type::BYTE_4 . | |
Variant & | operator= (const int64_t v) |
Assigns int64_t value to current variant object and changing it’s type to Type::BYTE_8 . | |
Variant & | operator= (const uint8_t v) |
Assigns uint8_t value to current variant object and changing it’s type to Type::UBYTE_1 . | |
Variant & | operator= (const uint16_t v) |
Assigns uint16_t value to current variant object and changing it’s type to Type::UBYTE_2 . | |
Variant & | operator= (const uint32_t v) |
Assigns uint32_t value to current variant object and changing it’s type to Type::UBYTE_4 . | |
Variant & | operator= (const uint64_t v) |
Assigns uint64_t value to current variant object and changing it’s type to Type::UBYTE_8 . | |
Variant & | operator= (const double v) |
Assigns double value to current variant object and changing it’s type to Type::DOUBLE . | |
Variant & | operator= (const bool v) |
Assigns bool value to current variant object and changing it’s type to Type::BOOL . | |
Variant & | operator= (const std::string &v) |
Assigns std::string& value to current variant object and changing it’s type to Type::STRING . | |
Variant & | operator= (const char *v) |
Assigns char* value to current variant object and changing it’s type to Type::STRING . | |
Variant & | operator= (const ByteArray_t &v) |
Assigns ByteArray_t& value to current variant object and changing it’s type to Type::BYTEARRAY . | |
Variant & | operator= (const VariantVector_t &v) |
Assigns VariantVector_t& value to current variant object and changing it’s type to Type::VECTOR . | |
Variant & | operator= (const VariantList_t &v) |
Assigns VariantList_t& value to current variant object and changing it’s type to Type::LIST . | |
Variant & | operator= (const VariantMap_t &v) |
Assigns VariantMap_t& value to current variant object and changing it’s type to Type::MAP . | |
Variant & | operator= (const VariantPair_t &v) |
Assigns VariantPair_t& value to current variant object and changing it’s type to Type::PAIR . | |
Variant & | operator= (const T &v) |
Assigns T& value to current variant object and changing it’s type to Type::CUSTOM . | |
void | clear () |
Resets value of the Varaint object and frees all allocated memory. After calling this method internal type will become Type::UNKNOWN. | |
inline Type | getType () |
Gets the type of data stored in the Variant object. | |
operator bool () | |
Checks if variant contains value or not. | |
bool | isEmpty () |
Check if the Variant object doesn’t contain any value. | |
bool | isNumeric () |
Check if the Variant object contains a numeric value. | |
bool | isSignedNumeric () |
Check if the Variant object contains a signed numeric value. | |
bool | isUnsignedNumeric () |
Check if the Variant object contains an unsigned numeric value. | |
bool | isBool () |
Check if the Variant object contains a boolean value. | |
bool | isString () |
Check if the Variant object contains a string. | |
bool | isByteArray () |
Check if the Variant object contains a byte array. | |
bool | isVector () |
Check if the Variant object contains a vector. | |
bool | isList () |
Check if the Variant object contains a list. | |
bool | isMap () |
Check if the Variant object contains a map. | |
bool | isPair () |
Check if the Variant object contains a pair. | |
bool | isCustomType () |
Check if the Variant object contains a custom type. | |
int64_t | toInt64 () |
Returns the variant value represented as int64. | |
uint64_t | toUInt64 () |
Returns the variant value represented as uint64. | |
double | toDouble () |
Returns the variant value represented as double. | |
bool | toBool () |
Returns the variant value represented as bool. | |
std::string | toString () |
Returns the variant value represented as string. | |
ByteArray_t | toByteArray () |
Returns the variant value represented as byte array. | |
std::shared_ptr<ByteArray_t> | getByteArray () |
Returns pointer to internal byte array data. | |
std::shared_ptr<VariantVector_t> | getVector () |
Returns pointer to internal vector data. | |
std::vector<T> | toVector (const std::function<T(const Variant&)> &converter) |
Copies internal data to a requested std::vector type. Usage example: | |
std::shared_ptr<VariantList_t> | getList () |
Returns pointer to internal list data. | |
std::list<T> | toList (const std::function<T(const Variant&)> &converter) |
Copies internal data to a requested std::list type. Usage example: | |
std::shared_ptr<VariantMap_t> | getMap () |
Returns pointer to internal map data. | |
std::map<K, V> | toMap (const std::function<K(const Variant&)> &converterKey, const std::function<V(const Variant&)> &converterValue) |
Copies internal data to a requested std::map type. Usage example: | |
std::shared_ptr<VariantPair_t> | getPair () |
Returns pointer to internal pair data. | |
std::pair<TFirst, TSecond> | toPair (const std::function<TFirst(const Variant&)> &converterFirst, const std::function<TSecond(const Variant&)> &converterSecond) |
Copies internal data to a requested std::pair type. | |
std::shared_ptr<T> | getCustomType () |
Returns pointer to custom type data. | |
bool | operator== (const Variant &val) |
Compares this Variant object to another Variant object for equality. | |
bool | operator!= (const Variant &val) |
Compares this Variant object to another Variant object for inequality. | |
bool | operator> (const Variant &val) |
Checks if this Variant object is greater than another Variant object. | |
bool | operator>= (const Variant &val) |
Checks if this Variant object is greater than or equal to another Variant object. | |
bool | operator< (const Variant &val) |
Checks if this Variant object is less than another Variant object. | |
bool | operator<= (const Variant &val) |
Checks if this Variant object is less than or equal to another Variant object. | |
static Variant | make (const int8_t v) |
Creates a Variant object with a value of type Type::BYTE_1 . | |
static Variant | make (const int16_t v) |
Creates a Variant object with a value of type Type::BYTE_2 . | |
static Variant | make (const int32_t v) |
Creates a Variant object with a value of type Type::BYTE_4 . | |
static Variant | make (const int64_t v) |
Creates a Variant object with a value of type Type::BYTE_8 . | |
static Variant | make (const uint8_t v) |
Creates a Variant object with a value of type Type::UBYTE_1 . | |
static Variant | make (const uint16_t v) |
Creates a Variant object with a value of type Type::UBYTE_2 . | |
static Variant | make (const uint32_t v) |
Creates a Variant object with a value of type Type::UBYTE_4 . | |
static Variant | make (const uint64_t v) |
Creates a Variant object with a value of type Type::UBYTE_8 . | |
static Variant | make (const double v) |
Creates a Variant object with a value of type Type::DOUBLE . | |
static Variant | make (const bool v) |
Creates a Variant object with a value of type Type::BOOL . | |
static Variant | make (const std::string &v) |
Creates a Variant object with a value of type Type::STRING . | |
static Variant | make (const char *v) |
Creates a Variant object with a value of type Type::STRING . | |
static Variant | make (const ByteArray_t &v) |
Creates a Variant object with a value of type Type::VECTOR . | |
static Variant | make (const char *binaryData, const size_t bytesCount) |
Creates a Variant object with a value of type Type::VECTOR. | |
static Variant | make (const VariantVector_t &v) |
Creates a Variant object with a value of type Type::VECTOR . | |
static Variant | make (const std::vector<T> &v) |
Creates a Variant object with a value of type Type::VECTOR . | |
static Variant | make (const VariantList_t &v) |
Creates a Variant object with a value of type Type::LIST . | |
static Variant | make (const std::list<T> &v) |
Creates a Variant object with a value of type Type::LIST . | |
static Variant | make (const VariantMap_t &v) |
Creates a Variant object with a value of type Type::MAP . | |
static Variant | make (const std::map<K, V> &v) |
Creates a Variant object with a value of type Type::MAP . | |
static Variant | make (const VariantPair_t &v) |
Creates a Variant object with a value of type Type::PAIR . | |
static Variant | make (const TFirst &first, const TSecond &second) |
Creates a Variant object with a value of type Type::PAIR. | |
static Variant | make (const T &v) |
Creates a Variant object with a value of type Type::CUSTOM . | |
static Variant | makeCustom (const T &v) |
Creates a Variant object with a value of type Type::CUSTOM . | |
static Variant | make (const Variant &v) |
Create a new Variant object from an existing Variant object. | |
Class Documentation
-
class Variant
Variant class represents a type-safe union.
Variant class is supposed to be a substitute for std::variant which is not available prior to C++17.
The Variant class is a generic container that can hold various types of data (similar to union). It is designed to be flexible and efficient by allowing the user to store data of different types and sizes, and retrieve them in a type-safe manner. The class definition provides an Variant::Type enumeration of different data types that the Variant object can hold, including numeric types, boolean values, strings, and more complex types like vectors, lists, maps and pairs.
A Variant object holds a single value of a single type at a time or none if it’s empty. Value can be retrieved as a copy using toX() methods (for example toString(), toInt64(), etc.). When asked for a type that can be generated from the stored type, toX() copies and converts and leaves the object itself unchanged. When asked for a type that cannot be generated from the stored type, the result depends on the type (see each the function’s documentation for details).
Here is some example code to demonstrate the use of Variant:
Variant v; // v is empty and has Type::UNKNOWN if (!v) { v = 42; // v now contains numeric value (usually Type::BYTE_4, but depends on compiler) std::cout << "v is an int: " << v.isNumeric() << std::endl; // output: v is an int: 1 std::cout << "v == 42: " << (v == Variant(42)) << std::endl; // output: v == 42: 1 std::cout << "v == \"42\": " << (v.toString() == "42") << std::endl; // output: v == "42": 1 v = "Hello, world!"; // v now contains a Type::STRING value std::cout << "v is a string: " << v.isString() << std::endl; // output: v is a string: 1 std::cout << "v == \"Hello, world!\": " << (v == "Hello, world!") << std::endl; // output: v == "Hello, world!": 1 std::cout << "v.toInt64(): " << v.toInt64() << std::endl; // output: v.toInt64(): 0 v = 3.14; // v now contains a Type::DOUBLE value std::cout << "v is a double: " << v.isDouble() << std::endl; // output: v is a double: 1 std::cout << "v > 2.0: " << (v > 2.0) << std::endl; // output: v > 2.0: 1 }
Public Types
-
enum class Type
Describes types that can be stored inside Variant container.
Values:
-
enumerator UNKNOWN
empty object
-
enumerator BYTE_1
A 1-byte signed integer.
-
enumerator BYTE_2
A 2-byte signed integer.
-
enumerator BYTE_4
A 4-byte signed integer.
-
enumerator BYTE_8
An 8-byte signed integer.
-
enumerator UBYTE_1
A 1-byte unsigned integer.
-
enumerator UBYTE_2
A 2-byte unsigned integer.
-
enumerator UBYTE_4
A 4-byte unsigned integer.
-
enumerator UBYTE_8
An 8-byte unsigned integer.
-
enumerator DOUBLE
A double-precision floating-point number.
-
enumerator BOOL
A boolean value.
-
enumerator STRING
std::string
-
enumerator BYTEARRAY
ByteArray_t.
-
enumerator LIST
VariantList_t.
-
enumerator VECTOR
VariantVector_t.
-
enumerator MAP
VariantMap_t.
-
enumerator PAIR
VariantPair_t.
-
enumerator CUSTOM
any type
-
enumerator UNKNOWN
Public Functions
-
Variant() = default
Default constructor.
Constructs a new empty Variant object with Type::UNKNOWN and null data.
-
Variant(const Variant &v)
Copy constructor for Variant object.
- Parameters
v – Another Variant object to copy from.
-
Variant(Variant &&v) noexcept
Move constructor for Variant object.
- Parameters
v – Another Variant object to move from.
-
explicit Variant(const int8_t v)
Constructs a new variant with an int8_t value.
-
explicit Variant(const int16_t v)
Constructs a new variant with an int16_t value.
-
explicit Variant(const int32_t v)
Constructs a new variant with an int32_t value.
-
explicit Variant(const int64_t v)
Constructs a new variant with an int64_t value.
-
explicit Variant(const uint8_t v)
Constructs a new variant with an uint8_t value.
-
explicit Variant(const uint16_t v)
Constructs a new variant with an uint16_t value.
-
explicit Variant(const uint32_t v)
Constructs a new variant with an uint32_t value.
-
explicit Variant(const uint64_t v)
Constructs a new variant with an uint64_t value.
-
explicit Variant(const double v)
Constructs a new variant with an double value.
-
explicit Variant(const bool v)
Constructs a new variant with an bool value.
-
explicit Variant(const std::string &v)
Constructs a new variant with an std::string& value.
-
explicit Variant(const char *v)
Constructs a new variant object of type Type::STRING with an const char* value.
-
explicit Variant(const ByteArray_t &v)
Constructs a new variant with an ByteArray_t& value.
-
explicit Variant(const char *binaryData, const size_t bytesCount)
Constructs a new variant of type Type::BYTEARRAY. Copies bytesCount bytes from binaryData buffer.
-
explicit Variant(const VariantVector_t &v)
Constructs a new variant with an VariantVector_t& value.
-
explicit Variant(const VariantList_t &v)
Constructs a new variant with an VariantList_t& value.
-
explicit Variant(const VariantMap_t &v)
Constructs a new variant with an VariantMap_t& value.
-
explicit Variant(const VariantPair_t &v)
Constructs a new variant with an VariantPair_t& value.
-
template<typename T>
explicit Variant(const std::vector<T> &v) Constructs a new variant with an std::vector<T>& value.
-
template<typename T>
explicit Variant(const std::list<T> &v) Constructs a new variant with an std::list<T>& value.
-
template<typename TFirst, typename TSecond>
explicit Variant(const TFirst &first, const TSecond &second) Constructs a new variant object of type Type::PAIR.
- Parameters
first – stored as first value of a pair
second – stored as second value of a pair
-
template<typename T>
explicit Variant(const T &v) Constructs a new variant object of type Type::CUSTOM using custom types.
-
Variant &operator=(const int8_t v)
Assigns int8_t value to current variant object and changing it’s type to Type::BYTE_1 .
-
Variant &operator=(const int16_t v)
Assigns int16_t value to current variant object and changing it’s type to Type::BYTE_2 .
-
Variant &operator=(const int32_t v)
Assigns int32_t value to current variant object and changing it’s type to Type::BYTE_4 .
-
Variant &operator=(const int64_t v)
Assigns int64_t value to current variant object and changing it’s type to Type::BYTE_8 .
-
Variant &operator=(const uint8_t v)
Assigns uint8_t value to current variant object and changing it’s type to Type::UBYTE_1 .
-
Variant &operator=(const uint16_t v)
Assigns uint16_t value to current variant object and changing it’s type to Type::UBYTE_2 .
-
Variant &operator=(const uint32_t v)
Assigns uint32_t value to current variant object and changing it’s type to Type::UBYTE_4 .
-
Variant &operator=(const uint64_t v)
Assigns uint64_t value to current variant object and changing it’s type to Type::UBYTE_8 .
-
Variant &operator=(const double v)
Assigns double value to current variant object and changing it’s type to Type::DOUBLE .
-
Variant &operator=(const bool v)
Assigns bool value to current variant object and changing it’s type to Type::BOOL .
-
Variant &operator=(const std::string &v)
Assigns std::string& value to current variant object and changing it’s type to Type::STRING .
-
Variant &operator=(const char *v)
Assigns char* value to current variant object and changing it’s type to Type::STRING .
-
Variant &operator=(const ByteArray_t &v)
Assigns ByteArray_t& value to current variant object and changing it’s type to Type::BYTEARRAY .
-
Variant &operator=(const VariantVector_t &v)
Assigns VariantVector_t& value to current variant object and changing it’s type to Type::VECTOR .
-
Variant &operator=(const VariantList_t &v)
Assigns VariantList_t& value to current variant object and changing it’s type to Type::LIST .
-
Variant &operator=(const VariantMap_t &v)
Assigns VariantMap_t& value to current variant object and changing it’s type to Type::MAP .
-
Variant &operator=(const VariantPair_t &v)
Assigns VariantPair_t& value to current variant object and changing it’s type to Type::PAIR .
-
template<typename T>
Variant &operator=(const T &v) Assigns T& value to current variant object and changing it’s type to Type::CUSTOM .
-
void clear()
Resets value of the Varaint object and frees all allocated memory. After calling this method internal type will become Type::UNKNOWN.
-
inline Type getType() const
Gets the type of data stored in the Variant object.
- Returns
type of the stored data
-
operator bool() const
Checks if variant contains value or not.
- Return values
true – variant was initialzied with a value and it’s type is not Type::UNKNOWN
false – no value was set for the variant and it’s type is Type::UNKNOWN
-
bool isEmpty() const
Check if the Variant object doesn’t contain any value.
- Returns
true if the Variant object doesn’t contain any value, false otherwise
-
bool isNumeric() const
Check if the Variant object contains a numeric value.
- Returns
true if the Variant object contains a numeric value, false otherwise
-
bool isSignedNumeric() const
Check if the Variant object contains a signed numeric value.
- Returns
true if the Variant object contains a signed numeric value, false otherwise
-
bool isUnsignedNumeric() const
Check if the Variant object contains an unsigned numeric value.
- Returns
true if the Variant object contains an unsigned numeric value, false otherwise
-
bool isBool() const
Check if the Variant object contains a boolean value.
- Returns
true if the Variant object contains a boolean value, false otherwise
-
bool isString() const
Check if the Variant object contains a string.
- Returns
true if the Variant object contains a string, false otherwise
-
bool isByteArray() const
Check if the Variant object contains a byte array.
- Returns
true if the Variant object contains a byte array, false otherwise
-
bool isVector() const
Check if the Variant object contains a vector.
- Returns
true if the Variant object contains a vector, false otherwise
-
bool isList() const
Check if the Variant object contains a list.
- Returns
true if the Variant object contains a list, false otherwise
-
bool isMap() const
Check if the Variant object contains a map.
- Returns
true if the Variant object contains a map, false otherwise
-
bool isPair() const
Check if the Variant object contains a pair.
- Returns
true if the Variant object contains a pair, false otherwise
-
bool isCustomType() const
Check if the Variant object contains a custom type.
- Returns
true if the Variant object contains a custom type, false otherwise
-
int64_t toInt64() const
Returns the variant value represented as int64.
Supported data types are:
Type::BOOL - returns 0 if the value is false and 1 if it’s true
Type::STRING - tries to convert string to number or returns 0
- Returns
numeric representation of the Variant object (calling this method on an unsupported variant type returns 0).
-
uint64_t toUInt64() const
Returns the variant value represented as uint64.
Supported data types are:
Type::BOOL - returns 0 if the value is false and 1 if it’s true
Type::STRING - tries to convert string to number or returns 0
- Returns
numeric representation of the Variant object (calling this method on an unsupported variant type returns 0).
-
double toDouble() const
Returns the variant value represented as double.
Supported data types are:
Type::BOOL - returns 0 if the value is false and 1 if it’s true
Type::STRING - tries to convert string to number or returns 0
- Returns
numeric representation of the Variant object (calling this method on an unsupported variant type returns 0).
-
bool toBool() const
Returns the variant value represented as bool.
Supported data types are:
Type::STRING - returns true if string value is “true” or it represents a number different from 0
- Returns
boolean representation of the Variant object (calling this method on an unsupported variant type returns false).
-
std::string toString() const
Returns the variant value represented as string.
If stored value is not a string, method will try to convert it. Supported data types are:
- Returns
string representation of the Variant object (calling this method on an unsupported variant type returns an empty string).
-
ByteArray_t toByteArray() const
Returns the variant value represented as byte array.
If stored value is not a byte array, method will try to convert it. Supported data types are:
Remark
This function always returns a copy of data. If you know that internal type is Type::BYTEARRAY and just want to access data it’s better to use toByteArrayPtr() method.
- Returns
byte array representation of the Variant object (calling this method on an unsupported variant type returns an empty vector).
-
std::shared_ptr<ByteArray_t> getByteArray() const
Returns pointer to internal byte array data.
- Returns
pointer to internal data or nullptr if data type is not Type::BYTEARRAY
-
std::shared_ptr<VariantVector_t> getVector() const
Returns pointer to internal vector data.
- Returns
pointer to internal data or nullptr if data type is not Type::VECTOR
-
template<typename T>
std::vector<T> toVector(const std::function<T(const Variant&)> &converter) const Copies internal data to a requested std::vector type. Usage example:
std::vector<int> intData = {1, 2, 3}; Variant v(intData); std::vector<int> intDataConverted = v.toVector<int>([](const Variant& v){ return v.toInt64(); })); // intDataConverted = {1, 2, 3} std::vector<std::string> strDataConverted = v.toVector<std::string>([](const Variant& v){ return v.toString(); })); // strDataConverted = {"1", "2", "3"}
- Parameters
converter – functor which will be called for each vector item to convert it from Variant to requested type
- Returns
std::vector filled with data (empty container if data type is not Type::VECTOR)
-
std::shared_ptr<VariantList_t> getList() const
Returns pointer to internal list data.
- Returns
pointer to internal data or nullptr if data type is not Type::LIST
-
template<typename T>
std::list<T> toList(const std::function<T(const Variant&)> &converter) const Copies internal data to a requested std::list type. Usage example:
std::list<int> intData = {1, 2, 3}; Variant v(intData); std::list<int> intDataConverted = v.toList<int>([](const Variant& v){ return v.toInt64(); })); // intDataConverted = {1, 2, 3} std::list<std::string> strDataConverted = v.toList<std::string>([](const Variant& v){ return v.toString(); })); // strDataConverted = {"1", "2", "3"}
- Parameters
converter – functor which will be called for each list item to convert it from Variant to requested type
- Returns
std::list filled with data (empty container if data type is not Type::LIST)
-
std::shared_ptr<VariantMap_t> getMap() const
Returns pointer to internal map data.
- Returns
pointer to internal data or nullptr if data type is not Type::MAP
-
template<typename K, typename V>
std::map<K, V> toMap(const std::function<K(const Variant&)> &converterKey, const std::function<V(const Variant&)> &converterValue) const Copies internal data to a requested std::map type. Usage example:
std::map<int, std::string> intStrData = {{1, "aa"}, {2, "bb"}, {3, "cc"}}; Variant v = Variant::make(intStrData); std::map<int, std::string> intStrDataConverted = v.toMap<int, std::string>([](const Variant& key){ return key.toInt64(); }, [](const Variant& value){ return value.toString(); }));
- Parameters
- Returns
std::map filled with data (empty container if data type is not Type::MAP)
-
std::shared_ptr<VariantPair_t> getPair() const
Returns pointer to internal pair data.
- Returns
pointer to internal data or nullptr if data type is not Type::PAIR
-
template<typename TFirst, typename TSecond>
std::pair<TFirst, TSecond> toPair(const std::function<TFirst(const Variant&)> &converterFirst, const std::function<TSecond(const Variant&)> &converterSecond) const Copies internal data to a requested std::pair type.
Usage example:
std::pair<int, std::string> intStrData = {1, "aa"}; Variant v = Variant::make(intStrData); std::pair<int, std::string> intStrDataConverted = v.toPair<int, std::string>([](const Variant& first){ return first.toInt64(); }, [](const Variant& second){ return second.toString(); }));
- Parameters
- Returns
std::pair filled with data (empty container if data type is not Type::PAIR)
Returns pointer to custom type data.
Warning
This function is not type-safe and simply does static_cast to requested type. User is responsible for specifying same type that was used for initialization of a variant object.
- Returns
pointer to internal data or nullptr if data type is not Type::CUSTOM
-
bool operator==(const Variant &val) const
Compares this Variant object to another Variant object for equality.
Variant uses the == operator of the stored type to check for equality. Variants of different types will always compare as not equal.
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is equal to val, false otherwise.
-
bool operator!=(const Variant &val) const
Compares this Variant object to another Variant object for inequality.
Impelemented using operator==().
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is not equal to val, false otherwise.
-
bool operator>(const Variant &val) const
Checks if this Variant object is greater than another Variant object.
Operator only compares variants of the same type. Comparing logic depends on the underlying type:
numeric, bool or string values are compared using standard operator >
vector or list values are compared by size (A.size() > B.size())
other types are not supported and false is always returned
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is greater than val, false otherwise.
-
bool operator>=(const Variant &val) const
Checks if this Variant object is greater than or equal to another Variant object.
Impelemented using operator==() and operator>().
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is greater than or equal to val, false otherwise.
-
bool operator<(const Variant &val) const
Checks if this Variant object is less than another Variant object.
Impelemented using operator!=() and operator>().
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is less than val, false otherwise.
-
bool operator<=(const Variant &val) const
Checks if this Variant object is less than or equal to another Variant object.
Impelemented using operator==() and operator>().
- Parameters
val – The Variant object to compare to.
- Returns
true if this object is less than or equal to val, false otherwise.
Public Static Functions
-
static Variant make(const int8_t v)
Creates a Variant object with a value of type Type::BYTE_1 .
-
static Variant make(const int16_t v)
Creates a Variant object with a value of type Type::BYTE_2 .
-
static Variant make(const int32_t v)
Creates a Variant object with a value of type Type::BYTE_4 .
-
static Variant make(const int64_t v)
Creates a Variant object with a value of type Type::BYTE_8 .
-
static Variant make(const uint8_t v)
Creates a Variant object with a value of type Type::UBYTE_1 .
-
static Variant make(const uint16_t v)
Creates a Variant object with a value of type Type::UBYTE_2 .
-
static Variant make(const uint32_t v)
Creates a Variant object with a value of type Type::UBYTE_4 .
-
static Variant make(const uint64_t v)
Creates a Variant object with a value of type Type::UBYTE_8 .
-
static Variant make(const double v)
Creates a Variant object with a value of type Type::DOUBLE .
-
static Variant make(const bool v)
Creates a Variant object with a value of type Type::BOOL .
-
static Variant make(const std::string &v)
Creates a Variant object with a value of type Type::STRING .
-
static Variant make(const char *v)
Creates a Variant object with a value of type Type::STRING .
-
static Variant make(const ByteArray_t &v)
Creates a Variant object with a value of type Type::VECTOR .
-
static Variant make(const char *binaryData, const size_t bytesCount)
Creates a Variant object with a value of type Type::VECTOR.
- Parameters
binaryData – The binary data to store.
bytesCount – The size of the binaryData in bytes.
- Returns
Newly constructed Variant object.
-
static Variant make(const VariantVector_t &v)
Creates a Variant object with a value of type Type::VECTOR .
-
template<typename T>
static Variant make(const std::vector<T> &v) Creates a Variant object with a value of type Type::VECTOR .
-
static Variant make(const VariantList_t &v)
Creates a Variant object with a value of type Type::LIST .
-
template<typename T>
static Variant make(const std::list<T> &v) Creates a Variant object with a value of type Type::LIST .
-
static Variant make(const VariantMap_t &v)
-
static Variant make(const VariantPair_t &v)
Creates a Variant object with a value of type Type::PAIR .
-
template<typename TFirst, typename TSecond>
static Variant make(const TFirst &first, const TSecond &second) Creates a Variant object with a value of type Type::PAIR.
- Parameters
first – stored as first value of a pair
second – stored as second value of a pair
- Returns
Newly constructed Variant object.
-
template<typename T>
static Variant make(const T &v) Creates a Variant object with a value of type Type::CUSTOM .
-
template<typename T>
static Variant makeCustom(const T &v) Creates a Variant object with a value of type Type::CUSTOM .
-
enum class Type