Class: Rbind::RNamespace

Inherits:
RDataType show all
Includes:
Hooks
Defined in:
lib/rbind/core/rnamespace.rb

Direct Known Subclasses

ClangParser, DefaultParser, RClass

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from RDataType

#cdelete_method, #check_type, #invalid_value, #ptr, #ref, #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 RDataType

#==, #basic_type?, #check_type?, #cname, #const?, #ptr?, #raw?, #ref?, #remove_const, #template?, #to_const, #to_ptr, #to_raw, #to_ref, #to_single_ptr, #typedef?

Methods inherited from RBase

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

Constructor Details

#initialize(name = nil, root = nil) ⇒ RNamespace

Returns a new instance of RNamespace.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbind/core/rnamespace.rb', line 34

def initialize(name=nil,root = nil)
    @consts = Hash.new
    @types = Hash.new
    @type_alias = Hash.new
    @operations = Hash.new{|hash,key| hash[key] = Array.new}
    @operation_alias = Hash.new{|hash,key| hash[key] = Array.new}
    @used_namespaces = Array.new
    name ||= begin
                 @root = true
                 "root"
             end
    if root
        #share varaibales accross root namesapces
        raise "#{root} is not a root namespace" unless root.root?
        @consts = root.instance_variable_get(:@consts)
        @types = root.instance_variable_get(:@types)
        @type_alias = root.instance_variable_get(:@type_alias)
        @operations = root.instance_variable_get(:@operations)
        @operation_alias =root.instance_variable_get(:@operation_alias)
        @used_namespaces = root.instance_variable_get(:@used_namespaces)
    end
    super(name)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/rbind/core/rnamespace.rb', line 489

def method_missing(m,*args)
    if m != :to_ary && args.empty?
        t = type(m.to_s,false,false)
        return t if t

        op = operation(m.to_s,false)
        return op if op

        c = const(m.to_s,false)
        return c if c
    end
    super
end

Class Attribute Details

.default_type_aliasObject

Returns the value of attribute default_type_alias.



14
15
16
# File 'lib/rbind/core/rnamespace.rb', line 14

def default_type_alias
  @default_type_alias
end

.default_type_namesObject

Returns the value of attribute default_type_names.



13
14
15
# File 'lib/rbind/core/rnamespace.rb', line 13

def default_type_names
  @default_type_names
end

Instance Attribute Details

#constsObject (readonly)

Returns the value of attribute consts.



29
30
31
# File 'lib/rbind/core/rnamespace.rb', line 29

def consts
  @consts
end

#operation_aliasObject (readonly)

Returns the value of attribute operation_alias.



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

def operation_alias
  @operation_alias
end

#operationsObject (readonly)

Returns the value of attribute operations.



27
28
29
# File 'lib/rbind/core/rnamespace.rb', line 27

def operations
  @operations
end

#rootObject (readonly)

Returns the value of attribute root.



32
33
34
# File 'lib/rbind/core/rnamespace.rb', line 32

def root
  @root
end

#type_aliasObject (readonly)

Returns the value of attribute type_alias.



31
32
33
# File 'lib/rbind/core/rnamespace.rb', line 31

def type_alias
  @type_alias
end

#used_namespacesObject (readonly)

Returns the value of attribute used_namespaces.



30
31
32
# File 'lib/rbind/core/rnamespace.rb', line 30

def used_namespaces
  @used_namespaces
end

Instance Method Details

#add_const(const) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rbind/core/rnamespace.rb', line 258

def add_const(const)
    const.const!
    if(c = const(const.full_name,false,false))
        if c.type.full_name == const.type.full_name && c.default_value == const.default_value
            ::Rbind.log.warn "A const with the name #{const.full_name} already exists"
            return c
        else
            raise ArgumentError,"#A different const with the name #{const.full_name} already exists: #{const} != #{c}"
        end
    end
    if const.namespace? && self.full_name != const.namespace
        t=type(const.namespace,false)
        t ||= add_namespace(const.namespace)
        t.add_const(const)
    else
        const.owner = self
        @consts[const.name] = const
    end
    const
