add ImVector template types

This commit is contained in:
sonoro1234
2018-10-18 13:05:57 +02:00
parent 7a54d1276d
commit aca412fe7a
4 changed files with 168 additions and 89 deletions

View File

@@ -418,11 +418,15 @@ local function struct_parser()
--nothing
else
local linea = line:gsub("%S+",{class="struct",mutable=""})
local template = linea:match("ImVector<([%w_]+)>")
if template then
ImVector_templates[template] = true
end
linea = linea:gsub("(%b<>)","/*%1*/") --comment template parameters
local template = linea:match("ImVector<(.+)>")
if template then
local te = template:gsub("%s","_")
te = te:gsub("%*","Ptr")
ImVector_templates[template] = true
linea = linea:gsub("(%b<>)","_"..te) --comment template parameters
end
--linea = linea:gsub("(%b<>)","/*%1*/") --comment template parameters
--linea = linea:gsub("<([%w_]+)>","_%1") --ImVector expand templates
table.insert(structcdefs,linea..comment)
end
return
@@ -602,8 +606,10 @@ local function func_parser()
-- end
--argscsinpars = argscsinpars:gsub("&","")
local template = argscsinpars:match("ImVector<([%w_]+)>")
local template = argscsinpars:match("ImVector<(.+)>")
if template then
--template = template:gsub("%s","_")
--template = template:gsub("%*","Ptr")
ImVector_templates[template] = true
end
@@ -929,8 +935,14 @@ local function gen_structs_and_enums_table(cdefs)
end
--split type name1,name2; in several lines
local typen,rest = line:match("([^,]+)%s(%S+[,;])")
local template_type = typen:match("/%*<(.+)>%*/")
if template_type then typen = typen:match("(.+)/%*") end
--local template_type = typen:match("/%*<(.+)>%*/")
--if template_type then typen = typen:match("(.+)/%*") end
local template_type = typen:match("ImVector_(.+)")
if template_type then
typen = "ImVector"
template_type = template_type:gsub("_"," ")
template_type = template_type:gsub("Ptr","%*")
end
for name in rest:gmatch("([^%s,;]+)%s?[,;]") do
table.insert(outtab.structs[structnames[#structnames]],{type=typen,template_type=template_type,name=name})
end
@@ -985,8 +997,18 @@ local function gen_structs_and_enums_table(cdefs)
return outtab, typedefs_dict
end
local function generate_templates(code,templates,typedefs)
for k,v in pairs(templates) do
--[[typedef struct ImVector{int Size;int Capacity;void* Data;} ImVector;]]
local te = k:gsub("%s","_")
te = te:gsub("%*","Ptr")
--table.insert(code,"typedef struct ImVector_"..te.." {int Size;int Capacity;"..k.."* Data;} ImVector_"..te..";\n")
table.insert(code,"struct ImVector_"..te.." {int Size;int Capacity;"..k.."* Data;} ImVector_"..te..";\n")
table.insert(typedefs,"typedef struct ImVector_"..te.." ImVector_"..te..";\n")
end
end
local function gen_structs_and_enums(cdefs)
local function gen_structs_and_enums(cdefs,templates)
local function_closing_re = "}"
local namespace_re = "namespace"
local in_namespace = false
@@ -998,7 +1020,7 @@ local function gen_structs_and_enums(cdefs)
local innerstructs = {}
local typedefs_table = {}
local typedefs_dict = {}
local linetypedefs = 1 --math.huge
local outtab = {}
-- Output the file
--table.insert(outtab,"/////////////// BEGIN AUTOGENERATED SEGMENT\n")
@@ -1027,15 +1049,8 @@ local function gen_structs_and_enums(cdefs)
-- ImVector special treatment
if structnames[#structnames] == "ImVector" then
if line:match(struct_closing_re) then
table.insert(outtab,[[
struct ImVector
{
int Size;
int Capacity;
void* Data;
};
typedef struct ImVector ImVector;
]])
table.insert(outtab,[[typedef struct ImVector{int Size;int Capacity;void* Data;} ImVector;]].."\n")
generate_templates(outtab,templates,typedefs_table)
structnames[#structnames] = nil
end
break -- dont write
@@ -1049,9 +1064,14 @@ typedef struct ImVector ImVector;
end
if #structnames < 2 then -- not inner
if line:match("typedef") and line:match("ImDrawIdx") then --save typedefs of ImDrawIdx
table.insert(typedefs_table,line..";\n")
--linetypedefs = math.min(linetypedefs,#outtab)
break
end
if (#structnames > 0) then
if line:match("typedef") then --dont allow inner typedefs
break
break --already saved
elseif not line:match("^{$") and not line:match(struct_closing_re) then --avoid tab { and };
--line = " "..line
end
@@ -1060,6 +1080,7 @@ typedef struct ImVector ImVector;
local struct_closed_name = line:match(struct_closed_re)
if struct_closed_name then
table.insert(typedefs_table,"typedef struct "..struct_closed_name.." "..struct_closed_name..";\n")
--linetypedefs = math.min(linetypedefs,#outtab)
typedefs_dict[struct_closed_name] = "struct "..struct_closed_name
end
end
@@ -1076,6 +1097,7 @@ typedef struct ImVector ImVector;
local structname = structnames[#structnames]
--st[#st + 1] = string.format("typedef struct %s %s;\n",structname,structname)
table.insert(typedefs_table,string.format("typedef struct %s %s;\n",structname,structname))
--linetypedefs = math.min(linetypedefs,#outtab)
typedefs_dict[structname] = "struct "..structname
structnames[#structnames] = nil
end
@@ -1083,6 +1105,7 @@ typedef struct ImVector ImVector;
local structname = structnames[#structnames]
--table.insert(outtab,"typedef struct "..structname.." "..structname..";\n")
table.insert(typedefs_table,"typedef struct "..structname.." "..structname..";\n")
--linetypedefs = math.min(linetypedefs,#outtab)
typedefs_dict[structname] = "struct "..structname
structnames[#structnames] = nil
end
@@ -1102,7 +1125,7 @@ typedef struct ImVector ImVector;
for i,l in ipairs(typedefs_table) do
if not uniques[l] then
uniques[l] = true
table.insert(outtab,2,l)
table.insert(outtab,linetypedefs,l)
end
end
local cstructsstr = table.concat(outtab)
@@ -1145,10 +1168,10 @@ local function func_header_generate(FP)
for k,v in pairs(FP.ImVector_templates) do
table.insert(outtab,"typedef ImVector<"..k.."> ImVector_"..k..";\n")
end
table.insert(outtab,"#else //CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
for k,v in pairs(FP.ImVector_templates) do
table.insert(outtab,"typedef ImVector ImVector_"..k..";\n")
end
-- table.insert(outtab,"#else //CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
-- for k,v in pairs(FP.ImVector_templates) do
-- table.insert(outtab,"typedef ImVector ImVector_"..k..";\n")
-- end
table.insert(outtab,"#endif //CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
for _,t in ipairs(FP.cdefs) do
if t.cimguiname then
@@ -1389,11 +1412,13 @@ local function set_defines(fdefs)
end
--generate cimgui.cpp cimgui.h and auto versions depending on postfix
local function cimgui_generation(postfix,STP,FP)
--get all ImVector templates
local ImVector_templates = mergeT(STP.ImVector_templates,FP.ImVector_templates)
--merge it in cimgui_template.h to cimgui.h
local hfile = io.open("./cimgui_template.h","r")
local hstrfile = hfile:read"*a"
hfile:close()
local cstructsstr,typedefs_dict = gen_structs_and_enums(STP.lines)
local cstructsstr,typedefs_dict = gen_structs_and_enums(STP.lines,ImVector_templates)
--for not gcc parsing
if postfix == "_nopreprocess" then
cstructsstr = "typedef unsigned short ImDrawIdx;\ntypedef void* ImTextureID;\n"..cstructsstr