Module: FFI::Utilities::StructExtensions

Included in:
ManagedStruct, Struct
Defined in:
lib/FFI/utilities/struct_extensions.rb

Defined Under Namespace

Modules: ClassMethods Classes: NoLayout, NotALayoutMember

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/FFI/utilities/struct_extensions.rb', line 9

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#read_attribute(attr) ⇒ Object



97
98
99
# File 'lib/FFI/utilities/struct_extensions.rb', line 97

def read_attribute(attr)
  self[attr]
end

#read_char_attribute(attr) ⇒ Object



119
120
121
# File 'lib/FFI/utilities/struct_extensions.rb', line 119

def read_char_attribute(attr)
  self[attr].chr
end

#write_attribute(attr, val) ⇒ Object

write_attribute(attr, val)

writes val into property attr. Care is taken to manage strings properly, even tough there seems to be a general suggestion that assigning strings is a Bad Thing (tm)



108
109
110
111
112
113
114
115
116
117
# File 'lib/FFI/utilities/struct_extensions.rb', line 108

def write_attribute(attr, val)
  if self.class.layout[attr].type == FFI::Type::Builtin::STRING
    pos = offset_of(attr)
    sp = val.nil? ? FFI::MemoryPointer::NULL : FFI::MemoryPointer.from_string(val)
    self.pointer.put_pointer(pos, sp)
  else
    self[attr] = val
  end
  self[attr]
end

#write_char_attribute(attr, val) ⇒ Object



123
124
125
126
# File 'lib/FFI/utilities/struct_extensions.rb', line 123

def write_char_attribute(attr, val)
  self[attr] = val.each_byte.map { |b| b }.first.to_i
  read_char_attribute(attr)
end