Class: SDL2::StructArray

Inherits:
Object
  • Object
show all
Defined in:
lib/struct_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct_class, count) ⇒ StructArray

Returns a new instance of StructArray.



13
14
15
16
17
18
19
# File 'lib/struct_array.rb', line 13

def initialize(struct_class, count)
  @count = count
  @struct_class = struct_class
  # TODO: Make sure this will free the memory when this 
  # struct array is garbage collected.
  @pointer = FFI::MemoryPointer.new(struct_class, @count)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



21
22
23
# File 'lib/struct_array.rb', line 21

def count
  @count
end

#pointerObject (readonly)

Returns the value of attribute pointer.



21
22
23
# File 'lib/struct_array.rb', line 21

def pointer
  @pointer
end

Class Method Details

.clone_from(array, struct_class) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/struct_array.rb', line 4

def self.clone_from(array, struct_class)
  cloned_array = self.new(struct_class, array.count)
  array.each_with_index do |item, idx|
    cloned_array[idx].update_members(item)
  end
  cloned_array
end

Instance Method Details

#[](idx) ⇒ Object



23
24
25
26
# File 'lib/struct_array.rb', line 23

def [](idx)
  raise "Invalid index #{idx}, count is #{@count}" unless (0 <= idx) and (idx < @count)
  @struct_class.new(pointer + (idx*@struct_class.size))
end

#first(count = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/struct_array.rb', line 28

def first(count = nil)
  if count.nil?
    self[0]
  else
    count.times.map do |idx|
      self[idx]
    end
  end        
end

#lastObject



38
39
40
# File 'lib/struct_array.rb', line 38

def last
  self[@count-1]
end