Class: DL::CUnionEntity

Inherits:
CStructEntity show all
Includes:
PackInfo
Defined in:
lib/dl/struct.rb

Overview

A C union wrapper

Constant Summary

Constants included from PackInfo

PackInfo::ALIGN_MAP, PackInfo::PACK_MAP, PackInfo::SIZE_MAP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PackInfo

align

Methods inherited from CStructEntity

#[], #[]=, #assign_names, #initialize, #to_s

Methods included from ValueUtil

#signed_value, #unsigned_value, #wrap_arg, #wrap_args

Constructor Details

This class inherits a constructor from DL::CStructEntity

Class Method Details

.malloc(types, func = nil) ⇒ Object

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


212
213
214
215
# File '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) ⇒ Object

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

222
223
224
225
226
# File 'lib/dl/struct.rb', line 222

def CUnionEntity.size(types)
  types.map { |type, count = 1|
    PackInfo::SIZE_MAP[type] * count
  }.max
end

Instance Method Details

#set_ctypes(types) ⇒ Object

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


229
230
231
232
233
# File 'lib/dl/struct.rb', line 229

def set_ctypes(types)
  @ctypes = types
  @offset = Array.new(types.length, 0)
  @size   = self.class.size types
end