Class: Dub::Klass

Inherits:
Object
  • Object
show all
Includes:
MemberExtraction
Defined in:
lib/dub/klass.rb,
lib/dub/member_extraction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MemberExtraction

#get_member, #insert_member, #member, #members_prefix, #parse_members, #template_method

Constructor Details

#initialize(parent, name, xml, prefix = '') ⇒ Klass

Returns a new instance of Klass.


14
15
16
17
18
19
20
21
22
23
# File 'lib/dub/klass.rb', line 14

def initialize(parent, name, xml, prefix = '')
  @parent, @name, @xml, @prefix = parent, name, xml, prefix

  @alias_names = []
  @enums       = []
  @ignores     = []
  @instanciations = {}
  @opts        = {}
  parse_xml
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(met, *args) ⇒ Object (private)


313
314
315
316
317
318
319
320
321
# File 'lib/dub/klass.rb', line 313

def method_missing(met, *args)
  if args.empty? && @opts.has_key?(met.to_sym)
    @opts[met.to_sym]
  elsif args.size == 1 && met.to_s =~ /^(.+)=$/
    @opts[$1.to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#alias_namesObject (readonly)

Returns the value of attribute alias_names


11
12
13
# File 'lib/dub/klass.rb', line 11

def alias_names
  @alias_names
end

#ancestorsObject (readonly)

Returns the value of attribute ancestors


11
12
13
# File 'lib/dub/klass.rb', line 11

def ancestors
  @ancestors
end

#constructorObject (readonly)

Returns the value of attribute constructor


11
12
13
# File 'lib/dub/klass.rb', line 11

def constructor
  @constructor
end

#enumsObject (readonly)

Returns the value of attribute enums


11
12
13
# File 'lib/dub/klass.rb', line 11

def enums
  @enums
end

#instanciationsObject (readonly)

Returns the value of attribute instanciations


11
12
13
# File 'lib/dub/klass.rb', line 11

def instanciations
  @instanciations
end

#nameObject (readonly)

Returns the value of attribute name


11
12
13
# File 'lib/dub/klass.rb', line 11

def name
  @name
end

#optsObject

Returns the value of attribute opts


12
13
14
# File 'lib/dub/klass.rb', line 12

def opts
  @opts
end

#parentObject (readonly)

Returns the value of attribute parent


11
12
13
# File 'lib/dub/klass.rb', line 11

def parent
  @parent
end

#prefixObject (readonly)

Returns the value of attribute prefix


11
12
13
# File 'lib/dub/klass.rb', line 11

def prefix
  @prefix
end

#xmlObject (readonly)

Returns the value of attribute xml


11
12
13
# File 'lib/dub/klass.rb', line 11

def xml
  @xml
end

Instance Method Details

#<=>(other) ⇒ Object


86
87
88
# File 'lib/dub/klass.rb', line 86

def <=>(other)
  (name || "") <=> (other.name || "")
end

#[](name) ⇒ Object


90
91
92
# File 'lib/dub/klass.rb', line 90

def [](name)
  name.to_s == @name ? constructor : get_member(name.to_s)
end

#bind(generator) ⇒ Object


25
26
27
# File 'lib/dub/klass.rb', line 25

def bind(generator)
  self.gen = generator.class_generator
end

#class_methodsObject


94
95
96
# File 'lib/dub/klass.rb', line 94

def class_methods
  []
end

#class_with_params(template_params) ⇒ Object


127
128
129
# File 'lib/dub/klass.rb', line 127

def class_with_params(template_params)
  @instanciations[template_params]
end

#custom_bind(lang) ⇒ Object

Uses the bind declaration to read function bodies from a yml file:


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dub/klass.rb', line 189

def custom_bind(lang)
  if !@bind_hash then
    if file = @opts[:bind] then
      header = (@xml/'location').first[:file]
      path = header.split('/')
      path.pop
      path = (path + [file]).join('/')
      if File.exist?(path) then
        data = File.read(path)
        @bind_hash = YAML::load(data)
      else
        @bind_hash = {}
        Dub.logger.warn "Missing binding file '#{path}'"
      end
    else
      @bind_hash = {}
    end
  end
  @bind_hash[lang] ||= {}
end

#custom_constructor?Boolean

Use a static method as constructor.

Returns:

  • (Boolean)

119
120
121
# File 'lib/dub/klass.rb', line 119

def custom_constructor?
  @opts[:constructor]
end

#custom_destructor?Boolean

Special or no destructor: class construction and method calls are treated differently because the object can be destroyed out of Lua (and Lua should call a special method on garbage collection).

Returns:

  • (Boolean)

114
115
116
# File 'lib/dub/klass.rb', line 114

def custom_destructor?
  @opts[:destructor]
end

#destructor_nameObject


156
157
158
# File 'lib/dub/klass.rb', line 156

def destructor_name
  "#{name}_destructor"
end

#full_typeObject


144
145
146
# File 'lib/dub/klass.rb', line 144

def full_type
  @parent ? "#{@parent.full_type}::#{name}" : name
end

#function_generatorObject


42
43
44
45
46
47
48
# File 'lib/dub/klass.rb', line 42

def function_generator
  if generator = self.generator
    generator.function_generator
  else
    nil
  end
end

#gen=(generator) ⇒ Object


29
30
31
32
# File 'lib/dub/klass.rb', line 29

def gen=(generator)
  @gen = generator
  @gen_members = nil
end

#generatorObject Also known as: gen


38
39
40
# File 'lib/dub/klass.rb', line 38

def generator
  @gen || (@parent && @parent.class_generator)
end

#has_constants?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'lib/dub/klass.rb', line 102

def has_constants?
  has_enums?
end

#has_enums?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'lib/dub/klass.rb', line 106

def has_enums?
  !@enums.empty?
end

#headerObject


140
141
142
# File 'lib/dub/klass.rb', line 140

def header
  @opts[:header] ||= (@xml/'location').first.attributes['file'].split('/').last
end

#id_name(name = self.name) ⇒ Object


152
153
154
# File 'lib/dub/klass.rb', line 152

def id_name(name = self.name)
  "#{prefix}.#{name}"
end

#ignore(list) ⇒ Object


52
53
54
55
56
57
# File 'lib/dub/klass.rb', line 52

def ignore(list)
  list = [list].flatten
  @ignores += list
  @ignores.uniq!
  @gen_members = nil
end

#is_deleted_nameObject


160
161
162
# File 'lib/dub/klass.rb', line 160

def is_deleted_name
  "#{name}_deleted"
end

#lib_nameObject


148
149
150
# File 'lib/dub/klass.rb', line 148

def lib_name
  @opts[:lib_name] || name
end

#membersObject


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dub/klass.rb', line 59

def members
  list = super(@ignores)
  if self.generator
    list = @gen_members ||= self.generator.members_list(list.reject {|m| m.constructor?})
  end
  # Copy members from super classes
  superclasses = (@opts[:super] || '').split(',').map(&:strip)
  @super_members ||= superclasses.map do |id_name|
    if id_name =~ /([^\.])\.(.*)/
      namespace = Dub::Namespace.find($1)
      class_name = $2
    else
      namespace = self.parent
      class_name = id_name
    end
    if x = namespace[class_name]
      x.members
    else
      Dub.logger.warn "Could not find superclass '#{id_name}'"
      nil
    end
  end.compact.flatten
  (list + @super_members).sort do |a,b|
    a.name <=> b.name
  end
end

#namesObject


183
184
185
# File 'lib/dub/klass.rb', line 183

def names
  [@name] + @alias_names
end

#register_instanciation(template_params, klass) ⇒ Object


131
132
133
# File 'lib/dub/klass.rb', line 131

def register_instanciation(template_params, klass)
  @instanciations[template_params] = klass
end

#sourceObject


135
136
137
138
# File 'lib/dub/klass.rb', line 135

def source
  loc = (@xml/'location').first.attributes
  "#{loc['file'].split('/')[-3..-1].join('/')}:#{loc['line']}"
end

#template?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'lib/dub/klass.rb', line 98

def template?
  !@template_params.nil?
end

#template_paramsObject


123
124
125
# File 'lib/dub/klass.rb', line 123

def template_params
  @template_params
end

#to_sObject


34
35
36
# File 'lib/dub/klass.rb', line 34

def to_s
  generator.klass(self)
end

#tostring_nameObject


164
165
166
# File 'lib/dub/klass.rb', line 164

def tostring_name
  "#{name}__tostring"
end