Class | DL::Importable::Internal::Struct
In: | dl/lib/dl/struct.rb
Parent: | Object

### Methods

malloc members new new parse parse_elem size

### Public Class methods

new(types, contents)

[Source]

    # File dl/lib/dl/struct.rb, line 65
            def initialize(types, contents)
              @names = []
              @ty   = {}
              @len  = {}
              @enc  = {}
              @dec  = {}
              @size = 0
              @tys  = ""
              @types = types
              parse(contents)
            end

### Public Instance methods

malloc(size = nil)

[Source]

    # File dl/lib/dl/struct.rb, line 92
            def malloc(size = nil)
              if( !size )
                size = @size
              end
              ptr = DL::malloc(size)
              return new(ptr)
            end

members()

[Source]

    # File dl/lib/dl/struct.rb, line 81
            def members
              return @names
            end

new(ptr)

ptr must be a PtrData object.

[Source]

    # File dl/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)

[Source]

    # File dl/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)

[Source]

    # File dl/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

size()

[Source]

    # File dl/lib/dl/struct.rb, line 77
            def size
              return @size
            end

[Validate]