123456789_123456789_123456789_123456789_123456789_

Class: Fiddle::CUnionEntity

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, CStructEntity, Pointer, FFI::DataConverter
Instance Chain:
Inherits: Fiddle::CStructEntity
Defined in: 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

Instance Attribute Summary

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 integer stored at index.

#[]=, #call_free, #eql?,
#initialize

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

#inspect, #ptr, #ref, #to_i,
#to_int

Alias for Pointer#to_i.

#to_ptr,
#to_s

without 0.

#to_str, #to_value

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 'lib/fiddle/struct.rb', line 542

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 'lib/fiddle/struct.rb', line 554

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