Function EnsureSize

  • @EnsureSize decorator force the decorated property to meet a size constraint. If that size is not met the cursor will be moved.

    Type Parameters

    • This extends object

      The type of the class the decorator is applied to.

    • Args extends string

    Parameters

    • Optionalsize: number | StringFormattedRecursiveKeyOf<This, Args> | ((instance: This, cursor: Cursor) => number)

      The size to move the cursor to before reading or writing the decorated property. It can be:

      • A static number, indicating a fixed offset.
      • A string that refer to a property of the current instance.
      • A function that computes the offset dynamically based on the current instance and cursor.
    • Optionalopt: Partial<PrePostOptions>

      Optional configution.

    Returns ClassAndPropertyDecoratorType<This>

    The class or property decorator function.

    In the following example the structure of the bitfield is only known based on the value of the most significant bit. For this I use @Peek to check the content of the next value and then I properly read it in the correct form.

    @EnsureSize('_size')
    class Block {
    @NullTerminatedString()
    @Relation(PrimitiveSymbol.char)
    content: string

    constructor(public _size: number) {}
    }

    class Protocol {
    @Relation(PrimitiveSymbol.u16)
    block_size: number

    @Relation(PrimitiveSymbol.u32)
    block_count: number

    @Count('block_count')
    @Relation(Block, 'block_size')
    blocks: Block[]
    }

    If the size is known before parsing the file, use the alignment in the controller instead