summaryrefslogtreecommitdiffstats
path: root/tolua++-1.0.93/src/bin/lua/template_class.lua
blob: b1ed05abe5fee4d6aafb1dc37410e1e87f064d73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

_global_templates = {}

classTemplateClass = {

	name = '',
	body = '',
	parents = {},
	args = {}, -- the template arguments
}

classTemplateClass.__index = classTemplateClass


function classTemplateClass:throw(types, local_scope)

	--if table.getn(types) ~= table.getn(self.args) then
	--	error("#invalid parameter count")
	--end

	-- replace
	for i =1 , types.n do

		local Il = split_c_tokens(types[i], " ")
		if table.getn(Il) ~= table.getn(self.args) then
			error("#invalid parameter count for "..types[i])
		end
		local bI = self.body
		local pI = {}
		for j = 1,self.args.n do
			--Tl[j] = findtype(Tl[j]) or Tl[j]
			bI = string.gsub(bI, "([^_%w])"..self.args[j].."([^_%w])", "%1"..Il[j].."%2")
			if self.parents then
				for i=1,table.getn(self.parents) do
					pI[i] = string.gsub(self.parents[i], "([^_%w]?)"..self.args[j].."([^_%w]?)", "%1"..Il[j].."%2")
				end
			end
		end
		--local append = "<"..string.gsub(types[i], "%s+", ",")..">"
		local append = "<"..concat(Il, 1, table.getn(Il), ",")..">"
		append = string.gsub(append, "%s*,%s*", ",")
		append = string.gsub(append, ">>", "> >")
		for i=1,table.getn(pI) do
			--pI[i] = string.gsub(pI[i], ">>", "> >")
			pI[i] = resolve_template_types(pI[i])
		end
		bI = string.gsub(bI, ">>", "> >")
		local n = self.name
		if local_scope then
			n = self.local_name
		end

		Class(n..append, pI, bI)
	end
end


function TemplateClass(name, parents, body, parameters)

	local o = {
	
		parents = parents,
		body = body,
		args = parameters,
	}
	
	local oname = string.gsub(name, "@.*$", "")
	oname = getnamespace(classContainer.curr)..oname
	o.name = oname

	o.local_name = name
	
	setmetatable(o, classTemplateClass)

	if _global_templates[oname] then
		warning("Duplicate declaration of template "..oname)
	else
		_global_templates[oname] = o
	end

	return o
end