firebase:: Variant
#include <variant.h>
Variant data type used by Firebase libraries.
Summary
Constructors and Destructors | |
|---|---|
Variant()Construct a nullVariant. | |
Variant(T value)Construct aVariant with the given templated type. | |
Variant(const std::string & value)Construct aVariant containing the given string value (makes a copy). | |
Variant(const std::vector<Variant > & value) | |
Variant(const std::vector< T > & value) | |
Variant(const T array_of_values[], size_t array_size)Construct aVariant from an array of supported types into a Vector. | |
Variant(const std::map<Variant,Variant > & value) | |
Variant(const std::map< K, V > & value) | |
Variant(constVariant & other)Copy constructor. | |
Variant(Variant && other)Move constructor. | |
~Variant()Destructor. Frees the memory that thisVariant owns. |
Public types | |
|---|---|
Type{ | enum Type of data that this variant object contains. |
Public functions | |
|---|---|
AsBool() const | Get the currentVariant converted into a boolean. |
AsDouble() const | Get the currentVariant converted into a floating-point number. |
AsInt64() const | Get the currentVariant converted into an integer. |
AsString() const | Get the currentVariant converted into a string. |
AssignMap(std::map<Variant,Variant > **map) | voidAssigns an existing map which was allocated on the heap into theVariant without performing a copy. |
AssignMutableString(std::string **str) | voidAssigns an existing string which was allocated on the heap into theVariant without performing a copy. |
AssignVector(std::vector<Variant > **vect) | voidAssigns an existing vector which was allocated on the heap into theVariant without performing a copy. |
Clear(Type new_type) | voidClear the givenVariant data, optionally into a new type. |
blob_data() const | const uint8_t *Get the pointer to the binary data contained in a blob. |
blob_size() const | size_tGet the size of a blob. |
bool_value() const | const bool &Const accessor for aVariant containing a bool. |
double_value() const | doubleConst accessor for aVariant containing a double. |
int64_value() const | int64_tConst accessor for aVariant containing an integer. |
is_blob() const | boolGet whether thisVariant contains a blob. |
is_bool() const | boolGet whether thisVariant contains a bool. |
is_container_type() const | boolGet whether thisVariant contains a container type: Vector or Map. |
is_double() const | boolGet whether thisVariant contains a double. |
is_fundamental_type() const | boolGet whether thisVariant contains a fundamental type: Null, Int64, Double, Bool, or one of the two String types. |
is_int64() const | boolGet whether thisVariant contains an integer. |
is_map() const | boolGet whether thisVariant contains a map. |
is_mutable_blob() const | boolGet whether thisVariant contains a mutable blob. |
is_mutable_string() const | boolGet whether thisVariant contains a mutable string. |
is_null() const | boolGet whether thisVariant is currently null. |
is_numeric() const | boolGet whether thisVariant contains a numeric type, Int64 or Double. |
is_static_blob() const | boolGet whether thisVariant contains a static blob. |
is_static_string() const | boolGet whether thisVariant contains a static string. |
is_string() const | boolGet whether thisVariant contains a string. |
is_vector() const | boolGet whether thisVariant contains a vector. |
map() | |
map() const | |
mutable_blob_data() | uint8_t *Get a mutable pointer to the binary data contained in a blob. |
mutable_blob_data() const | uint8_t *Const accessor for aVariant contianing mutable blob data. |
mutable_string() | std::string &Mutable accessor for aVariant containing a string. |
mutable_string() const | std::stringConst accessor for aVariant containing a string. |
operator!=(constVariant & other) const | boolInequality operator: x != y is evaluated as !(x == y). |
operator<(constVariant & other) const | boolInequality operator, only meant for internal use. |
operator<=(constVariant & other) const | boolInequality operator: x<= y is evaluated as !(x > y) |
operator=(constVariant & other) | Variant &Copy assignment operator. |
operator=(Variant && other) noexcept | Variant &Move assignment operator. |
operator==(constVariant & other) const | boolEquality operator. |
operator>(constVariant & other) const | boolInequality operator: x > y is evaluated as y< x. |
operator>=(constVariant & other) const | boolInequality operator: x >= y is evaluated as !(x< y) |
set_bool_value(bool value) | voidSets theVariant to the given boolean value. |
set_double_value(double value) | voidSets theVariant to an double-precision floating point value. |
set_int64_value(int64_t value) | voidSets theVariant to an 64-bit integer value. |
set_map(const std::map<Variant,Variant > & value) | voidSets theVariant to a copy of the given map. |
set_mutable_blob(const void *src_data, size_t size_bytes) | voidSets theVariant to a copy of the given binary data. |
set_mutable_string(const std::string & value, bool use_small_string) | voidSets theVariant to a copy of the given string. |
set_null() | voidSets theVariant value to null. |
set_static_blob(const void *static_data, size_t size_bytes) | voidSets theVariant to point to static binary data. |
set_string_value(const char *value) | voidSets theVariant to point to a static string buffer. |
set_string_value(char *value) | voidSets theVariant to a mutable string. |
set_string_value(const std::string & value) | voidSets theVariant to a mutable string. |
set_vector(const std::vector<Variant > & value) | voidSets theVariant to a copy of the given vector. |
string_value() const | const char *Const accessor for aVariant containing a string. |
type() const | Get the current type contained in thisVariant. |
vector() | std::vector<Variant > & |
vector() const | const std::vector<Variant > & |
Public static functions | |
|---|---|
EmptyMap() | Get aVariant containing an empty map. |
EmptyMutableBlob(size_t size_bytes) | Return aVariant containing an empty mutable blob of the requested size, filled with 0-bytes. |
EmptyMutableString() | Get aVariant containing an empty mutable string. |
EmptyString() | Get an empty string variant. |
EmptyVector() | Get aVariant containing an empty vector. |
False() | Get aVariant of bool value false. |
FromBool(bool value) | Return aVariant from a boolean. |
FromDouble(double value) | Return aVariant from a double-precision floating point number. |
FromInt64(int64_t value) | Return aVariant from a 64-bit integer. |
FromMutableBlob(const void *src_data, size_t size_bytes) | Return aVariant containing a copy of binary data. |
FromMutableString(const std::string & value) | Return aVariant from a string. |
FromStaticBlob(const void *static_data, size_t size_bytes) | Return aVariant that points to static binary data. |
FromStaticString(const char *value) | Return aVariant from a static string. |
MutableStringFromStaticString(const char *value) | Return aVariant from a string, but make it mutable. |
Null() | Get aVariant of type Null. |
One() | Get aVariant of integer value 1. |
OnePointZero() | Get aVariant of double value 1.0. |
True() | Get aVariant of bool value true. |
TypeName(Type type) | const char *Get the human-readable type name of aVariant type. |
Zero() | Get aVariant of integer value 0. |
ZeroPointZero() | Get aVariant of double value 0.0. |
Public types
Type
TypeType of data that this variant object contains.
| Properties | |
|---|---|
kTypeBool | A boolean value. |
kTypeDouble | A double-precision floating point number. |
kTypeInt64 | A 64-bit integer. |
kTypeMap | |
kTypeMutableBlob | A blob of data that theVariant holds. Never constructed by default. UseVariant::FromMutableBlob() to create aVariant of this type, and copy binary data from an existing source. |
kTypeMutableString | A std::string. |
kTypeNull | Null, or no data. |
kTypeStaticBlob | An statically-allocated blob of data that we point to. Never constructed by default. UseVariant::FromStaticBlob() to create aVariant of this type. |
kTypeStaticString | A statically-allocated string we point to. |
kTypeVector | A std::vector ofVariant. |
Public functions
AsBool
VariantAsBool()const
AsDouble
VariantAsDouble()const
AsInt64
VariantAsInt64()const
Get the currentVariant converted into an integer.
Only valid for fundamental types.
Special cases: If a String can be parsed as a number via strtol(), it will be. If a Bool is true, this will return 1. All other cases (including non-fundamental types) will return 0.
| Details | |
|---|---|
| Returns |
AsString
VariantAsString()const
Get the currentVariant converted into a string.
Only valid for fundamental types.
Special cases: Booleans will be returned as "true" or "false". Null will be returned as an empty string. The returned string may be either mutable or static, depending on the source type. All other cases will return an empty string.
| Details | |
|---|---|
| Returns |
AssignMap
voidAssignMap(std::map<Variant,Variant>**map)
Assigns an existing map which was allocated on the heap into theVariant without performing a copy.
This object will take over ownership of the map, and will set the std::map** you pass in to NULL.
TheVariant's type will be set to Map.
| Details | |||
|---|---|---|---|
| Parameters |
|
AssignMutableString
voidAssignMutableString(std::string**str)
Assigns an existing string which was allocated on the heap into theVariant without performing a copy.
This object will take over ownership of the pointer, and will set the std::string* you pass in to NULL.
TheVariant's type will be set to MutableString.
| Details | |||
|---|---|---|---|
| Parameters |
|
AssignVector
voidAssignVector(std::vector<Variant>**vect)
Assigns an existing vector which was allocated on the heap into theVariant without performing a copy.
This object will take over ownership of the pointer, and will set the std::vector* you pass in to NULL.
TheVariant's type will be set to Vector.
| Details | |||
|---|---|---|---|
| Parameters |
|
Clear
voidClear(Typenew_type)
Variant
Variant(Tvalue)
Construct aVariant with the given templated type.
Valid types for this constructor areint,int64_t,float,double,bool,const char*, andchar* (but see below for additionalVariant types).
| Details | |||
|---|---|---|---|
| Parameters |
|
Typeint orint64_t:
- TheVariant constructed will be of type Int64.
Typedouble orfloat:
- TheVariant constructed will be of type Double.
Typebool:
- TheVariant constructed will be of type Bool.
Typeconst char*:
- TheVariant constructed will be of type StaticString, andis_string() will return true.Note: If you use this constructor, you must ensure that the memory pointed to stays valid for the life of theVariant, otherwise callmutable_string() orset_mutable_string(), which will copy the string to an internal buffer.
Typechar*:
- TheVariant constructed will be of type MutableString, andis_string() will return true.
Other types will result in compiler error unless using the following constructor overloads:
Variant
Variant(conststd::string&value)
Construct aVariant containing the given string value (makes a copy).
TheVariant constructed will be of type MutableString, andis_string() will return true.
| Details | |||
|---|---|---|---|
| Parameters |
|
Variant
Variant(conststd::vector<Variant>&value)
Variant
Variant(conststd::vector<T>&value)
Variant
Variant(constTarray_of_values[],size_tarray_size)
Variant
Variant(conststd::map<K,V>&value)
Variant
Variant(constVariant&other)
Variant
Variant(Variant&&other)noexcept
Move constructor.
Efficiently moves the more complex data types by simply reassigning pointer ownership.
| Details | |||
|---|---|---|---|
| Parameters |
|
blob_data
constuint8_t*blob_data()const
Get the pointer to the binary data contained in a blob.
This method works with both static and mutable blob.
| Details | |
|---|---|
| Returns | Pointer to the binary data. Useblob_size() to get the number of bytes. |
blob_size
size_tblob_size()const
Get the size of a blob.
This method works with both static and mutable blobs.
| Details | |
|---|---|
| Returns | Number of bytes of binary data contained in the blob. |
bool_value
constbool&bool_value()const
Const accessor for aVariant containing a bool.
Note: If theVariant is not of Bool type, this will assert.
| Details | |
|---|---|
| Returns | The bool contained in thisVariant. |
double_value
doubledouble_value()const
Const accessor for aVariant containing a double.
Note: If theVariant is not of Double type, this will assert.
| Details | |
|---|---|
| Returns | The double contained in thisVariant. |
int64_value
int64_tint64_value()const
Const accessor for aVariant containing an integer.
Note: If theVariant is not of Int64 type, this will assert.
| Details | |
|---|---|
| Returns | The integer contained in thisVariant. |
is_blob
boolis_blob()const
Get whether thisVariant contains a blob.
Note: No matter which type of blob theVariant contains, you can read its data viablob_data() and get its size viablob_size().
| Details | |
|---|---|
| Returns | True if theVariant's type is either StaticBlob or MutableBlob; false otherwise. |
is_bool
boolis_bool()const
is_container_type
boolis_container_type()const
is_double
boolis_double()const
is_fundamental_type
boolis_fundamental_type()const
is_int64
boolis_int64()const
is_map
boolis_map()const
is_mutable_blob
boolis_mutable_blob()const
is_mutable_string
boolis_mutable_string()const
is_null
boolis_null()const
is_numeric
boolis_numeric()const
is_static_blob
boolis_static_blob()const
is_static_string
boolis_static_string()const
is_string
boolis_string()const
Get whether thisVariant contains a string.
Note: No matter which type of string theVariant contains, you can read its value viastring_value().
| Details | |
|---|---|
| Returns | True if theVariant's type is either StaticString or MutableString or SmallString; false otherwise. |
is_vector
boolis_vector()const
map
std::map<Variant,Variant>&map()
Mutable accessor for aVariant containing a map ofVariant data.
Note: If theVariant is not of Map type, this will assert.
| Details | |
|---|---|
| Returns | Reference to the map contained in thisVariant. |
map
conststd::map<Variant,Variant>&map()const
Const accessor for aVariant containing a map of strings toVariant data.
Note: If theVariant is not of Map type, this will assert.
| Details | |
|---|---|
| Returns | Reference to the map contained in thisVariant. |
mutable_blob_data
uint8_t*mutable_blob_data()
Get a mutable pointer to the binary data contained in a blob.
If theVariant contains a static blob, it will be converted into a mutable blob, which copies the binary data into theVariant's buffer.
| Details | |
|---|---|
| Returns | Pointer to a mutable buffer of binary data. The size of the buffer cannot be changed, but the contents are mutable. |
mutable_blob_data
uint8_t*mutable_blob_data()const
Const accessor for aVariant contianing mutable blob data.
Note: Unlike the non-const accessor, this accessor cannot "promote" a static blob to mutable, and thus will assert if theVariant you pass in is not of MutableBlob type.
| Details | |
|---|---|
| Returns | Pointer to a mutable buffer of binary data. The size of the buffer cannot be changed, but the contents are mutable. |
mutable_string
std::string&mutable_string()
Mutable accessor for aVariant containing a string.
If theVariant contains a static string, it will be converted into a mutable string, which copies the const char*'s data into a std::string.
Note: If theVariant is not one of the two String types, this will assert.
| Details | |
|---|---|
| Returns | Reference to the string contained in thisVariant. |
mutable_string
std::stringmutable_string()const
operator!=
booloperator!=(constVariant&other)const
Inequality operator: x != y is evaluated as !(x == y).
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Results of the comparison. |
operator<
booloperator<(constVariant&other)const
Inequality operator, only meant for internal use.
Explanation: In order to useVariant as a key for std::map, we must provide a comparison function. This comparison function is ONLY for std::map to be able to use aVariant as a map key.
We define v1< v2 IFF:
- If different types, compare type as int: v1.type()< v2.type() (note: this means that Variant(1)<Variant(0.0) - be careful!)
- If both are int64: v1.int64_value()< v2.int64_value();
- If both are double: v1.double_value()< v2.double_value()
- If both are bool: v1.bool_value()< v2.bool_value();
- If both are either static or mutable strings: strcmp(v1, v2)< 0
- If both are vectors:
- If v1[0]< v2[0], that means v1< v2 == true. Otherwise:
- If v1[0] > v2[0], that means v1< v2 == false. Otherwise:
- Continue to the next element of both vectors and compare again.
- If you reach the end of one vector first, that vector is considered to be lesser.
- If both are maps, iterate similar to vectors (since maps are ordered), but for each element, first compare the key, then the value.
- If both are blobs, the smaller-sized blob is considered lesser. If both blobs are the same size, use memcmp to compare the bytes.
We have defined this operation such that if !(v1< v2) && !(v2< v1), it must follow that v1 == v2.
Note: This will not give you the results you expect if you compare Variants of different types! For example,Variant(0.0)< Variant(1).
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Results of the comparison, as described in this documentation. |
operator<=
booloperator<=(constVariant&other)const
Inequality operator: x<= y is evaluated as !(x > y)
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Results of the comparison. |
operator=
Variant&operator=(Variant&&other)noexcept
Move assignment operator.
Efficiently moves the more complex data types by simply reassigning pointer ownership.
| Details | |||
|---|---|---|---|
| Parameters |
|
operator==
booloperator==(constVariant&other)const
Equality operator.
Both the type and the value must be equal (except that static strings CAN be == to mutable strings). For container types, element-by-element comparison is performed. For strings, string comparison is performed.
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | True if the Variants are of identical types and values, false otherwise. |
operator>
booloperator>(constVariant&other)const
Inequality operator: x > y is evaluated as y< x.
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Results of the comparison. |
operator>=
booloperator>=(constVariant&other)const
Inequality operator: x >= y is evaluated as !(x< y)
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Results of the comparison. |
set_bool_value
voidset_bool_value(boolvalue)
set_double_value
voidset_double_value(doublevalue)
set_int64_value
voidset_int64_value(int64_tvalue)
set_mutable_blob
voidset_mutable_blob(constvoid*src_data,size_tsize_bytes)
set_mutable_string
voidset_mutable_string(conststd::string&value,booluse_small_string)
Sets theVariant to a copy of the given string.
TheVariant's type will be set to SmallString if the size of the string is less than kMaxSmallStringSize (8 bytes on x86, 16 bytes on x64) or otherwise set to MutableString.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
set_static_blob
voidset_static_blob(constvoid*static_data,size_tsize_bytes)
Sets theVariant to point to static binary data.
TheVariant's type will be set to kTypeStaticBlob.
Note: If you use this method, you must ensure that the memory pointer to stays valid for the life of theVariant, or otherwise callmutable_blob_data() orset_mutable_blob(), which will copy the data into an internal buffer.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
|
set_string_value
voidset_string_value(constchar*value)
Sets theVariant to point to a static string buffer.
TheVariant's type will be set to StaticString.
Note: If you use this method, you must ensure that the memory pointed to stays valid for the life of theVariant, or otherwise callmutable_string() orset_mutable_string(), which will copy the string to an internal buffer.
| Details | |||
|---|---|---|---|
| Parameters |
|
set_string_value
voidset_string_value(char*value)
set_string_value
voidset_string_value(conststd::string&value)
set_vector
voidset_vector(conststd::vector<Variant>&value)
string_value
constchar*string_value()const
Const accessor for aVariant containing a string.
This can return both static and mutable strings. The pointer is only guaranteed to persist if thisVariant's type is StaticString.
Note: If theVariant is not of StaticString or MutableString type, this will assert.
| Details | |
|---|---|
| Returns | The string contained in thisVariant. |
vector
std::vector<Variant>&vector()
Mutable accessor for aVariant containing a vector ofVariant data.
Note: If theVariant is not of Vector type, this will assert.
| Details | |
|---|---|
| Returns | Reference to the vector contained in thisVariant. |
vector
conststd::vector<Variant>&vector()const
Const accessor for aVariant containing a vector ofVariant data.
Note: If theVariant is not of Vector type, this will assert.
| Details | |
|---|---|
| Returns | Reference to the vector contained in thisVariant. |
Public static functions
EmptyMap
VariantEmptyMap()
EmptyMutableBlob
VariantEmptyMutableBlob(size_tsize_bytes)
EmptyMutableString
VariantEmptyMutableString()
EmptyString
VariantEmptyString()
Get an empty string variant.
| Details | |
|---|---|
| Returns | AVariant of type StaticString, referring to an empty string. |
EmptyVector
VariantEmptyVector()
False
VariantFalse()
FromBool
VariantFromBool(boolvalue)
FromDouble
VariantFromDouble(doublevalue)
FromInt64
VariantFromInt64(int64_tvalue)
FromMutableBlob
VariantFromMutableBlob(constvoid*src_data,size_tsize_bytes)
FromMutableString
VariantFromMutableString(conststd::string&value)
FromStaticBlob
VariantFromStaticBlob(constvoid*static_data,size_tsize_bytes)
Return aVariant that points to static binary data.
Note: If you use this function, you must ensure that the memory pointed to stays valid for the life of theVariant, otherwise call mutable_blob() orset_mutable_blob(), which will copy the data to an internal buffer.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
| ||||
| Returns | AVariant pointing to the binary data. |
FromStaticString
VariantFromStaticString(constchar*value)
Return aVariant from a static string.
Note: If you use this function, you must ensure that the memory pointed to stays valid for the life of theVariant, otherwise callmutable_string() orset_mutable_string(), which will copy the string to an internal buffer.
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | AVariant referring to the string pointer you passed in. |
MutableStringFromStaticString
VariantMutableStringFromStaticString(constchar*value)
OnePointZero
VariantOnePointZero()
True
VariantTrue()
TypeName
constchar*TypeName(Typetype)
Zero
VariantZero()
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-01-23 UTC.