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.



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

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.



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

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.



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

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



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

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



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

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.



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

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

#to_hashObject



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

def to_hash
  to_a.to_h
end