Class: Rbind::StdVector

Inherits:
RTemplateClass show all
Defined in:
lib/rbind/types/std_vector.rb

Instance Attribute Summary

Attributes inherited from RTemplateClass

#template_parameters

Attributes included from Logger

#log

Attributes inherited from RClass

#attributes, #parent_classes

Attributes inherited from RNamespace

#consts, #operation_alias, #operations, #root, #type_alias, #used_namespaces

Attributes inherited from RDataType

#cdelete_method, #check_type, #invalid_value, #typedef

Attributes inherited from RBase

#alias, #auto_alias, #cname, #csignature, #doc, #extern_package_name, #ignore, #name, #namespace, #owner, #signature, #version

Instance Method Summary collapse

Methods inherited from RTemplateClass

#add_type, #do_specialize, #initialize, #resolve_type, #template?

Methods included from Logger

extend_object

Methods inherited from RClass

#add_attribute, #add_parent, #attribute, #basic_type?, #cdelete_method, #constructor?, #empty?, #initialize, #operation, #operations, #parent_class, #pretty_print, #pretty_print_name, #used_namespaces

Methods inherited from RNamespace

#add_const, #add_default_types, #add_namespace, #add_operation, #add_simple_type, #add_simple_types, #add_type, #add_type_alias, #const, #constructor?, #container?, default_operators, #delete_type, #each_const, #each_container, #each_operation, #each_type, #empty?, #extern?, #initialize, #method_missing, #operation, #operation?, #pretty_print, #pretty_print_name, #root?, #type, #types, #use_namespace

Methods inherited from RDataType

#==, #basic_type?, #check_type?, #cname, #const?, #container?, #initialize, #ownership?, #ptr?, #raw?, #ref?, #remove_const, #remove_ownership, #remove_ptr, #remove_ref, #template?, #to_const, #to_ownership, #to_ptr, #to_raw, #to_ref, #to_single_ptr, #typedef?

Methods inherited from RBase

basename, #binding, #delete!, #doc?, #extern?, #full_name, #generate_signatures, #ignore?, #initialize, #map_to_namespace, namespace, #namespace?, normalize, #pretty_print, #rename, #specialize_ruby, split_name, to_cname, #to_s

Constructor Details

This class inherits a constructor from Rbind::RTemplateClass

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rbind::RNamespace

Instance Method Details

#specialize(klass, *parameters) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rbind/types/std_vector.rb', line 3

def specialize(klass,*parameters)
    if parameters.size != 1
        raise ArgumentError,"StdVector does only support one template parameter. Got: #{parameters}}"
    end
    vector_type = parameters.flatten.first

    klass.add_operation ROperation.new(klass.name,nil)
    klass.add_operation ROperation.new(klass.name,nil,RParameter.new("other",klass).to_const)

    para = Array.new
    para <<  RParameter.new("size",type("size_t"))
    para <<  RParameter.new("val",vector_type).default_value(vector_type.full_name+"()").to_const
    klass.add_operation ROperation.new("resize",type("void"),para)
    klass.add_operation ROperation.new("size",type("size_t"))
    klass.add_operation ROperation.new("clear",type("void"))
    klass.add_operation ROperation.new("capacity",type("size_t"))
    klass.add_operation ROperation.new("empty",type("bool"))
    klass.add_operation ROperation.new("reserve",type("void"),RParameter.new("size",type("size_t")))
    klass.add_operation ROperation.new("operator[]",vector_type,RParameter.new("size",type("size_t")))
    klass.add_operation ROperation.new("at",vector_type,RParameter.new("size",type("size_t")))
    klass.add_operation ROperation.new("front",vector_type)
    klass.add_operation ROperation.new("back",vector_type)
    klass.add_operation ROperation.new("data",type("void *"))
    klass.add_operation ROperation.new("push_back",type("void"),RParameter.new("other",vector_type).to_const)
    klass.add_operation ROperation.new("pop_back",type("void"))
    klass.add_operation ROperation.new("swap",type("void"),RParameter.new("other",klass))
    # add ruby code to the front of the method
    klass.operation("operator[]").specialize_ruby do
        "validate_index(size)"
    end
    klass.operation("at").specialize_ruby do
        "validate_index(size)"
    end

    specialize_ruby do
    %Q$     def self.new(type,*args)
    klass,elements = if type.class == Class
                        [type.name,[]]
                    else
                        e = Array(type) + args.flatten
                        args = []
                        [type.class.name,e]
                    end
    #remove module name
    klass = klass.split("::")
    klass.shift if klass.size > 1
    klass = klass.join("_")
    raise ArgumentError,"no std::vector defined for \#{type}" unless self.const_defined?(klass)
    v = self.const_get(klass).new(*args)
    elements.each do |e|
        v << e
    end
    v
    end$
    end

    klass
end

#specialize_ruby_specialization(klass) ⇒ Object

called from RTemplate when ruby_specialize is called for the instance



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbind/types/std_vector.rb', line 63

def specialize_ruby_specialization(klass)
    %Q$ include Enumerable
    alias get_element []
    def [](idx)
        validate_index(idx)
        get_element(idx)
    end

    def validate_index(idx)
        if idx < 0 || idx >= size
            raise RangeError,"\#{idx} is out of range [0..\#{size-1}]"
        end
    end
    def each(&block)
        if block
             s = size
             0.upto(s-1) do |i|
                 yield self[i]
             end
        else
            Enumerator.new(self)
        end
    end
    def <<(val)
        push_back(val)
        self
    end
    def delete_if(&block)
        v = self.class.new
        each do |i|
             v << i if !yield(i)
        end
        v.swap(self)
        self
    end$
end