Class: SDL2::Struct

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

Overview

FFI::Struct class with some useful additions.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StructHelper

member_readers, member_writers

Constructor Details

#initialize(*args, &block) ⇒ Struct

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



56
57
58
59
60
61
62
63
# File 'lib/sdl2.rb', line 56

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

.cast(something) ⇒ Object

Default cast handler



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sdl2.rb', line 131

def self.cast(something)
  if something.kind_of? self
    return something
  elsif something.kind_of? Hash
    common = (self.members & something.keys)
    if common.empty?
      raise "#{self} can't cast this Hash: #{something.inspect}"
    else
      tmp = self.new
      common.each do |field|
        tmp[field] = something[field]
      end
      return tmp
    end        
  elsif something.nil?
    return something #TODO: Assume NUL is ok?
  else
    raise "#{self} can't cast #{something.insepct}"
  end
end

.release(pointer) ⇒ Object

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



66
67
68
# File 'lib/sdl2.rb', line 66

def self.release(pointer)
  pointer.free
end

Instance Method Details

#==(other) ⇒ Object

Compare two structures by class and values.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sdl2.rb', line 85

def ==(other)
  if PrintDebug
    @@rec ||= -1
    @@rec += 1
    pad = "\t"*@@rec

    puts
    puts " #{pad}COMPARING #{self} to #{other}"
  end

  result = catch(:result) do
    unless self.class == other.class
      puts "Class Mismatch" if PrintDebug
      throw :result, false
    end

    if self.null? or other.null?
      unless self.null? and other.null?
        puts "AHHHAOne is null and the other is not" if PrintDebug
        throw :result, false
      end
    else
      self.class.members.each do |field|
        print "#{pad} #{field}:#{self[field].class} = " if PrintDebug

        unless self[field] == other[field]
          binding.pry
          puts "NO MATCH: #{self[field].to_s} #{other[field].to_s}" if PrintDebug
          throw :result, false
        end
        puts "MATCH" if PrintDebug
      end
    end

    true # Everything passed.

  end
  if PrintDebug
    @@rec += -1
    puts
    puts "#{pad}RESULT = #{result}"
  end
  return result
end