Class: RbPlusPlus::Builders::InstanceVariableNode

Inherits:
Base
  • Object
show all
Defined in:
lib/rbplusplus/builders/instance_variable.rb

Overview

Wrap up a class instance variable

Instance Attribute Summary

Attributes inherited from Base

#code, #declarations, #global_nodes, #includes, #nodes, #parent, #registrations, #rice_variable, #rice_variable_type

Instance Method Summary collapse

Methods inherited from Base

#has_children?, #initialize, #qualified_name, #sort

Constructor Details

This class inherits a constructor from RbPlusPlus::Builders::Base

Instance Method Details

#buildObject



7
8
# File 'lib/rbplusplus/builders/instance_variable.rb', line 7

def build
end

#writeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbplusplus/builders/instance_variable.rb', line 10

def write
  ruby_name = Inflector.underscore(code.name)
  parent_name = parent.code.qualified_name.as_variable

  # Setter, only if it isn't const
  if !code.cpp_type.const?
    method_name = "wrap_#{parent_name}_#{code.name}_set"
    declarations << "void #{method_name}(#{parent.code.qualified_name}* self, #{code.cpp_type.to_cpp} val) {"
    declarations << "\tself->#{code.name} = val;"
    declarations << "}"

    registrations << "\t#{parent.rice_variable}.define_method(\"#{ruby_name}=\", &#{method_name});"
  end

  # Getter
  method_name = "wrap_#{parent_name}_#{code.name}_get"
  declarations << "#{code.cpp_type.to_cpp} #{method_name}(#{parent.code.qualified_name}* self) {"
  declarations << "\treturn self->#{code.name};"
  declarations << "}"

  registrations << "\t#{parent.rice_variable}.define_method(\"#{ruby_name}\", &#{method_name});"
end