Module: Snow::CStruct::StructArrayBase

Includes:
Enumerable
Defined in:
lib/snow-data/c_struct/array_base.rb

Overview

Array base for struct type arrays. Provides fetch/store and allocators.

Fetch and store operations both depend on an internal cache of wrapper Memory objects that point to structs in the array.

Defined Under Namespace

Modules: Allocators

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lengthObject (readonly)

The length of the array.



33
34
35
# File 'lib/snow-data/c_struct/array_base.rb', line 33

def length
  @length
end

Class Method Details

.included(array_klass) ⇒ Object



24
25
26
# File 'lib/snow-data/c_struct/array_base.rb', line 24

def self.included(array_klass)
  array_klass.extend(Allocators)
end

Instance Method Details

#each(&block) ⇒ Object

:nodoc:



45
46
47
48
49
# File 'lib/snow-data/c_struct/array_base.rb', line 45

def each(&block) # :nodoc:
  return to_enum(:each) unless block_given?
  (0 ... self.length).each { |index| yield fetch(index) }
  self
end

#fetch(index) ⇒ Object Also known as: []

:nodoc:

Raises:

  • (RuntimeError)


70
71
72
73
74
75
# File 'lib/snow-data/c_struct/array_base.rb', line 70

def fetch(index) # :nodoc:
  raise RuntimeError, "Attempt to access deallocated array" if @length == 0
  raise RangeError, "Attempt to access out-of-bounds index in #{self.class}" if index < 0 || @length <= index
  __build_cache__ if ! @__cache__
  @__cache__[index]
end

#free!Object

:nodoc:



97
98
99
100
101
# File 'lib/snow-data/c_struct/array_base.rb', line 97

def free! # :nodoc:
  __free_cache__
  @length = 0
  super
end

#map(&block) ⇒ Object

:nodoc:



52
53
54
55
# File 'lib/snow-data/c_struct/array_base.rb', line 52

def map(&block) # :nodoc:
  return to_enum(:map) unless block_given?
  self.dup.map!(&block)
end

#map!(&block) ⇒ Object

:nodoc:



58
59
60
61
62
# File 'lib/snow-data/c_struct/array_base.rb', line 58

def map!(&block) # :nodoc:
  return to_enum(:map!) unless block_given?
  (0 ... self.length).each { |index| store(index, yield(fetch(index))) }
  self
end

#resize!(new_length) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
# File 'lib/snow-data/c_struct/array_base.rb', line 36

def resize!(new_length) # :nodoc:
  raise ArgumentError, "Length must be greater than zero" if new_length < 1
  realloc!(new_length * self.class::BASE::SIZE, self.class::BASE::ALIGNMENT)
  @length = new_length
  __free_cache__
  self
end

#store(index, data) ⇒ Object Also known as: []=

You can use this to assign any Data subclass to an array value, but keep in mind that the data assigned MUST – again, MUST – be at least as large as the array’s base struct type in bytes or the assigned data object MUST respond to a bytesize message to get its size in bytes.

Raises:

  • (RuntimeError)


86
87
88
89
90
91
92
# File 'lib/snow-data/c_struct/array_base.rb', line 86

def store(index, data) # :nodoc:
  raise RuntimeError, "Attempt to access deallocated array" if @length == 0
  raise TypeError, "Invalid value type, must be Data, but got #{data.class}" if ! data.kind_of?(Data)
  raise RangeError, "Attempt to access out-of-bounds index in #{self.class}" if index < 0 || @length <= index
  @__cache__[index].copy!(data)
  data
end

#to_aObject

:nodoc:



65
66
67
# File 'lib/snow-data/c_struct/array_base.rb', line 65

def to_a # :nodoc:
  (0 ... self.length).map { |index| fetch(index) }
end