docs
Loading...
Searching...
No Matches
Value

#include <Ultralight/dom/data/Value.h>

Overview

A generic value passed to data-binding formatters and handlers.

A Value represents incoming data in formatters and in action and change handlers that take a generic parameter instead of a concrete C++ type.

DefineFormat() passes the bound field to your formatter as a Value:

// Formats 12500 as "12,500".
ctx.DefineFormat("comma", [](dd::Value v) {
std::string digits = std::to_string(v.Or(int64_t(0)));
for (int i = int(digits.size()) - 3; i > 0; i -= 3)
digits.insert(i, ",");
return digits;
});

What a Value Holds

An instance represents one of several data shapes:

  • An empty state holds no data. Actions dispatched without a payload arrive empty, including every action fired from markup.
  • A leaf holds a single value. Formatters, change handlers, and scalar action payloads provide one boolean, numeric, or string value.
  • An object holds named members. Actions dispatched with a payload struct provide fields that you look up by name.

Brackets look up an object's members by name:

binding.OnAction<"moveItem">([&](dd::Value request) {
int from = request["from"].Or(0);
int to = request["to"].Or(0);
bag.MoveItem(from, to);
});

Converting Values

Choose the extraction method that matches your error-handling style:

  • To() returns a Result. Failure yields a TypeError explaining the mismatch.
  • Maybe() returns an optional. Failure yields std::nullopt without an error.
  • Or() returns a fallback. Failure returns the default value you supplied.

Conversions target bool, integer types, floating-point types, String, std::string, and reflected enums. Integer values convert to floating-point types, but floating-point values don't convert to integers. Reflected enums convert from string values using their enumerator names.

Indexing a missing member or an empty Value returns an empty Value. Because conversions fail safely on empty instances, you don't need to check Contains() or IsEmpty() before reading a member with Or() or Maybe().

Note
dom::data::Value and js::Value are unrelated. A dom::data::Value holds an owned copy of native data rather than a handle into a page's JavaScript context.
See also
dom::data::Binding::OnAction(), dom::data::Binding::OnChange(), dom::data::Context::DefineFormat(), dom::data::ActionInfo

Static Public Member Functions

static Value FromLeaf (const ULDOMDataLeafValue &value)
 Create a leaf Value from a C leaf value.
static Value FromPayload (const ULDOMDataPayloadEntry *payload, size_t payload_count)
 Create an object Value from a C action payload.

Public Member Functions

 Value ()=default
 Create an empty Value.
bool IsEmpty () const
 Whether or not this Value holds nothing.
 operator bool () const
 Whether or not this Value holds something (the opposite of IsEmpty()).
bool IsBoolean () const
 Whether or not this Value is a boolean leaf.
bool IsInt64 () const
 Whether or not this Value is an integer leaf.
bool IsDouble () const
 Whether or not this Value is a floating-point leaf.
bool IsString () const
 Whether or not this Value is a string leaf.
bool IsObject () const
 Whether or not this Value is an object (the members of an action's payload struct).
Value operator[] (const char *name) const
 Get a member of an object.
bool Contains (const char *name) const
 Whether or not this Value is an object with a member of the given name.
size_t member_count () const
 Get the number of members (0 unless this Value is an object).
template<typename T>
Result< T > To () const
 Convert to a C++ type (see "Converting Values" in the class description).
template<typename T>
std::optional< T > Maybe () const
 Convert to a C++ type like To() but without the failure reason.
template<typename T>
T Or (T fallback) const
 Convert to a C++ type like To() but with a fallback.
std::string Or (const char *fallback) const
 Convert to a std::string like To() but with a string-literal fallback.

Constructor & Destructor Documentation

◆ Value()

Value ( )
default

Create an empty Value.

Member Function Documentation

◆ Contains()

bool Contains ( const char * name) const
inline

Whether or not this Value is an object with a member of the given name.

Parameters
nameThe member's name.

◆ FromLeaf()

Value FromLeaf ( const ULDOMDataLeafValue & value)
inlinestatic

Create a leaf Value from a C leaf value.

You only need this when you receive values through the C API (eg, in a change callback you set with ulDOMDataBindingSetChangeCallback()).

Parameters
valueThe C value. Its string is copied (exactly string_length bytes).
Returns
Returns the leaf Value. A color, a StyleValue, or a kind that isn't a leaf gives an empty Value.

◆ FromPayload()

Value FromPayload ( const ULDOMDataPayloadEntry * payload,
size_t payload_count )
inlinestatic

Create an object Value from a C action payload.

Parameters
payloadThe payload's members (members with a null name are skipped).
payload_countThe number of members in payload.
Returns
Returns the object Value or an empty Value if payload is nullptr or payload_count is 0.
Note
A scalar payload arrives as one member named value, so this gives an object with that one member (the typed handlers deliver a leaf instead).

◆ IsBoolean()

bool IsBoolean ( ) const
inline

Whether or not this Value is a boolean leaf.

◆ IsDouble()

bool IsDouble ( ) const
inline

Whether or not this Value is a floating-point leaf.

◆ IsEmpty()

bool IsEmpty ( ) const
inline

Whether or not this Value holds nothing.

◆ IsInt64()

bool IsInt64 ( ) const
inline

Whether or not this Value is an integer leaf.

◆ IsObject()

bool IsObject ( ) const
inline

Whether or not this Value is an object (the members of an action's payload struct).

◆ IsString()

bool IsString ( ) const
inline

Whether or not this Value is a string leaf.

◆ Maybe()

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

Convert to a C++ type like To() but without the failure reason.

Returns
Returns the converted value or nullopt on any failure.

◆ member_count()

size_t member_count ( ) const
inline

Get the number of members (0 unless this Value is an object).

◆ operator bool()

operator bool ( ) const
inlineexplicit

Whether or not this Value holds something (the opposite of IsEmpty()).

◆ operator[]()

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

Get a member of an object.

Parameters
nameThe member's name.
Returns
Returns a copy of the member's value or an empty Value if this Value isn't an object or has no such member.

◆ Or() [1/2]

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

Convert to a std::string like To() but with a string-literal fallback.

Parameters
fallbackThe text to return if the conversion fails (nullptr returns an empty string).
Returns
Returns the converted string or fallback on any failure.

◆ Or() [2/2]

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

Convert to a C++ type like To() but with a fallback.

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

◆ To()

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

Convert to a C++ type (see "Converting Values" in the class description).

Returns
Returns the converted value. Fails with a TypeError if this Value is empty or isn't a leaf of the requested kind.

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