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.
9 10 11 12 13 |
# File 'lib/gir_ffi/sized_array.rb', line 9 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.
7 8 9 |
# File 'lib/gir_ffi/sized_array.rb', line 7 def element_type @element_type end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/gir_ffi/sized_array.rb', line 7 def size @size end |
Class Method Details
.copy_from(element_type, size, enumerable) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gir_ffi/sized_array.rb', line 75 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
42 43 44 45 |
# File 'lib/gir_ffi/sized_array.rb', line 42 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
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gir_ffi/sized_array.rb', line 62 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
38 39 40 |
# File 'lib/gir_ffi/sized_array.rb', line 38 def self.get_value_from_pointer(pointer, offset) pointer + offset end |
.wrap(element_type, size, pointer) ⇒ Object
47 48 49 |
# File 'lib/gir_ffi/sized_array.rb', line 47 def self.wrap(element_type, size, pointer) new element_type, size, pointer unless pointer.null? end |
Instance Method Details
#==(other) ⇒ Object
30 31 32 |
# File 'lib/gir_ffi/sized_array.rb', line 30 def ==(other) to_a.eql? other.to_a end |
#each ⇒ Object
24 25 26 27 28 |
# File 'lib/gir_ffi/sized_array.rb', line 24 def each size.times do |idx| yield index(idx) end end |
#index(idx) ⇒ Object
19 20 21 22 |
# File 'lib/gir_ffi/sized_array.rb', line 19 def index(idx) ptr = InOutPointer.new element_type, @pointer + idx * element_size ptr.to_ruby_value end |
#size_in_bytes ⇒ Object
34 35 36 |
# File 'lib/gir_ffi/sized_array.rb', line 34 def size_in_bytes size * element_size end |
#to_ptr ⇒ Object
15 16 17 |
# File 'lib/gir_ffi/sized_array.rb', line 15 def to_ptr @pointer end |