end

#add_default_typesObject



279
280
281
282
283
284
285
286
287
288
# File 'lib/rbind/core/rnamespace.rb', line 279

def add_default_types
    add_simple_types RNamespace.default_type_names
    type("uchar").cname("unsigned char")
    type("c_string").cname("char *")
    RNamespace.default_type_alias.each_pair do |key,val|
        next if @type_alias.has_key?(key)
        @type_alias[key.to_s] = type(val)
    end
    self
end

#add_namespace(namespace_name) ⇒ Object

TODO rename to add_namespace_name



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rbind/core/rnamespace.rb', line 242

def add_namespace(namespace_name)
    names = namespace_name.split("::")
    current_type = self
    while !names.empty?
        name = names.shift
        temp = current_type.type(name,false,false)
        current_type = if temp
                           temp
                       else
                           ::Rbind.log.debug "missing namespace: add #{current_type.full_name unless current_type.root?}::#{name}"
                           current_type.add_type(RNamespace.new(name))
                       end
    end
    current_type
end

#add_operation(op, &block) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rbind/core/rnamespace.rb', line 207

def add_operation(op,&block)
    if op.is_a? String
        op = ROperation.new(op,void)
        op.owner = self
        instance_exec(op,&block) if block
        return add_operation(op)
    end

    op.owner = self
    # make sure there is no name clash
    other = @operations[op.name].find do |o|
        o.cname == op.cname
    end
    other ||= @operation_alias[op.name].find do |o|
        o.cname == op.cname
    end
    op.alias = if !other
                   op.alias
               elsif op.alias
                   name = "#{op.alias}#{@operations[op.name].size+1}"
                   ::Rbind.log.debug "name clash: aliasing #{op.alias} --> #{name}"
                   name
               else
                   op.auto_alias = true
                   name = "#{op.name}#{@operations[op.name].size+1}"
                   ::Rbind.log.debug "name clash: #{op.name} --> #{name}"
                   name
               end
    op.index = @operations[op.name].size
    @operations[op.name] << op
    @operation_alias[op.alias] << op if op.alias
    op
end

#add_simple_type(name) ⇒ Object



290
291
292
# File 'lib/rbind/core/rnamespace.rb', line 290

def add_simple_type(name)
    add_type(RDataType.new(name))
end

#add_simple_types(*names) ⇒ Object



294
295
296
297
298
299
# File 'lib/rbind/core/rnamespace.rb', line 294

def add_simple_types(*names)
    names.flatten!
    names.each do |n|
        add_simple_type(n)
    end
end

#add_type(type, check_exist = true) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rbind/core/rnamespace.rb', line 301

