firebase::Variant

#include <variant.h>

Variant data type used by Firebase libraries.

Summary

Constructors and Destructors

Variant()
Construct a null Variant.
Variant(int64_t value)
Construct a Variant containing the given 64-bit integer.
Variant(int value)
Construct a Variant containing the given integer.
Variant(double value)
Construct a Variant containing the given double-precision floating point value.
Variant(float value)
Construct a Variant containing the given single-precision floating point value.
Variant(bool value)
Construct a Variant containing the given boolean value.
Variant(const char *value)
Construct a Variant with the given static const string (no copy).
Variant(char *value)
Construct a Variant containing the given string value (makes a copy).
Variant(const std::string & value)
Construct a Variant containing the given string value (makes a copy).
Variant(const std::vector< Variant > & value)
Construct a Variant containing the given std::vector of Variant.
Variant(const std::vector< T > & value)
Construct a Variant containing the given std::vector of something that can be constructed into a Variant.
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)
Construct a Variatn containing the given std::map of Variant to Variant.
Variant(const std::map< K, V > & value)
Construct a Variant containing the given std::map of something that can be constructed into a Variant, to something that can be constructed into a Variant.
Variant(const Variant & other)
Copy constructor.
Variant(Variant && other)
Move constructor.
~Variant()
Destructor. Frees the memory that this Variant owns.

Public types

Type{
  kTypeNull,
  kTypeInt64,
  kTypeDouble,
  kTypeBool,
  kTypeStaticString,
  kTypeMutableString,
  kTypeVector,
  kTypeMap,
  kTypeStaticBlob,
  kTypeMutableBlob
}
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()
std::map< Variant, Variant > &
Mutable accessor for a Variant containing a map of Variant data.
map() const
const std::map< Variant, Variant > &
Const accessor for a Variant containing a map of strings to Variant data.
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
const std::string &
Const accessor for a Variant containing a mutable string only.
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)
Copy assignment operator.
operator=(Variant && other)
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)
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 > &
Mutable accessor for a Variant containing a vector of Variant data.
vector() const
const std::vector< Variant > &
Const accessor for a Variant containing a vector of Variant data.

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

A std::map, mapping Variant to Variant.

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 

Get the current Variant converted into a boolean.

Null, 0, 0.0, empty strings, empty vectors, empty maps, blobs of size 0, and "false" (case-sensitive) are all considered false. All other values are true.

Details
Returns
A Variant of type Bool containing the original Variant interpreted as a Bool.

AsDouble

Variant AsDouble() const 

Get the current Variant converted into a floating-point number.

Only valid for fundamental types.

Special cases: If a Bool is true, this will return 1. All other cases will return 0.

Details
Returns
A Variant containing a Double that represents the value of this original Variant.

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
A Variant containing an Int64 that represents the value of this original Variant.

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
A Variant containing a String that represents the value of this original Variant.

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
map
Pointer to a pointer to an STL map. The Variant will take over ownership of the pointer to the map, and set the pointer you passed in to NULL.

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
str
Pointer to a pointer to an STL string. The Variant will take over ownership of the pointer to the string, and set the pointer you passed in to NULL.

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
vect
Pointer to a pointer to an STL vector. The Variant will take over ownership of the pointer to the vector, and set the pointer you passed in to NULL.

Clear

void Clear(
  Type new_type
)

Clear the given Variant data, optionally into a new type.

Frees up any memory that might have been allocated. After calling this, you can access the Variant as the new type.

Details
Parameters
new_type
Optional new type to clear the Variant to. You may immediately begin using the Variant as that new type.

Variant

 Variant()

Construct a null Variant.

The Variant constructed will be of type Null.

Variant

 Variant(
  int64_t value
)

Construct a Variant containing the given 64-bit integer.

The Variant constructed will be of type Int64.

param[in] value The 64-bit integer value for the Variant.

Variant

 Variant(
  int value
)

Construct a Variant containing the given integer.

The Variant constructed will be of type Int64.

