Sha256: 16546d04b439e5eb7ecf5d68031ecc3c6806c672b559ca700dea63838b0ce8e6

Contents?: true

Size: 947 Bytes

Versions: 2

Compression:

Stored size: 947 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
        bare_value.to_utf8
      when Array
        value_type[1].wrap bare_value
      when Class
        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

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.15.5 lib/gir_ffi/array_element_convertor.rb
gir_ffi-0.15.4 lib/gir_ffi/array_element_convertor.rb