Variant

Simple variant class that can hold an integer, boolean, string, double, dictionary, array or null value. More...

#include <unity/scopes/Variant.h>

Public Types

enum  Type {
  Null, Int, Bool, String,
  Double, Dict, Array, Int64
}
 Type of value held by a Variant instance.
 

Public Member Functions

Copy and assignment

Copy and assignment operators (move and non-move versions) have the usual value semantics.

 Variant (Variant const &)
 
 Variant (Variant &&)
 
Variantoperator= (Variant const &)
 
Variantoperator= (Variant &&)
 
Value assignment

The assignment operators replace the value currently held by a Variant with the supplied value, potentially changing the type of the value held by the Variant. Assigning a const char* to a Variant stores the corresponding std::string value.

Variantoperator= (int val) noexcept
 
Variantoperator= (int64_t val) noexcept
 
Variantoperator= (double val) noexcept
 
Variantoperator= (bool val) noexcept
 
Variantoperator= (std::string const &val)
 
Variantoperator= (char const *val)
 
Variantoperator= (VariantMap const &val)
 
Variantoperator= (VariantArray const &val)
 
Comparison operators

Two variants are considered equal if they both store values of the same type and equal value.

For Variants storing values of different type, ordering follows the types defined in the Type enum. For example, any integer value compares less than any boolean value. For Variants of the same type, the stored values determine order as usual.

bool operator== (Variant const &) const noexcept
 
bool operator< (Variant const &) const noexcept
 
Value accessors

The accessor methods retrieve a value of the specified type.

If a Variant currently stores a value of different type, these methods throw unity::LogicException.

int get_int () const
 
int64_t get_int64_t () const
 
double get_double () const
 
bool get_bool () const
 
std::string get_string () const
 
VariantMap get_dict () const
 
VariantArray get_array () const
 
bool is_null () const
 Test if variant holds null value. More...
 
Observers
Type which () const noexcept
 Returns the type of value currently stored by this Variant.
 
Modifiers
void swap (Variant &other) noexcept
 Swaps the contents of this Variant with other.
 

Constructors and destructor

 Variant () noexcept
 The default constructor creates a Variant instance containing a null.
 
 Variant (int val) noexcept
 Creates a Variant instance that stores the supplied integer.
 
 Variant (int64_t val) noexcept
 
 Variant (double val) noexcept
 Creates a Variant instance that stores the supplied double.
 
 Variant (bool val) noexcept
 Creates a Variant instance that stores the supplied boolean.
 
 Variant (std::string const &val)
 Creates a Variant instance that stores the supplied string.
 
 Variant (char const *val)
 Converts the supplied pointer to a string and stores the string in the Variant instance.
 
 Variant (VariantMap const &val)
 
 Variant (VariantArray const &val)
 
 ~Variant ()
 Destructor.
 
static Variant const & null ()
 Construct a null variant.
 

Serialization

std::string serialize_json () const
 Serializes the variant to a JSON encoded string.
 
static Variant deserialize_json (std::string const &json_string)
 Deserializes a JSON encoded string to a Variant.
 

Detailed Description

Simple variant class that can hold an integer, boolean, string, double, dictionary, array or null value.

Member Function Documentation

bool unity::scopes::Variant::is_null ( ) const

Test if variant holds null value.

Returns
True if variant holds null.