class Fiddle::CUnionEntity

Parent:
Fiddle::CStructEntity

A C union wrapper

Public Class Methods

malloc(types, func=nil) Show source
# File ext/fiddle/lib/fiddle/struct.rb, line 216
def CUnionEntity.malloc(types, func=nil)
  addr = Fiddle.malloc(CUnionEntity.size(types))
  CUnionEntity.new(addr, types, func)
end

Allocates a C union the types provided.

When the instance is garbage collected, the C function func is called.

size(types) Show source
# File ext/fiddle/lib/fiddle/struct.rb, line 228
def CUnionEntity.size(types)
  types.map { |type, count = 1|
    PackInfo::SIZE_MAP[type] * count
  }.max
end

Returns the size needed for the union with the given types.

Fiddle::CUnionEntity.size(
  [ Fiddle::TYPE_DOUBLE,
    Fiddle::TYPE_INT,
    Fiddle::TYPE_CHAR,
    Fiddle::TYPE_VOIDP ]) #=> 8

Public Instance Methods

set_ctypes(types) Show source
# File ext/fiddle/lib/fiddle/struct.rb, line 236
def set_ctypes(types)
  @ctypes = types
  @offset = Array.new(types.length, 0)
  @size   = self.class.size types
end

Calculate the necessary offset and for each union member with the given types

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.