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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sdl2.rb', line 150

def self.cast(something)
  
  if something.kind_of? self
  
    return something
  
  elsif something.kind_of? Hash

    return self.create(something)

  elsif something.nil?
  
    return something #TODO: Assume NUL is ok?
  
  else
    
    raise "#{self} can't cast #{something.insepct}"
  
  end
end

.create(values = {}) ⇒ Object

Placeholder for Structs that need to initialize values.



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

def self.create(values = {})
  created = self.new
  created.update_members(values)
  created
end

.release(pointer) ⇒ Object

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



73
74
75
# File 'lib/sdl2.rb', line 73

def self.release(pointer)
  pointer.free
end

Instance Method Details

#==(other) ⇒ Object

Compare two structures by class and values. This will return true if compared to a “partial hash”, if all the keys the hash defines equal



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sdl2.rb', line 98

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 or other.kind_of? Hash
      puts "Class Mismatch" if PrintDebug
      throw :result, false
    end

    if (other.kind_of? Hash) and (other.keys - members).any?
      puts "Extra Keys: #{other.keys-members}"
      thorw :result, false
    end

    if (other.respond_to?:null?) and (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
      fields = other.kind_of?(Hash) ? members & other.keys : members
      fields.each do |field|
        print "#{pad} #{field}:#{self[field].class} = " if PrintDebug

        unless self[field] == other[field]

          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

#freeObject



77
78
79
# File 'lib/sdl2.rb', line 77

def free()
  self.pointer.free
end

#update_members(values) ⇒ Object

Set members to values contained within hash.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sdl2.rb', line 172

def update_members(values)
  if values.kind_of? Array
    raise "#{self} has less fields then #{values.inspect}" if values.count > members.count
    members.first(values.count).each_with_index do |field, idx|
      self[field] = values[idx]
    end

  elsif values.kind_of? Hash
    common = (self.members & values.keys)
    common.each do |field|
      self[field] = values[field]
    end
  else
    raise "#{self}#update_members unable to update from #{values.inspect}"
  end
end