Loading...
Searching...
No Matches
juce::var Class Reference

## Detailed Description

A variant class, that can be used to hold a range of primitive values.

A var object can hold a range of simple primitive values, strings, or any kind of ReferenceCountedObject. The var class is intended to act like the kind of values used in dynamic scripting languages.

You can save/load var objects either in a small, proprietary binary format using writeToStream()/readFromStream(), or as JSON by using the JSON class.

See also
  JSON, DynamicObject

The documentation for this class was generated from the following file:

- juce_core/containers/juce_Variant.h

## Classes

struct | NativeFunctionArgs
| This structure is passed to a NativeFunction callback, and contains invocation details about the function's arguments and context. More...

## Public Types

using | NativeFunction = std::function<var (const NativeFunctionArgs&)>

## Public Member Functions

| var () noexcept
| Creates a void variant.
| ~var () noexcept
| Destructor.
| var (const var &valueToCopy)
| var (int value) noexcept
| var (int64 value) noexcept
| var (bool value) noexcept
| var (double value) noexcept
| var (const char *value)
| var (const wchar_t *value)
| var (const String &value)
| var (const Array< var > &value)
| var (const StringArray &value)
| var (ReferenceCountedObject *object)
| var (NativeFunction method) noexcept
| var (const void *binaryData, size_t dataSize)
| var (const MemoryBlock &binaryData)
var & | operator= (const var &valueToCopy)
var & | operator= (int value)
var & | operator= (int64 value)
var & | operator= (bool value)
var & | operator= (double value)
var & | operator= (const char *value)
var & | operator= (const wchar_t *value)
var & | operator= (const String &value)
var & | operator= (const MemoryBlock &value)
var & | operator= (const Array< var > &value)
var & | operator= (ReferenceCountedObject *object)
var & | operator= (NativeFunction method)
| var (var &&) noexcept
| var (String &&)
| var (MemoryBlock &&)
| var (Array< var > &&)
var & | operator= (var &&) noexcept
var & | operator= (String &&)
void | swapWith (var &other) noexcept
| operator int () const noexcept
| operator int64 () const noexcept
| operator bool () const noexcept
| operator float () const noexcept
| operator double () const noexcept
| operator String () const
String | toString () const
Array< var > * | getArray () const noexcept
| If this variant holds an array, this provides access to it.
MemoryBlock * | getBinaryData () const noexcept
| If this variant holds a memory block, this provides access to it.
ReferenceCountedObject * | getObject () const noexcept
DynamicObject * | getDynamicObject () const noexcept
bool | isVoid () const noexcept
bool | isUndefined () const noexcept
bool | isInt () const noexcept
bool | isInt64 () const noexcept
bool | isBool () const noexcept
bool | isDouble () const noexcept
bool | isString () const noexcept
bool | isObject () const noexcept
bool | isArray () const noexcept
bool | isBinaryData () const noexcept
bool | isMethod () const noexcept
bool | equals (const var &other) const noexcept
| Returns true if this var has the same value as the one supplied.
bool | equalsWithSameType (const var &other) const noexcept
| Returns true if this var has the same value and type as the one supplied.
bool | hasSameTypeAs (const var &other) const noexcept
| Returns true if this var has the same type as the one supplied.
var | clone () const noexcept
| Returns a deep copy of this object.
int | size () const
| If the var is an array, this returns the number of elements.
const var & | operator[] (int arrayIndex) const
| If the var is an array, this can be used to return one of its elements.
var & | operator[] (int arrayIndex)
| If the var is an array, this can be used to return one of its elements.
void | append (const var &valueToAppend)
| Appends an element to the var, converting it to an array if it isn't already one.
void | insert (int index, const var &value)
| Inserts an element to the var, converting it to an array if it isn't already one.
void | remove (int index)
| If the var is an array, this removes one of its elements.
void | resize (int numArrayElementsWanted)
| Treating the var as an array, this resizes it to contain the specified number of elements.
int | indexOf (const var &value) const
| If the var is an array, this searches it for the first occurrence of the specified value, and returns its index.
const var & | operator[] (const Identifier &propertyName) const
| If this variant is an object, this returns one of its properties.
const var & | operator[] (const char *propertyName) const
| If this variant is an object, this returns one of its properties.
var | getProperty (const Identifier &propertyName, const var &defaultReturnValue) const
| If this variant is an object, this returns one of its properties, or a default fallback value if the property is not set.
bool | hasProperty (const Identifier &propertyName) const noexcept
| Returns true if this variant is an object and if it has the given property.
var | call (const Identifier &method) const
| Invokes a named method call with no arguments.
var | call (const Identifier &method, const var &arg1) const
| Invokes a named method call with one argument.
var | call (const Identifier &method, const var &arg1, const var &arg2) const
| Invokes a named method call with 2 arguments.
var | call (const Identifier &method, const var &arg1, const var &arg2, const var &arg3)
| Invokes a named method call with 3 arguments.
var | call (const Identifier &method, const var &arg1, const var &arg2, const var &arg3, const var &arg4) const
| Invokes a named method call with 4 arguments.
var | call (const Identifier &method, const var &arg1, const var &arg2, const var &arg3, const var &arg4, const var &arg5) const
| Invokes a named method call with 5 arguments.
var | invoke (const Identifier &method, const var *arguments, int numArguments) const
| Invokes a named method call with a list of arguments.
NativeFunction | getNativeFunction () const
| If this object is a method, this returns the function pointer.
void | writeToStream (OutputStream &output) const
| Writes a binary representation of this value to a stream.

