Sha256: b04c6c86eed56dc7242dbedde088d7f0d86be74a665480604d7e05df6f6fc9e0

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

require 'gir_ffi/allocation_helper'
require 'gir_ffi/builder'

module GirFFI
  module ArgHelper
    OBJECT_STORE = {}

    def self.ptr_to_utf8_length ptr, len
      ptr.null? ? nil : ptr.read_string(len)
    end

    def self.check_error errpp
      err = GLib::Error.wrap(errpp.read_pointer)
      raise err.message if err
    end

    def self.check_fixed_array_size size, arr, name
      unless arr.size == size
        raise ArgumentError, "#{name} should have size #{size}"
      end
    end

    def self.cast_from_pointer type, it
      case type
      when :utf8, :filename
        it.to_utf8
      when :gint32, :gint8
        cast_pointer_to_int32 it
      when Class
        type.wrap it
      when :guint32
        it.address
      when Array
        main_type, subtype = *type
        raise "Unexpected main type #{main_type}" if main_type != :pointer
        case subtype
        when Array
          container_type, *element_type = *subtype
          raise "Unexpected container type #{container_type}" if container_type != :ghash
          GLib::HashTable.wrap(element_type, it)
        else
          raise "Unexpected subtype #{subtype}"
        end

      else
        raise "Don't know how to cast #{type}"
      end
    end

    def self.cast_uint32_to_int32 val
      if val >= 0x80000000
        -(0x100000000-val)
      else
        val
      end
    end

    def self.cast_pointer_to_int32 ptr
      cast_uint32_to_int32(ptr.address & 0xffffffff)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gir_ffi-0.7.3 lib/gir_ffi/arg_helper.rb
gir_ffi-0.7.2 lib/gir_ffi/arg_helper.rb
gir_ffi-0.7.1 lib/gir_ffi/arg_helper.rb
gir_ffi-0.7.0 lib/gir_ffi/arg_helper.rb