Class: GVariantArrayType

Inherits:
GVariantOffsetType show all
Defined in:
lib/gvariant.rb

Instance Attribute Summary

Attributes inherited from GVariantBasicType

#alignment, #default_value, #fixed_size, #id

Instance Method Summary collapse

Methods inherited from GVariantOffsetType

#read_offset

Methods inherited from GVariantBasicType

#align

Constructor Details

#initialize(id, array_element) ⇒ GVariantArrayType

Returns a new instance of GVariantArrayType.



183
184
185
186
# File 'lib/gvariant.rb', line 183

def initialize(id, array_element)
  super(id, array_element.alignment, [])
  @element = array_element
end

Instance Method Details

#read(buf) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/gvariant.rb', line 188

def read(buf)
  size = buf.length

  if size == 0
    return @element.is_a?(GVariantDictionaryType) ? {} : @default_value
  end

  values = []
  c = 0

  if (@element.fixed_size)
    return [] if (size % @element.fixed_size != 0)
    n = size / @element.fixed_size

    n.times do
      values << @element.read(buf[c, @element.fixed_size])
      c += @element.fixed_size
    end
  else
    n = (size - read_offset(buf, -1)) / @offset_size

    n.times do |i|
      o = read_offset(buf, -n + i)
      values << @element.read(buf[c..o - 1])
      c = @element.align(o)
    end
  end

  values
end