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, #write_offsets

Methods inherited from GVariantBasicType

#align, #pad

Constructor Details

#initialize(id, array_element) ⇒ GVariantArrayType

Returns a new instance of GVariantArrayType.



260
261
262
263
# File 'lib/gvariant.rb', line 260

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

Instance Method Details

#read(buf) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/gvariant.rb', line 265

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 @default_value 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

#write(vals) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/gvariant.rb', line 296

def write(vals)
  buf = ""
  offsets = []

  vals.each do |val|
    v = @element.write(val)
    @element.pad(buf)
    buf << v
    offsets << buf.length
  end

  write_offsets(buf, offsets) unless @element.fixed_size
  buf
end