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.

Direct Known Subclasses

VFuncBase

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



57
58
59
# File 'lib/gir_ffi/callback_base.rb', line 57

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

.from(prc) ⇒ Object

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



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

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

.from_native(value, _context) ⇒ Object



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

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

.get_value_from_pointer(pointer) ⇒ Object



61
62
63
# File 'lib/gir_ffi/callback_base.rb', line 61

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

.native_typeObject



9
10
11
# File 'lib/gir_ffi/callback_base.rb', line 9

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

.store_callback(prc) ⇒ Object



26
27
28
# File 'lib/gir_ffi/callback_base.rb', line 26

def self.store_callback prc
  CALLBACKS << prc
end

.to_ffitypeObject



46
47
48
# File 'lib/gir_ffi/callback_base.rb', line 46

def self.to_ffitype
  self
end

.to_native(value, _context) ⇒ Object



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

def self.to_native(value, _context)
  return nil unless value
  return value if FFI::Function === value
  value.to_native
end

.wrap_in_callback_args_mapper(prc) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/gir_ffi/callback_base.rb', line 38

def self.wrap_in_callback_args_mapper prc
  return nil unless prc
  return prc if FFI::Function === prc
  return self.new do |*args|
    call_with_argument_mapping(prc, *args)
  end
end

Instance Method Details

#to_nativeObject



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

def to_native
  @to_native ||= begin
                   builder = self.class.gir_ffi_builder
                   FFI::Function.new builder.return_type, builder.argument_types, self
                 end
end