Class: Rstruct::ArrayType

Inherits:
ContainerType show all
Defined in:
lib/rstruct/base_types/array_type.rb

Instance Attribute Summary collapse

Attributes inherited from Type

#name, #params

Instance Method Summary collapse

Methods inherited from ContainerType

#claim_value, #field_names, #field_types, #format, #groupable?, #read, #sizeof

Methods included from Packable

#pack_value, #read

Methods inherited from Type

#claim_value, #container?, #groupable?, #register, #sizeof

Constructor Details

#initialize(name, typ, count, opts = {}, &block) ⇒ ArrayType

Returns a new instance of ArrayType.



13
14
15
16
17
18
19
20
21
# File 'lib/rstruct/base_types/array_type.rb', line 13

def initialize(name, typ, count, opts={}, &block)
  opts = opts.dup
  lkupreg = (opts.delete(:fields_from) || opts.delete(:register) || Rstruct.default_registry)
  super(name, opts.merge(:register => false), &block)
  @count = count
  unless @rtype = lkupreg[typ]
    raise(InvalidTypeError, "invalid array type #{typ.inspect}")
  end
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



11
12
13
# File 'lib/rstruct/base_types/array_type.rb', line 11

def count
  @count
end

Instance Method Details

#fieldsObject



27
28
29
# File 'lib/rstruct/base_types/array_type.rb', line 27

def fields
  @fields ||= Array.new(self.count){|i| Field.new(i, @rtype, nil, nil) }.freeze
end

#instance(*values) ⇒ Object



31
32
33
34
35
36
# File 'lib/rstruct/base_types/array_type.rb', line 31

def instance(*values)
  vals = values.flatten
  ary = ArrayContainer.new(self.count){|f| vals.shift }
  ary.rstruct_type = self
  return ary
end

#offset_of(idx) ⇒ Object



38
39
40
# File 'lib/rstruct/base_types/array_type.rb', line 38

def offset_of(idx)
  rtype.sizeof * idx
end

#typObject



23
24
25
# File 'lib/rstruct/base_types/array_type.rb', line 23

def typ
  self
end