Class: GirFFI::ObjectStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gir_ffi/object_store.rb

Overview

Helper class for storing objects for later retrieval. Used to store user data arguments.

Instance Method Summary collapse

Constructor Details

#initializeObjectStore

Returns a new instance of ObjectStore.



7
8
9
# File 'lib/gir_ffi/object_store.rb', line 7

def initialize
  @store = {}
end

Instance Method Details

#fetch(ptr) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/gir_ffi/object_store.rb', line 20

def fetch(ptr)
  return if ptr.null?

  key = ptr.address
  if @store.key? key
    @store[key]
  else
    ptr
  end
end

#store(obj) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/gir_ffi/object_store.rb', line 11

def store(obj)
  return FFI::Pointer::NULL if obj.nil?

  # FIXME: Don't use object_id!
  key = obj.object_id
  @store[key] = obj
  FFI::Pointer.new(key)
end