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

copy_value_to_pointer(value, pointer) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 68
def self.copy_value_to_pointer(value, pointer)
  pointer.put_pointer 0, to_native(value, nil)
end
from(prc) click to toggle source

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

# File lib/gir_ffi/callback_base.rb, line 42
def self.from(prc)
  wrap_in_callback_args_mapper(prc).tap do |cb|
    store_callback cb
  end
end
from_native(value, _context) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 15
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) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 72
def self.get_value_from_pointer(pointer, offset)
  from_native pointer.get_pointer(offset), nil
end
native_type() click to toggle source
# File lib/gir_ffi/callback_base.rb, line 11
def self.native_type
  FFI::Type::POINTER
end
store_callback(prc) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 36
def self.store_callback(prc)
  CALLBACKS << prc
end
to_ffi_type() click to toggle source
# File lib/gir_ffi/callback_base.rb, line 56
def self.to_ffi_type
  self
end
to_native(value, _context) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 21
def self.to_native(value, _context)
  case value
  when CallbackBase
    value.to_native
  when FFI::Function
    value
  end
end
wrap(ptr) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 30
def self.wrap(ptr)
  from_native ptr, nil
end
wrap_in_callback_args_mapper(prc) click to toggle source
# File lib/gir_ffi/callback_base.rb, line 48
def self.wrap_in_callback_args_mapper(prc)
  return unless prc

  new do |*args|
    call_with_argument_mapping(prc, *args)
  end
end

Public Instance Methods

to_native() click to toggle source
# File lib/gir_ffi/callback_base.rb, line 60
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