lib/rbplusplus/builders/extension.rb in rbplusplus-0.8 vs lib/rbplusplus/builders/extension.rb in rbplusplus-0.9
- old
+ new
@@ -1,73 +1,61 @@
module RbPlusPlus
module Builders
- # This class takes in all classes to be wrapped and builds
- # the top-level extension Init code
- class ExtensionBuilder < Base
-
- # Need to be given the list of modules as they are a special case
- attr_accessor :modules
+ # Extension node.
+ # There is only ever one of these in a project as this is
+ # the top level node for building a Ruby extension.
+ #
+ # Extensions are effectively Modules with some slightly different
+ # symantics, in that they expose to Kernel and have slightly
+ # different function handling and code generation.
+ class ExtensionNode < ModuleNode
- def build
- includes << "#include <rice/global_function.hpp>"
+ attr_reader :additional_includes
- add_additional_includes
+ def initialize(name, code, modules)
+ self.name = name
+ self.modules = modules
- body << "extern \"C\""
- body << "void Init_#{@name}() {"
+ @additional_includes = []
- # Explicitly ignore anything from the :: namespace
- if @node.name != "::"
-
- #Build a hash table to handle overloaded functions
- func_hash = {}
- @node.functions.each do |func|
- next if func.ignored? || func.moved?
-
- func_hash[func.name] ||= []
- func_hash[func.name] << func
- end
- #Iterate through the hash table to handle overloaded functions
- func_hash.each do |key, funcs|
- funcs.each_with_index do |func, i|
- add_includes_for func
-
- #append _#{i} to overloaded methods
- #this needs to be done in both the wrapper function and the ruby function
- func_append = ""
- func_append = "_#{i}" if funcs.size > 1
- wrapper_name = func.special_qualified_name || build_function_wrapper(func, func_append)
+ super(code, nil)
+ end
- if func.return_type.const? || func.const?
- TypesManager.build_const_converter(func.return_type)
- end
+ def qualified_name
+ name
+ end
- ruby_name = "#{Inflector.underscore(func.name)}#{func_append}"
- body << "\tdefine_global_function(\"#{ruby_name}\", &#{wrapper_name});"
- end
- end
-
- build_enumerations
- build_classes
+ def add_includes(includes)
+ @additional_includes << includes
+ includes.each do |inc|
+ add_child IncludeNode.new(self, inc)
end
+ end
- build_modules
+ def build
+ super
+ self.rice_variable = nil
+ self.rice_variable_type = nil
end
- def build_modules
- @modules.each do |mod|
- builder = ModuleBuilder.new(self, mod)
- builder.build
- builders << builder
- end
+ def write
+ # Let nodes build their code, splitting up code blocks into
+ # includes, declarations, and registrations,
+ # then wrap it up in our own template
+ registrations << "extern \"C\""
+ registrations << "void Init_#{@name}() {"
end
- # Finish up the required code before doing final output
- def to_s
- includes << "using namespace Rice;"
-
- super + "\n}"
+ private
+
+ def with_module_functions
+ @code.functions.each do |func|
+ next if do_not_wrap?(func)
+ add_child GlobalFunctionNode.new(func, self)
+ end
end
+
end
+
end
end