Class: GirFFI::Builders::MarshallingMethodBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gir_ffi/builders/marshalling_method_builder.rb

Overview

Implements the creation mapping method for a callback or signal handler. This method converts arguments from C to Ruby, and the result from Ruby to C.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argument_builder_collection) ⇒ MarshallingMethodBuilder

Returns a new instance of MarshallingMethodBuilder.



22
23
24
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 22

def initialize argument_builder_collection
  @argument_builder_collection = argument_builder_collection
end

Class Method Details

.for_signal(receiver_info, argument_infos, return_value_info) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 11

def self.for_signal receiver_info, argument_infos, return_value_info
  vargen = VariableNameGenerator.new

  receiver_builder = ClosureArgumentBuilder.new vargen, receiver_info
  argument_builders = argument_infos.map {|arg| ClosureArgumentBuilder.new vargen, arg }
  return_value_builder = CallbackReturnValueBuilder.new(vargen, return_value_info)

  new ArgumentBuilderCollection.new(return_value_builder, argument_builders,
                                    receiver_builder: receiver_builder)
end

Instance Method Details

#call_to_closureObject



48
49
50
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 48

def call_to_closure
  ["#{capture}wrap(closure.to_ptr).invoke_block(#{@argument_builder_collection.call_argument_names.join(', ')})"]
end

#captureObject



56
57
58
59
60
61
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 56

def capture
  @capture ||= begin
                 names = @argument_builder_collection.capture_variable_names
                 names.any? ? "#{names.join(", ")} = " : ""
               end
end

#marshaller_argumentsObject



68
69
70
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 68

def marshaller_arguments
  %w(closure return_value param_values _invocation_hint _marshal_data)
end

#method_argumentsObject



63
64
65
66
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 63

def method_arguments
  # FIXME: Don't add _ if method_argument_names has more than one element
  @method_arguments ||= @argument_builder_collection.method_argument_names.dup.push('_')
end

#method_definitionObject



26
27
28
29
30
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 26

def method_definition
  code = "def self.marshaller(#{marshaller_arguments.join(', ')})"
  method_lines.each { |line| code << "\n  #{line}" }
  code << "\nend\n"
end

#method_linesObject



32
33
34
35
36
37
38
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 32

def method_lines
  param_values_unpack +
    @argument_builder_collection.parameter_preparation +
    call_to_closure +
    @argument_builder_collection.return_value_conversion +
    return_value
end

#param_values_unpackObject



52
53
54
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 52

def param_values_unpack
  ["#{method_arguments.join(", ")} = param_values.map(&:get_value_plain)" ]
end

#return_valueObject



40
41
42
43
44
45
46
# File 'lib/gir_ffi/builders/marshalling_method_builder.rb', line 40

def return_value
  if (name = @argument_builder_collection.return_value_name)
    ["return_value.set_value #{name}"]
  else
    []
  end
end