class RbPlusPlus::Writers::MultipleFilesWriter::FileWriter

For every file to write out, we build an instance of a FileWriter here. This class needs to be given all the nodes it will be writing out to a file

To handle parents calling register_#{name}() on their children, it’s up to the children writers to inform the parents of their existence

Attributes

base_name[R]
node[R]

Public Class Methods

new(node, parent) click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 127
def initialize(node, parent)
  @node = node
  @base_name = node.qualified_name.as_variable

  @register_method = nil
  @register_methods = []
  @register_includes = []


  @header = parent ? "_#{@base_name}.rb.hpp" : nil
  @source = parent ? "_#{@base_name}.rb.cpp" : "#{@base_name}.rb.cpp"
  @parent = parent

  @require_custom = false

  @needs_closing = true

  register_with_parent if @parent

  @nodes = [@node]
end

Public Instance Methods

<<(node) click to toggle source

Add a node to this file writer

# File lib/rbplusplus/writers/multiple_files_writer.rb, line 150
def <<(node)
  @nodes << node
end
add_register_method(node_name, header) click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 175
def add_register_method(node_name, header)
  @register_includes << "#include \"#{header}\""
  @register_methods << "register_#{node_name}(#{has_rice_variable? ? self.rice_variable : ""});"
end
has_rice_variable?() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 171
def has_rice_variable?
  !@node.rice_variable.nil?
end
rice_type() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 163
def rice_type
  @node.rice_variable_type
end
rice_variable() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 167
def rice_variable
  @node.rice_variable
end
write(build_dir, custom_includes = []) click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 154
def write(build_dir, custom_includes = [])
  @build_dir = build_dir
  @custom_includes = custom_includes.flatten

  build_source
  write_header if @header
  write_source
end

Protected Instance Methods

build_source() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 187
def build_source
  @includes = []
  @declarations = []
  @registrations = []

  @nodes.each do |node|
    node.write
    @includes << node.includes
    @declarations << node.declarations
    @registrations << node.registrations
  end
end
parent_signature() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 200
def parent_signature
  if @parent && @parent.has_rice_variable?
    "#{@parent.rice_type} #{@parent.rice_variable}"
  else
    ""
  end
end
register_with_parent() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 182
def register_with_parent
  @register_method = "void register_#{@base_name}"
  @parent.add_register_method @base_name, @header
end
write_header() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 208
def write_header
  include_guard = "__RICE_GENERATED_#{@base_name}_HPP__"

  File.open(File.join(@build_dir, @header), "w+") do |hpp|
    hpp.puts "#ifndef #{include_guard}"
    hpp.puts "#define #{include_guard}"
    hpp.puts

    custom_name = "_rbpp_custom.rb.hpp"
    hpp.puts "#include \"#{custom_name}\"" if File.exists?(File.join(@build_dir, custom_name))

    if @register_method
      hpp.puts "#{@register_method}(#{parent_signature});"
    end

    hpp.puts
    hpp.puts "#endif // #{include_guard}"
  end
end
write_source() click to toggle source
# File lib/rbplusplus/writers/multiple_files_writer.rb, line 228
def write_source
  File.open(File.join(@build_dir, @source), "w+") do |cpp|
    if (incls = @includes.flatten.compact).any?
      incl_output = incls.uniq.sort.reverse.join("\n")
      cpp.puts "", incl_output, ""
    end

    @custom_includes.each do |incl|
      cpp.puts "#include \"#{incl}\"" unless incl_output =~ %r{#{incl}}
    end

    if @register_method
      cpp.puts "", "#include \"#{@header}\"", ""
    end

    if @require_custom
      custom_name = "_rbpp_custom.rb.hpp"
      cpp.puts "#include \"#{custom_name}\"" if File.exists?(File.join(@build_dir, custom_name))
    end

    if @register_includes
      @register_includes.each do |i|
        cpp.puts i
      end
    end

    if (decls = @declarations.flatten.compact).any?
      cpp.puts "", decls.join("\n"), ""
    end

    if @register_method
      cpp.puts "#{@register_method}(#{parent_signature}) {"
    end

    if @register_methods
      # Ug, hack. I've seriously got to rethink this whole
      # code generation system ... again
      @register_methods.reverse.each do |reg|
        @registrations.insert(3, reg)
      end
    end

    if (regs = @registrations.flatten.compact).any?
      cpp.puts regs.join("\n")
    end

    # I really need a better way of handling this
    if @needs_closing
      cpp.puts "} RUBY_CATCH" unless @parent
      cpp.puts "}"
    end
  end
end