Class: Rbind::GeneratorRuby::RTypeHelper

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

Defined Under Namespace

Classes: OperationHelper, OverloadedOperationHelper

Instance Method Summary collapse

Methods inherited from HelperBase

#binding

Constructor Details

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

Returns a new instance of RTypeHelper.



504
505
506
507
508
509
510
511
512
513
514
# File 'lib/rbind/generator_ruby.rb', line 504

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



537
538
539
540
541
542
543
544
545
# File 'lib/rbind/generator_ruby.rb', line 537

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



547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/rbind/generator_ruby.rb', line 547

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



562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/rbind/generator_ruby.rb', line 562

def add_consts(root=@root)
    str = @root.consts.map do |c|
        next if c.extern? || c.ignore?
        "    #{c.name} = #{GeneratorRuby::normalize_type_name(c.default_value)}\n"
    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



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

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

#add_methods(root = @root) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/rbind/generator_ruby.rb', line 586

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|
        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
    end
    # render method
    str = ""
    ops.each_value do |o|
        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
    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



528
529
530
531
532
533
534
535
# File 'lib/rbind/generator_ruby.rb', line 528

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



576
577
578
579
580
581
582
583
584
# File 'lib/rbind/generator_ruby.rb', line 576

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



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/rbind/generator_ruby.rb', line 627

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



524
525
526
# File 'lib/rbind/generator_ruby.rb', line 524

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

#cnameObject



520
521
522
# File 'lib/rbind/generator_ruby.rb', line 520

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

#full_nameObject



643
644
645
# File 'lib/rbind/generator_ruby.rb', line 643

def full_name
    @root.full_name
end

#nameObject



516
517
518
# File 'lib/rbind/generator_ruby.rb', line 516

def name
    GeneratorRuby.normalize_type_name(@name)
end

#resultObject



651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/rbind/generator_ruby.rb', line 651

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