Register a new variant structure within a union. The newly created variant is returned.
initializer for {@link VariantLayout#variant|variant}.
initializer for {@link VariantLayout#layout|layout}.
initializer for {@link Layout#property|property}.
Implement {@link Layout#decode|decode} for Union.
If the variant is {@link Union#addVariant|registered} the return value is an instance of that variant, with no explicit discriminator. Otherwise the {@link Union#defaultLayout|default layout} is used to decode the content.
Method to infer a registered Union variant compatible with src
.
The first satisfied rule in the following sequence defines the return value:
src
has properties matching the Union discriminator and
the default layout, undefined
is returned regardless of the
value of the discriminator property (this ensures the default
layout will be used);src
has a property matching the Union discriminator, the
value of the discriminator identifies a registered variant, and
either (a) the variant has no layout, or (b) src
has the
variant's property, then the variant is returned (because the
source satisfies the constraints of the variant it identifies);src
does not have a property matching the Union
discriminator, but does have a property matching a registered
variant, then the variant is returned (because the source
matches a variant without an explicit conflict);an object presumed to be compatible with the content of the Union.
Implement {@link Layout#encode|encode} for Union.
This API assumes the src
object is consistent with the union's
{@link Union#defaultLayout|default layout}. To encode variants
use the appropriate variant-specific {@link VariantLayout#encode}
method.
Create an object from layout properties and an array of values.
NOTE This function returns undefined
if invoked on a layout
that does not return its value as an Object. Objects are
returned for things that are a Structure, which includes
variant layouts if they are structures, and
excludes Unions. If you want this feature for a union
you must use getVariant to select the
desired layout.
an array of values that correspond to the default order for properties. As with {@link Layout#decode|decode} layout elements that have no property name are skipped when iterating over the array values. Only the top-level properties are assigned; arguments are not assigned to properties of contained layouts. Any unused values are ignored.
Get the layout associated with a registered variant.
If vb
does not produce a registered variant the function returns
undefined
.
either the variant number, or a buffer from which the discriminator is to be read.
offset into vb
for the start of the
union. Used only when vb
is an instance of {Uint8Array}.
|undefined)}
Function to create an Object into which decoded properties will be written.
Used only for layouts that {@link Layout#decode|decode} to Object instances, which means:
If left undefined the JavaScript representation of these layouts will be Object instances.
Replicate the layout using a new property.
This function must be used to get a structurally-equivalent layout with a different name since all Layout instances are immutable.
NOTE This is a shallow copy. All fields except {@link Layout#property|property} are strictly equal to the origin layout.
the value for {@link Layout#property|property} in the replica.
property
.Generated using TypeDoc
Represent any number of span-compatible layouts.
Factory: {@link module:Layout.union|union}
If the union has a {@link Union#defaultLayout|default layout} that layout must have a non-negative {@link Layout#span|span}. The span of a fixed-span union includes its {@link Union#discriminator|discriminator} if the variant is a {@link Union#usesPrefixDiscriminator|prefix of the union}, plus the span of its {@link Union#defaultLayout|default layout}.
If the union does not have a default layout then the encoded span of the union depends on the encoded span of its variant (which may be fixed or variable).
{@link VariantLayout#layout|Variant layout}s are added through {@link Union#addVariant|addVariant}. If the union has a default layout, the span of the {@link VariantLayout#layout|layout contained by the variant} must not exceed the span of the {@link Union#defaultLayout|default layout} (minus the span of a {@link Union#usesPrefixDiscriminator|prefix disriminator}, if used). The span of the variant will equal the span of the union itself.
The variant for a buffer can only be identified from the {@link Union#discriminator|discriminator} {@link UnionDiscriminator#property|property} (in the case of the {@link Union#defaultLayout|default layout}), or by using {@link Union#getVariant|getVariant} and examining the resulting VariantLayout instance.
A variant compatible with a JavaScript object can be identified using {@link Union#getSourceVariant|getSourceVariant}.
How to identify the layout used to interpret the union contents. The parameter must be an instance of UnionDiscriminator, an ExternalLayout that satisfies {@link ExternalLayout#isCount|isCount()}, or UInt (or UIntBE). When a non-external layout element is passed the layout appears at the start of the union. In all cases the (synthesized) UnionDiscriminator instance is recorded as {@link Union#discriminator|discriminator}.
initializer for {@link Union#defaultLayout|defaultLayout}. If absent defaults to
null
. Ifnull
there is no default layout: the union has data-dependent length and attempts to decode or encode unrecognized variants will throw an exception. A Layout instance must have a non-negative {@link Layout#span|span}, and if it lacks a {@link Layout#property|property} the {@link Union#defaultLayout|defaultLayout} will be a {@link Layout#replicate|replica} with propertycontent
.initializer for {@link Layout#property|property}.