class GirFFI::Builders::CToRubyConvertor

Builder that generates code to convert values from C to Ruby. Used by argument builders.

Public Class Methods

new(type_info, argument, length_arg, ownership_transfer: nil) click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 8
def initialize(type_info, argument, length_arg, ownership_transfer: nil)
  @type_info = type_info
  @argument = argument
  @length_arg = length_arg
  @ownership_transfer = ownership_transfer
end

Public Instance Methods

conversion() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 15
def conversion
  case @type_info.flattened_tag
  when :utf8, :filename
    if @ownership_transfer == :everything
      "GirFFI::AllocationHelper.free_after #{@argument}, &:to_utf8"
    else
      "#{@argument}.to_utf8"
    end
  when :object
    base = "#{@type_info.argument_class_name}.wrap(#{conversion_argument_list})"
    @ownership_transfer == :nothing ? "#{base}.tap { |it| it && it.ref }" : base
  else
    "#{argument_class}.#{conversion_method}(#{conversion_argument_list})"
  end
end

Private Instance Methods

argument_class() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 66
def argument_class
  @type_info.argument_class_name
end
array_size() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 62
def array_size
  @length_arg || @type_info.array_fixed_size
end
conversion_argument_list() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 49
def conversion_argument_list
  conversion_arguments.join(", ")
end
conversion_arguments() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 53
def conversion_arguments
  case @type_info.flattened_tag
  when :c
    [@type_info.element_type.inspect, array_size, @argument]
  else
    @type_info.extra_conversion_arguments.map(&:inspect).push(@argument)
  end
end
conversion_method() click to toggle source
# File lib/gir_ffi/builders/c_to_ruby_convertor.rb, line 33
def conversion_method
  case @type_info.flattened_tag
  when :struct, :union
    case @ownership_transfer
    when :everything
      "wrap_own"
    when :nothing
      "wrap_copy"
    else
      "wrap"
    end
  else
    "wrap"
  end
end