Class: GirFFI::Builders::FieldBuilder

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

Overview

Creates field getter and setter code for a given IFieldInfo.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_info) ⇒ FieldBuilder

Returns a new instance of FieldBuilder.



12
13
14
# File 'lib/gir_ffi/builders/field_builder.rb', line 12

def initialize field_info
  @info = field_info
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



10
11
12
# File 'lib/gir_ffi/builders/field_builder.rb', line 10

def info
  @info
end

Instance Method Details

#buildObject



16
17
18
19
# File 'lib/gir_ffi/builders/field_builder.rb', line 16

def build
  setup_getter
  setup_setter
end

#container_defines_getter_method?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/gir_ffi/builders/field_builder.rb', line 25

def container_defines_getter_method?
  container_info.find_instance_method info.name
end

#getter_defObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gir_ffi/builders/field_builder.rb', line 37

def getter_def
  builder = return_value_builder

  field_ptr = builder.new_variable
  typed_ptr = builder.new_variable

  return "  def \#{info.name}\n    \#{field_ptr} = @struct.to_ptr + \#{info.offset}\n    \#{typed_ptr} = GirFFI::InOutPointer.new(\#{field_type_tag_or_class.inspect}, \#{field_ptr})\n    \#{builder.capture_variable_name} = \#{typed_ptr}.to_value\n    \#{builder.post_conversion.join(\"\\n\")}\n    \#{builder.return_value_name}\n  end\n  CODE\nend\n".reset_indentation

#is_writable_field?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/gir_ffi/builders/field_builder.rb', line 33

def is_writable_field?
  info.writable?
end

#setter_defObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gir_ffi/builders/field_builder.rb', line 54

def setter_def
  builder = setter_builder

  field_ptr = builder.new_variable
  typed_ptr = builder.new_variable

  return "  def \#{info.name}= \#{builder.method_argument_name}\n    \#{field_ptr} = @struct.to_ptr + \#{info.offset}\n    \#{typed_ptr} = GirFFI::InOutPointer.new(\#{field_type_tag_or_class.inspect}, \#{field_ptr})\n    \#{builder.pre_conversion.join(\"\\n          \")}\n    \#{typed_ptr}.set_value \#{builder.callarg}\n  end\n  CODE\nend\n".reset_indentation

#setup_getterObject



21
22
23
# File 'lib/gir_ffi/builders/field_builder.rb', line 21

def setup_getter
  container_class.class_eval getter_def unless container_defines_getter_method?
end

#setup_setterObject



29
30
31
# File 'lib/gir_ffi/builders/field_builder.rb', line 29

def setup_setter
  container_class.class_eval setter_def if is_writable_field?
end