Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more

In Files

  • dl/lib/dl/struct.rb

Included Modules

DL::CUnionEntity

A C union wrapper

Public Class Methods

malloc(types, func=nil) click to toggle source

Allocates a C union the types provided. The C function func is called when the instance is garbage collected.

 
               # File dl/lib/dl/struct.rb, line 212
def CUnionEntity.malloc(types, func=nil)
  addr = DL.malloc(CUnionEntity.size(types))
  CUnionEntity.new(addr, types, func)
end
            
size(types) click to toggle source

Given types, returns the size needed for the union.

DL::CUnionEntity.size([DL::TYPE_DOUBLE, DL::TYPE_INT, DL::TYPE_CHAR,
                       DL::TYPE_VOIDP])
=> 8
 
               # File dl/lib/dl/struct.rb, line 222
def CUnionEntity.size(types)
  types.map { |type, count = 1|
    PackInfo::SIZE_MAP[type] * count
  }.max
end
            

Public Instance Methods

set_ctypes(types) click to toggle source

Given types, calculate the necessary offset and for each union member

 
               # File dl/lib/dl/struct.rb, line 229
def set_ctypes(types)
  @ctypes = types
  @offset = Array.new(types.length, 0)
  @size   = self.class.size types
end