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, item) ⇒ Object
- .copy_value_to_pointer(value, pointer) ⇒ 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.
8 9 10 11 12 |
# File 'lib/gir_ffi/sized_array.rb', line 8 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.
6 7 8 |
# File 'lib/gir_ffi/sized_array.rb', line 6 def element_type @element_type end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
6 7 8 |
# File 'lib/gir_ffi/sized_array.rb', line 6 def size @size end |
Class Method Details
.copy_from(element_type, size, item) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gir_ffi/sized_array.rb', line 74 def copy_from(element_type, size, item) return unless item enumerable = case item when FFI::Pointer wrap(element_type, size, item).to_a when self item.to_a else item end case element_type when Array _main_type, sub_type = *element_type enumerable = enumerable.map { |it| sub_type.copy_from it } end from_enumerable element_type, size, enumerable end |
.copy_value_to_pointer(value, pointer) ⇒ Object
41 42 43 44 |
# File 'lib/gir_ffi/sized_array.rb', line 41 def self.copy_value_to_pointer(value, pointer) size = value.size_in_bytes pointer.put_bytes(0, value.to_ptr.read_bytes(size)) end |
.from(element_type, size, item) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gir_ffi/sized_array.rb', line 61 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
37 38 39 |
# File 'lib/gir_ffi/sized_array.rb', line 37 def self.get_value_from_pointer(pointer, offset) pointer + offset end |
.wrap(element_type, size, pointer) ⇒ Object
46 47 48 |
# File 'lib/gir_ffi/sized_array.rb', line 46 def self.wrap(element_type, size, pointer) new element_type, size, pointer unless pointer.null? end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/gir_ffi/sized_array.rb', line 29 def ==(other) to_a.eql? other.to_a end |
#each ⇒ Object
23 24 25 26 27 |
# File 'lib/gir_ffi/sized_array.rb', line 23 def each size.times do |idx| yield index(idx) end end |
#index(idx) ⇒ Object
18 19 20 21 |
# File 'lib/gir_ffi/sized_array.rb', line 18 def index(idx) ptr = InOutPointer.new element_type, @pointer + idx * element_size ptr.to_ruby_value end |
#size_in_bytes ⇒ Object
33 34 35 |
# File 'lib/gir_ffi/sized_array.rb', line 33 def size_in_bytes size * element_size end |
#to_ptr ⇒ Object
14 15 16 |
# File 'lib/gir_ffi/sized_array.rb', line 14 def to_ptr @pointer end |