Details
Parameters
value
The 32-bit integer value for the Variant.

Variant

 Variant(
  double value
)

Construct a Variant containing the given double-precision floating point value.

The Variant constructed will be of type Double.

Details
Parameters
value
The double-precision floating point value for the Variant.

Variant

 Variant(
  float value
)

Construct a Variant containing the given single-precision floating point value.

The Variant constructed will be of type Double.

Details
Parameters
value
The single-precision floating point value for the Variant.

Variant

 Variant(
  bool value
)

Construct a Variant containing the given boolean value.

The Variant constructed will be of type Bool.

Details
Parameters
value
The boolean value for the Variant.

Variant

 Variant(
  const char *value
)

Construct a Variant with the given static const string (no copy).

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.

Details
Parameters
value
A pointer to the static null-terminated string for the Variant.

Variant

 Variant(
  char *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
value
A pointer to a null-terminated string, which will be copied into to the Variant.

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
value
The string to use for the Variant.

Variant

 Variant(
  const std::vector< Variant > & value
)

Construct a Variant containing the given std::vector of Variant.

The Variant constructed will be of type Vector.

Details
Parameters
value
The STL vector to copy into the Variant.

Variant

 Variant(
  const std::vector< T > & value
)

Construct a Variant containing the given std::vector of something that can be constructed into a Variant.

The Variant constructed will be of type Vector.

Details
Parameters
value
An STL vector containing elements that can be converted to Variant (such as ints, strings, vectors). A Variant will be created for each element, and copied into the Vector Variant constructed here.

Variant

 Variant(
  const T array_of_values[],
  size_t array_size
)

Construct a Variant from an array of supported types into a Vector.

The Variant constructed will be of type Vector.

Details
Parameters
array_of_values
A C array containing elements that can be converted to Variant (such as ints, strings, vectors). A Variant will be created for each element, and copied into the Vector Variant constructed here.
array_size
Number of elements of the array.

Variant

 Variant(
  const std::map< Variant, Variant > & value
)

Construct a Variatn containing the given std::map of Variant to Variant.

The Variant constructed will be of type Map.

Details
Parameters
value
The STL map to copy into the Variant.

Variant

 Variant(
  const std::map< K, V > & value
)

Construct a Variant containing the given std::map of something that can be constructed into a Variant, to something that can be constructed into a Variant.

The Variant constructed will be of type Map.

Details
Parameters
value
An STL map containing keys and values that can be converted to Variant (such as ints, strings, vectors). A Variant will be created for each key and for each value, and copied by pairs into the Map Variant constructed here.

Variant

 Variant(
  const Variant & other
)

Copy constructor.

Performs a deep copy.

Details
Parameters
other
Source Variant to copy from.

Variant

 Variant(
  Variant && other
)

Move constructor.

Efficiently moves the more complex data types by simply reassigning pointer ownership.

Details
Parameters
other
Source Variant to move from.

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 

Const accessor for a Variant containing a bool.

Note:If the Variant is not of Bool type, this will assert.

Details
Returns
The bool contained in this Variant.

double_value

double double_value() const 

Const accessor for a Variant containing a double.

Note:If the Variant is not of Double type, this will assert.

Details
Returns
The double contained in this Variant.

int64_value

int64_t int64_value() const 

Const accessor for a Variant containing an integer.

Note:If the Variant is not of Int64 type, this will assert.

Details
Returns
The integer contained in this Variant.

is_blob

bool is_blob() const 

Get whether this Variant contains a blob.

Note:No matter which type of blob the Variant contains, you can read its data via blob_data() and get its size via blob_size().

Details
Returns
True if the Variant's type is either StaticBlob or MutableBlob; false otherwise.

is_bool

bool is_bool() const 

Get whether this Variant contains a bool.

Details
Returns
True if the Variant's type is Bool, false otherwise.

is_container_type

bool is_container_type() const 

Get whether this Variant contains a container type: Vector or Map.

Details
Returns
True if the Variant's type is Vector or Map; false otherwise.

is_double

bool is_double() const 

Get whether this Variant contains a double.

Details
Returns
True if the Variant's type is Double, false otherwise.

is_fundamental_type

bool is_fundamental_type() const 

Get whether this Variant contains a fundamental type: Null, Int64, Double, Bool, or one of the two String types.

Essentially !is_containerType().

Details
Returns
True if the Variant's type is Int64, Double, Bool, or Null; false otherwise.

is_int64

bool is_int64() const 

Get whether this Variant contains an integer.

Details
Returns
True if the Variant's type is Int64, false otherwise.

is_map

bool is_map() const 

Get whether this Variant contains a map.

Details
Returns
True if the Variant's type is Map, false otherwise.

is_mutable_blob

bool is_mutable_blob() const 

Get whether this Variant contains a mutable blob.

Details
Returns
True if the Variant's type is MutableBlob, false otherwise.

is_mutable_string

bool is_mutable_string() const 

Get whether this Variant contains a mutable string.

Details
Returns
True if the Variant's type is MutableString, false otherwise.

is_null

bool is_null() const 

Get whether this Variant is currently null.

Details
Returns
True if the Variant is Null, false otherwise.

is_numeric

bool is_numeric() const 

Get whether this Variant contains a numeric type, Int64 or Double.

Details
Returns
True if the Variant's type is either Int64 or Double; false otherwise.

is_static_blob

bool is_static_blob() const 

Get whether this Variant contains a static blob.

Details
Returns
True if the Variant's type is StaticBlob, false otherwise.

is_static_string

bool is_static_string() const 

Get whether this Variant contains a static string.

Details
Returns
True if the Variant's type is StaticString, false otherwise.

is_string

bool is_string() const 

Get whether this Variant contains a string.

Note:No matter which type of string the Variant contains, you can read its value via string_value().

Details
Returns
True if the Variant's type is either StaticString or MutableString; false otherwise.

is_vector

bool is_vector() const 

Get whether this Variant contains a vector.

Details
Returns
True if the Variant's type is Vector, false otherwise.

map

std::map< Variant, Variant > & map()

Mutable accessor for a Variant containing a map of Variant data.

Note:If the Variant is not of Map type, this will assert.

Details
Returns
Reference to the map contained in this Variant.

map

const std::map< Variant, Variant > & map() const 

Const accessor for a Variant containing a map of strings to Variant data.

Note:If the Variant is not of Map type, this will assert.

Details
Returns
Reference to the map contained in this Variant.

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.

Note:Unlike the non-const accessor, this accessor cannot "promote" a static blob to mutable, and thus will assert if the Variant 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 a Variant containing a string.

If the Variant 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 the Variant is not one of the two String types, this will assert.

Details
Returns
Reference to the string contained in this Variant.

mutable_string

const std::string & mutable_string() const 

Const accessor for a Variant containing a mutable string only.

Note:Unlike the non-const accessor, this accessor cannot "promote" a static string to mutable, and thus will assert if the Variant you pass in is not of MutableString type.

Details
Returns
Reference to the string contained in this Variant.

operator!=

bool operator!=(
  const Variant & other
) const 

Inequality operator: x != y is evaluated as !(x == y).

Details
Parameters
other
Variant to compare to.
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.

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
other
Variant to compare to.
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
other
Variant to compare to.
Returns
Results of the comparison.

operator=

Variant & operator=(
  const Variant & other
)

Copy assignment operator.

Performs a deep copy.

Details
Parameters
other
Source Variant to copy from.

operator=

Variant & operator=(
  Variant && other
)

Move assignment operator.

Efficiently moves the more complex data types by simply reassigning pointer ownership.

Details
Parameters
other
Source Variant to move from.

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
other
Variant to compare to.
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
other
Variant to compare to.
Returns
Results of the comparison.

operator>=

bool operator>=(
  const Variant & other
) const 

Inequality operator: x >= y is evaluated as !(x < y)

Details
Parameters
other
Variant to compare to.
Returns
Results of the comparison.

set_bool_value

void set_bool_value(
  bool value
)

Sets the Variant to the given boolean value.

The Variant's type will be set to Bool.

Details
Parameters
value
The boolean value for the Variant.

set_double_value

void set_double_value(
  double value
)

Sets the Variant to an double-precision floating point value.

The Variant's type will be set to Double.

Details
Parameters
value
The double-precision floating point value for the Variant.

set_int64_value

void set_int64_value(
  int64_t value
)

Sets the Variant to an 64-bit integer value.

The Variant's type will be set to Int64.

Details
Parameters
value
The 64-bit integer value for the Variant.

set_map

void set_map(
  const std::map< Variant, Variant > & value
)

Sets the Variant to a copy of the given map.

The Variant's type will be set to Map.

Details
Parameters
value
The STL map to copy into the Variant.

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
src_data
The data to use for the Variant. If you pass in nullptr, no data will be copied, but a buffer of the requested size will be allocated.
size_bytes
The size of the data, in bytes.

set_mutable_string

void set_mutable_string(
  const std::string & value
)

Sets the Variant to a copy of the given string.

The Variant's type will be set to MutableString.

Details
Parameters
value
The string to use for the Variant.

set_null

void set_null()

Sets the Variant value to null.

The Variant's type will be Null.

set_static_blob

void set_static_blob(
  const void *static_data,
  size_t size_bytes
)

Sets the Variant to point to static binary data.

The Variant'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 the Variant, or otherwise call mutable_blob_data() or set_mutable_blob(), which will copy the data into an internal buffer.

Details
Parameters
static_data
Pointer to statically-allocated binary data. The Variant will point to the data, not copy it.
size_bytes
Size of the data, in bytes.

set_string_value

void set_string_value(
  const char *value
)

Sets the Variant to point to a static string buffer.

The Variant'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 the Variant, or otherwise call mutable_string() or set_mutable_string(), which will copy the string to an internal buffer.

Details
Parameters
value
A pointer to the static null-terminated string for the Variant.

set_string_value

void set_string_value(
  char *value
)

Sets the Variant to a mutable string.

The Variant's type will be set to MutableString.

Details
Parameters
value
A pointer to a null-terminated string, which will be copied into to the Variant.

set_string_value

void set_string_value(
  const std::string & value
)

Sets the Variant to a mutable string.

The Variant's type will be set to MutableString.

Details
Parameters
value
The string to use for the Variant.

set_vector

void set_vector(
  const std::vector< Variant > & value
)

Sets the Variant to a copy of the given vector.

The Variant's type will be set to Vector.

Details
Parameters
value
The STL vector to copy into the Variant.

string_value

const char * string_value() const 

Const accessor for a Variant containing a string.

This can return both static and mutable strings. The pointer is only guaranteed to persist if this Variant's type is StaticString.

Note:If the Variant is not of StaticString or MutableString type, this will assert.

Details
Returns
The string contained in this Variant.

type

Type type() const 

Get the current type contained in this Variant.

Details
Returns
The Variant's type.

vector

std::vector< Variant > & vector()

Mutable accessor for a Variant containing a vector of Variant data.

Note:If the Variant is not of Vector type, this will assert.

Details
Returns
Reference to the vector contained in this Variant.

vector

const std::vector< Variant > & vector() const 

Const accessor for a Variant containing a vector of Variant data.

Note:If the Variant is not of Vector type, this will assert.

Details
Returns
Reference to the vector contained in this Variant.

~Variant

 ~Variant()

Destructor. Frees the memory that this Variant owns.

Public static functions

EmptyMap

Variant EmptyMap()

Get a Variant containing an empty map.

You can immediately call map() on it to work with the map it contains.

Details
Returns
A Variant of type Map, containing no elements.

EmptyMutableBlob

Variant EmptyMutableBlob(
  size_t size_bytes
)

Return a Variant containing an empty mutable blob of the requested size, filled with 0-bytes.

Details
Parameters
size_bytes
Size of the buffer you want, in bytes.
Returns
A Variant containing a mutable blob of the requested size, filled with 0-bytes.

EmptyMutableString

Variant EmptyMutableString()

Get a Variant containing an empty mutable string.

Details
Returns
A Variant of type MutableString, containing an empty string.

EmptyString

Variant EmptyString()

Get an empty string variant.

Details
Returns
A Variant of type StaticString, referring to an empty string.

EmptyVector

Variant EmptyVector()

Get a Variant containing an empty vector.

You can immediately call vector() on it to work with the vector it contains.

Details
Returns
A Variant of type Vector, containing no elements.

False

Variant False()

Get a Variant of bool value false.

Details
Returns
A Variant of type Bool, with value false.

FromBool

Variant FromBool(
  bool value
)

Return a Variant from a boolean.

Details
Parameters
value
Boolean value to put into the Variant.
Returns
A Variant containing the Boolean.

FromDouble

Variant FromDouble(
  double value
)

Return a Variant from a double-precision floating point number.

Details
Parameters
value
Double-precision floating point value to put into the Variant;
Returns
A Variant containing the double-precision floating point number.

FromInt64

Variant FromInt64(
  int64_t value
)

Return a Variant from a 64-bit integer.

Details
Parameters
value
64-bit integer value to put into the Variant.
Returns
A Variant containing the 64-bit integer.

FromMutableBlob

Variant FromMutableBlob(
  const void *src_data,
  size_t size_bytes
)

Return a Variant containing a copy of binary data.

Details
Parameters
src_data
Pointer to binary data to be copied into the Variant.
size_bytes
Size of the data, in bytes.
Returns
A Variant containing a copy of the binary data.

FromMutableString

Variant FromMutableString(
  const std::string & value
)

Return a Variant from a string.

This method makes a copy of the string.

Details
Parameters
value
String value to copy into the Variant.
Returns
A Variant containing a copy of the string.

FromStaticBlob

Variant FromStaticBlob(
  const void *static_data,
  size_t size_bytes
)

Return a Variant 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 the Variant, otherwise call mutable_blob() or set_mutable_blob(), which will copy the data to an internal buffer.

Details
Parameters
static_data
Pointer to statically-allocated binary data. The Variant will point to the data, not copy it.
size_bytes
Size of the data, in bytes.
Returns
A Variant pointing to the binary data.

FromStaticString

Variant FromStaticString(
  const char *value
)

Return a Variant from a static string.

Note:If you use this function, 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.

Details
Parameters
value
Pointer to statically-allocated null-terminated string.
Returns
A Variant referring to the string pointer you passed in.

MutableStringFromStaticString

Variant MutableStringFromStaticString(
  const char *value
)

Return a Variant from a string, but make it mutable.

Only copies the string once, unlike Variant(std::string(value)), which copies the string twice.

Details
Parameters
value
String value to copy into the Variant and make mutable.
Returns
A Variant containing a mutable copy of the string.

Null

Variant Null()

Get a Variant of type Null.

Details
Returns
A Variant of type Null.

One

Variant One()

Get a Variant of integer value 1.

Details
Returns
A Variant of type Int64, with value 1.

OnePointZero

Variant OnePointZero()

Get a Variant of double value 1.0.

Details
Returns
A Variant of type Double, with value 1.0.

True

Variant True()

Get a Variant of bool value true.

Details
Returns
A Variant of type Bool, with value true.

TypeName

const char * TypeName(
  Type type
)

Get the human-readable type name of a Variant type.

Details
Parameters
type
Variant type to describe.
Returns
A string describing the type, suitable for error messages or debugging. For example "Int64" or "MutableString".

Zero

Variant Zero()

Get a Variant of integer value 0.

Details
Returns
A Variant of type Int64, with value 0.

ZeroPointZero

Variant ZeroPointZero()

Get a Variant of double value 0.0.

Details
Returns
A Variant of type Double, with value 0.0.

Send feedback about...

Need help? Visit our support page.