Class: Dub::Lua::ClassGen

Inherits:
Generator show all
Defined in:
lib/dub/lua/class_gen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

#comment

Constructor Details

#initializeClassGen

Returns a new instance of ClassGen.



10
11
12
# File 'lib/dub/lua/class_gen.rb', line 10

def initialize
  load_erb
end

Instance Attribute Details

#template_pathObject

Returns the value of attribute template_path.



8
9
10
# File 'lib/dub/lua/class_gen.rb', line 8

def template_path
  @template_path
end

Instance Method Details

#constants_registration(klass = @class) ⇒ Object



69
70
71
72
73
# File 'lib/dub/lua/class_gen.rb', line 69

def constants_registration(klass = @class)
  klass.enums.map do |name|
    "{%-20s, #{klass.full_type}::#{name}}" % name.inspect
  end.join(",\n")
end

#function_generatorObject



24
25
26
# File 'lib/dub/lua/class_gen.rb', line 24

def function_generator
  Lua.function_generator
end

#klass(klass) ⇒ Object



19
20
21
22
# File 'lib/dub/lua/class_gen.rb', line 19

def klass(klass)
  @class = klass
  @class_template.result(binding)
end

#load_erbObject



91
92
93
# File 'lib/dub/lua/class_gen.rb', line 91

def load_erb
  @class_template = ::ERB.new(File.read(@template_path || File.join(File.dirname(__FILE__), 'class.cpp.erb')))
end

#members_list(all_members) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dub/lua/class_gen.rb', line 75

def members_list(all_members)
  all_members
  #list = all_members.map do |member_or_group|
  #  if member_or_group.kind_of?(Array)
  #    members_list(member_or_group)
  #  elsif ignore_member?(member_or_group)
  #    nil
  #  else
  #    member_or_group
  #  end
  #end
  #
  #list.compact!
  #list == [] ? nil : list
end

#method_registration(klass = @class) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dub/lua/class_gen.rb', line 28

def method_registration(klass = @class)
  member_methods = (klass.members || []).map do |method|
    next if method.static?
    "{%-20s, #{method.method_name(0)}}" % method.name.inspect
  end.compact

  member_methods << "{%-20s, #{klass.tostring_name}}" % "__tostring".inspect
  if klass.opts[:destructor] != ''
    member_methods << "{%-20s, #{klass.destructor_name}}" % "__gc".inspect
  end
  if klass.custom_destructor?
    member_methods << "{%-20s, #{klass.is_deleted_name}}" % "deleted".inspect
  end

  member_methods.join(",\n")
end

#namespace_methods_registration(klass = @class) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dub/lua/class_gen.rb', line 45

def namespace_methods_registration(klass = @class)
  if custom_ctor = klass.opts[:constructor]
    ctor = klass[custom_ctor.to_sym]
    raise "#{klass.name} custom constructor '#{custom_ctor}' not found !" unless ctor
    raise "#{klass.name} custom constructor '#{custom_ctor}' not a static function !" unless ctor.static?

    global_methods = klass.names.map do |name|
      "{%-20s, #{ctor.method_name(0)}}" % name.inspect
    end
  else
    global_methods = klass.names.map do |name|
      "{%-20s, #{(klass[klass.opts[:constructor]] || klass.constructor).method_name(0)}}" % name.inspect
    end
  end

  (klass.members || []).map do |method|
    next unless method.static?
    next if method.name == custom_ctor
    global_methods << "{%-20s, #{method.method_name(0)}}" % "#{klass.name}_#{method.name}".inspect
  end

  global_methods.join(",\n")
end