Sha256: 063310d722a0aff22c457c394da79b96852af842d4bcccd0bef9668393be0b73

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module GirFFI
  # The InPointer class handles conversion from ruby types to pointers for
  # arguments with direction :in. This is used for arguments that are
  # arrays, strings, or interfaces.
  class InPointer < FFI::Pointer
    def self.from_array type, ary
      return nil if ary.nil?
      return from_utf8_array ary if type == :utf8
      return from_interface_pointer_array ary if type == :interface_pointer

      ffi_type = TypeMap.map_basic_type type
      block = ArgHelper.allocate_array_of_type ffi_type, ary.length
      block.send "put_array_of_#{ffi_type}", 0, ary

      self.new block
    end

    def self.from type, val
      return nil if val.nil?
      case type
      when :utf8
        from_utf8 val
      when :gint32, :gint8
        self.new val
      else
        raise NotImplementedError
      end
    end

    class << self

      private

      def from_utf8_array ary
        ptr_ary = ary.map {|str| self.from :utf8, str}
        ptr_ary << nil
        self.from_array :pointer, ptr_ary
      end

      def from_interface_pointer_array ary
        ptr_ary = ary.map {|ifc| ifc.to_ptr}
        ptr_ary << nil
        self.from_array :pointer, ptr_ary
      end

      def from_utf8 str
        len = str.bytesize
        ptr = AllocationHelper.safe_malloc(len + 1).write_string(str).put_char(len, 0)
        self.new ptr
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-0.2.2 lib/gir_ffi/in_pointer.rb
gir_ffi-0.2.1 lib/gir_ffi/in_pointer.rb
gir_ffi-0.2.0 lib/gir_ffi/in_pointer.rb