1 | |
---|
2 | _global_templates = {} |
---|
3 | |
---|
4 | classTemplateClass = { |
---|
5 | |
---|
6 | name = '', |
---|
7 | body = '', |
---|
8 | parents = {}, |
---|
9 | args = {}, -- the template arguments |
---|
10 | } |
---|
11 | |
---|
12 | classTemplateClass.__index = classTemplateClass |
---|
13 | |
---|
14 | |
---|
15 | function classTemplateClass:throw(types, local_scope) |
---|
16 | |
---|
17 | --if table.getn(types) ~= table.getn(self.args) then |
---|
18 | -- error("#invalid parameter count") |
---|
19 | --end |
---|
20 | |
---|
21 | -- replace |
---|
22 | for i =1 , types.n do |
---|
23 | |
---|
24 | local Il = split_c_tokens(types[i], " ") |
---|
25 | if table.getn(Il) ~= table.getn(self.args) then |
---|
26 | error("#invalid parameter count for "..types[i]) |
---|
27 | end |
---|
28 | local bI = self.body |
---|
29 | local pI = {} |
---|
30 | for j = 1,self.args.n do |
---|
31 | --Tl[j] = findtype(Tl[j]) or Tl[j] |
---|
32 | bI = string.gsub(bI, "([^_%w])"..self.args[j].."([^_%w])", "%1"..Il[j].."%2") |
---|
33 | if self.parents then |
---|
34 | for i=1,table.getn(self.parents) do |
---|
35 | pI[i] = string.gsub(self.parents[i], "([^_%w]?)"..self.args[j].."([^_%w]?)", "%1"..Il[j].."%2") |
---|
36 | end |
---|
37 | end |
---|
38 | end |
---|
39 | --local append = "<"..string.gsub(types[i], "%s+", ",")..">" |
---|
40 | local append = "<"..concat(Il, 1, table.getn(Il), ",")..">" |
---|
41 | append = string.gsub(append, "%s*,%s*", ",") |
---|
42 | append = string.gsub(append, ">>", "> >") |
---|
43 | for i=1,table.getn(pI) do |
---|
44 | --pI[i] = string.gsub(pI[i], ">>", "> >") |
---|
45 | pI[i] = resolve_template_types(pI[i]) |
---|
46 | end |
---|
47 | bI = string.gsub(bI, ">>", "> >") |
---|
48 | local n = self.name |
---|
49 | if local_scope then |
---|
50 | n = self.local_name |
---|
51 | end |
---|
52 | |
---|
53 | Class(n..append, pI, bI) |
---|
54 | end |
---|
55 | end |
---|
56 | |
---|
57 | |
---|
58 | function TemplateClass(name, parents, body, parameters) |
---|
59 | |
---|
60 | local o = { |
---|
61 | |
---|
62 | parents = parents, |
---|
63 | body = body, |
---|
64 | args = parameters, |
---|
65 | } |
---|
66 | |
---|
67 | local oname = string.gsub(name, "@.*$", "") |
---|
68 | oname = getnamespace(classContainer.curr)..oname |
---|
69 | o.name = oname |
---|
70 | |
---|
71 | o.local_name = name |
---|
72 | |
---|
73 | setmetatable(o, classTemplateClass) |
---|
74 | |
---|
75 | if _global_templates[oname] then |
---|
76 | warning("Duplicate declaration of template "..oname) |
---|
77 | else |
---|
78 | _global_templates[oname] = o |
---|
79 | end |
---|
80 | |
---|
81 | return o |
---|
82 | end |
---|