Sha256: 9f24b1dd632fdda8ca27baf13877b56d0d3db6b6a0156ab75689a968a74fca1c

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
module GirFFI
  module Builders
    # Builder that generates code to convert values from Ruby to C. Used by
    # argument builders.
    class RubyToCConvertor
      def initialize(type_info, argument_name, ownership_transfer: nil)
        @type_info = type_info
        @argument_name = argument_name
        @ownership_transfer = ownership_transfer
      end

      def conversion
        args = conversion_arguments @argument_name
        "#{@type_info.argument_class_name}.#{conversion_method}(#{args})"
      end

      def conversion_arguments(name)
        @type_info.extra_conversion_arguments.map(&:inspect).push(name).join(', ')
      end

      private

      def conversion_method
        case @type_info.flattened_tag
        when :utf8
          'from_utf8'
        when :struct, :c
          case @ownership_transfer
          when :everything
            'copy_from'
          else
            'from'
          end
        else
          'from'
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gir_ffi-0.11.1 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.11.0 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.10.2 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.10.1 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.10.0 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.10.0.pre1 lib/gir_ffi/builders/ruby_to_c_convertor.rb