Class: Fiddle::StructArray

Inherits:
Array
  • Object
show all
Includes:
ValueUtil
Defined in:
lib/fiddle/struct.rb

Overview

Wrapper for arrays within a struct

Instance Method Summary collapse

Methods included from ValueUtil

#signed_value, #unsigned_value, #wrap_arg, #wrap_args

Constructor Details

#initialize(ptr, type, initial_values) ⇒ StructArray

Returns a new instance of StructArray.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fiddle/struct.rb', line 84

def initialize(ptr, type, initial_values)
  @ptr = ptr
  @type = type
  @is_struct = @type.respond_to?(:entity_class)
  if @is_struct
    super(initial_values)
  else
    @size = Fiddle::PackInfo::SIZE_MAP[type]
    @pack_format = Fiddle::PackInfo::PACK_MAP[type]
    super(initial_values.collect { |v| unsigned_value(v, type) })
  end
end

Instance Method Details

#[]=(index, value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fiddle/struct.rb', line 101

def []=(index, value)
  if index < 0 || index >= size
    raise IndexError, 'index %d outside of array bounds 0...%d' % [index, size]
  end

  if @is_struct
    self[index].replace(value)
  else
    to_ptr[index * @size, @size] = [value].pack(@pack_format)
    super(index, value)
  end
end

#to_ptrObject



97
98
99
# File 'lib/fiddle/struct.rb', line 97

def to_ptr
  @ptr
end