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

    • size: NumberOrRecursiveKey<This, Args> | (instance: This, cursor: Cursor) => number

      The size of the decorated property or class.

      • A static number, indicating a fixed size.
      • A string that refer to a property of type number.
      • 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 Block is fixed based on a property known at runtime. The @EnsureSize decorator move the cursor to always move the cursor to the correct position after read or write.

    @EnsureSize('_size')
    class Block {
    @NullTerminatedString()
    content: string

    constructor(public _size: number) {}
    }

    class Protocol {
    @Uint16
    block_size: number

    @Uint32
    block_count: number

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