Class: Rbind::RClass

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

Defined Under Namespace

Classes: ChildClass, 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_const_val, #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, #enum_hash, #extern?, #method_missing, #operation?, #root?, #type, #type?, #types, #use_namespace, #used_extern_types

Methods included from Hooks

included

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, #overwrite_c, #overwrite_ruby, #rename, #specialize_ruby, split_name, to_cname, #to_s

Constructor Details

#initialize(name, *parent_classes) ⇒ RClass

Returns a new instance of RClass.



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

def initialize(name,*parent_classes)
    super(name)
    @parent_classes  = Hash.new
    @child_classes  = Hash.new
    @attributes = Hash.new
    parent_classes.flatten!
    parent_classes.each do |p|
        add_parent(p)
    end
    # 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

#polymorphicObject

Returns the value of attribute polymorphic.



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

def polymorphic
  @polymorphic
end

Instance Method Details

#add_attribute(attr) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbind/core/rclass.rb', line 41

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_child(klass, accessor = :public) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rbind/core/rclass.rb', line 261

def add_child(klass,accessor=:public)
    klass,accessor = if klass.is_a?(ChildClass)
                         [klass.type,klass.accessor]
                     else
                         [klass,accessor]
                     end
    if !klass.name || klass.name.empty?
        raise ArgumentError, "klass name is empty"
    end
    if child? klass
        raise ArgumentError,"#A child 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 child of its self"
    end
    @polymorphic ||= true
    @child_classes[klass.name] = ChildClass.new(klass,accessor)
    klass.add_parent(self,accessor) unless klass.parent?(self)
    self
end

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rbind/core/rclass.rb', line 213

def add_parent(klass,accessor=:public)
    klass,accessor = if klass.is_a?(ParentClass)
                         [klass.type,klass.accessor]
                     else
                         [klass,accessor]
                     end
    if !klass.name || klass.name.empty?
        raise ArgumentError, "klass name is empty"
    end
    if parent? klass
        puts "ignore: parent class #{klass.name} was added multiple time to class #{name}"
        return self
    end
    if klass.full_name == full_name || klass == self
        puts "ignore: class #{klass.name} cannot be parent class of itself"
        return self
    end
    @parent_classes[klass.name] = ParentClass.new(klass,accessor)
      raise "Cannot use namespace #{klass.full_name} as parent class for #{self.full_name}" unless(klass.respond_to?(:child?))
    klass.add_child(self,accessor) unless klass.child?(self)
    self
end

#attribute(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbind/core/rclass.rb', line 73

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

#basic_type?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rbind/core/rclass.rb', line 28

def basic_type?
    false
end

#cdelete_methodObject



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

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

#child?(name) ⇒ Boolean

Returns:

  • (Boolean)


282
283
284
285
286
287
288
289
# File 'lib/rbind/core/rclass.rb', line 282

def child?(name)
    name = if name.respond_to?(:name)
               name.name
           else
               name
           end
    @child_classes.key?(name)
end

#child_class(name) ⇒ Object



291
292
293
294
295
296
297
298
# File 'lib/rbind/core/rclass.rb', line 291

def child_class(name)
    name = if name.respond_to?(:name)
               name.name
           else
               name
           end
    @child_classes[name].type
end

#child_classes(accessor = :public) ⇒ Object



300
301
302
303
304
305
# File 'lib/rbind/core/rclass.rb', line 300

def child_classes(accessor = :public)
    childs = @child_classes.values.find_all do |k|
        k.accessor == accessor
    end
    childs.map(&:type)
end

#constructor?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/rbind/core/rclass.rb', line 32

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)


175
176
177
# File 'lib/rbind/core/rclass.rb', line 175

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

#operation(name, raise_ = true) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rbind/core/rclass.rb', line 141

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.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



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rbind/core/rclass.rb', line 85

def operations
    # temporarily add all base class operations
    own_ops = @operations.dup
    parent_classes.each do |k|
    add_operation RCastOperation.new("castTo#{k.name.gsub(/[:,<>]/,"_")}",k)
        if k.polymorphic? && polymorphic?
            add_operation RCastOperation.new("castFrom#{k.name.gsub(/[:,<>]/,"_")}",self,k)
        end
    k.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? || other_op.static?
      #check for name hiding
      if own_ops.has_key?(other_op.name)
          #puts "#{other_op} is shadowed by #{own_ops[other_op.name].first}"
          next true
      end
      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?(name) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
239
240
241
242
243
# File 'lib/rbind/core/rclass.rb', line 236

def parent?(name)
    name = if name.respond_to?(:name)
               name.name
           else
               name
           end
    @parent_classes.key?(name)
end

#parent_class(name) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/rbind/core/rclass.rb', line 245

def parent_class(name)
    name = if name.respond_to?(:name)
               name.name
           else
               name
           end
    @parent_class[name].type
end

#polymorphic?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rbind/core/rclass.rb', line 24

def polymorphic?
    !!@polymorphic
end

#pretty_print(pp) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbind/core/rclass.rb', line 197

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rbind/core/rclass.rb', line 179

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

    str
end

#used_namespacesObject



133
134
135
136
137
138
139
# File 'lib/rbind/core/rclass.rb', line 133

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