Class: Rbind::RNamespace

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

Direct Known Subclasses

DefaultParser, RStruct

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from RDataType

#cdelete_method, #check_type, #extern_package_name, #invalid_value, #ptr, #ref, #typedef

Attributes inherited from RBase

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

Instance Method Summary collapse

Methods inherited from RDataType

#==, #basic_type?, #check_type?, #cname, #delete!, #generate_signatures, #ptr?, #ref?, #to_ptr, #to_value, #typedef?, #valid_flags

Methods inherited from RBase

#add_flag, basename, #binding, #full_name, #generate_signatures, #ignore?, #map_to_namespace, namespace, #namespace?, normalize, to_cname, #valid_flags, #validate_flags

Constructor Details

#initialize(name, *flags) ⇒ RNamespace

Returns a new instance of RNamespace.



16
17
18
19
20
21
22
23
24
# File 'lib/rbind/core/rnamespace.rb', line 16

def initialize(name,*flags)
    @consts = Hash.new
    @types = Hash.new
    @types_alias = Hash.new
    @operations = Hash.new{|hash,key| hash[key] = Array.new}
    @operation_alias = Hash.new{|hash,key| hash[key] = Array.new}
    @used_namespaces = Hash.new
    super(name,*flags)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



356
357
358
359
360
361
362
363
364
# File 'lib/rbind/core/rnamespace.rb', line 356

def method_missing(m,*args)
    t = type(m.to_s,false)
    return t if t

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

    super
end

Class Attribute Details

.default_type_namesObject

Returns the value of attribute default_type_names.



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

def default_type_names
  @default_type_names
end

Instance Attribute Details

#constsObject (readonly)

Returns the value of attribute consts.



11
12
13
# File 'lib/rbind/core/rnamespace.rb', line 11

def consts
  @consts
end

#operation_aliasObject (readonly)

Returns the value of attribute operation_alias.



10
11
12
# File 'lib/rbind/core/rnamespace.rb', line 10

def operation_alias
  @operation_alias
end

#operationsObject (readonly)

Returns the value of attribute operations.



9
10
11
# File 'lib/rbind/core/rnamespace.rb', line 9

def operations
  @operations
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

#types_aliasObject

Returns the value of attribute types_alias.



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

def types_alias
  @types_alias
end

#used_namespacesObject (readonly)

Returns the value of attribute used_namespaces.



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

def used_namespaces
  @used_namespaces
end

Instance Method Details

#add_const(const) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rbind/core/rnamespace.rb', line 209

def add_const(const)
    if const(const.full_name,false,false)
        raise ArgumentError,"#A const with the name #{const.full_name} already exists"
    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



224
225
226
227
228
229
# File 'lib/rbind/core/rnamespace.rb', line 224

def add_default_types
    add_simple_types RNamespace.default_type_names
    add_type ::Rbind::RDataType.new("uchar").cname("unsigned char")
    add_type ::Rbind::RDataType.new("c_string").cname("char *")
    add_type ::Rbind::RDataType.new("const_c_string").cname("const char *")
end

#add_namespace(namespace) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rbind/core/rnamespace.rb', line 193

def add_namespace(namespace)
    names = namespace.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) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rbind/core/rnamespace.rb', line 165

def add_operation(op)
    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



231
232
233
# File 'lib/rbind/core/rnamespace.rb', line 231

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

#add_simple_types(*names) ⇒ Object



235
236
237
238
239
240
# File 'lib/rbind/core/rnamespace.rb', line 235

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

#add_type(type) ⇒ Object

Raises:

  • (ArgumentError)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rbind/core/rnamespace.rb', line 242

def add_type(type)
    raise ArgumentError, "wrong parmeter type #{type}" unless type.is_a? RDataType
    if 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 type(type.alias,false,false)
                raise ArgumentError,"A type with the name alias #{type.alias} already exists"
            end
            @types_alias[type.alias] = type
        end
        @types[type.name] = type
    end
    type
end

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

Raises:

  • (RuntimeError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbind/core/rnamespace.rb', line 74

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

#constructor?Boolean

Returns:

  • (Boolean)


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

def constructor?
    false
end

#container?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/rbind/core/rnamespace.rb', line 145

def container?
    true
end

#delete_type(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rbind/core/rnamespace.rb', line 38

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rbind/core/rnamespace.rb', line 96

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(all) do |t|
            t.each_const(childs,all,&block)
        end
    else
        Enumerator.new(self,:each_const,childs,all)
    end
end

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



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

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

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



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbind/core/rnamespace.rb', line 128

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



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbind/core/rnamespace.rb', line 51

def each_type(childs=true,all=false,&block)
    if block_given?
        types.each do |t|
            next if !all && (t.ignore? || t.extern?)
            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

#extern?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbind/core/rnamespace.rb', line 111

def extern?
    return super() if self.is_a?(RStruct)

    # 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
    true
end

#operation(name, raise_ = true) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rbind/core/rnamespace.rb', line 149

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)


161
162
163
# File 'lib/rbind/core/rnamespace.rb', line 161

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

#pretty_print(pp) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
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
# File 'lib/rbind/core/rnamespace.rb', line 312

def pretty_print(pp)
    pp.text pretty_print_name

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



304
305
306
# File 'lib/rbind/core/rnamespace.rb', line 304

def pretty_print_name
    "namespace #{full_name}#{" Flags: #{flags.join(", ")}" unless flags.empty?}"
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
    !!root
end

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

Raises:

  • (RuntimeError)


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
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rbind/core/rnamespace.rb', line 265

def type(name,raise_ = true,search_owner = true)
    ptr = name.include?("*")
    ref = name.include?("&")
    if(ptr && ref)
        raise ArgumentError,"given type is a reference and pointer at the same time: #{name}"
    end
    name = name.gsub("*","").gsub("&","")
    name = name.chomp(" ")
    t = if @types.has_key?(name)
            @types[name]
        elsif @types_alias.has_key?(name)
            @types_alias[name]
        else
            if !!(ns = RBase.namespace(name))
                ns = ns.split("::")
                ns << RBase.basename(name)
                t = type(ns.shift,false)
                t.type(ns.join("::"),false,false) if t
            end
        end
    t ||= begin
              used_namespaces.values.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
    raise RuntimeError,"#{full_name} has no type called #{name}" if raise_ && !t
    if t && (ptr || ref)
        t = t.clone
        t.ref = ref
        t.ptr = ptr
    end
    t
end

#typesObject



34
35
36
# File 'lib/rbind/core/rnamespace.rb', line 34

def types
    @types.values
end

#use_namespace(namespace) ⇒ Object



47
48
49
# File 'lib/rbind/core/rnamespace.rb', line 47

def use_namespace(namespace)
    @used_namespaces[namespace.name] = namespace
end