Class: GObject::RubyClosure

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

Overview

This class encapsulates Ruby blocks as GObject Closures.

Defined Under Namespace

Classes: Struct

Constant Summary collapse

BLOCK_STORE =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Closure

#set_marshal

Class Method Details

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



40
41
42
43
44
45
46
47
48
49
# File 'lib/ffi-gobject/ruby_closure.rb', line 40

def self.marshaller closure, return_value, param_values, _invocation_hint, _marshal_data
  rclosure = wrap(closure.to_ptr)
  param_values ||= []

  args = param_values.map {|value| value.get_value }

  result = rclosure.invoke_block(*args)

  return_value.set_ruby_value(result) if return_value
end

.new(&block) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.new &block
  raise ArgumentError unless block_given?

  closure = wrap(new_simple(self::Struct.size, nil).to_ptr)
  closure.block = block
  closure.set_marshal Proc.new {|*args| marshaller(*args)}

  closure
end

Instance Method Details

#blockObject



16
17
18
# File 'lib/ffi-gobject/ruby_closure.rb', line 16

def block
  BLOCK_STORE[@struct[:block_id]]
end

#block=(block) ⇒ Object



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

def block= block
  id = block.object_id
  BLOCK_STORE[id] = block
  @struct[:block_id] = id
end

#invoke_block(*args) ⇒ Object



26
27
28
# File 'lib/ffi-gobject/ruby_closure.rb', line 26

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