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, #invalid_value, #ptr, #ref, #typedef

Attributes inherited from RBase

#alias, #cname, #csignature, #flags, #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?

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



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

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



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

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



167
168
169
170
171
172
173
# File 'lib/rbind/core/rnamespace.rb', line 167

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("uint64").typedef("unsigned long long")
    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



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

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rbind/core/rnamespace.rb', line 110

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



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

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

#add_simple_types(*names) ⇒ Object



179
180
181
182
183
184
# File 'lib/rbind/core/rnamespace.rb', line 179

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

#add_type(type) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbind/core/rnamespace.rb', line 64

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)


90
91
92
# File 'lib/rbind/core/rnamespace.rb', line 90

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_type(childs = true, &block) ⇒ Object



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

def each_type(childs=true,&block)
    if block_given?
        types.each do |t|
            yield t
            t.each_type(&block) if childs && t.respond_to?(:each_type)
        end
    else
        Enumerator.new(self,:each_type,childs)
    end
end

#operation(name, raise_ = true) ⇒ Object



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

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)


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

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

#pretty_print(pp) ⇒ Object



248
249
250
251
252
253
254
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 248

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



240
241
242
# File 'lib/rbind/core/rnamespace.rb', line 240

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)


203
204
205
206
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
# File 'lib/rbind/core/rnamespace.rb', line 203

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