### Home Classes Methods

### In Files

- lib/ffi/struct_layout_builder.rb

### Parent

Object

### Methods

- ::new
- #add
- #add_array
- #add_field
- #add_struct
- #alignment=
- #build
- #packed=
- #size=
- #union=
- #union?

### Files

- Makefile
- LICENSE
- compile
- configure
- depcomp
- stamp-vti
- install-sh
- libtool-version
- mdate-sh
- missing

### Class/Module Index

Quicksearch

- FFI
- FFI::ArrayType
- FFI::AutoPointer
- FFI::AutoPointer::CallableReleaser
- FFI::AutoPointer::DefaultReleaser
- FFI::AutoPointer::Releaser
- FFI::ConstGenerator
- FFI::ConstGenerator::Constant
- FFI::DynamicLibrary
- FFI::DynamicLibrary::Symbol
- FFI::Enum
- FFI::Enums
- FFI::Function
- FFI::Generator
- FFI::Generator::Task
- FFI::IO
- FFI::ManagedStruct
- FFI::Platform
- FFI::PlatformError
- FFI::Pointer
- FFI::StrPtrConverter
- FFI::Struct
- FFI::Struct::InlineArray
- FFI::Struct::ManagedStructConverter
- FFI::StructByReference
- FFI::StructByValue
- FFI::StructGenerator
- FFI::StructGenerator::Field
- FFI::StructLayout
- FFI::StructLayout::Enum
- FFI::StructLayout::InnerStruct
- FFI::StructLayout::Mapped
- FFI::StructLayoutBuilder
- FFI::TypesGenerator
- FFI::Union
- FFI::VariadicInvoker

No matching classes.

# FFI::StructLayoutBuilder

Build a {StructLayout struct layout}.

### Constants

NUMBER_TYPES

  List of number types

### Attributes

alignment[R]
size[R]

### Public Class Methods

new() click to toggle source

    # 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

add(name, type, offset = nil) click to toggle source

@param [String, Symbol] name name of the field @param [Array, DataConverter, Struct, StructLayout::Field, Symbol, Type] type type of the field @param [Numeric, 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 118
    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

@param name (see add) @param type (see add) @param [Numeric] 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 156
    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

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

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

add_struct(name, type, offset = nil) click to toggle source

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

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

alignment=(align) click to toggle source

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

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

build() click to toggle source

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

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

packed=(packed) click to toggle source

Set packed attribute @overload packed=(packed)

    @param [Fixnum] packed
    @return [packed]
    Set alignment and packed attributes to +packed+.

@overload packed=(packed)

    @param packed
    @return [0,1]
    Set packed attribute.

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

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

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

union=(is_union) click to toggle source

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

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

union?() click to toggle source

@return [Boolean] Building a {Union} or a {Struct} ?

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.