Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • dl/lib/dl/struct.rb

DL::Importable::Internal::Struct

Public Class Methods

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

Public Instance Methods

malloc(size = nil) click to toggle source
 
               # File dl/lib/dl/struct.rb, line 91
def malloc(size = nil)
  if( !size )
    size = @size
  end
  ptr = DL::malloc(size)
  return new(ptr)
end
            
members() click to toggle source
 
               # File dl/lib/dl/struct.rb, line 80
def members
  return @names
end
            
new(ptr) click to toggle source

ptr must be a PtrData object.

 
               # File dl/lib/dl/struct.rb, line 85
def new(ptr)
  ptr.struct!(@tys, *@names)
  mem = Memory.new(ptr, @names, @ty, @len, @enc, @dec)
  return mem
end
            
parse(contents) click to toggle source
 
               # File dl/lib/dl/struct.rb, line 99
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) click to toggle source
 
               # File dl/lib/dl/struct.rb, line 116
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() click to toggle source
 
               # File dl/lib/dl/struct.rb, line 76
def size
  return @size
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus