Class: GirFFI::SizedArray
- Inherits:
-
Object
- Object
- GirFFI::SizedArray
- Includes:
- Enumerable
- Defined in:
- lib/gir_ffi/sized_array.rb
Overview
Class representing an array with a determined size
Instance Attribute Summary collapse
-
#element_type ⇒ Object
readonly
Returns the value of attribute element_type.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
- .copy_from(element_type, size, enumerable) ⇒ Object
- .copy_value_to_pointer(value, pointer, offset = 0) ⇒ Object
- .from(element_type, size, item) ⇒ Object
- .get_value_from_pointer(pointer, offset) ⇒ Object
- .wrap(element_type, size, pointer) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #each ⇒ Object
- #index(idx) ⇒ Object
-
#initialize(element_type, size, pointer) ⇒ SizedArray
constructor
A new instance of SizedArray.
- #size_in_bytes ⇒ Object
- #to_ptr ⇒ Object
Constructor Details
#initialize(element_type, size, pointer) ⇒ SizedArray
Returns a new instance of SizedArray.
11 12 13 14 15 |
# File 'lib/gir_ffi/sized_array.rb', line 11 def initialize(element_type, size, pointer) @element_type = element_type @size = size @pointer = pointer end |
Instance Attribute Details
#element_type ⇒ Object (readonly)
Returns the value of attribute element_type.
9 10 11 |
# File 'lib/gir_ffi/sized_array.rb', line 9 def element_type @element_type end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
9 10 11 |
# File 'lib/gir_ffi/sized_array.rb', line 9 def size @size end |
Class Method Details
.copy_from(element_type, size, enumerable) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gir_ffi/sized_array.rb', line 77 def copy_from(element_type, size, enumerable) return unless enumerable arr = enumerable.to_a case element_type when Array _main_type, sub_type = *element_type arr = arr.map { |it| sub_type.copy_from it } end from_enumerable element_type, size, arr end |
.copy_value_to_pointer(value, pointer, offset = 0) ⇒ Object
44 45 46 47 |
# File 'lib/gir_ffi/sized_array.rb', line 44 def self.copy_value_to_pointer(value, pointer, offset = 0) size = value.size_in_bytes pointer.put_bytes(offset, value.to_ptr.read_bytes(size)) end |
.from(element_type, size, item) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gir_ffi/sized_array.rb', line 64 def from(element_type, size, item) return unless item case item when FFI::Pointer wrap element_type, size, item when self from_sized_array size, item else from_enumerable element_type, size, item end end |
.get_value_from_pointer(pointer, offset) ⇒ Object
40 41 42 |
# File 'lib/gir_ffi/sized_array.rb', line 40 def self.get_value_from_pointer(pointer, offset) pointer + offset end |
.wrap(element_type, size, pointer) ⇒ Object
49 50 51 |
# File 'lib/gir_ffi/sized_array.rb', line 49 def self.wrap(element_type, size, pointer) new element_type, size, pointer unless pointer.null? end |
Instance Method Details
#==(other) ⇒ Object
32 33 34 |
# File 'lib/gir_ffi/sized_array.rb', line 32 def ==(other) to_a.eql? other.to_a end |
#each ⇒ Object
26 27 28 29 30 |
# File 'lib/gir_ffi/sized_array.rb', line 26 def each size.times do |idx| yield index(idx) end end |
#index(idx) ⇒ Object
21 22 23 24 |
# File 'lib/gir_ffi/sized_array.rb', line 21 def index(idx) convertor = ArrayElementConvertor.new element_type, @pointer + idx * element_size convertor.to_ruby_value end |
#size_in_bytes ⇒ Object
36 37 38 |
# File 'lib/gir_ffi/sized_array.rb', line 36 def size_in_bytes size * element_size end |
#to_ptr ⇒ Object
17 18 19 |
# File 'lib/gir_ffi/sized_array.rb', line 17 def to_ptr @pointer end |