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, n_param_values, param_values, invocation_hint, marshal_data) ⇒ Object



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

def self.marshaller(closure, return_value, n_param_values,
                    param_values, invocation_hint, marshal_data)
  rclosure = wrap(closure.to_ptr)

  args = n_param_values.times.map {|idx|
    Value.wrap(param_values.to_ptr + idx * Value::Struct.size).ruby_value
  }

  result = rclosure.invoke_block(*args)

  return_value.set_ruby_value(result) unless return_value.nil?
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)}

  return 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