class FFI::StructLayoutBuilder

Constants

NUMBER_TYPES

Attributes

alignment[R]
size[R]

Public Instance Methods

add(name, type, offset = nil) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 77
def add(name, type, offset = nil)

  if offset.nil? || offset == -1
    offset = @union ? 0 : align(@size, @packed ? [ @packed, type.alignment ].min : [ @min_alignment, type.alignment ].max)
  end

  #
  # If a FFI::Type type was passed in as the field arg, try and convert to a StructLayout::Field instance
  #
  field = type.is_a?(StructLayout::Field) ? type : field_for_type(name, offset, type)
  @fields << field
  @alignment = [ @alignment, field.alignment ].max unless @packed
  @size = [ @size, field.size + (@union ? 0 : field.offset) ].max

  return self
end
add_array(name, type, count, offset = nil) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 102
def add_array(name, type, count, offset = nil)
  add(name, Type::Array.new(type, count), offset)
end
add_field(name, type, offset = nil) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 94
def add_field(name, type, offset = nil)
  add(name, type, offset)
end
add_struct(name, type, offset = nil) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 98
def add_struct(name, type, offset = nil)
  add(name, Type::Struct.new(type), offset)
end
alignment=(align) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 38
def alignment=(align)
  @alignment = align if align > @alignment
  @min_alignment = align
end
build() click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 106
def build
  # Add tail padding if the struct is not packed
  size = @packed ? @size : align(@size, @alignment)
  
  StructLayout.new(@fields, size, @alignment)
end
packed=(packed) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 51
def packed=(packed)
  if packed.is_a?(Fixnum)
    @alignment = packed
    @packed = packed
  else
    @packed = packed ? 1 : 0
  end
end
size=(size) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 34
def size=(size)
  @size = size if size > @size
end
union=(is_union) click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 43
def union=(is_union)
  @union = is_union
end
union?() click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 47
def union?
  @union
end

Public Class Methods

new() click to toggle source
# File lib/ffi/struct_layout_builder.rb, line 25
def initialize
  @size = 0
  @alignment = 1
  @min_alignment = 1
  @packed = false
  @union = false
  @fields = Array.new
end