Class: Rstruct::ContainerType

Inherits:
Type
  • Object
show all
Includes:
Packable
Defined in:
lib/rstruct/base_types/container_type.rb

Direct Known Subclasses

ArrayType, Structure

Instance Attribute Summary

Attributes inherited from Type

#name, #params

Instance Method Summary collapse

Methods included from Packable

#pack_value

Methods inherited from Type

#container?, #register

Constructor Details

#initialize(*args, &block) ⇒ ContainerType

Returns a new instance of ContainerType.



54
55
56
57
# File 'lib/rstruct/base_types/container_type.rb', line 54

def initialize(*args, &block)
  @countainer = true
  super(*args, &block)
end

Instance Method Details

#claim_value(vals, obj = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rstruct/base_types/container_type.rb', line 100

def claim_value(vals, obj=nil)
  if @claim_cb
    @claim_cb.call(vals, obj)
  else
    # create our struct container
    s = instance()

    # iterate through the fields assigning values in the
    # container and pass it along with values to each
    # field's claim_value method.
    self.fields.do {|f| s[f.name] = f.typ.claim_value(vals,s) }
    return s
  end
end

#field_namesObject



83
84
85
# File 'lib/rstruct/base_types/container_type.rb', line 83

def field_names
  self.fields.map{|f| f.name }
end

#field_typesObject



87
88
89
# File 'lib/rstruct/base_types/container_type.rb', line 87

def field_types 
  self.fields.map{|f| f.typ }
end

#formatObject



63
64
65
66
67
68
69
70
71
# File 'lib/rstruct/base_types/container_type.rb', line 63

def format
  self.fields.map do |f| 
    if f.groupable?
      f.format 
    else
      return nil
    end
  end.join
end

#groupable?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rstruct/base_types/container_type.rb', line 59

def groupable?
  self.fields.find {|f| not f.groupable? }.nil?
end

#read(raw, obj = nil) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/rstruct/base_types/container_type.rb', line 91

def read(raw, obj=nil)
  raw = StringIO.new(raw) if raw.is_a?(String)
  obj = self.instance()
  fields.each do |f|
    obj[f.name] = f.typ.read(raw, obj)
  end
  return obj
end

#sizeofObject



73
74
75
76
77
78
79
80
81
# File 'lib/rstruct/base_types/container_type.rb', line 73

def sizeof
  self.fields.inject(0) do |s,v| 
    if vs=v.typ.sizeof
      s+=vs
    else
      return nil
    end
  end
end