Module: FFI::Utilities::StructExtensions::ClassMethods

Defined in:
lib/FFI/utilities/struct_extensions.rb

Instance Method Summary collapse

Instance Method Details

#attr_accessor(*m) ⇒ Object



57
58
59
60
# File 'lib/FFI/utilities/struct_extensions.rb', line 57

def attr_accessor(*m)
  attr_reader(*m)
  attr_writer(*m)
end

#attr_char_accessor(*m) ⇒ Object



72
73
74
75
# File 'lib/FFI/utilities/struct_extensions.rb', line 72

def attr_char_accessor(*m)
  attr_char_reader(*m)
  attr_char_writer(*m)
end

#attr_char_reader(*m) ⇒ Object



62
63
64
65
# File 'lib/FFI/utilities/struct_extensions.rb', line 62

def attr_char_reader(*m)
  method = 'read_char'
  m.flatten.each { |a| common_read_eval(a, method) }
end

#attr_char_writer(*m) ⇒ Object



67
68
69
70
# File 'lib/FFI/utilities/struct_extensions.rb', line 67

def attr_char_writer(*m)
  method = 'write_char'
  m.flatten.each { |a| common_write_eval(a, method) }
end

#attr_reader(*m) ⇒ Object

attr_reader, attr_writer, attr_accessor +attr_char_reader, attr_char_writer, attr_char_accessor

Two basic sets of read/write methods are created, because we have to differentiate whether we want to be able to convert to/from a single char (which ruby will treat as a String object C treats differently)



47
48
49
50
# File 'lib/FFI/utilities/struct_extensions.rb', line 47

def attr_reader(*m)
  method = 'read'
  m.flatten.each { |a| common_read_eval(a, method) }
end

#attr_writer(*m) ⇒ Object



52
53
54
55
# File 'lib/FFI/utilities/struct_extensions.rb', line 52

def attr_writer(*m)
  method = 'write'
  m.flatten.each { |a| common_write_eval(a, method) }
end

#cast_pointer(p) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
# File 'lib/FFI/utilities/struct_extensions.rb', line 33

def cast_pointer(p)
  raise ArgumentError, 'bad pointer' unless p.kind_of?(FFI::MemoryPointer)
  new(p)
end

#create(*args) {|this_self, args| ... } ⇒ Object

create(*args) [{ |this_object, args| ... }]

does all the necessary FFI memory housekeeping for data structures and then passes a pointer to self and its arguments to a block.

Also, this method may be overridden in subclasses and then called with super() if necessary.

Yields:

  • (this_self, args)


24
25
26
27
28
29
30
31
# File 'lib/FFI/utilities/struct_extensions.rb', line 24

def create(*args)
  mp = FFI::MemoryPointer.new(self.size, 1)
  p  = FFI::Pointer.new(mp)
  this_self = new(p)
  this_self.send(:struct_initialize, *args)
  yield(this_self, *args) if block_given?
  this_self
end