Class: Rbind::RNamespace
Class Attribute Summary collapse
Instance Attribute Summary collapse
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
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_const(const) ⇒ Object
-
#add_const_val(name, value) ⇒ Object
-
#add_default_types ⇒ Object
-
#add_namespace(namespace_name) ⇒ Object
TODO rename to add_namespace_name.
-
#add_operation(op, &block) ⇒ Object
-
#add_simple_type(name) ⇒ Object
-
#add_simple_types(*names) ⇒ Object
-
#add_type(type, check_exist = true) ⇒ Object
-
#add_type_alias(known_type, alias_name, check_exist = true) ⇒ Object
Adding a type alias in the main namespace, e.g.
-
#const(name, raise_ = true, search_owner = true) ⇒ Object
-
#constructor? ⇒ Boolean
-
#container? ⇒ Boolean
-
#delete_type(name) ⇒ Object
-
#each_const(childs = true, all = false, &block) ⇒ Object
-
#each_container(childs = true, all = false, &block) ⇒ Object
-
#each_operation(all = false, &block) ⇒ Object
-
#each_type(childs = true, all = false, &block) ⇒ Object
-
#empty? ⇒ Boolean
-
#enum_hash ⇒ Object
-
#extern? ⇒ Boolean
-
#initialize(name = nil, root = nil) ⇒ RNamespace
constructor
A new instance of RNamespace.
-
#method_missing(m, *args) ⇒ Object
-
#operation(name, raise_ = true) ⇒ Object
-
#operation?(name) ⇒ Boolean
-
#pretty_print(pp) ⇒ Object
-
#pretty_print_name ⇒ Object
-
#root? ⇒ Boolean
-
#type(name, raise_ = true, search_owner = true) ⇒ Object
-
#type?(name, search_owner = true) ⇒ Boolean
-
#types ⇒ Object
-
#use_namespace(namespace) ⇒ Object
-
#used_extern_types ⇒ Object
Methods included from Hooks
included
Methods inherited from RDataType
#==, #basic_type?, #check_type?, #cname, #const?, #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?, #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 = nil, root = nil) ⇒ RNamespace
Returns a new instance of RNamespace.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/rbind/core/rnamespace.rb', line 57
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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
|
# File 'lib/rbind/core/rnamespace.rb', line 589
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_operator_alias ⇒ Object
Returns the value of attribute default_operator_alias.
13
14
15
|
# File 'lib/rbind/core/rnamespace.rb', line 13
def default_operator_alias
@default_operator_alias
end
|
.default_type_alias ⇒ Object
Returns the value of attribute default_type_alias.
12
13
14
|
# File 'lib/rbind/core/rnamespace.rb', line 12
def default_type_alias
@default_type_alias
end
|
.default_type_names ⇒ Object
Returns the value of attribute default_type_names.
11
12
13
|
# File 'lib/rbind/core/rnamespace.rb', line 11
def default_type_names
@default_type_names
end
|
Instance Attribute Details
#consts ⇒ Object
Returns the value of attribute consts.
52
53
54
|
# File 'lib/rbind/core/rnamespace.rb', line 52
def consts
@consts
end
|
#operation_alias ⇒ Object
Returns the value of attribute operation_alias.
51
52
53
|
# File 'lib/rbind/core/rnamespace.rb', line 51
def operation_alias
@operation_alias
end
|
#operations ⇒ Object
Returns the value of attribute operations.
50
51
52
|
# File 'lib/rbind/core/rnamespace.rb', line 50
def operations
@operations
end
|
#root ⇒ Object
Returns the value of attribute root.
55
56
57
|
# File 'lib/rbind/core/rnamespace.rb', line 55
def root
@root
end
|
#type_alias ⇒ Object
Returns the value of attribute type_alias.
54
55
56
|
# File 'lib/rbind/core/rnamespace.rb', line 54
def type_alias
@type_alias
end
|
#used_namespaces ⇒ Object
Returns the value of attribute used_namespaces.
53
54
55
|
# File 'lib/rbind/core/rnamespace.rb', line 53
def used_namespaces
@used_namespaces
end
|
Class Method Details
.default_operators ⇒ Object
585
586
587
|
# File 'lib/rbind/core/rnamespace.rb', line 585
def self.default_operators
default_operator_alias.keys
end
|
Instance Method Details
#add_const(const) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/rbind/core/rnamespace.rb', line 315
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_const_val(name, value) ⇒ Object
310
311
312
313
|
# File 'lib/rbind/core/rnamespace.rb', line 310
def add_const_val(name,value)
c = RParameter.new(name,type("const int"),value)
add_const(c)
end
|
#add_default_types ⇒ Object
336
337
338
339
340
341
342
343
344
345
|
# File 'lib/rbind/core/rnamespace.rb', line 336
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/rbind/core/rnamespace.rb', line 294
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/rbind/core/rnamespace.rb', line 259
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
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 "add_operation: name clash: aliasing #{op.alias} --> #{name}"
name
else
op.auto_alias = true
name = "#{op.name}__#{@operations[op.name].size+1}"
::Rbind.log.debug "add_operation: 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
347
348
349
|
# File 'lib/rbind/core/rnamespace.rb', line 347
def add_simple_type(name)
add_type(RDataType.new(name))
end
|
#add_simple_types(*names) ⇒ Object
351
352
353
354
355
356
|
# File 'lib/rbind/core/rnamespace.rb', line 351
def add_simple_types(*names)
names.flatten!
names.each do |n|
add_simple_type(n)
end
end
|
#add_type(type, check_exist = true) ⇒ Object
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
# File 'lib/rbind/core/rnamespace.rb', line 358
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 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
add_type_alias(type, type.alias, check_exist)
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
|
#add_type_alias(known_type, alias_name, check_exist = true) ⇒ Object
Adding a type alias in the main namespace, e.g. to register existing typedefs
raises if the alias is already register under a given typename
571
572
573
574
575
576
577
578
579
580
581
582
583
|
# File 'lib/rbind/core/rnamespace.rb', line 571
def add_type_alias(known_type, alias_name, check_exist = true)
if check_exist && type_alias.has_key?(alias_name)
existing_type = type(alias_name, false, false)
if known_type.name == existing_type.name
::Rbind.log.warn "Alias: '#{alias_name} for type '#{known_type.name}' already registered'"
else
raise ArgumentError, "Cannot register alias: #{alias_name} for type '#{known_type.name}'. Alias already registered with type: '#{existing_type.name}'"
end
else
::Rbind.log.debug "Alias: '#{alias_name} for type '#{known_type.name}' registered"
@type_alias[alias_name] = known_type.to_raw
end
end
|
#const(name, raise_ = true, search_owner = true) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/rbind/core/rnamespace.rb', line 153
def const(name,raise_ = true,search_owner = true)
hash = @consts.merge(enum_hash)
c = if hash.has_key?(name)
hash[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
85
86
87
|
# File 'lib/rbind/core/rnamespace.rb', line 85
def constructor?
false
end
|
#container? ⇒ Boolean
239
240
241
|
# File 'lib/rbind/core/rnamespace.rb', line 239
def container?
true
end
|
#delete_type(name) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/rbind/core/rnamespace.rb', line 93
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/rbind/core/rnamespace.rb', line 177
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
128
129
130
131
132
133
|
# File 'lib/rbind/core/rnamespace.rb', line 128
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
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/rbind/core/rnamespace.rb', line 208
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
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/rbind/core/rnamespace.rb', line 116
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
506
507
508
|
# File 'lib/rbind/core/rnamespace.rb', line 506
def empty?
consts.empty? && types.empty? && operations.empty?
end
|
#enum_hash ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/rbind/core/rnamespace.rb', line 135
def enum_hash
@enum_hash ||= Hash.new @types.each_value do |t|
next unless t.is_a? REnum
t.values.each do |key,value|
next if @enum_hash.has_key?(key)
p = RParameter.new(key,type("const int"),value)
p.owner = self
@enum_hash[key] = p
end
end
@enum_hash
end
|
#extern? ⇒ Boolean
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/rbind/core/rnamespace.rb', line 192
def extern?
return super if self.is_a? RClass
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
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/rbind/core/rnamespace.rb', line 243
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
255
256
257
|
# File 'lib/rbind/core/rnamespace.rb', line 255
def operation?(name)
!!operation(name,false)
end
|
#pretty_print(pp) ⇒ Object
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
# File 'lib/rbind/core/rnamespace.rb', line 510
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_name ⇒ Object
498
499
500
|
# File 'lib/rbind/core/rnamespace.rb', line 498
def pretty_print_name
"namespace #{full_name}"
end
|
#root? ⇒ Boolean
81
82
83
|
# File 'lib/rbind/core/rnamespace.rb', line 81
def root?
!!root
end
|
#type(name, raise_ = true, search_owner = true) ⇒ Object
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
419
420
421
422
423
424
425
426
427
428
429
430
431
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
488
489
490
491
492
493
494
495
496
|
# File 'lib/rbind/core/rnamespace.rb', line 382
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
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
end
end
end
t ||= if search_owner && name =~ /([:\w]*)<(.*)>$/
t = type($1,false) if $1 && !$1.empty?
t2 = if $2 && !$2.empty?
t2 = $2.split(',')
bok = true
t2 = t2.map do |t3|
t3 = type(t3,false)
bok = false unless t3
t3
end
if bok
t2
else
nil
end
end
if t && t2
name = "#{t.name}<#{t2.map{|t|t.full_name}.join(",")}>"
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
elsif t2 && $1 && !$1.empty?
real_name = t2.map{|v|v.full_name}.join(",");
real_name = "#{$1}<#{real_name}>".gsub(">>","> >")
type(real_name,false) if(real_name != name) else
nil
end
end
if !t && raise_
if RNamespace.callbacks_for_hook(:on_type_not_found)
results = RNamespace.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
|
#type?(name, search_owner = true) ⇒ Boolean
378
379
380
|
# File 'lib/rbind/core/rnamespace.rb', line 378
def type?(name,search_owner = true)
!type(name,false,search_owner).nil?
end
|
#types ⇒ Object
89
90
91
|
# File 'lib/rbind/core/rnamespace.rb', line 89
def types
@types.values
end
|
#use_namespace(namespace) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/rbind/core/rnamespace.rb', line 102
def use_namespace(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
|
#used_extern_types ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/rbind/core/rnamespace.rb', line 221
def used_extern_types
extern = Set.new
each_operation do |op|
extern << op.return_type.to_raw if op.return_type && op.return_type.extern?
op.parameters.each do |arg|
extern << arg.type.to_raw if arg.type.extern?
end
end
each_container do |type|
extern += type.used_extern_types
end
extern
end
|