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 (readonly)

Returns the value of attribute key_type.



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

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



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

def value_type
  @value_type
end

Class Method Details

.equality_function_for(keytype) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# 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_function('g_str_equal'))
  else
    nil
  end
end

.find_support_function(name) ⇒ Object



76
77
78
79
# File 'lib/ffi-glib/hash_table.rb', line 76

def self.find_support_function name
  lib = ::GLib::Lib.ffi_libraries.first
  lib.find_function(name)
end

.from_enumerable(typespec, hash) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/ffi-glib/hash_table.rb', line 46

def self.from_enumerable typespec, hash
  ghash = new(*typespec)
  hash.each do |key, val|
    ghash.insert key, val
  end
  ghash
end

.hash_function_for(keytype) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/ffi-glib/hash_table.rb', line 54

def self.hash_function_for keytype
  case keytype
  when :utf8
    FFI::Function.new(:uint,
                      [:pointer],
                      find_support_function('g_str_hash'))
  else
    nil
  end
end

.new(keytype, valtype) ⇒ Object



36
37
38
39
# File 'lib/ffi-glib/hash_table.rb', line 36

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



14
15
16
17
18
19
20
21
22
# File 'lib/ffi-glib/hash_table.rb', line 14

def each
  prc = proc {|keyptr, valptr, _userdata|
    key = GirFFI::ArgHelper.cast_from_pointer key_type, keyptr
    val = GirFFI::ArgHelper.cast_from_pointer value_type, valptr
    yield key, val
  }
  callback = GLib::HFunc.from prc
  ::GLib::Lib.g_hash_table_foreach to_ptr, callback, 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 to_ptr, keyptr, valptr
end

#reset_typespec(typespec) ⇒ Object



41
42
43
44
# File 'lib/ffi-glib/hash_table.rb', line 41

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

#to_hashObject



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

def to_hash
  Hash[to_a]
end