Class: GirFFI::CallbackBase

Inherits:
Proc
  • Object
show all
Extended by:
FFI::DataConverter, TypeBase
Defined in:
lib/gir_ffi/callback_base.rb

Overview

Base module for callbacks and vfuncs. NOTE: Another option would be to derive this class from FFI::Function, allowing a more natural implementation of from_native, to_native and wrap.

Constant Summary collapse

CALLBACKS =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeBase

gir_ffi_builder, gir_info

Class Method Details

.copy_value_to_pointer(value, pointer) ⇒ Object



83
84
85
# File 'lib/gir_ffi/callback_base.rb', line 83

def self.copy_value_to_pointer(value, pointer)
  pointer.put_pointer 0, to_native(value, nil)
end

.drop_callback(prc) ⇒ Object



43
44
45
# File 'lib/gir_ffi/callback_base.rb', line 43

def self.drop_callback(prc)
  CALLBACKS.delete prc.object_id
end

.from(prc) ⇒ Object

Create Callback from a Proc. Makes sure arguments are properly wrapped, and the callback is stored to prevent garbage collection.



49
50
51
52
53
# File 'lib/gir_ffi/callback_base.rb', line 49

def self.from(prc)
  wrap_proc(prc).tap do |cb|
    store_callback cb
  end
end

.from_native(value, _context) ⇒ Object

TODO: Return instance of this class



18
19
20
21
22
# File 'lib/gir_ffi/callback_base.rb', line 18

def self.from_native(value, _context)
  return nil if !value || value.null?
  FFI::Function.new(gir_ffi_builder.return_ffi_type,
                    gir_ffi_builder.argument_ffi_types, value)
end

.get_value_from_pointer(pointer, offset) ⇒ Object



87
88
89
# File 'lib/gir_ffi/callback_base.rb', line 87

def self.get_value_from_pointer(pointer, offset)
  from_native pointer.get_pointer(offset), nil
end

.native_typeObject



13
14
15
# File 'lib/gir_ffi/callback_base.rb', line 13

def self.native_type
  FFI::Type::POINTER
end

.store_callback(prc) ⇒ Object



39
40
41
# File 'lib/gir_ffi/callback_base.rb', line 39

def self.store_callback(prc)
  CALLBACKS[prc.object_id] = prc
end

.to_ffi_typeObject



67
68
69
# File 'lib/gir_ffi/callback_base.rb', line 67

def self.to_ffi_type
  self
end

.to_native(value, _context) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/gir_ffi/callback_base.rb', line 24

def self.to_native(value, _context)
  case value
  when CallbackBase
    value.to_native
  when FFI::Function
    value
  end
end

.wrap(ptr) ⇒ Object



33
34
35
# File 'lib/gir_ffi/callback_base.rb', line 33

def self.wrap(ptr)
  from_native ptr, nil
end

.wrap_proc(prc) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gir_ffi/callback_base.rb', line 55

def self.wrap_proc(prc)
  return unless prc

  new do |*args|
    begin
      call_with_argument_mapping(prc, *args)
    rescue StandardError => e
      GLib::MainLoop.handle_exception e
    end
  end
end

Instance Method Details

#to_nativeObject



71
72
73
74
75
76
77
# File 'lib/gir_ffi/callback_base.rb', line 71

def to_native
  @to_native ||= begin
                   builder = self.class.gir_ffi_builder
                   FFI::Function.new(builder.return_ffi_type,
                                     builder.argument_ffi_types, self)
                 end
end

#to_ptrObject



79
80
81
# File 'lib/gir_ffi/callback_base.rb', line 79

def to_ptr
  to_native.to_ptr
end