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.
12 13 14 15 16 |
# File 'lib/gir_ffi/sized_array.rb', line 12 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.
10 11 12 |
# File 'lib/gir_ffi/sized_array.rb', line 10 def element_type @element_type end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
10 11 12 |
# File 'lib/gir_ffi/sized_array.rb', line 10 def size @size end |
Class Method Details
.copy_from(element_type, size, enumerable) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/gir_ffi/sized_array.rb', line 78 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 { sub_type.copy_from _1 } unless sub_type == :strv end from_enumerable element_type, size, arr end |
.copy_value_to_pointer(value, pointer, offset = 0) ⇒ Object
45 46 47 48 |
# File 'lib/gir_ffi/sized_array.rb', line 45 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
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/gir_ffi/sized_array.rb', line 65 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
41 42 43 |
# File 'lib/gir_ffi/sized_array.rb', line 41 def self.get_value_from_pointer(pointer, offset) pointer + offset end |
.wrap(element_type, size, pointer) ⇒ Object
50 51 52 |
# File 'lib/gir_ffi/sized_array.rb', line 50 def self.wrap(element_type, size, pointer) new element_type, size, pointer unless pointer.null? end |
Instance Method Details
#==(other) ⇒ Object
33 34 35 |
# File 'lib/gir_ffi/sized_array.rb', line 33 def ==(other) to_a.eql? other.to_a end |
#each ⇒ Object
27 28 29 30 31 |
# File 'lib/gir_ffi/sized_array.rb', line 27 def each size.times do |idx| yield index(idx) end end |
#index(idx) ⇒ Object
22 23 24 25 |
# File 'lib/gir_ffi/sized_array.rb', line 22 def index(idx) convertor = ArrayElementConvertor.new element_type, @pointer + idx * element_size convertor.to_ruby_value end |
#size_in_bytes ⇒ Object
37 38 39 |
# File 'lib/gir_ffi/sized_array.rb', line 37 def size_in_bytes size * element_size end |
#to_ptr ⇒ Object
18 19 20 |
# File 'lib/gir_ffi/sized_array.rb', line 18 def to_ptr @pointer end |