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

Constructor Details

#initialize(key_type, value_type) ⇒ HashTable

Returns a new instance of HashTable.



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

def initialize(key_type, value_type)
  @key_type = key_type
  @value_type = value_type
  store_pointer Lib.g_hash_table_new(
    hash_function_for_key_type, equality_function_for_key_type)
end

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



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

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



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

def value_type
  @value_type
end

Class Method Details

.from_enumerable(typespec, hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

Instance Method Details

#eachObject



32
33
34
35
36
37
38
39
40
# File 'lib/ffi-glib/hash_table.rb', line 32

def each
  prc = proc do |keyptr, valptr, _userdata|
    key = GirFFI::ArgHelper.cast_from_pointer key_type, keyptr
    val = GirFFI::ArgHelper.cast_from_pointer value_type, valptr
    yield key, val
  end
  callback = GLib::HFunc.from prc
  ::GLib::Lib.g_hash_table_foreach to_ptr, callback, nil
end

#insert(key, value) ⇒ Object



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
# File 'lib/ffi-glib/hash_table.rb', line 54

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

#to_hashObject



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

def to_hash
  Hash[to_a]
end