Class: Rbind::GeneratorRuby::RTypeHelper

Inherits:
HelperBase
  • Object
show all
Defined in:
lib/rbind/generator_ruby.rb

Defined Under Namespace

Classes: OperationHelper, OverloadedOperationHelper

Instance Attribute Summary

Attributes included from Logger

#log

Instance Method Summary collapse

Methods inherited from HelperBase

#binding

Methods included from Logger

extend_object

Constructor Details

#initialize(name, root, compact_namespace = false) ⇒ RTypeHelper

Returns a new instance of RTypeHelper.



636
637
638
639
640
641
642
643
644
645
646
# File 'lib/rbind/generator_ruby.rb', line 636

def initialize(name, root,compact_namespace = false)
    @type_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","rtype.rb")).read,nil,"-")
    @namespace_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","rnamespace.rb")).read,nil,"-")
    @static_method_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","rstatic_method.rb")).read,nil,"-")
    @method_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","rmethod.rb")).read,nil,'-')
    @overloaded_method_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","roverloaded_method.rb")).read,nil,"-")
    @overloaded_static_method_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","roverloaded_static_method.rb")).read,nil,"-")
    @overloaded_method_call_wrapper = ERB.new(File.open(File.join(File.dirname(__FILE__),"templates","ruby","roverloaded_method_call.rb")).read,nil,"-")
    @compact_namespace = compact_namespace
    super(name,root)
end

Instance Method Details

#add_constructorObject



676
677
678
679
680
681
682
683
684
# File 'lib/rbind/generator_ruby.rb', line 676

def add_constructor
    raise "there is no constructor for namespaces!" if self.is_a?(RNamespace)
    ops = Array(@root.operation(@root.name,false))
    ops.map do |c|
        next if c.ignore?
        ch = OperationHelper.new(c)
        @overloaded_method_call_wrapper.result(ch.binding)
    end.join("\n")
end

#add_constructor_docObject



686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/rbind/generator_ruby.rb', line 686

def add_constructor_doc
    ops = Array(@root.operation(@root.name,false))
    ops = ops.map do |c|
        next if c.ignore?
        OperationHelper.new(c)
    end.compact
    if ops.empty?
        nil
    elsif ops.size == 1
        ops.first.add_doc
    else
        OverloadedOperationHelper.new(ops).add_doc
    end
end

#add_consts(root = @root) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/rbind/generator_ruby.rb', line 701

def add_consts(root=@root)
    str = root.consts.map do |c|
        next if c.extern? || c.ignore?
        if not c.default_value
            HelperBase.log.warn "#{c.name}: no default value"
            next
        else
            val = begin
                      eval(c.default_value)
                  rescue
                      GeneratorRuby::normalize_const_value(c.default_value)
                  end
        "    #{c.name} = #{val}\n"
        end
    end.join
    return str unless @compact_namespace
    root.each_type(false) do |t|
        next if t.basic_type? && !t.is_a?(RNamespace)
        str += add_consts(t) if name == GeneratorRuby.normalize_type_name(t.full_name)
    end
    str
end

#add_docObject



804
805
806
# File 'lib/rbind/generator_ruby.rb', line 804

def add_doc
    GeneratorRuby::normalize_doc(@root.doc)
end

#add_methods(root = @root) ⇒ Object



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/rbind/generator_ruby.rb', line 734

def add_methods(root=@root)
    # sort all method according their target name
    ops = Hash.new do |h,k|
        h[k] = Array.new
    end
    root.each_operation do |o|
        begin
            next if o.constructor? || o.ignore?
            op = OperationHelper.new(o)
            if op.instance_method?
                ops["rbind_instance_#{op.name}"] << op
            else
                ops["rbind_static_#{op.name}"] << op
            end
        rescue => e
            HelperBase.log.warn "Operation '#{o}' not added. #{e}"
        end
    end
    # render method
    str = ""
    ops.each_value do |o|
        begin
            if o.size == 1
                op = o.first
                str += if op.instance_method?
                           @method_wrapper.result(op.binding)
                       else
                           @static_method_wrapper.result(op.binding)
                       end
            else
                helper = OverloadedOperationHelper.new(o)
                str += if o.first.instance_method?
                           @overloaded_method_wrapper.result(helper.binding)
                       else
                           @overloaded_static_method_wrapper.result(helper.binding)
                       end
            end
        rescue => e
            pp e
            HelperBase.log.error "Operation '#{o}' could not be rendered. #{e}"
        end
    end
    return str unless @compact_namespace
    root.each_type(false) do |t|
        next if t.basic_type? && !t.is_a?(RNamespace)
        str += add_methods(t) if name == GeneratorRuby.normalize_type_name(t.full_name)
    end
    str
end

#add_specializing(root = @root) ⇒ Object



667
668
669
670
671
672
673
674
# File 'lib/rbind/generator_ruby.rb', line 667

def add_specializing(root = @root)
    str = root.specialize_ruby.to_s
    root.each_type(false) do |t|
        next if t.basic_type? && !t.is_a?(RNamespace)
        str += add_specialize(t) if name == GeneratorRuby.normalize_type_name(t.full_name)
    end
    str
end

#add_to_sObject



724
725
726
727
728
729
730
731
732
# File 'lib/rbind/generator_ruby.rb', line 724

def add_to_s
    str = []
    @root.each_operation do |o|
        next unless o.is_a? RGetter
        op = OperationHelper.new(o)
        str << "#{op.name}=\#{self.#{op.name}}"
    end
    "\"#<#{full_name} #{str.join(" ")}>\""
end

#add_types(root = @root) ⇒ Object



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/rbind/generator_ruby.rb', line 784

def add_types(root = @root)
    str = ""
    root.each_type(false,true) do |t|
        next if t.ignore? || t.extern?
        next if t.basic_type? && !t.is_a?(RNamespace)
        str += if @compact_namespace && name == GeneratorRuby.normalize_type_name(t.full_name)
                   add_types(t)
               elsif t.template?
                   RTypeTemplateHelper.new(t.name,t).result
               else
                   RTypeHelper.new(t.name,t).result
               end
    end
    str
end

#cdelete_methodObject



656
657
658
# File 'lib/rbind/generator_ruby.rb', line 656

def cdelete_method
    GeneratorRuby.normalize_method_name(@root.cdelete_method)
end

#cnameObject



652
653
654
# File 'lib/rbind/generator_ruby.rb', line 652

def cname
    GeneratorRuby.normalize_type_name(@root.cname)
end

#full_nameObject



800
801
802
# File 'lib/rbind/generator_ruby.rb', line 800

def full_name
    @root.full_name
end

#nameObject



648
649
650
# File 'lib/rbind/generator_ruby.rb', line 648

def name
    GeneratorRuby.normalize_type_name(@name)
end

#rbind_from_nativeObject



660
661
662
663
664
# File 'lib/rbind/generator_ruby.rb', line 660

def rbind_from_native
		overwrite = @root.overwrite_ruby
		return overwrite if overwrite
		"#{name}.new(ptr)"
end

#resultObject



808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/rbind/generator_ruby.rb', line 808

def result
    return "" if @root.extern?
    str = if @root.is_a? RClass
              @type_wrapper.result(self.binding)
          else
              @namespace_wrapper.result(self.binding)
          end
    if(@root.root?)
        str
    else
        str.gsub!("\n","\n    ").gsub!("    \n","\n")
        "    "+str[0,str.size-4]
    end
end