Class: SDL2::Struct

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/sdl2.rb

Overview

FFI::Struct class with some useful additions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Struct

Allows creation and use within block, automatically freeing pointer after block.



104
105
106
107
108
109
110
111
# File 'lib/sdl2.rb', line 104

def initialize(*args, &block)
  super(*args)
  if block_given?
    throw 'Release must be defined to use block' unless self.class.respond_to?(:release)
    yield self
    self.class.release(self.pointer)
  end
end

Class Method Details

.release(pointer) ⇒ Object

A default release scheme is defined, but should be redefined where appropriate.



114
115
116
# File 'lib/sdl2.rb', line 114

def self.release(pointer)
  pointer.free
end

Instance Method Details

#==(other) ⇒ Object

Compare two structures by class and values.



128
129
130
131
132
133
134
# File 'lib/sdl2.rb', line 128

def ==(other)
  return false unless self.class == other.class
  self.class.members.each do |field|
    return false unless self[field] == other[field]
  end
  true # return true if we get this far.
end

#inspectObject

A human-readable representation of the struct and it’s values.



119
120
121
122
123
124
125
# File 'lib/sdl2.rb', line 119

def inspect
  report = "struct #{self.class.to_s}{"
  report += self.class.members.collect do |field|
    "#{field}->#{self[field].inspect}"
  end.join(' ')
  report += "}"
end