Class: Nl::DataTypes::PackedArray

Inherits:
Base
  • Object
show all
Defined in:
nl/lib/nl/datatypes.rb

Overview

An array whose elements are packed consecutively without per-element headers or alignment padding.

Instance Method Summary collapse

Methods inherited from Base

#nlattr_type_flags

Constructor Details

#initialize(sub_type, check: nil) ⇒ PackedArray

Returns a new instance of PackedArray.



120
121
122
123
# File 'nl/lib/nl/datatypes.rb', line 120

def initialize(sub_type, check: nil)
  super(check:)
  @sub_type = sub_type
end

Instance Method Details

#coerce(values, context: nil) ⇒ Object



136
137
138
# File 'nl/lib/nl/datatypes.rb', line 136

def coerce(values, context: nil)
  values.map { @sub_type.coerce(it, context:) }
end

#decode(decoder, context: nil, nlattr_type_flags: 0) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'nl/lib/nl/datatypes.rb', line 140

def decode(decoder, context: nil, nlattr_type_flags: 0)
  if @check
    payload = checked(decoder.get_string)
    decoder = Decoder.new(IO::Buffer.for(payload))
  end

  result = []
  result << @sub_type.decode(decoder, context:, nlattr_type_flags: 0) while decoder.available?
  result
end

#encode(encoder, values, context: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'nl/lib/nl/datatypes.rb', line 125

def encode(encoder, values, context: nil)
  values = coerce(values, context:)
  if @check
    temporary = Encoder.new
    values.each { @sub_type.encode(temporary, it, context:) }
    encoder.put_string(checked(temporary.buffer.get_string))
  else
    values.each { @sub_type.encode(encoder, it, context:) }
  end
end