docs
Loading...
Searching...
No Matches
Value::Ref

#include <Ultralight/js/Value.h>

Overview

A reference to a property of a Value (returned by Value::operator[]).

Reading a Ref gives you the property's value, assigning to it sets the property, and calling it calls the property as a method of the object. Refs chain (obj["a"]["b"] = 1).

Use a Ref within one expression. To keep a property's value, read it into a Value.

Note
A Ref has To(), Maybe(), and Or(). For the other conversions, read the property first (eg, obj["volume"].Get().ToNumber()).

Public Member Functions

Value Get () const
 Read the property.
 operator Value () const
 Read the property (the same as Get()).
template<typename T>
Ref & operator= (T &&value)
 Set the property to value (converted through js::TypeTraits).
Ref operator[] (const char *name) const
 Read this property and access a property of its value.
template<typename I>
requires (std::is_integral_v<I> && !std::is_same_v<I, bool>)
Ref operator[] (I index) const
template<typename R = Value, typename... A>
Result< R > Invoke (A &&... args) const
 Call the property as a method of its object (otherwise the same as Value::Invoke()).
template<typename... A>
Result< Value > operator() (A &&... args) const
 Call the property as a method: obj["save"](path) is the same as obj["save"].Invoke(path).
template<typename T>
Result< T > To () const
 Read the property and convert it to a C++ type (see Value::To()).
template<typename T>
std::optional< T > Maybe () const
 Read the property and convert it to a C++ type like To() but without the failure reason.
template<typename T>
T Or (T fallback) const
 Read the property and convert it to a C++ type with a fallback.
std::string Or (const char *fallback) const
 Read the property and convert it to a std::string with a string-literal fallback.
 Ref (const Ref &)=default
 Copy constructor (refers to the same property).
Ref & operator= (const Ref &other)
 Set this property to the value of another Ref's property.

Friends

class Value
class Context

Constructor & Destructor Documentation

◆ Ref()

Ref ( const Ref & )
default

Copy constructor (refers to the same property).

Member Function Documentation

◆ Get()

Value Get ( ) const
inlinenodiscard

Read the property.

Returns
Returns the property value (an empty Value if the read fails, see Value::operator[]()).

◆ Invoke()

template<typename R = Value, typename... A>
Result< R > Invoke ( A &&... args) const

Call the property as a method of its object (otherwise the same as Value::Invoke()).

global["JSON"]["stringify"](payload);
js::Result<double> total = cart["total"].Invoke<double>();
Expected< T, Error > Result
The result of a JavaScript operation that can fail: a T or a js::Error.
Definition Error.h:520
Parameters
argsThe arguments to pass to the method.
Returns
Returns the call's result converted to R and fails the same way as Value::Invoke().

◆ Maybe()

template<typename T>
std::optional< T > Maybe ( ) const
nodiscard

Read the property and convert it to a C++ type like To() but without the failure reason.

Returns
Returns the converted value or nullopt on any failure.

◆ operator Value()

operator Value ( ) const
inline

Read the property (the same as Get()).

◆ operator()()

template<typename... A>
Result< Value > operator() ( A &&... args) const
inline

Call the property as a method: obj["save"](path) is the same as obj["save"].Invoke(path).

Parameters
argsThe arguments to pass to the method.
Returns
Returns the call's result and fails the same way as Invoke().

◆ operator=() [1/2]

Ref & operator= ( const Ref & other)
inline

Set this property to the value of another Ref's property.

Parameters
otherThe Ref to read the value from.
Returns
Returns this Ref.
Note
A Ref always refers to the same property: assigning to it sets the property's value and never changes which property it refers to (like std::map's operator[]).

◆ operator=() [2/2]

template<typename T>
Ref & operator= ( T && value)

Set the property to value (converted through js::TypeTraits).

Parameters
valueThe value to assign.
Returns
Returns this Ref.
Note
Failures are ignored (eg, the page is gone or a setter throws). Use Value::SetProperty() if you need to check.

◆ operator[]() [1/2]

Ref operator[] ( const char * name) const
inline

Read this property and access a property of its value.

Parameters
nameThe property name.
Returns
Returns a Ref for the nested property.

◆ operator[]() [2/2]

template<typename I>
requires (std::is_integral_v<I> && !std::is_same_v<I, bool>)
Ref operator[] ( I index) const
inline

◆ Or() [1/2]

std::string Or ( const char * fallback) const
inlinenodiscard

Read the property and convert it to a std::string with a string-literal fallback.

Parameters
fallbackThe text to return if the read or the conversion fails.
Returns
Returns the converted string or fallback on any failure.

◆ Or() [2/2]

template<typename T>
T Or ( T fallback) const
nodiscard

Read the property and convert it to a C++ type with a fallback.

Parameters
fallbackThe value to return if the read or the conversion fails.
Returns
Returns the converted value or fallback on any failure.

◆ To()

template<typename T>
Result< T > To ( ) const
nodiscard

Read the property and convert it to a C++ type (see Value::To()).

Returns
Returns the converted value and fails the same way as Value::To().

◆ Context

friend class Context
friend

◆ Value

friend class Value
friend

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