Sha256: 431f9a862689c53137ba0df1c2a414230b07945a61030dfb3b4e2a7b1262d975

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module GirFFI
  # The InOutPointer class handles conversion between ruby types and
  # pointers for arguments with direction :inout and :out.
  class InOutPointer < FFI::Pointer
    def initialize ptr, type, ffi_type
      super ptr
      @ffi_type = ffi_type
      @value_type = type
    end

    def to_value
      value = self.send "get_#{@ffi_type}", 0
      adjust_value_out value
    end

    private

    def adjust_value_out value
      case @value_type
      when :gboolean
        (value != 0)
      when :utf8
        ArgHelper.ptr_to_utf8 value
      else
        value
      end
    end

    def self.for type
      ffi_type = type_to_ffi_type type
      ptr = AllocationHelper.safe_malloc(FFI.type_size ffi_type)
      ptr.send "put_#{ffi_type}", 0, nil_value_for(type)
      self.new ptr, type, ffi_type
    end

    def self.from type, value
      value = adjust_value_in type, value
      ffi_type = type_to_ffi_type type
      ptr = AllocationHelper.safe_malloc(FFI.type_size ffi_type)
      ptr.send "put_#{ffi_type}", 0, value
      self.new ptr, type, ffi_type
    end

    def self.from_array type, array
      return nil if array.nil?
      ptr = InPointer.from_array(type, array)
      self.from :pointer, ptr
    end

    class << self
      # TODO: Make separate module to hold type info.
      def type_to_ffi_type type
        case type
        when :gboolean
          :int32
        when :utf8
          :pointer
        else
          TypeMap.map_basic_type type
        end
      end

      def adjust_value_in type, value
        case type
        when :gboolean
          (value ? 1 : 0)
        when :utf8
          InPointer.from :utf8, value
        else
          value
        end
      end

      def nil_value_for type
        case type
        when :utf8, :pointer
          nil
        else
          0
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gir_ffi-0.1.0 lib/gir_ffi/in_out_pointer.rb