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



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

def initialize(name,*flags)
    @consts = Hash.new
    @types = 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



344
345
346
347
348
349
350
351
352
# File 'lib/rbind/core/rnamespace.rb', line 344

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

#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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rbind/core/rnamespace.rb', line 205

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



220
221
222
223
224
225
# File 'lib/rbind/core/rnamespace.rb', line 220

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



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

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



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

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
                   name = "#{op.name}#{@operations[op.name].size+1}"
                   ::Rbind.log.debug "name clash: #{op.name} --> #{name}"
                   name
               end
    @operations[op.name] << op
    @operation_alias[op.alias] << op if op.alias
    op
end

#add_simple_type(name) ⇒ Object



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

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

#add_simple_types(*names) ⇒ Object



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

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

#add_type(type) ⇒ Object

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rbind/core/rnamespace.rb', line 238

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
        @types[type.name] = type
    end
    type
end

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

Raises:

  • (RuntimeError)


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

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)


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

def constructor?
    false
end

#container?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/rbind/core/rnamespace.rb', line 143

def container?
    true
end

#delete_type(name) ⇒ Object



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

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



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

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



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

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



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

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



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

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)


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

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



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

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)


159
160
161
# File 'lib/rbind/core/rnamespace.rb', line 159

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

#pretty_print(pp) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
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
# File 'lib/rbind/core/rnamespace.rb', line 300

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



292
293
294
# File 'lib/rbind/core/rnamespace.rb', line 292

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

#root?Boolean

Returns:

  • (Boolean)


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

def root?
    !!root
end

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

Raises:

  • (RuntimeError)


255
256
257
258
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
# File 'lib/rbind/core/rnamespace.rb', line 255

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



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

def types
    @types.values
end

#use_namespace(namespace) ⇒ Object



45
46
47
# File 'lib/rbind/core/rnamespace.rb', line 45

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