Class: GObject::RubyClosure

Inherits:
Closure
  • Object
show all
Defined in:
lib/ffi-gobject/ruby_closure.rb

Overview

Encapsulates Ruby blocks as GObject Closures.

Defined Under Namespace

Classes: Struct

Constant Summary collapse

BLOCK_STORE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Closure

#invoke, #set_marshal, #store_pointer

Constructor Details

#initialize(&block) ⇒ RubyClosure

Returns a new instance of RubyClosure.



20
21
22
23
24
25
26
# File 'lib/ffi-gobject/ruby_closure.rb', line 20

def initialize(&block)
  block or raise ArgumentError, "Block needed"

  initialize_simple(self.class::Struct.size, nil)
  self.block = block
  set_marshal proc { |*args| self.class.marshaller(*args) }
end

Class Method Details

.marshaller(closure, return_value, param_values, _invocation_hint, _marshal_data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ffi-gobject/ruby_closure.rb', line 29

def self.marshaller(closure, return_value, param_values, _invocation_hint,
                    _marshal_data)
  # TODO: Improve by registering RubyClosure as a GObject type
  rclosure = wrap(closure.to_ptr)
  param_values ||= []

  args = param_values.map(&:get_value)

  result = rclosure.invoke_block(*args)

  return_value&.set_value(result)
end

Instance Method Details

#invoke_block(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: Re-structure so invoke_block can become a private method



44
45
46
# File 'lib/ffi-gobject/ruby_closure.rb', line 44

def invoke_block(*args)
  block.call(*args)
end