Added more templated classes
[lqt/mk.git] / generator / enums.lua
blob5602b65665687582d7cc14a13aabd836549c1b0d
1 module("enums", package.seeall)
3 local print_enum = fprint(assert(io.open(module_name.._src..module_name..'_enum.cpp', 'w')))
5 local function filter(enum)
6 local n = enum.xarg.name
7 if n~=string.lower(n) and not string.match(n, '_') then
8 return false
9 end
10 return true
11 end
13 local enum_list = {}
15 local function copy_enums(index)
16 for e in pairs(index) do
17 if e.label=='Enum'
18 and not string.match(e.xarg.fullname, '%b<>') then
19 if e.xarg.access=='public' and not filter(e) then
20 enum_list[e.xarg.fullname] = e
21 elseif e.xarg.access == 'protected' then
22 -- register it anyway
23 enum_list[e.xarg.fullname] = e
24 local c = fullnames[e.xarg.context]
25 assert(type(c) == "table" and c.label == "Class", "cannot find parent of enum "..e.xarg.fullname)
26 if not c.protected_enums then c.protected_enums = {} end
27 table.insert(c.protected_enums, e)
28 end
29 end
30 end
31 end
33 local function fill_enum_values()
34 for _,e in pairs(enum_list) do
35 local values = {}
36 for _, v in ipairs(e) do
37 if v.label=='Enumerator' then
38 table.insert(values, v)
39 end
40 end
41 e.values = values
42 end
43 end
45 function fill_enum_tables()
46 for _,e in pairs(enum_list) do
47 local table = 'lqt_Enum lqt_enum'..e.xarg.id..'[] = {\n'
48 for _,v in pairs(e.values) do
49 table = table .. ' { "' .. v.xarg.name
50 .. '", static_cast<int>('..v.xarg.fullname..') },\n'
51 end
52 table = table .. ' { 0, 0 }\n'
53 table = table .. '};\n'
54 e.enum_table = table
55 end
56 end
58 function fill_typesystem(types)
59 local etype = function(en)
60 return {
61 push = function(n)
62 return 'lqtL_pushenum(L, '..n..', "'..string.gsub(en, '::', '.')..'")', 1
63 end,
64 get = function(n)
65 return 'static_cast<'..en..'>'
66 ..'(lqtL_toenum(L, '..n..', "'..string.gsub(en, '::', '.')..'"))', 1
67 end,
68 test = function(n)
69 return 'lqtL_isenum(L, '..n..', "'..string.gsub(en, '::', '.')..'")', 1
70 end,
71 onstack = string.gsub(en, '::', '.')..',',
73 end
74 for _,e in pairs(enum_list) do
75 if not types[e.xarg.fullname] then
76 types[e.xarg.fullname] = etype(e.xarg.fullname)
77 else
78 --io.stderr:write(e.xarg.fullname, ': already present\n')
79 end
80 end
81 end
84 function print_enum_tables()
85 for _,e in pairs(enum_list) do
86 if e.xarg.access == 'public' then print_enum('static ' .. e.enum_table) end
87 end
88 return enums
89 end
91 function print_enum_creator(mod)
92 local out = 'static lqt_Enumlist lqt_enum_list[] = {\n'
93 for _,e in pairs(enum_list) do
94 if e.xarg.access == 'public' then
95 out = out..' { lqt_enum'..e.xarg.id..', "'..string.gsub(e.xarg.fullname, "::", ".")..'" },\n'
96 end
97 end
98 out = out..' { 0, 0 },\n};\n'
99 out = out .. 'void lqt_create_enums_'..mod..' (lua_State *L) {\n'
100 out = out .. ' lqtL_createenumlist(L, lqt_enum_list); return;\n}\n'
101 print_enum(out)
104 ---------------------------------------------------------------------
106 function preprocess(index)
107 copy_enums(index)
110 function process(index, types)
111 fill_enum_values()
112 fill_enum_tables()
113 fill_typesystem(types)
116 function output()
117 print_enum(output_includes)
118 print_enum_tables()
119 print_enum_creator(module_name)