Sha256: 129404a4495bceb37d845b0d5946e4c7237c89c806099f5499416b89cde6ebbc

Contents?: true

Size: 959 Bytes

Versions: 5

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

module GirFFI
  # The ArrayElementConvertor class handles conversion from C array elements to
  # ruby values
  class ArrayElementConvertor
    attr_reader :value_type, :pointer

    def initialize(type, ptr)
      @value_type = type
      @pointer = ptr
    end

    def to_ruby_value
      bare_value = to_value
      case value_type
      when :utf8, :filename
        bare_value.to_utf8
      when Array
        value_type[1].wrap bare_value
      when Module
        value_type.wrap bare_value
      else
        bare_value
      end
    end

    private

    def to_value
      case value_ffi_type
      when Module
        value_ffi_type.get_value_from_pointer(pointer, 0)
      when Symbol
        pointer.send("get_#{value_ffi_type}", 0)
      else
        raise NotImplementedError
      end
    end

    def value_ffi_type
      @value_ffi_type ||= TypeMap.type_specification_to_ffi_type value_type
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gir_ffi-0.16.1 lib/gir_ffi/array_element_convertor.rb
gir_ffi-0.16.0 lib/gir_ffi/array_element_convertor.rb
gir_ffi-0.15.9 lib/gir_ffi/array_element_convertor.rb
gir_ffi-0.15.8 lib/gir_ffi/array_element_convertor.rb
gir_ffi-0.15.7 lib/gir_ffi/array_element_convertor.rb