def add_type(type,check_exist = true)
    if check_exist && type(type.full_name,false,false)
        raise ArgumentError,"A type with the name #{type.full_name} already exists"
    end
    # if self is not the right namespace
    if type.namespace? && self.full_name != type.namespace && !(self.full_name =~/(.*)::#{type.namespace}/)
        t=type(type.namespace,false)
        t ||=add_namespace(type.namespace)
        t.add_type(type)
    else
        type.owner = self
        if type.alias
            if check_exist && type(type.alias,false,false)
                raise ArgumentError,"A type with the name alias #{type.alias} already exists"
            end
            raise ArgumentError,"A type alias with the name #{t.alias} already exists" if(t = @type_alias[type.alias])
            @type_alias[type.alias] = type.to_raw
        end
        raise ArgumentError,"A type with the name #{t.full_name} already exists" if(t = @types[type.name])
        @types[type.name] = type.to_raw
    end
    type
end

#const(name, raise_ = true, search_owner = true) ⇒ Object

Raises:

  • (RuntimeError)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rbind/core/rnamespace.rb', line 116

def const(name,raise_ = true,search_owner = true)
    c = if @consts.has_key?(name)
            @consts[name]
        else
            if !!(ns = RBase.namespace(name))
                t = type(ns,false)
                t.const(RBase.basename(name),false,false) if t
            end
        end
    c ||= begin
              used_namespaces.each do |ns|
                  c = ns.const(name,false,false)
                  break if c
              end
              c
          end
    c ||= if search_owner && owner
              owner.const(name,false)
          end
    raise RuntimeError,"#{full_name} has no const called #{name}" if raise_ && !c
    c
end

#constructor?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rbind/core/rnamespace.rb', line 62

def constructor?
    false
end

#container?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/rbind/core/rnamespace.rb', line 187

def container?
    true
end

#delete_type(name) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/rbind/core/rnamespace.rb', line 70

def delete_type(name)
    ns = RBase.namespace(name)
    if ns
        type(ns).delete_type(RBase.basename(name))
    else
        @types.delete(name)
    end
end

#each_const(childs = true, all = false, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rbind/core/rnamespace.rb', line 139

def each_const(childs=true,all=false,&block)
    if block_given?
        consts.each do |c|
            next if !all && (c.ignore? || c.extern?)
            yield c
        end
        return unless childs
        each_container(false,all) do |t|
            t.each_const(childs,all,&block)
        end
    else
        Enumerator.new(self,:each_const,childs,all)
    end
end

#each_container(childs = true, all = false, &block) ⇒ Object



105
106
107
108
109
110
# File 'lib/rbind/core/rnamespace.rb', line 105

def each_container(childs=true,all=false,&block)
    each_type(childs,all) do |t|
        next unless t.container?
        yield t
    end
end

#each_operation(all = false, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rbind/core/rnamespace.rb', line 170

def each_operation(all=false,&block)
    if block_given?
        operations.each do |ops|
            ops.each do |o|
                next if !all && o.ignore?
                yield o
            end
        end
    else
        Enumerator.new(self,:each_operaion,all)
    end
end

#each_type(childs = true, all = false, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbind/core/rnamespace.rb', line 93

def each_type(childs=true,all=false,&block)
    if block_given?
        types.each do |t|
            next if !all && (t.ignore? || t.extern? || t.template?)
            yield t
            t.each_type(childs,all,&block) if childs && t.respond_to?(:each_type)
        end
    else
        Enumerator.new(self,:each_type,childs,all)
    end
end

#empty?Boolean

Returns:

  • (Boolean)


428
429
430
# File 'lib/rbind/core/rnamespace.rb', line 428

def empty?
    consts.empty? && types.empty? && operations.empty?
end

#extern?Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbind/core/rnamespace.rb', line 154

def extern?
    return super if self.is_a? RClass

    # check if self is container holding only
    # extern objects
    each_type(false) do |t|
        return false if !t.extern?
    end
    each_const(false) do |c|
        return false if !c.extern?
    end
    each_operation do |t|
        return false
    end
end

#operation(name, raise_ = true) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbind/core/rnamespace.rb', line 191

def operation(name,raise_=true)
    ops = @operations[name]
    if(ops.size == 1)
        ops.first
    elsif ops.empty?
        @operations.delete name
        raise "#{full_name} has no operation called #{name}." if raise_
    else
        ops
    end
end

#operation?(name) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rbind/core/rnamespace.rb', line 203

def operation?(name)
    !!operation(name,false)
end

#pretty_print(pp) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/rbind/core/rnamespace.rb', line 432

def pretty_print(pp)
    pp.text pretty_print_name unless root?

    unless consts.empty?
        pp.nest(2) do
            pp.breakable
            pp.text "Consts:"
            pp.nest(2) do
                consts.each do |c|
                    pp.breakable
                    pp.pp(c)
                end
            end
        end
    end
    unless types.empty?
        pp.nest(2) do
            pp.breakable
            pp.text "Types:"
            pp.nest(2) do
                simple = []
                other = []
                types.each do |t|
                    if t.basic_type? && !t.container?
                        simple << t.name
                    else 
                        other << t
                    end
                end
                if !simple.empty?
                    pp.breakable
                    pp.text(simple.join(", "))
                end
                other.each do |t|
                    pp.breakable
                    pp.pp(t)
                end
            end
        end
    end

    unless operations.empty?
        pp.nest(2) do
            pp.breakable
            pp.text "Operations:"
            pp.nest(2) do
                operations.each do |op|
                    op.each do |o|
                        pp.breakable
                        pp.pp(o)
                    end
                end
            end
        end
    end
end

#pretty_print_nameObject



420
421
422
# File 'lib/rbind/core/rnamespace.rb', line 420

def pretty_print_name
    "namespace #{full_name}"
end

#root?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rbind/core/rnamespace.rb', line 58

def root?
    !!root
end

#type(name, raise_ = true, search_owner = true) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/rbind/core/rnamespace.rb', line 325

def type(name,raise_ = true,search_owner = true)
    name = name.to_s
    constant = if name =~ /const /
                   true
               else
                   false
               end
    name = name.gsub("unsigned ","u").gsub("const ","").gsub(" ","").gsub(">>","> >")

    t = if @types.has_key?(name)
            @types[name]
        elsif @type_alias.has_key?(name)
            @type_alias[name]
        else
            if !!(ns = RBase.namespace(name))
                ns = ns.split("::")
                ns << RBase.basename(name)
                t = type(ns.shift,false,false)
                t.type(ns.join("::"),false,false) if t
            end
        end
    t ||= begin
              used_namespaces.each do |ns|
                  t = ns.type(name,false,false)
                  break if t
              end
              t
          end
    t ||= if search_owner && owner
              owner.type(name,false)
          end
    # check if type is a pointer and pointee is registered
    t ||= begin
              ptr_level = $1.to_s.size if name  =~ /(\**)$/
              name2 = name.gsub("*","")
              ref_level = $1.to_s.size if name2  =~ /(&*)$/
              name2 = name2.gsub("&","")
              if ptr_level > 0 || ref_level > 0
                  t = type(name2,raise_,search_owner)
                  if t
                      1.upto(ptr_level) do
                          t = t.to_ptr
                      end
                      1.upto(ref_level) do
                          t = t.to_ref
                      end
                     # t.owner.add_type(t,false)
                      t
                  end
              end
          end

    # check if type is a template and a template is registered
    # under the given name
    t ||=  if search_owner && name =~ /([:\w]*)<(.*)>$/
              t = type($1,false) if $1 && !$1.empty?
              t2 = type($2,false) if $2 && !$2.empty?
              if t && t2
                  name = "#{t.name}<#{t2.full_name}>"
                  t3 ||= t.owner.type(name,false,false)
                  t3 ||= begin
                             if !t.template?
                                 ::Rbind.log.error "try to spezialize a class #{t.full_name} --> #{name}"
                                 nil
                             else
                                 t3 = t.do_specialize(name,t2)
                                 if !t3
                                     ::Rbind.log.error "Failed to specialize template #{t.full_name} --> #{name}"
                                     nil
                                 else
                                     t.owner.add_type(t3,false)
                                     ::Rbind.log.info "spezialize template #{t.full_name} --> #{t3.full_name}"
                                     t3
                                 end
                             end
                         end
              end
            end

    if !t && raise_
        if self.class.callbacks_for_hook(:on_type_not_found)
            results = self.run_hook(:on_type_not_found,self,name)
            t = results.find do |t|
                t.respond_to?(:type)
            end
        end
        raise RuntimeError,"#{full_name} has no type called #{name}" if !t
    end
    if constant
        t.to_const
    else
        t
    end
end

#typesObject



66
67
68
# File 'lib/rbind/core/rnamespace.rb', line 66

def types
    @types.values
end

#use_namespace(namespace) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbind/core/rnamespace.rb', line 79

def use_namespace(namespace)
    # Check if there is another root embedded
    # and add the corresponding namespace.
    # This is needed to support multiple roots providing
    # types to the same namespace
    @used_namespaces.each do |ns|
        next unless ns.root?
        if (ns2 = ns.type(namespace.full_name,false))
            @used_namespaces << ns2 if ns.object_id != namespace.object_id
        end
    end
    @used_namespaces << namespace
end