class GLib::HashTable

Overrides for GHashTable, GLib’s hash table implementation.

Attributes

key_type[RW]

TODO: Restructure so these can become attr_readers.

value_type[RW]

Public Class Methods

equality_function_for(keytype) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 65
def self.equality_function_for keytype
  case keytype
  when :utf8
    FFI::Function.new(:int, [:pointer, :pointer], find_support_funtion("g_str_equal"))
  else
    nil
  end
end
find_support_funtion(name) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 74
def self.find_support_funtion name
  lib = ::GLib::Lib.ffi_libraries.first
  lib.find_function(name)
end
from_enumerable(typespec, hash) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 48
def self.from_enumerable typespec, hash
  ghash = self.new(*typespec)
  hash.each do |key, val|
    ghash.insert key, val
  end
  ghash
end
hash_function_for(keytype) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 56
def self.hash_function_for keytype
  case keytype
  when :utf8
    FFI::Function.new(:uint, [:pointer], find_support_funtion("g_str_hash"))
  else
    nil
  end
end
new(keytype, valtype) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 38
def self.new keytype, valtype
  wrap [keytype, valtype], Lib.g_hash_table_new(
    hash_function_for(keytype), equality_function_for(keytype))
end

Public Instance Methods

each() { |key, val| ... } click to toggle source
# File lib/ffi-glib/hash_table.rb, line 15
def each
  prc = Proc.new {|keyptr, valptr, userdata|
    key = GirFFI::ArgHelper.cast_from_pointer key_type, keyptr
    val = GirFFI::ArgHelper.cast_from_pointer value_type, valptr
    yield key, val
  }
  ::GLib::Lib.g_hash_table_foreach self.to_ptr, prc, nil
end
insert(key, value) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 28
def insert key, value
  keyptr = GirFFI::InPointer.from key_type, key
  valptr = GirFFI::InPointer.from value_type, value
  ::GLib::Lib.g_hash_table_insert self.to_ptr, keyptr, valptr
end
reset_typespec(typespec) click to toggle source
# File lib/ffi-glib/hash_table.rb, line 43
def reset_typespec typespec
  self.key_type, self.value_type = *typespec
  self
end
to_hash() click to toggle source
# File lib/ffi-glib/hash_table.rb, line 24
def to_hash
  Hash[self.to_a]
end