Sha256: b2ce2f6942c47851fb868082bc78d731219ce23b5c3b3918d4e567b2ab8763fd

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 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
        when :enum
          'to_int'
        else
          'from'
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-0.14.1 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.14.0 lib/gir_ffi/builders/ruby_to_c_convertor.rb
gir_ffi-0.13.1 lib/gir_ffi/builders/ruby_to_c_convertor.rb