Class: VariantLayout

Layout~VariantLayout(union, variant, layoutopt, propertyopt)

Represent a specific variant within a containing union.

NOTE The span of the variant may include the span of the discriminator used to identify it, but values read and written using the variant strictly conform to the content of layout.

NOTE User code should not invoke this constructor directly. Use the union addVariant helper method.

Constructor

new VariantLayout(union, variant, layoutopt, propertyopt)

Parameters:
Name Type Attributes Description
union module:Layout~Union

initializer for union.

variant Number

initializer for variant.

layout module:Layout~Layout <optional>

initializer for layout. If absent the variant carries no data.

property String <optional>

initializer for property. Unlike many other layouts, variant layouts normally include a property name so they can be identified within their containing Union. The property identifier may be absent only if layout is is absent.

Source:

Extends

Members

layout

The Layout to be used when reading/writing the non-discriminator part of the union. If null the variant carries no data.

Source:

property

The property name used when this layout is represented in an Object.

Used only for layouts that decode to Object instances. If left undefined the span of the unnamed layout will be treated as padding: it will not be mutated by encode nor represented as a property in the decoded Object.

Inherited From:
Source:

span

The span of the layout in bytes.

Positive values are generally expected.

Zero will only appear in Constants and in Sequences where the count is zero.

A negative value indicates that the span is value-specific, and must be obtained using getSpan.

Inherited From:
Source:

union

The Union to which this variant belongs.

Source:

variant

The unsigned integral value identifying this variant within the discriminator of the containing union.

Source:

Methods

decode(b, offsetopt) → {Number|Array|Object}

Decode from a Buffer into an JavaScript value.

Parameters:
Name Type Attributes Description
b Buffer

the buffer from which encoded data is read.

offset Number <optional>

the offset at which the encoded data starts. If absent a zero offset is inferred.

Overrides:
Source:
Returns:
  • the value of the decoded data.
Type
Number | Array | Object

encode(src, b, offsetopt) → {Number}

Encode a JavaScript value into a Buffer.

Parameters:
Name Type Attributes Description
src Number | Array | Object

the value to be encoded into the buffer. The type accepted depends on the (sub-)type of Layout.

b Buffer

the buffer into which encoded data will be written.

offset Number <optional>

the offset at which the encoded data starts. If absent a zero offset is inferred.

Overrides:
Source:
Returns:
  • the number of bytes encoded, including the space skipped for internal padding, but excluding data such as lengths when stored externally. This is the adjustment to offset producing the offset where data for the next layout would be written.
Type
Number

fromArray()

Delegate fromArray to layout.

Overrides:
Source:

getSpan(b, offsetopt) → {Number}

Calculate the span of a specific instance of a layout.

Parameters:
Name Type Attributes Description
b Buffer

the buffer that contains an encoded instance.

offset Number <optional>

the offset at which the encoded instance starts. If absent a zero offset is inferred.

Overrides:
Source:
Throws:
  • if the length of the value cannot be determined.
Type
RangeError
Returns:
  • the number of bytes covered by the layout instance. If this method is not overridden in a subclass the definition-time constant span will be returned.
Type
Number

makeDestinationObject()

Function to create an Object into which decoded properties will be written.

Used only for layouts that decode to Object instances, which means:

If left undefined the JavaScript representation of these layouts will be Object instances.

See bindConstructorLayout.

Inherited From:
Source:

replicate(property) → {module:Layout~Layout}

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 property are strictly equal to the origin layout.

Parameters:
Name Type Description
property String

the value for property in the replica.

Inherited From:
Source:
Returns:
  • the copy with property set to property.
Type
module:Layout~Layout