Class: Rstruct::ArrayType
- Inherits:
-
ContainerType
- Object
- Type
- ContainerType
- Rstruct::ArrayType
- Defined in:
- lib/rstruct/base_types/array_type.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Attributes inherited from Type
Instance Method Summary collapse
- #fields ⇒ Object
-
#initialize(name, typ, count, opts = {}, &block) ⇒ ArrayType
constructor
A new instance of ArrayType.
- #instance(*values) ⇒ Object
- #offset_of(idx) ⇒ Object
- #typ ⇒ Object
Methods inherited from ContainerType
#claim_value, #field_names, #field_types, #format, #groupable?, #read, #sizeof
Methods included from Packable
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
#count ⇒ Object (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
#fields ⇒ Object
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 |
#typ ⇒ Object
23 24 25 |
# File 'lib/rstruct/base_types/array_type.rb', line 23 def typ self end |