cpp2ffi: genConversion take care of ptr to nonPOD fields, avoid infinite recursion on gen_structs_c

This commit is contained in:
sonoro1234
2025-11-10 13:53:03 +01:00
parent 794f63b480
commit c4b7680882
4 changed files with 16 additions and 9 deletions

View File

@@ -3818,7 +3818,6 @@ struct SDL_Window;
typedef union SDL_Event SDL_Event; typedef union SDL_Event SDL_Event;
#endif // CIMGUI_DEFINE_ENUMS_AND_STRUCTS #endif // CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#ifdef CIMGUI_DEFINE_ENUMS_AND_STRUCTS #ifdef CIMGUI_DEFINE_ENUMS_AND_STRUCTS
typedef struct ImTextureRef_c ImTextureRef; typedef struct ImTextureRef_c ImTextureRef;
typedef struct ImVec2_c ImVec2; typedef struct ImVec2_c ImVec2;
@@ -3860,6 +3859,8 @@ struct ImRect_c {
ImVec2_c Max; ImVec2_c Max;
}; };
#endif #endif
#ifndef CIMGUI_DEFINE_ENUMS_AND_STRUCTS #ifndef CIMGUI_DEFINE_ENUMS_AND_STRUCTS
typedef struct ImGuiTextFilter::ImGuiTextRange ImGuiTextRange; typedef struct ImGuiTextFilter::ImGuiTextRange ImGuiTextRange;
typedef ImStb::STB_TexteditState STB_TexteditState; typedef ImStb::STB_TexteditState STB_TexteditState;

View File

@@ -49,6 +49,7 @@ struct SDL_Window;
typedef union SDL_Event SDL_Event; typedef union SDL_Event SDL_Event;
#endif // CIMGUI_DEFINE_ENUMS_AND_STRUCTS #endif // CIMGUI_DEFINE_ENUMS_AND_STRUCTS
PLACE_STRUCTS_C
#include "auto_funcs.h" #include "auto_funcs.h"

View File

@@ -1104,12 +1104,13 @@ local function get_nonPOD(FP)
return nonPOD return nonPOD
end end
local function recur_calc_depth(FP, structs, k,n) local function recur_calc_depth(FP, structs, k,n)
--print("recur_calc_depth",k,n)
local struct = structs[k] local struct = structs[k]
local n1 = n local n1 = n
for i,field in ipairs(struct) do for i,field in ipairs(struct) do
local typ = field.type:gsub("const ","") local typ = field.type:gsub("const ","")
typ = typ:gsub("*","") typ = typ:gsub("*","")
if FP.nP_used[typ] then if k~=typ and FP.nP_used[typ] then
n1 = math.max(n1,recur_calc_depth(FP, structs, typ,n+1)) n1 = math.max(n1,recur_calc_depth(FP, structs, typ,n+1))
end end
end end
@@ -1163,12 +1164,16 @@ local function gen_structs_c(FP)
return table.concat(tabs_c,"\n").."\n"..table.concat(tabs,"\n") return table.concat(tabs_c,"\n").."\n"..table.concat(tabs,"\n")
--return table.concat(tabs,"\n") --return table.concat(tabs,"\n")
end end
local function gen_field_conversion(tab, struct, FP, prefix) local function gen_field_conversion(tab, struct, FP, to,prefix)
prefix = prefix or "" prefix = prefix or ""
local structs = FP.structs_and_enums_table.structs local structs = FP.structs_and_enums_table.structs
for i,field in ipairs(struct) do for i,field in ipairs(struct) do
local ftype = field.type:gsub("*","")
if FP.nP_used[field.type] then if FP.nP_used[field.type] then
gen_field_conversion(tab, structs[field.type],FP, prefix..field.name..".") gen_field_conversion(tab, structs[field.type],FP, to,prefix..field.name..".")
elseif FP.nP_used[ftype] then
local ftypec = field.type:gsub(ftype,not to and (ftype.."_c") or ftype)
insert(tab, " dest."..prefix..field.name.." = reinterpret_cast<"..ftypec..">(src."..prefix..field.name..");")
else else
insert(tab," dest."..prefix..field.name.." = src."..prefix..field.name..";") insert(tab," dest."..prefix..field.name.." = src."..prefix..field.name..";")
end end
@@ -1183,14 +1188,14 @@ local function genConversions(FP)
insert(convers,"{") insert(convers,"{")
insert(convers," "..k.." dest;") insert(convers," "..k.." dest;")
local struct = structs[k] local struct = structs[k]
gen_field_conversion(convers,struct,FP) gen_field_conversion(convers,struct,FP, true)
insert(convers," return dest;") insert(convers," return dest;")
insert(convers,"}") insert(convers,"}")
insert(convers,"static inline "..k.."_c ConvertFromCPP_"..k.."(const "..k.."& src)") insert(convers,"static inline "..k.."_c ConvertFromCPP_"..k.."(const "..k.."& src)")
insert(convers,"{") insert(convers,"{")
insert(convers," "..k.."_c dest;") insert(convers," "..k.."_c dest;")
local struct = structs[k] local struct = structs[k]
gen_field_conversion(convers,struct,FP) gen_field_conversion(convers,struct,FP, false)
insert(convers," return dest;") insert(convers," return dest;")
insert(convers,"}") insert(convers,"}")
end) end)
@@ -3128,8 +3133,8 @@ local function func_header_generate(FP)
--outtabf = M.header_subs_nonPOD(FP,outtabf) --outtabf = M.header_subs_nonPOD(FP,outtabf)
local cfuncsstr = table.concat(outtab)..outtabf local cfuncsstr = table.concat(outtab)..outtabf
cfuncsstr = cfuncsstr:gsub("\n+","\n") --several empty lines to one empty line cfuncsstr = cfuncsstr:gsub("\n+","\n") --several empty lines to one empty line
local structs_c = FP:gen_structs_c()
return structs_c..cfuncsstr return cfuncsstr
end end
M.func_header_generate = func_header_generate M.func_header_generate = func_header_generate

View File

@@ -286,7 +286,6 @@ local function cimgui_generation(parser)
local tdt = parser:generate_templates() local tdt = parser:generate_templates()
--local cstructsstr = "\n//7777estio es outpre\n"..outpre.."\n///////////////tdt\n"..tdt.."\n////////////////////outpost\n"..outpost
local cstructsstr = outpre..tdt..outpost local cstructsstr = outpre..tdt..outpost
if gdefines.IMGUI_HAS_DOCK then if gdefines.IMGUI_HAS_DOCK then
@@ -307,6 +306,7 @@ local function cimgui_generation(parser)
cstructsstr = colapse_defines(cstructsstr, "IMGUI_ENABLE_FREETYPE") cstructsstr = colapse_defines(cstructsstr, "IMGUI_ENABLE_FREETYPE")
hstrfile = hstrfile:gsub([[#include "imgui_structs%.h"]],cstructsstr) hstrfile = hstrfile:gsub([[#include "imgui_structs%.h"]],cstructsstr)
hstrfile = hstrfile:gsub([[PLACE_STRUCTS_C]],parser:gen_structs_c())
local cfuncsstr = func_header_generate(parser) local cfuncsstr = func_header_generate(parser)
cfuncsstr = colapse_defines(cfuncsstr, "IMGUI_ENABLE_FREETYPE") cfuncsstr = colapse_defines(cfuncsstr, "IMGUI_ENABLE_FREETYPE")
hstrfile = hstrfile:gsub([[#include "auto_funcs%.h"]],cfuncsstr) hstrfile = hstrfile:gsub([[#include "auto_funcs%.h"]],cfuncsstr)