Class: Rbind::RClass

Inherits:
RNamespace show all
Defined in:
lib/rbind/core/rclass.rb

Defined Under Namespace

Classes: ParentClass

Instance Attribute Summary collapse

Attributes inherited from RNamespace

#consts, #operation_alias, #root, #type_alias

Attributes inherited from RDataType

#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 RNamespace

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

Methods inherited from RDataType

#==, #check_type?, #cname, #const?, #container?, #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?, #map_to_namespace, namespace, #namespace?, normalize, #rename, #specialize_ruby, split_name, to_cname, #to_s

Constructor Details

#initialize(name, *parent_classes) ⇒ RClass

Returns a new instance of RClass.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbind/core/rclass.rb', line 8

def initialize(name,*parent_classes)
    @parent_classes  = Hash.new
    @attributes = Hash.new
    parent_classes.flatten!
    parent_classes.each do |p|
        add_parent(p)
    end
    super(name)
    # we have to disable the type check for classes
    # otherwise derived types cannot be parsed
    @check_type = false
end

Dynamic Method Handling

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/rbind/core/rclass.rb', line 5

def attributes
  @attributes
end

#parent_classes(accessor = :public) ⇒ Object (readonly)

Returns the value of attribute parent_classes.



4
5
6
# File 'lib/rbind/core/rclass.rb', line 4

def parent_classes
  @parent_classes
end

Instance Method Details

#add_attribute(attr) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbind/core/rclass.rb', line 34

def add_attribute(attr)
    if attr.namespace?
        type(attr.namespace).add_attribute(attr)
    else
        if @attributes.has_key? attr.name
            raise "#An attribute with the name #{attr.name} already exists"
        end
        attr.owner = self
        @attributes[attr.name] = attr
        # add getter and setter methods to the object
        add_operation(RGetter.new(attr)) if attr.readable?
        add_operation(RSetter.new(attr)) if attr.writeable?
    end
    self
end

#add_parent(klass, accessor = :public) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rbind/core/rclass.rb', line 189

def add_parent(klass,accessor=:public)
    klass,accessor = if klass.is_a?(ParentClass)
                         [klass.type,klass.accessor]
                     else
                         [klass,accessor]
                     end
    if @parent_classes.has_key? klass.name
        raise ArgumentError,"#A parent class with the name #{klass.name} already exists"
    end
    if klass.full_name == full_name || klass == self
        raise ArgumentError,"class #{klass.full_name} cannot be parent of its self"
    end
    @parent_classes[klass.name] = ParentClass.new(klass,accessor)
    self
end

#attribute(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rbind/core/rclass.rb', line 66

def attribute(name)
    attrib = @attributes[name]
    attrib ||= begin
                   p = parent_classes.find do |k|
                       k.type.attribute(name)
                   end
                   a = p.attribute(name).dup if p
                   a.owner = self if a
                   a
               end
end

#basic_type?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rbind/core/rclass.rb', line 21

def basic_type?
    false
end

#cdelete_methodObject



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rbind/core/rclass.rb', line 146

def cdelete_method
    if @cdelete_method
        @cdelete_method
    else
        if cname =~ /^#{RBase.cprefix}(.*)/
            "#{RBase.cprefix}delete_#{$1}"
        else
            "#{RBase.cprefix}delete_#{name}"
        end
    end
end

#constructor?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/rbind/core/rclass.rb', line 25

def constructor?
    ops = Array(operation(name,false))
    return false unless ops
    op = ops.find do |op|
        op.constructor?
    end
    !!op
end

#empty?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/rbind/core/rclass.rb', line 158

def empty?
    super && parent_classes.empty? && attributes.empty?
end

#operation(name, raise_ = true) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rbind/core/rclass.rb', line 124

def operation(name,raise_=true)
    ops = if @operations.has_key? name
              @operations[name].dup
          else
              []
          end
    parent_classes.each do |k|
        other_ops = Array(k.type.operation(name,false))
        other_ops.delete_if do |other_op|
            ops.include? other_op
        end
        ops += other_ops
    end
    if(ops.size == 1)
        ops.first
    elsif ops.empty?
        raise "#{full_name} has no operation called #{name}." if raise_
    else
        ops
    end
end

#operationsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbind/core/rclass.rb', line 78

def operations
    # temporarily add all base class operations
    own_ops = @operations.dup
    parent_classes.each do |k|
        k.type.operations.each do |other_ops|
            next if other_ops.empty?
            ops = if @operations.has_key?(other_ops.first.name)
                    @operations[other_ops.first.name]
                  else
                      []
                  end
            other_ops.delete_if do |other_op|
                next true if !other_op || other_op.constructor?
                op = ops.find do |o|
                    o == other_op
                end
                next false if !op
                next true if op.base_class == self
                next true if op.base_class == other_op.base_class
                # ambiguous name look up due to multi
                # inheritance
                op.ambiguous_name = true
                other_op.ambiguous_name = true
                false
            end
            other_ops = other_ops.map(&:dup)
            other_ops.each do |other|
                old = other.alias
                add_operation other
            end
        end
    end
    # copy embedded arrays other wise they might get modified outside
    result = @operations.values.map(&:dup)
    @operations = own_ops
    result
end

#parent_class(name) ⇒ Object



205
206
207
# File 'lib/rbind/core/rclass.rb', line 205

def parent_class(name)
    @parent_class[name].type
end

#pretty_print(pp) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbind/core/rclass.rb', line 173

def pretty_print(pp)
    super
    unless attributes.empty?
        pp.nest(2) do
            pp.breakable
            pp.text "Attributes:"
            pp.nest(2) do
                attributes.each do |a|
                    pp.breakable
                    pp.pp(a)
                end
            end
        end
    end
end

#pretty_print_nameObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/rbind/core/rclass.rb', line 162

def pretty_print_name
    str = "#{"template " if template?}class #{full_name}"
    unless parent_classes.empty?
        parents = parent_classes.map do |p|
            p.type.full_name
        end
        str += " : " +  parents.join(", ")
    end
    str
end

#used_namespacesObject



116
117
118
119
120
121
122
# File 'lib/rbind/core/rclass.rb', line 116

def used_namespaces
    namespaces = super.clone
    parent_classes.each do |k|
        namespaces += k.type.used_namespaces
    end
    namespaces
end