## Static Public Member Functions

static var | undefined () noexcept
| Returns a var object that can be used where you need the javascript "undefined" value.
static var | readFromStream (InputStream &input)
| Reads back a stored binary representation of a value.

## Member Typedef Documentation

## NativeFunction

using juce::var::NativeFunction = std::function<var (const NativeFunctionArgs&)>

## Constructors and Destructors

## var() [1/19]

juce::var::var | ( |  | ) |

noexcept

Creates a void variant.

Referenced by append(), call(), call(), call(), call(), call(), call(), canCompare, clone(), equals(), equalsWithSameType(), getArray(), getProperty(), hasSameTypeAs(), indexOf(), insert(), invoke(), juce::var::NativeFunctionArgs::NativeFunctionArgs(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator=(), operator[](), operator[](), operator[](), operator[](), readFromStream(), swapWith(), undefined(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), var(), and ~var().

## ~var()

juce::var::~var | ( |  | ) |

noexcept

Destructor.

References var().

## var() [2/19]

juce::var::var | ( | const var & | valueToCopy | ) |

References var().

## var() [3/19]

juce::var::var | ( | int | value | ) |

noexcept

References var().

## var() [4/19]

juce::var::var | ( | int64 | value | ) |

noexcept

References var().

## var() [5/19]

juce::var::var | ( | bool | value | ) |

noexcept

References var().

## var() [6/19]

juce::var::var | ( | double | value | ) |

noexcept

References var().

## var() [7/19]

juce::var::var | ( | const char * | value | ) |

References var().

## var() [8/19]

juce::var::var | ( | const wchar_t * | value | ) |

References var().

## var() [9/19]

juce::var::var | ( | const String & | value | ) |

References var().

## var() [10/19]

juce::var::var | ( | const Array< var > & | value | ) |

References var().

## var() [11/19]

juce::var::var | ( | const StringArray & | value | ) |

References var().

## var() [12/19]

juce::var::var | ( | ReferenceCountedObject * | object | ) |

References var().

## var() [13/19]

juce::var::var | ( | NativeFunction | method | ) |

noexcept

References var().

## var() [14/19]

juce::var::var | ( | const void * | binaryData,
|  | size_t | dataSize )

References var().

## var() [15/19]

juce::var::var | ( | const MemoryBlock & | binaryData | ) |

References var().

## var() [16/19]

juce::var::var | ( | var && |  | ) |

noexcept

References var().

## var() [17/19]

juce::var::var | ( | String && |  | ) |

References var().

## var() [18/19]

juce::var::var | ( | MemoryBlock && |  | ) |

References var().

## var() [19/19]

juce::var::var | ( | Array< var > && |  | ) |

References var().

## Member Functions

## operator=() [1/14]

var & juce::var::operator= | ( | const var & | valueToCopy | ) |

References var().

## operator=() [2/14]

var & juce::var::operator= | ( | int | value | ) |

References var().

## operator=() [3/14]

var & juce::var::operator= | ( | int64 | value | ) |

References var().

## operator=() [4/14]

var & juce::var::operator= | ( | bool | value | ) |

References var().

## operator=() [5/14]

var & juce::var::operator= | ( | double | value | ) |

References var().

## operator=() [6/14]

var & juce::var::operator= | ( | const char * | value | ) |

References var().

## operator=() [7/14]

var & juce::var::operator= | ( | const wchar_t * | value | ) |

References var().

## operator=() [8/14]

var & juce::var::operator= | ( | const String & | value | ) |

References var().

## operator=() [9/14]

var & juce::var::operator= | ( | const MemoryBlock & | value | ) |

References var().

## operator=() [10/14]

var & juce::var::operator= | ( | const Array< var > & | value | ) |

References var().

## operator=() [11/14]

var & juce::var::operator= | ( | ReferenceCountedObject * | object | ) |

References var().

## operator=() [12/14]

var & juce::var::operator= | ( | NativeFunction | method | ) |

References var().

## operator=() [13/14]

var & juce::var::operator= | ( | var && |  | ) |

noexcept

References var().

## operator=() [14/14]

var & juce::var::operator= | ( | String && |  | ) |

References var().

## swapWith()

void juce::var::swapWith | ( | var & | other | ) |

noexcept

References swapWith(), and var().

Referenced by swapWith().

## undefined()

var juce::var::undefined | ( |  | ) |

static noexcept

Returns a var object that can be used where you need the javascript "undefined" value.

References undefined(), and var().

Referenced by undefined().

## operator int()

juce::var::operator int | ( |  | ) | const

noexcept

## operator int64()

juce::var::operator int64 | ( |  | ) | const

noexcept

## operator bool()

juce::var::operator bool | ( |  | ) | const

noexcept

## operator float()

juce::var::operator float | ( |  | ) | const

noexcept

## operator double()

juce::var::operator double | ( |  | ) | const

noexcept

## operator String()

juce::var::operator String | ( |  | ) | const

## toString()

String juce::var::toString | ( |  | ) | const

References toString().

Referenced by toString().

## getArray()

Array< var > * juce::var::getArray | ( |  | ) | const

noexcept

If this variant holds an array, this provides access to it.

NOTE: Beware when you use this - the array pointer is only valid for the lifetime of the variant that returned it, so be very careful not to call this method on temporary var objects that are the return-value of a function, and which may go out of scope before you use the array!

References getArray(), and var().

Referenced by getArray(), and juce::ValueTreePropertyWithDefault::setValue().

## getBinaryData()

MemoryBlock * juce::var::getBinaryData | ( |  | ) | const

noexcept

If this variant holds a memory block, this provides access to it.

NOTE: Beware when you use this - the MemoryBlock pointer is only valid for the lifetime of the variant that returned it, so be very careful not to call this method on temporary var objects that are the return-value of a function, and which may go out of scope before you use the MemoryBlock!

References getBinaryData().

Referenced by getBinaryData().

## getObject()

ReferenceCountedObject * juce::var::getObject | ( |  | ) | const

noexcept

References getObject().

Referenced by getObject().

## getDynamicObject()

DynamicObject * juce::var::getDynamicObject | ( |  | ) | const

noexcept

References getDynamicObject().

Referenced by getDynamicObject().

## isVoid()

bool juce::var::isVoid | ( |  | ) | const

noexcept

References isVoid().

Referenced by isVoid().

## isUndefined()

bool juce::var::isUndefined | ( |  | ) | const

noexcept

References isUndefined().

Referenced by isUndefined().

## isInt()

bool juce::var::isInt | ( |  | ) | const

noexcept

References isInt().

Referenced by isInt().

## isInt64()

bool juce::var::isInt64 | ( |  | ) | const

noexcept

References isInt64().

Referenced by isInt64().

## isBool()

bool juce::var::isBool | ( |  | ) | const

noexcept

References isBool().

Referenced by isBool().

## isDouble()

bool juce::var::isDouble | ( |  | ) | const

noexcept

References isDouble().

Referenced by isDouble().

## isString()

bool juce::var::isString | ( |  | ) | const

noexcept

References isString().

Referenced by isString().

## isObject()

bool juce::var::isObject | ( |  | ) | const

noexcept

References isObject().

Referenced by isObject().

## isArray()

bool juce::var::isArray | ( |  | ) | const

noexcept

References isArray().

Referenced by isArray().

## isBinaryData()

bool juce::var::isBinaryData | ( |  | ) | const

noexcept

References isBinaryData().

Referenced by isBinaryData().

## isMethod()

bool juce::var::isMethod | ( |  | ) | const

noexcept

References isMethod().

Referenced by isMethod().

## equals()

bool juce::var::equals | ( | const var & | other | ) | const

noexcept

Returns true if this var has the same value as the one supplied.

Note that this ignores the type, so a string var "123" and an integer var with the value 123 are considered to be equal.

Note that equality checking depends on the "wrapped" type of the object on which equals() is called. That means the following code will convert the right-hand-side argument to a string and compare the string values, because the object on the left-hand-side was initialised from a string:

var ("123").equals (var (123))
juce::var::var
var() noexcept
Creates a void variant.

However, the following code will convert the right-hand-side argument to a double and compare the values as doubles, because the object on the left-hand-side was initialised from a double:

var (45.6).equals ("45.6000")

See also
  equalsWithSameType

References equals(), and var().

Referenced by equals().

## equalsWithSameType()

bool juce::var::equalsWithSameType | ( | const var & | other | ) | const

noexcept

Returns true if this var has the same value and type as the one supplied.

This differs from equals() because e.g. "123" and 123 will be considered different.

See also
  equals

References equalsWithSameType(), and var().

Referenced by equalsWithSameType().

## hasSameTypeAs()

bool juce::var::hasSameTypeAs | ( | const var & | other | ) | const

noexcept

Returns true if this var has the same type as the one supplied.

References hasSameTypeAs(), and var().

Referenced by hasSameTypeAs().

## clone()

var juce::var::clone | ( |  | ) | const

noexcept

Returns a deep copy of this object.

For simple types this just returns a copy, but if the object contains any arrays or DynamicObjects, they will be cloned (recursively).

References clone(), and var().

Referenced by clone().

## size()

int juce::var::size | ( |  | ) | const

If the var is an array, this returns the number of elements.

If the var isn't actually an array, this will return 0.

References size().

Referenced by size().

## operator[]() [1/4]

const var & juce::var::operator[] | ( | int | arrayIndex | ) | const

If the var is an array, this can be used to return one of its elements.

To call this method, you must make sure that the var is actually an array, and that the index is a valid number. If these conditions aren't met, behaviour is undefined. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References var().

## operator[]() [2/4]

var & juce::var::operator[] | ( | int | arrayIndex | ) |

If the var is an array, this can be used to return one of its elements.

To call this method, you must make sure that the var is actually an array, and that the index is a valid number. If these conditions aren't met, behaviour is undefined. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References var().

## append()

void juce::var::append | ( | const var & | valueToAppend | ) |

Appends an element to the var, converting it to an array if it isn't already one.

If the var isn't an array, it will be converted to one, and if its value was non-void, this value will be kept as the first element of the new array. The parameter value will then be appended to it. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References append(), and var().

Referenced by append().

## insert()

void juce::var::insert | ( | int | index,
|  | const var & | value )

Inserts an element to the var, converting it to an array if it isn't already one.

If the var isn't an array, it will be converted to one, and if its value was non-void, this value will be kept as the first element of the new array. The parameter value will then be inserted into it. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References insert(), and var().

Referenced by insert().

## remove()

void juce::var::remove | ( | int | index | ) |

If the var is an array, this removes one of its elements.

If the index is out-of-range or the var isn't an array, nothing will be done. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References remove().

Referenced by remove().

## resize()

void juce::var::resize | ( | int | numArrayElementsWanted | ) |

Treating the var as an array, this resizes it to contain the specified number of elements.

If the var isn't an array, it will be converted to one, and if its value was non-void, this value will be kept as the first element of the new array before resizing. For more control over the array's contents, you can call getArray() and manipulate it directly as an Array<var>.

References resize().

Referenced by resize().

## indexOf()

int juce::var::indexOf | ( | const var & | value | ) | const

If the var is an array, this searches it for the first occurrence of the specified value, and returns its index.

If the var isn't an array, or if the value isn't found, this returns -1.

References indexOf(), and var().

Referenced by indexOf().

## operator[]() [3/4]

const var & juce::var::operator[] | ( | const Identifier & | propertyName | ) | const

If this variant is an object, this returns one of its properties.

References var().

## operator[]() [4/4]

const var & juce::var::operator[] | ( | const char * | propertyName | ) | const

If this variant is an object, this returns one of its properties.

References var().

## getProperty()

var juce::var::getProperty | ( | const Identifier & | propertyName,
|  | const var & | defaultReturnValue ) const

If this variant is an object, this returns one of its properties, or a default fallback value if the property is not set.

References getProperty(), and var().

Referenced by getProperty().

## hasProperty()

bool juce::var::hasProperty | ( | const Identifier & | propertyName | ) | const

noexcept

Returns true if this variant is an object and if it has the given property.

References hasProperty().

Referenced by hasProperty().

## call() [1/6]

var juce::var::call | ( | const Identifier & | method | ) | const

Invokes a named method call with no arguments.

References call(), and var().

Referenced by call(), call(), call(), call(), call(), and call().

## call() [2/6]

var juce::var::call | ( | const Identifier & | method,
|  | const var & | arg1 ) const

Invokes a named method call with one argument.

References call(), and var().

## call() [3/6]

var juce::var::call | ( | const Identifier & | method,
|  | const var & | arg1,
|  | const var & | arg2 ) const

Invokes a named method call with 2 arguments.

References call(), and var().

## call() [4/6]

var juce::var::call | ( | const Identifier & | method,
|  | const var & | arg1,
|  | const var & | arg2,
|  | const var & | arg3 )

Invokes a named method call with 3 arguments.

References call(), and var().

## call() [5/6]

var juce::var::call | ( | const Identifier & | method,
|  | const var & | arg1,
|  | const var & | arg2,
|  | const var & | arg3,
|  | const var & | arg4 ) const

Invokes a named method call with 4 arguments.

References call(), and var().

## call() [6/6]

var juce::var::call | ( | const Identifier & | method,
|  | const var & | arg1,
|  | const var & | arg2,
|  | const var & | arg3,
|  | const var & | arg4,
|  | const var & | arg5 ) const

Invokes a named method call with 5 arguments.

References call(), and var().

## invoke()

var juce::var::invoke | ( | const Identifier & | method,
|  | const var * | arguments,
|  | int | numArguments ) const

Invokes a named method call with a list of arguments.

References invoke(), and var().

Referenced by invoke().

## getNativeFunction()

NativeFunction juce::var::getNativeFunction | ( |  | ) | const

If this object is a method, this returns the function pointer.

References getNativeFunction().

Referenced by getNativeFunction().

## writeToStream()

void juce::var::writeToStream | ( | OutputStream & | output | ) | const

Writes a binary representation of this value to a stream.

The data can be read back later using readFromStream().

See also
  JSON

References writeToStream().

Referenced by writeToStream().

## readFromStream()

var juce::var::readFromStream | ( | InputStream & | input | ) |

static

Reads back a stored binary representation of a value.

The data in the stream must have been written using writeToStream(), or this will have unpredictable results.

See also
  JSON

References readFromStream(), and var().

Referenced by readFromStream().

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited