firebase:: Variant
#include <variant.h>
Variant data type used by Firebase libraries.
Summary
Constructors and Destructors |
|
---|---|
Variant()
Construct a null Variant.
|
|
Variant(T value)
Construct a Variant with the given templated type.
|
|
Variant(const std::string & value)
Construct a Variant 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 a Variant from an array of supported types into a Vector.
|
|
Variant(const std::map< Variant, Variant > & value)
|
|
Variant(const std::map< K, V > & value)
|
|
Variant(const Variant & other)
Copy constructor.
|
|
Variant(Variant && other)
Move constructor.
|
|
~Variant()
Destructor. Frees the memory that this Variant owns.
|
Public types |
|
---|---|
Type{
|
enum Type of data that this variant object contains. |
Public functions |
|
---|---|
AsBool() const
|
Get the current Variant converted into a boolean.
|
AsDouble() const
|
Get the current Variant converted into a floating-point number.
|
AsInt64() const
|
Get the current Variant converted into an integer.
|
AsString() const
|
Get the current Variant converted into a string.
|
AssignMap(std::map< Variant, Variant > **map)
|
void
Assigns an existing map which was allocated on the heap into the Variant without performing a copy.
|
AssignMutableString(std::string **str)
|
void
Assigns an existing string which was allocated on the heap into the Variant without performing a copy.
|
AssignVector(std::vector< Variant > **vect)
|
void
Assigns an existing vector which was allocated on the heap into the Variant without performing a copy.
|
Clear(Type new_type)
|
void
Clear the given Variant 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_t
Get the size of a blob.
|
bool_value() const
|
const bool &
Const accessor for a Variant containing a bool.
|
double_value() const
|
double
Const accessor for a Variant containing a double.
|
int64_value() const
|
int64_t
Const accessor for a Variant containing an integer.
|
is_blob() const
|
bool
Get whether this Variant contains a blob.
|
is_bool() const
|
bool
Get whether this Variant contains a bool.
|
is_container_type() const
|
bool
Get whether this Variant contains a container type: Vector or Map.
|
is_double() const
|
bool
Get whether this Variant contains a double.
|
is_fundamental_type() const
|
bool
Get whether this Variant contains a fundamental type: Null, Int64, Double, Bool, or one of the two String types.
|
is_int64() const
|
bool
Get whether this Variant contains an integer.
|
is_map() const
|
bool
Get whether this Variant contains a map.
|
is_mutable_blob() const
|
bool
Get whether this Variant contains a mutable blob.
|
is_mutable_string() const
|
bool
Get whether this Variant contains a mutable string.
|
is_null() const
|
bool
Get whether this Variant is currently null.
|
is_numeric() const
|
bool
Get whether this Variant contains a numeric type, Int64 or Double.
|
is_static_blob() const
|
bool
Get whether this Variant contains a static blob.
|
is_static_string() const
|
bool
Get whether this Variant contains a static string.
|
is_string() const
|
bool
Get whether this Variant contains a string.
|
is_vector() const
|
bool
Get whether this Variant 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 a Variant contianing mutable blob data.
|
mutable_string()
|
std::string &
Mutable accessor for a Variant containing a string.
|
mutable_string() const
|
std::string
Const accessor for a Variant containing a string.
|
operator!=(const Variant & other) const
|
bool
Inequality operator: x != y is evaluated as !(x == y).
|
operator<(const Variant & other) const
|
bool
Inequality operator, only meant for internal use.
|
operator<=(const Variant & other) const
|
bool
Inequality operator: x <= y is evaluated as !(x > y)
|
operator=(const Variant & other)
|
Variant &
Copy assignment operator.
|
operator=(Variant && other) noexcept
|
Variant &
Move assignment operator.
|
operator==(const Variant & other) const
|
bool
Equality operator.
|
operator>(const Variant & other) const
|
bool
Inequality operator: x > y is evaluated as y < x.
|
operator>=(const Variant & other) const
|
bool
Inequality operator: x >= y is evaluated as !(x < y)
|
set_bool_value(bool value)
|
void
Sets the Variant to the given boolean value.
|
set_double_value(double value)
|
void
Sets the Variant to an double-precision floating point value.
|
set_int64_value(int64_t value)
|
void
Sets the Variant to an 64-bit integer value.
|
set_map(const std::map< Variant, Variant > & value)
|
void
Sets the Variant to a copy of the given map.
|
set_mutable_blob(const void *src_data, size_t size_bytes)
|
void
Sets the Variant to a copy of the given binary data.
|
set_mutable_string(const std::string & value, bool use_small_string)
|
void
Sets the Variant to a copy of the given string.
|
set_null()
|
void
Sets the Variant value to null.
|
set_static_blob(const void *static_data, size_t size_bytes)
|
void
Sets the Variant to point to static binary data.
|
set_string_value(const char *value)
|
void
Sets the Variant to point to a static string buffer.
|
set_string_value(char *value)
|
void
Sets the Variant to a mutable string.
|
set_string_value(const std::string & value)
|
void
Sets the Variant to a mutable string.
|
set_vector(const std::vector< Variant > & value)
|
void
Sets the Variant to a copy of the given vector.
|
string_value() const
|
const char *
Const accessor for a Variant containing a string.
|
type() const
|
Get the current type contained in this Variant.
|
vector()
|
std::vector< Variant > &
|
vector() const
|
const std::vector< Variant > &
|
Public static functions |
|
---|---|
EmptyMap()
|
Get a Variant containing an empty map.
|
EmptyMutableBlob(size_t size_bytes)
|
Return a Variant containing an empty mutable blob of the requested size, filled with 0-bytes.
|
EmptyMutableString()
|
Get a Variant containing an empty mutable string.
|
EmptyString()
|
Get an empty string variant.
|
EmptyVector()
|
Get a Variant containing an empty vector.
|
False()
|
Get a Variant of bool value false.
|
FromBool(bool value)
|
Return a Variant from a boolean.
|
FromDouble(double value)
|
Return a Variant from a double-precision floating point number.
|
FromInt64(int64_t value)
|
Return a Variant from a 64-bit integer.
|
FromMutableBlob(const void *src_data, size_t size_bytes)
|
Return a Variant containing a copy of binary data.
|
FromMutableString(const std::string & value)
|
Return a Variant from a string.
|
FromStaticBlob(const void *static_data, size_t size_bytes)
|
Return a Variant that points to static binary data.
|
FromStaticString(const char *value)
|
Return a Variant from a static string.
|
MutableStringFromStaticString(const char *value)
|
Return a Variant from a string, but make it mutable.
|
Null()
|
Get a Variant of type Null.
|
One()
|
Get a Variant of integer value 1.
|
OnePointZero()
|
Get a Variant of double value 1.0.
|
True()
|
Get a Variant of bool value true.
|
TypeName(Type type)
|
const char *
Get the human-readable type name of a Variant type.
|
Zero()
|
Get a Variant of integer value 0.
|
ZeroPointZero()
|
Get a Variant of double value 0.0.
|
Public types
Type
Type
Type 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 the Variant holds. Never constructed by default. Use Variant::FromMutableBlob() to create a Variant 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. Use Variant::FromStaticBlob() to create a Variant of this type. |
kTypeStaticString
|
A statically-allocated string we point to. |
kTypeVector
|
A std::vector of Variant. |
Public functions
AsBool
Variant AsBool() const
AsDouble
Variant AsDouble() const
AsInt64
Variant AsInt64() const
Get the current Variant 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
Variant AsString() const
Get the current Variant 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
void AssignMap( std::map< Variant, Variant > **map )
Assigns an existing map which was allocated on the heap into the Variant without performing a copy.
This object will take over ownership of the map, and will set the std::map** you pass in to NULL.
The Variant's type will be set to Map.
Details | |||
---|---|---|---|
Parameters |
|
AssignMutableString
void AssignMutableString( std::string **str )
Assigns an existing string which was allocated on the heap into the Variant without performing a copy.
This object will take over ownership of the pointer, and will set the std::string* you pass in to NULL.
The Variant's type will be set to MutableString.
Details | |||
---|---|---|---|
Parameters |
|
AssignVector
void AssignVector( std::vector< Variant > **vect )
Assigns an existing vector which was allocated on the heap into the Variant without performing a copy.
This object will take over ownership of the pointer, and will set the std::vector* you pass in to NULL.
The Variant's type will be set to Vector.
Details | |||
---|---|---|---|
Parameters |
|
Clear
void Clear( Type new_type )
Variant
Variant( T value )
Construct a Variant with the given templated type.
Valid types for this constructor are int
, int64_t
, float
, double
, bool
, const char*
, and char*
(but see below for additional Variant types).
Details | |||
---|---|---|---|
Parameters |
|
Type int
or int64_t
:
- The Variant constructed will be of type Int64.
Type double
or float
:
- The Variant constructed will be of type Double.
Type bool
:
- The Variant constructed will be of type Bool.
Type const char*
:
- The Variant constructed will be of type StaticString, and is_string() will return true. Note: If you use this constructor, you must ensure that the memory pointed to stays valid for the life of the Variant, otherwise call mutable_string() or set_mutable_string(), which will copy the string to an internal buffer.
Type char*
:
- The Variant constructed will be of type MutableString, and is_string() will return true.
Other types will result in compiler error unless using the following constructor overloads:
Variant
Variant( const std::string & value )
Construct a Variant containing the given string value (makes a copy).
The Variant constructed will be of type MutableString, and is_string() will return true.
Details | |||
---|---|---|---|
Parameters |
|
Variant
Variant( const std::vector< Variant > & value )
Variant
Variant( const std::vector< T > & value )
Variant
Variant( const T array_of_values[], size_t array_size )
Variant
Variant( const std::map< K, V > & value )
Variant
Variant( const Variant & other )
Variant
Variant( Variant && other ) noexcept
Move constructor.
Efficiently moves the more complex data types by simply reassigning pointer ownership.
Details | |||
---|---|---|---|
Parameters |
|
blob_data
const uint8_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. Use blob_size() to get the number of bytes.
|
blob_size
size_t blob_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
const bool & bool_value() const
double_value
double double_value() const
int64_value
int64_t int64_value() const
is_blob
bool is_blob() const
is_bool
bool is_bool() const
is_container_type
bool is_container_type() const
is_double
bool is_double() const
is_fundamental_type
bool is_fundamental_type() const
is_int64
bool is_int64() const
is_map
bool is_map() const
is_mutable_blob
bool is_mutable_blob() const
is_mutable_string
bool is_mutable_string() const
is_null
bool is_null() const
is_numeric
bool is_numeric() const
is_static_blob
bool is_static_blob() const
is_static_string
bool is_static_string() const
is_string
bool is_string() const
is_vector
bool is_vector() const
mutable_blob_data
uint8_t * mutable_blob_data()
Get a mutable pointer to the binary data contained in a blob.
If the Variant contains a static blob, it will be converted into a mutable blob, which copies the binary data into the Variant'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 a Variant contianing mutable blob data.
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_string
std::string mutable_string() const
operator!=
bool operator!=( const Variant & other ) const
Inequality operator: x != y is evaluated as !(x == y).
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
Results of the comparison.
|
operator<
bool operator<( const Variant & other ) const
Inequality operator, only meant for internal use.
Explanation: In order to use Variant 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 a Variant 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.
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
Results of the comparison, as described in this documentation.
|
operator<=
bool operator<=( const Variant & other ) const
Inequality operator: x <= y is evaluated as !(x > y)
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
Results of the comparison.
|
operator=
Variant & operator=( const Variant & other )
Copy assignment operator.
Performs a deep copy.
Details | |||
---|---|---|---|
Parameters |
|
operator=
Variant & operator=( Variant && other ) noexcept
Move assignment operator.
Efficiently moves the more complex data types by simply reassigning pointer ownership.
Details | |||
---|---|---|---|
Parameters |
|
operator==
bool operator==( const Variant & 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>
bool operator>( const Variant & other ) const
Inequality operator: x > y is evaluated as y < x.
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
Results of the comparison.
|
operator>=
bool operator>=( const Variant & other ) const
Inequality operator: x >= y is evaluated as !(x < y)
Details | |||
---|---|---|---|
Parameters |
|
||
Returns |
Results of the comparison.
|
set_bool_value
void set_bool_value( bool value )
set_double_value
void set_double_value( double value )
set_int64_value
void set_int64_value( int64_t value )
set_mutable_blob
void set_mutable_blob( const void *src_data, size_t size_bytes )
Sets the Variant to a copy of the given binary data.
The Variant's type will be set to MutableBlob.
Details | |||||
---|---|---|---|---|---|
Parameters |
|
set_mutable_string
void set_mutable_string( const std::string & value, bool use_small_string )
Sets the Variant to a copy of the given string.
The Variant'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
void set_static_blob( const void *static_data, size_t size_bytes )
set_string_value
void set_string_value( const char *value )
set_string_value
void set_string_value( char *value )
set_string_value
void set_string_value( const std::string & value )
set_vector
void set_vector( const std::vector< Variant > & value )
string_value
const char * string_value() const
type
Type type() const
vector
std::vector< Variant > & vector()
vector
const std::vector< Variant > & vector() const
Public static functions
EmptyMap
Variant EmptyMap()
EmptyMutableBlob
Variant EmptyMutableBlob( size_t size_bytes )
EmptyMutableString
Variant EmptyMutableString()
EmptyString
Variant EmptyString()
Get an empty string variant.
Details | |
---|---|
Returns |
A Variant of type StaticString, referring to an empty string.
|
EmptyVector
Variant EmptyVector()
False
Variant False()
FromBool
Variant FromBool( bool value )
FromDouble
Variant FromDouble( double value )
FromInt64
Variant FromInt64( int64_t value )
FromMutableBlob
Variant FromMutableBlob( const void *src_data, size_t size_bytes )
FromMutableString
Variant FromMutableString( const std::string & value )
FromStaticBlob
Variant FromStaticBlob( const void *static_data, size_t size_bytes )
FromStaticString
Variant FromStaticString( const char *value )
MutableStringFromStaticString
Variant MutableStringFromStaticString( const char *value )
One
Variant One()
OnePointZero
Variant OnePointZero()
True
Variant True()
TypeName
const char * TypeName( Type type )
Zero
Variant Zero()