Class: GLib::HashTable

Inherits:
Object
  • Object
show all
Extended by:
ContainerClassMethods
Includes:
Enumerable
Defined in:
lib/ffi-glib/hash_table.rb

Overview

Overrides for GHashTable, GLib’s hash table implementation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContainerClassMethods

from, wrap

Instance Attribute Details

#key_typeObject

TODO: Restructure so these can become attr_readers.



12
13
14
# File 'lib/ffi-glib/hash_table.rb', line 12

def key_type
  @key_type
end

#value_typeObject

Returns the value of attribute value_type.



13
14
15
# File 'lib/ffi-glib/hash_table.rb', line 13

def value_type
  @value_type
end

Class Method Details

.equality_function_for(keytype) ⇒ Object



65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
77
# 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) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



56
57
58
59
60
61
62
63
# 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) ⇒ Object



38
39
40
41
# 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

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
# 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) ⇒ Object



28
29
30
31
32
# 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) ⇒ Object



43
44
45
46
# File 'lib/ffi-glib/hash_table.rb', line 43

def reset_typespec typespec
  self.key_type, self.value_type = *typespec
  self
end

#to_hashObject



24
25
26
# File 'lib/ffi-glib/hash_table.rb', line 24

def to_hash
  Hash[self.to_a]
end