Class: FFIAdditions::Struct

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/ffi-additions/struct.rb

Class Method Summary collapse

Class Method Details

.layout(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ffi-additions/struct.rb', line 5

def self.layout(*args)
  args = args.first.map do |k, v|
    if v == :string
      v = :pointer
      define_method "#{k}=", ->(s){ self[k] = FFI::MemoryPointer.from_string(s) }
    end
    [k, v]
  end.to_h
  # puts args
  super(args)
  members.each do |name|
    unless method_defined?(name)
      define_method name, ->{ self[name] }
    end
    unless method_defined?("#{name}=")
      define_method "#{name}=", ->(v){ self[name] = v }
    end
  end
end