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.

Raises:

  • (ArgumentError)


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

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.



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

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



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

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