Constructor
new BitStructure(word, msbopt, propertyopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
word |
module:Layout~Layout | initializer for word. The parameter must be an instance of UInt (or UIntBE) that is no more than 4 bytes wide. |
|
msb |
bool |
<optional> |
|
property |
string |
<optional> |
initializer for property. |
Extends
Members
fields
The sequence of BitField layouts that comprise the packed structure.
NOTE The array remains mutable to allow fields to be added after construction. Users should not manipulate the content of this property.
msb
Whether the bit sequences are packed starting at the most
significant bit growing down (true
), or the least significant
bit growing up (false
).
NOTE Regardless of this value, the least significant bit of any BitField value is the least significant bit of the corresponding section of the packed value.
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:
word
Methods
addBoolean(property) → {module:Layout~Boolean}
As with addField for single-bit
fields with boolean
value representation.
Parameters:
Name | Type | Description |
---|---|---|
property |
string | initializer for property. |
Returns:
addField(bits, property) → {module:Layout~BitField}
Register a new bitfield with a containing bit structure. The resulting bitfield is returned.
Parameters:
Name | Type | Description |
---|---|---|
bits |
Number | initializer for bits. |
property |
string | initializer for property. |
Returns:
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()
Implement encode for BitStructure.
If src
is missing a property for a member with a defined property the corresponding region of the packed
value is left unmodified. Unused bits are also left unmodified.
- Overrides:
- Source:
fieldFor(property) → {module:Layout~BitField}
Get access to the bit field for a given property.
Parameters:
Name | Type | Description |
---|---|---|
property |
String | the bit field of interest. |
Returns:
- the field associated with
property
, or undefined if there is no such property.
fromArray(values) → {Object|undefined}
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.
Parameters:
Name | Type | Description |
---|---|---|
values |
Array | an array of values that correspond to the default order for properties. As with 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. |
- Inherited From:
- Source:
Returns:
- Type
- Object | undefined
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. |
- Inherited From:
- 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.
- 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