class FFI::StructLayoutBuilder

Build a {StructLayout struct layout}.

Constants

NUMBER_TYPES

List of number types

Attributes

Public Class Methods

# File lib/ffi/struct_layout_builder.rb, line 39
def initialize
  @size = 0
  @alignment = 1
  @min_alignment = 1
  @packed = false
  @union = false
  @fields = Array.new
end

Public Instance Methods

@param [String, Symbol] name name of the field @param [Array, DataConverter, Struct, StructLayout::Field, Symbol, Type] type type of the field @param [Integer, nil] offset @return [self] Add a field to the builder. @note Setting offset to nil or -1 is equivalent to 0.

# File lib/ffi/struct_layout_builder.rb, line 123
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

@param name (see add) @param type (see add) @param [Integer] count array length @param offset (see add) @return (see add) Add an array as a field to the builder.

# File lib/ffi/struct_layout_builder.rb, line 161
def add_array(name, type, count, offset = nil)
  add(name, Type::Array.new(type, count), offset)
end

@param (see add) @return (see add) Same as {#add}. @see add

# File lib/ffi/struct_layout_builder.rb, line 144
def add_field(name, type, offset = nil)
  add(name, type, offset)
end

@param (see add) @return (see add) Add a struct as a field to the builder.

# File lib/ffi/struct_layout_builder.rb, line 151
def add_struct(name, type, offset = nil)
  add(name, Type::Struct.new(type), offset)
end

Set alignment attribute with align only if it is greater than attribute value. @param [Integer] align

# File lib/ffi/struct_layout_builder.rb, line 56
def alignment=(align)
  @alignment = align if align > @alignment
  @min_alignment = align
end

@return [StructLayout] Build and return the struct layout.

# File lib/ffi/struct_layout_builder.rb, line 167
def build
  # Add tail padding if the struct is not packed
  size = @packed ? @size : align(@size, @alignment)

  layout = StructLayout.new(@fields, size, @alignment)
  layout.__union! if @union
  layout
end

Set packed attribute @overload packed=(packed) Set alignment and packed attributes to

+packed+.

@param [Integer] packed

@return [packed]

@overload packed=(packed) Set packed attribute.

@param packed

@return [0,1]
# File lib/ffi/struct_layout_builder.rb, line 89
def packed=(packed)
  if packed.is_a?(0.class)
    @alignment = packed
    @packed = packed
  else
    @packed = packed ? 1 : 0
  end
end

Set size attribute with size only if size is greater than attribute value. @param [Integer] size

# File lib/ffi/struct_layout_builder.rb, line 50
def size=(size)
  @size = size if size > @size
end

Set union attribute. Set to true to build a {Union} instead of a {Struct}. @param [Boolean] is_union @return [is_union]

# File lib/ffi/struct_layout_builder.rb, line 65
def union=(is_union)
  @union = is_union
end

Building a {Union} or a {Struct} ?

@return [Boolean]

# File lib/ffi/struct_layout_builder.rb, line 73
def union?
  @union
end