Sha256: 3b9aec6fb30cd7d3254f821481c88a507aa250c0cb3cd20c32caf4583b3a2442

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 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?
      from_utf8 val
    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

1 entries across 1 versions & 1 rubygems

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