Debian Codegraph @

3rdparty/toluapp/src/bin/lua/enumerate.lua

raw
1 -- tolua: enumerate class
2 -- Written by Waldemar Celes
3 -- TeCGraf/PUC-Rio
4 -- Jul 1998
5 -- $Id: enumerate.lua,v 1.3 2000/01/24 20:41:15 celes Exp $
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 -- Enumerate class
14 -- Represents enumeration
15 -- The following fields are stored:
16 -- {i} = list of constant names
17 classEnumerate = {
18 }
19 classEnumerate.__index = classEnumerate
20 setmetatable(classEnumerate,classFeature)
21
22 -- register enumeration
23 function classEnumerate:register (pre)
24 if not self:check_public_access() then
25 return
26 end
27 pre = pre or ''
28 local nspace = getnamespace(classContainer.curr)
29 local i=1
30 while self[i] do
31 if self.lnames[i] and self.lnames[i] ~= "" then
32
33 output(pre..'tolua_constant(tolua_S,"'..self.lnames[i]..'",'..nspace..self[i]..');')
34 end
35 i = i+1
36 end
37 end
38
39 -- Print method
40 function classEnumerate:print (ident,close)
41 print(ident.."Enumerate{")
42 print(ident.." name = "..self.name)
43 local i=1
44 while self[i] do
45 print(ident.." '"..self[i].."'("..self.lnames[i].."),")
46 i = i+1
47 end
48 print(ident.."}"..close)
49 end
50
51 -- Internal constructor
52 function _Enumerate (t,varname)
53 setmetatable(t,classEnumerate)
54 append(t)
55 appendenum(t)
56 if varname and varname ~= "" then
57 if t.name ~= "" then
58 Variable(t.name.." "..varname)
59 else
60 local ns = getcurrnamespace()
61 warning("Variable "..ns..varname.." of type <anonymous enum> is declared as read-only")
62 Variable("tolua_readonly int "..varname)
63 end
64 end
65 local parent = classContainer.curr
66 if parent then
67 t.access = parent.curr_member_access
68 t.global_access = t:check_public_access()
69 end
70 return t
71 end
72
73 -- Constructor
74 -- Expects a string representing the enumerate body
75 function Enumerate (n,b,varname)
76 b = string.gsub(b, ",[%s\n]*}", "\n}") -- eliminate last ','
77 local t = split(strsub(b,2,-2),',') -- eliminate braces
78 local i = 1
79 local e = {n=0}
80 while t[i] do
81 local tt = split(t[i],'=') -- discard initial value
82 e.n = e.n + 1
83 e[e.n] = tt[1]
84 i = i+1
85 end
86 -- set lua names
87 i = 1
88 e.lnames = {}
89 local ns = getcurrnamespace()
90 while e[i] do
91 local t = split(e[i],'@')
92 e[i] = t[1]
93 if not t[2] then
94 t[2] = applyrenaming(t[1])
95 end
96 e.lnames[i] = t[2] or t[1]
97 _global_enums[ ns..e[i] ] = (ns..e[i])
98 i = i+1
99 end
100 e.name = n
101 if n ~= "" then
102 Typedef("int "..n)
103 end
104 return _Enumerate(e, varname)
105 end
106