module Rstruct::ContainerMixins

Public Instance Methods

rstruct_type() click to toggle source
# File lib/rstruct/base_types/container_type.rb, line 6
def rstruct_type
  @rstruct_type
end
rstruct_type=(val) click to toggle source
# File lib/rstruct/base_types/container_type.rb, line 10
def rstruct_type=(val)
  if @rstruct_type
    raise(ArgumentError, "Can't override the rstruct_type once it is set")
  else
    @rstruct_type = val
  end
end
write(dst=nil, pvals=nil) click to toggle source
# File lib/rstruct/base_types/container_type.rb, line 18
def write(dst=nil, pvals=nil)
  if dst.is_a?(String)
    l = dst.size
    dst = StringIO.new(dst)
    dst.pos = l
  elsif dst.nil?
    dst = StringIO.new
  end

  typ ||= self.rstruct_type


  vals = (pvals.respond_to?(:values) ? pvals.values : pvals)
  vals ||= self.values

  opos = dst.pos if dst.respond_to?(:pos)
  typ.fields.each_with_index do |f, i|
    fldval = vals[i]
    if fldval.respond_to?(:write)
      fldval.write(dst, fldval)
    else
      dst.write(f.typ.pack_value((fldval || 0), self))
    end
  end
  if dst.is_a?(StringIO) and pvals.nil?
    dst.pos = opos
    return(dst.read)
  elsif opos and dst.respond_to?(:pos)
    return dst.pos - opos
  end
end