# 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
# File lib/ffi/struct_layout_builder.rb, line 76 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
# File lib/ffi/struct_layout_builder.rb, line 101 def add_array(name, type, count, offset = nil) add(name, Type::Array.new(type, count), offset) end
# File lib/ffi/struct_layout_builder.rb, line 93 def add_field(name, type, offset = nil) add(name, type, offset) end
# File lib/ffi/struct_layout_builder.rb, line 97 def add_struct(name, type, offset = nil) add(name, Type::Struct.new(type), offset) end
# File lib/ffi/struct_layout_builder.rb, line 38 def alignment=(align) @alignment = align if align > @alignment @min_alignment = align end
# File lib/ffi/struct_layout_builder.rb, line 105 def build # Add tail padding if the struct is not packed size = @packed ? @size : align(@size, @alignment) StructLayout.new(@fields, size, @alignment) end
# 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
# File lib/ffi/struct_layout_builder.rb, line 34 def size=(size) @size = size if size > @size end
# File lib/ffi/struct_layout_builder.rb, line 43 def union=(is_union) @union = is_union end
# File lib/ffi/struct_layout_builder.rb, line 47 def union? @union end