Module: FFI::BitField::Layout

Included in:
FFI::BitStruct, ManagedBitStruct
Defined in:
lib/ffi/bit_field/layout.rb

Overview

Layout provides the ‘bit_fields` method for registering field members.

Instance Method Summary collapse

Instance Method Details

#bit_fields(*layout_args) ⇒ Symbol Also known as: bit_field

Returns parent_name.

Parameters:

  • layout_args (Array)

Returns:

  • (Symbol)

    parent_name



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ffi/bit_field/layout.rb', line 9

def bit_fields(*layout_args)
  # The reason for using class instance variable here instead of class variable
  # is not because class instance variables are clean,
  # but because sub-class of FFI::Struct cannot be inherited again.
  @bit_field_hash_table = {} unless instance_variable_defined?(:@bit_field_hash_table)

  parent_name = layout_args.shift.to_sym
  member_names = []
  widths = []
  layout_args.each_slice(2) do |name, width|
    member_names << name.to_sym
    widths << width.to_i
  end
  starts = widths.inject([0]) do |result, width|
    result << (result.last + width)
  end
  member_names.zip(starts, widths).each do |name, start, width|
    @bit_field_hash_table[name] = [parent_name, start, width]
  end

  parent_name
end