Class: DL::Importable::Internal::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/dl/struct.rb

Direct Known Subclasses

Union

Instance Method Summary collapse

Constructor Details

#initialize(types, contents) ⇒ Struct

Returns a new instance of Struct.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dl/struct.rb', line 65

def initialize(types, contents)
  @names = []
  @ty   = {}
  @len  = {}
  @enc  = {}
  @dec  = {}
  @size = 0
  @tys  = ""
  @types = types
  parse(contents)
end

Instance Method Details

#malloc(size = nil) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/dl/struct.rb', line 92

def malloc(size = nil)
  if( !size )
    size = @size
  end
  ptr = DL::malloc(size)
  return new(ptr)
end

#membersObject



81
82
83
# File 'lib/dl/struct.rb', line 81

def members
  return @names
end

#new(ptr) ⇒ Object

ptr must be a PtrData object.



86
87
88
89
90
# File 'lib/dl/struct.rb', line 86

def new(ptr)
  ptr.struct!(@tys, *@names)
  mem = Memory.new(ptr, @names, @ty, @len, @enc, @dec)
  return mem
end

#parse(contents) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dl/struct.rb', line 100

def parse(contents)
  contents.each{|elem|
    name,ty,num,enc,dec = parse_elem(elem)
    @names.push(name)
    @ty[name]  = ty
    @len[name] = num
    @enc[name] = enc
    @dec[name] = dec
    if( num )
      @tys += "#{ty}#{num}"
    else
      @tys += ty
    end
  }
  @size = DL.sizeof(@tys)
end

#parse_elem(elem) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dl/struct.rb', line 117

def parse_elem(elem)
  elem.strip!
  case elem
  when /^([\w\d_\*]+)([\*\s]+)([\w\d_]+)$/
    ty = ($1 + $2).strip
    name = $3
    num = nil;
  when /^([\w\d_\*]+)([\*\s]+)([\w\d_]+)\[(\d+)\]$/
    ty = ($1 + $2).strip
    name = $3
    num = $4.to_i
  else
    raise(RuntimeError, "invalid element: #{elem}")
  end
  ty,enc,dec = @types.encode_struct_type(ty)
         if( !ty )
           raise(TypeError, "unsupported type: #{ty}")
         end
  return [name,ty,num,enc,dec]
end

#sizeObject



77
78
79
# File 'lib/dl/struct.rb', line 77

def size
  return @size
end