Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ChoiceType<E, READ_E>

A type storing a value of one of several fixed types.

Example:

let hexType = new sb.StructType({
  hex: new sb.StringType
})
let rgbType = new sb.StructType({
  r: new sb.FloatType,
  g: new sb.FloatType,
  b: new sb.FloatType
})
let hueType = new sb.FloatType
let type = new sb.ChoiceType([
  hexType,
  rgbType,
  hueType
])

Type parameters

  • E

    The type of value this choice type can write. If you provide, e.g. a Type<A> and a Type<B> and a Type<C> to the constructor, E should be A | B | C. In TypeScript, you have to declare this manually unless all the value types are identical.

  • READ_E: E = E

    The type of values this type will read

Hierarchy

  • AbsoluteType<E, READ_E>
    • ChoiceType

Index

Constructors

constructor

  • new ChoiceType<E, READ_E>(types: Type<E, READ_E>[]): ChoiceType<E, READ_E>
  • Type parameters

    • E

    • READ_E = E

    Parameters

    • types: Type<E, READ_E>[]

      The list of possible types. Values will be written using the first type in the list that successfully writes the value, so place higher priority types earlier.

    Returns ChoiceType<E, READ_E>

Properties

Readonly types

types: Type<E, READ_E>[]

Accessors

Static _value

  • get _value(): number

Methods

addToBuffer

consumeValue

  • consumeValue(buffer: ArrayBuffer, offset: number): ReadResult<READ_E>
  • Parameters

    • buffer: ArrayBuffer
    • offset: number

    Returns ReadResult<READ_E>

equals

  • equals(otherType: unknown): boolean
  • Parameters

    • otherType: unknown

    Returns boolean

getHash

  • getHash(): string

getSignature

  • getSignature(): string

Private isBuffer

Private isSameType

  • isSameType(otherType: unknown): otherType is ChoiceType<E, READ_E>
  • Determines whether the input is a Type with the same class

    Parameters

    • otherType: unknown

      A value, usually a Type instance

    Returns otherType is ChoiceType<E, READ_E>

    whether this and otherType are instances of the same Type class

readValue

  • readValue(valueBuffer: ArrayBuffer | Uint8Array, offset?: number): READ_E
  • Parameters

    • valueBuffer: ArrayBuffer | Uint8Array
    • offset: number = 0

    Returns READ_E

toBuffer

  • toBuffer(): ArrayBuffer

valueBuffer

  • valueBuffer(value: E): ArrayBuffer
  • Parameters

    • value: E

    Returns ArrayBuffer

writeValue

  • Appends value bytes to an AppendableBuffer according to the type

    Examples:

    type.writeValue(buffer, {hex: '#abcdef'}) //writes using hexType
    

    or

    type.writeValue(buffer, {r: 1, g: 0, b: 0.5}) //writes using rgbType
    

    or

    type.writeValue(buffer, 180) //writes using hueType
    
    throws

    If the value doesn't match the type, e.g. new sb.StringType().writeValue(buffer, 23)

    Parameters

    • buffer: AppendableBuffer

      The buffer to which to append

    • value: E

      The value to write

    Returns void

Generated using TypeDoc