class GirFFI::CallbackBase
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.
Constants
- CALLBACKS
Public Class Methods
Source
# File lib/gir_ffi/callback_base.rb, line 83 def self.copy_value_to_pointer(value, pointer, offset = 0) pointer.put_pointer offset, to_native(value, nil) end
Source
# File lib/gir_ffi/callback_base.rb, line 45 def self.drop_callback(prc) CALLBACKS.delete prc end
Source
# File lib/gir_ffi/callback_base.rb, line 51 def self.from(prc) wrap_proc(prc).tap do |cb| store_callback cb end end
Create Callback from a Proc. Makes sure arguments are properly wrapped, and the callback is stored to prevent garbage collection.
Source
# File lib/gir_ffi/callback_base.rb, line 19 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
TODO: Return instance of this class
Source
# 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
Source
# File lib/gir_ffi/callback_base.rb, line 14 def self.native_type FFI::Type::POINTER end
Source
# File lib/gir_ffi/callback_base.rb, line 41 def self.store_callback(prc) CALLBACKS.add prc end
Source
# File lib/gir_ffi/callback_base.rb, line 26 def self.to_native(value, _context) case value when CallbackBase value.to_native when FFI::Function value end end
Source
# File lib/gir_ffi/callback_base.rb, line 35 def self.wrap(ptr) from_native ptr, nil end
Source
# File lib/gir_ffi/callback_base.rb, line 57 def self.wrap_proc(prc) return unless prc new do |*args| call_with_argument_mapping(prc, *args) rescue StandardError => e GLib::MainLoop.handle_exception e end end
Public Instance Methods
Source
# 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