123456789_123456789_123456789_123456789_123456789_

Class: Fiddle::CUnionEntity

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Fiddle::CStructEntity
Defined in: ext/fiddle/lib/fiddle/struct.rb

Overview

A C union wrapper

Class Method Summary

CStructEntity - Inherited

.malloc

Allocates a C struct with the types provided.

.new

Wraps the C pointer addr as a C struct with the given types.

.size

Returns the offset for the packed sizes for the given types.

Pointer - Inherited

.[]

Get the underlying pointer for ruby object val and return it as a Pointer object.

.new

Create a new pointer to address with an optional .size and freefunc.

.to_ptr

Alias for Pointer.[].

Instance Attribute Summary

Pointer - Inherited

#free

Get the free function for this pointer.

#free=

Set the free function for this pointer to function in the given Function.

#size

Get the size of this pointer.

#size=

Set the size of this pointer to .size

#null?

Returns true if this is a null pointer.

Instance Method Summary

  • #set_ctypes(types)

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

CStructEntity - Inherited

#[]

Fetch struct member name

#[]=

Set struct member name, to value val

#assign_names

Set the names of the members in this C struct.

#set_ctypes

Calculates the offsets and sizes for the given types in the struct.

Pointer - Inherited

#+

Returns a new pointer instance that has been advanced n bytes.

#+@

Returns a new Pointer instance that is a dereferenced pointer for this pointer.

#-

Returns a new pointer instance that has been moved back n bytes.

#-@

Returns a new Pointer instance that is a reference pointer for this pointer.

#<=>

Returns -1 if less than, 0 if equal to, 1 if greater than other.

#==

Returns true if other wraps the same pointer, otherwise returns false.

#[]

Returns integer stored at index.

#[]=

Set the value at index to int.

#eql?

Alias for Pointer#==.

#inspect

Returns a string formatted with an easily readable representation of the internal state of the pointer.

#ptr

Alias for Pointer#+@.

#ref

Alias for Pointer#-@.

#to_i

Returns the integer memory location of this pointer.

#to_int

Alias for Pointer#to_i.

#to_s

Returns the pointer contents as a string.

#to_str

Returns the pointer contents as a string.

#to_value

Cast this pointer to a ruby object.

Constructor Details

This class inherits a constructor from Fiddle::CStructEntity

Class Method Details

.malloc(types, func = nil)

Allocates a C union the types provided.

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

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 217

def CUnionEntity.malloc(types, func=nil)
  addr = Fiddle.malloc(CUnionEntity.size(types))
  CUnionEntity.new(addr, types, func)
end

.size(types)

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
[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 229

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

Instance Method Details

#set_ctypes(types)

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

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 237

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