| 1 |
-- tolua: class class |
| 2 |
-- Written by Waldemar Celes |
| 3 |
-- TeCGraf/PUC-Rio |
| 4 |
-- Jul 1998 |
| 5 |
-- $Id: $ |
| 6 |
|
| 7 |
-- This code is free software; you can redistribute it and/or modify it. |
| 8 |
-- The software provided hereunder is on an "as is" basis, and |
| 9 |
-- the author has no obligation to provide maintenance, support, updates, |
| 10 |
-- enhancements, or modifications. |
| 11 |
|
| 12 |
|
| 13 |
-- Class class |
| 14 |
-- Represents a class definition. |
| 15 |
-- Stores the following fields: |
| 16 |
-- name = class name |
| 17 |
-- base = class base, if any (only single inheritance is supported) |
| 18 |
-- {i} = list of members |
| 19 |
classClass = { |
| 20 |
classtype = 'class', |
| 21 |
name = '', |
| 22 |
base = '', |
| 23 |
type = '', |
| 24 |
btype = '', |
| 25 |
ctype = '', |
| 26 |
} |
| 27 |
classClass.__index = classClass |
| 28 |
setmetatable(classClass,classContainer) |
| 29 |
|
| 30 |
|
| 31 |
-- register class |
| 32 |
function classClass:register (pre) |
| 33 |
|
| 34 |
if not self:check_public_access() then |
| 35 |
return |
| 36 |
end |
| 37 |
|
| 38 |
pre = pre or '' |
| 39 |
push(self) |
| 40 |
if _collect[self.type] then |
| 41 |
output(pre,'#ifdef __cplusplus\n') |
| 42 |
output(pre..'tolua_cclass(tolua_S,"'..self.lname..'","'..self.type..'","'..self.btype..'",'.._collect[self.type]..');') |
| 43 |
output(pre,'#else\n') |
| 44 |
output(pre..'tolua_cclass(tolua_S,"'..self.lname..'","'..self.type..'","'..self.btype..'",NULL);') |
| 45 |
output(pre,'#endif\n') |
| 46 |
else |
| 47 |
output(pre..'tolua_cclass(tolua_S,"'..self.lname..'","'..self.type..'","'..self.btype..'",NULL);') |
| 48 |
end |
| 49 |
if self.extra_bases then |
| 50 |
for k,base in ipairs(self.extra_bases) do |
| 51 |
-- not now |
| 52 |
--output(pre..' tolua_addbase(tolua_S, "'..self.type..'", "'..base..'");') |
| 53 |
end |
| 54 |
end |
| 55 |
output(pre..'tolua_beginmodule(tolua_S,"'..self.lname..'");') |
| 56 |
local i=1 |
| 57 |
while self[i] do |
| 58 |
self[i]:register(pre..' ') |
| 59 |
i = i+1 |
| 60 |
end |
| 61 |
output(pre..'tolua_endmodule(tolua_S);') |
| 62 |
pop() |
| 63 |
end |
| 64 |
|
| 65 |
-- return collection requirement |
| 66 |
function classClass:requirecollection (t) |
| 67 |
if self.flags.protected_destructor or (not self:check_public_access()) then |
| 68 |
return false |
| 69 |
end |
| 70 |
push(self) |
| 71 |
local r = false |
| 72 |
local i=1 |
| 73 |
while self[i] do |
| 74 |
r = self[i]:requirecollection(t) or r |
| 75 |
i = i+1 |
| 76 |
end |
| 77 |
pop() |
| 78 |
-- only class that exports destructor can be appropriately collected |
| 79 |
-- classes that export constructors need to have a collector (overrided by -D flag on command line) |
| 80 |
if self._delete or ((not flags['D']) and self._new) then |
| 81 |
--t[self.type] = "tolua_collect_" .. gsub(self.type,"::","_") |
| 82 |
t[self.type] = "tolua_collect_" .. clean_template(self.type) |
| 83 |
r = true |
| 84 |
end |
| 85 |
return r |
| 86 |
end |
| 87 |
|
| 88 |
-- output tags |
| 89 |
function classClass:decltype () |
| 90 |
push(self) |
| 91 |
self.type = regtype(self.original_name or self.name) |
| 92 |
self.btype = typevar(self.base) |
| 93 |
self.ctype = 'const '..self.type |
| 94 |
if self.extra_bases then |
| 95 |
for i=1,#self.extra_bases do |
| 96 |
self.extra_bases[i] = typevar(self.extra_bases[i]) |
| 97 |
end |
| 98 |
end |
| 99 |
local i=1 |
| 100 |
while self[i] do |
| 101 |
self[i]:decltype() |
| 102 |
i = i+1 |
| 103 |
end |
| 104 |
pop() |
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
-- Print method |
| 109 |
function classClass:print (ident,close) |
| 110 |
print(ident.."Class{") |
| 111 |
print(ident.." name = '"..self.name.."',") |
| 112 |
print(ident.." base = '"..self.base.."';") |
| 113 |
print(ident.." lname = '"..self.lname.."',") |
| 114 |
print(ident.." type = '"..self.type.."',") |
| 115 |
print(ident.." btype = '"..self.btype.."',") |
| 116 |
print(ident.." ctype = '"..self.ctype.."',") |
| 117 |
local i=1 |
| 118 |
while self[i] do |
| 119 |
self[i]:print(ident.." ",",") |
| 120 |
i = i+1 |
| 121 |
end |
| 122 |
print(ident.."}"..close) |
| 123 |
end |
| 124 |
|
| 125 |
function classClass:set_protected_destructor(p) |
| 126 |
self.flags.protected_destructor = self.flags.protected_destructor or p |
| 127 |
end |
| 128 |
|
| 129 |
-- Internal constructor |
| 130 |
function _Class (t) |
| 131 |
setmetatable(t,classClass) |
| 132 |
t:buildnames() |
| 133 |
append(t) |
| 134 |
return t |
| 135 |
end |
| 136 |
|
| 137 |
-- Constructor |
| 138 |
-- Expects the name, the base (array) and the body of the class. |
| 139 |
function Class (n,p,b) |
| 140 |
|
| 141 |
if #p > 1 then |
| 142 |
b = string.sub(b, 1, -2) |
| 143 |
for i=2,#p,1 do |
| 144 |
b = b.."\n tolua_inherits "..p[i].." __"..p[i].."__;\n" |
| 145 |
end |
| 146 |
b = b.."\n}" |
| 147 |
end |
| 148 |
|
| 149 |
-- check for template |
| 150 |
b = string.gsub(b, "^{%s*TEMPLATE_BIND", "{\nTOLUA_TEMPLATE_BIND") |
| 151 |
local t,_,T,I = string.find(b, '^{%s*TOLUA_TEMPLATE_BIND%s*%(+%s*\"?([^\",]*)\"?%s*,%s*([^%)]*)%s*%)+') |
| 152 |
if t then |
| 153 |
|
| 154 |
-- remove quotes |
| 155 |
I = string.gsub(I, "\"", "") |
| 156 |
T = string.gsub(T, "\"", "") |
| 157 |
-- get type list |
| 158 |
local types = split_c_tokens(I, ",") |
| 159 |
-- remove TEMPLATE_BIND line |
| 160 |
local bs = string.gsub(b, "^{%s*TOLUA_TEMPLATE_BIND[^\n]*\n", "{\n") |
| 161 |
|
| 162 |
local Tl = split(T, " ") |
| 163 |
local tc = TemplateClass(n, p, bs, Tl) |
| 164 |
|
| 165 |
|
| 166 |
tc:throw(types, true) |
| 167 |
--for i=1,types.n do |
| 168 |
-- tc:throw(split_c_tokens(types[i], " "), true) |
| 169 |
--end |
| 170 |
return |
| 171 |
end |
| 172 |
|
| 173 |
local mbase |
| 174 |
|
| 175 |
if p then |
| 176 |
mbase = table.remove(p, 1) |
| 177 |
if not p[1] then p = nil end |
| 178 |
end |
| 179 |
|
| 180 |
mbase = mbase and resolve_template_types(mbase) |
| 181 |
|
| 182 |
local c |
| 183 |
local oname = string.gsub(n, "@.*$", "") |
| 184 |
oname = getnamespace(classContainer.curr)..oname |
| 185 |
|
| 186 |
if _global_classes[oname] then |
| 187 |
c = _global_classes[oname] |
| 188 |
if mbase and ((not c.base) or c.base == "") then |
| 189 |
c.base = mbase |
| 190 |
end |
| 191 |
else |
| 192 |
c = _Class(_Container{name=n, base=mbase, extra_bases=p}) |
| 193 |
|
| 194 |
local ft = getnamespace(c.parent)..c.original_name |
| 195 |
append_global_type(ft, c) |
| 196 |
end |
| 197 |
|
| 198 |
push(c) |
| 199 |
c:parse(strsub(b,2,strlen(b)-1)) -- eliminate braces |
| 200 |
pop() |
| 201 |
end |
| 202 |
|