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 pointer to a C union

Constant Summary

PackInfo - Included

ALIGN_MAP, PACK_MAP, SIZE_MAP

Class Method Summary

  • .size(types)

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

CStructEntity - Inherited

.alignment,
.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.

#freed?

Returns if the free function for this pointer has been called.

#null?

Returns true if this is a null pointer.

#size

Get the size of this pointer.

#size=

Set the size of this pointer to .size

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 if only one argument is specified.

#[]=

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.

#to_s

ValueUtil - Included

PackInfo - Included

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.

#call_free

Call the free function for this pointer.

#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

.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 450

def CUnionEntity.size(types)
  types.map { |type, count = 1|
    if type.respond_to?(:entity_class)
      type.size * count
    else
      PackInfo::SIZE_MAP[type] * count
    end
  }.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 462

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