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

#set_marshal

Constructor Details

#initialize(&block) ⇒ RubyClosure

Returns a new instance of RubyClosure.

Raises:

  • (ArgumentError)


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

def initialize(&block)
  raise ArgumentError unless block_given?

  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.



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

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) if return_value
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



42
43
44
# File 'lib/ffi-gobject/ruby_closure.rb', line 42

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