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.



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

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.



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

.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.



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

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



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

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



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

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.



52
53
54
55
# File 'lib/ffi-glib/hash_table.rb', line 52

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

#to_hashObject



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

def to_hash
  Hash[to_a]
end