[1650] | 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) |
---|
[2710] | 24 | pre = pre or '' |
---|
| 25 | local nspace = getnamespace(classContainer.curr) |
---|
| 26 | local i=1 |
---|
| 27 | while self[i] do |
---|
| 28 | output(pre..'tolua_constant(tolua_S,"'..self.lnames[i]..'",'..nspace..self[i]..');') |
---|
| 29 | i = i+1 |
---|
| 30 | end |
---|
[1650] | 31 | end |
---|
| 32 | |
---|
| 33 | -- Print method |
---|
| 34 | function classEnumerate:print (ident,close) |
---|
[2710] | 35 | print(ident.."Enumerate{") |
---|
| 36 | print(ident.." name = "..self.name) |
---|
| 37 | local i=1 |
---|
| 38 | while self[i] do |
---|
| 39 | print(ident.." '"..self[i].."'("..self.lnames[i].."),") |
---|
| 40 | i = i+1 |
---|
| 41 | end |
---|
| 42 | print(ident.."}"..close) |
---|
[1650] | 43 | end |
---|
| 44 | |
---|
| 45 | -- Internal constructor |
---|
| 46 | function _Enumerate (t,varname) |
---|
[2710] | 47 | setmetatable(t,classEnumerate) |
---|
| 48 | append(t) |
---|
| 49 | appendenum(t) |
---|
| 50 | if varname and varname ~= "" then |
---|
| 51 | if t.name ~= "" then |
---|
| 52 | Variable(t.name.." "..varname) |
---|
| 53 | else |
---|
| 54 | local ns = getcurrnamespace() |
---|
| 55 | warning("Variable "..ns..varname.." of type <anonymous enum> is declared as read-only") |
---|
| 56 | Variable("tolua_readonly int "..varname) |
---|
| 57 | end |
---|
| 58 | end |
---|
| 59 | return t |
---|
[1650] | 60 | end |
---|
| 61 | |
---|
| 62 | -- Constructor |
---|
| 63 | -- Expects a string representing the enumerate body |
---|
| 64 | function Enumerate (n,b,varname) |
---|
[2710] | 65 | b = string.gsub(b, ",[%s\n]*}", "\n}") -- eliminate last ',' |
---|
| 66 | local t = split(strsub(b,2,-2),',') -- eliminate braces |
---|
| 67 | local i = 1 |
---|
| 68 | local e = {n=0} |
---|
| 69 | while t[i] do |
---|
| 70 | local tt = split(t[i],'=') -- discard initial value |
---|
| 71 | e.n = e.n + 1 |
---|
| 72 | e[e.n] = tt[1] |
---|
| 73 | i = i+1 |
---|
| 74 | end |
---|
| 75 | -- set lua names |
---|
| 76 | i = 1 |
---|
| 77 | e.lnames = {} |
---|
| 78 | local ns = getcurrnamespace() |
---|
| 79 | while e[i] do |
---|
| 80 | local t = split(e[i],'@') |
---|
| 81 | e[i] = t[1] |
---|
| 82 | if not t[2] then |
---|
| 83 | t[2] = applyrenaming(t[1]) |
---|
| 84 | end |
---|
| 85 | e.lnames[i] = t[2] or t[1] |
---|
| 86 | _global_enums[ ns..e[i] ] = (ns..e[i]) |
---|
| 87 | i = i+1 |
---|
| 88 | end |
---|
| 89 | e.name = n |
---|
| 90 | if n ~= "" then |
---|
| 91 | Typedef("int "..n) |
---|
| 92 | end |
---|
| 93 | return _Enumerate(e, varname) |
---|
[1650] | 94 | end |
---|
| 95 | |
---|