Skip to content

Commit c47b585

Browse files
committed
add visitor name
1 parent 7d215f6 commit c47b585

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/tool/json2cpp2/generate.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,14 @@ namespace j2cp2 {
10041004
}
10051005
}
10061006

1007-
void write_visit_struct(const std::shared_ptr<ast::StructType>& s) {
1007+
void write_visit_struct(std::string_view struct_name, const std::shared_ptr<ast::StructType>& s) {
10081008
if (!add_visit) {
10091009
return;
10101010
}
1011+
w.writeln("constexpr static const char* visitor_name = \"", struct_name, "\";");
10111012
w.writeln("template<typename Visitor>");
10121013
w.writeln("void visit(Visitor&& v) {");
1013-
{
1014+
auto write_body = [&] {
10141015
auto indent = w.indent_scope();
10151016
for (auto& f : s->fields) {
10161017
if (auto field = ast::as<ast::Field>(f); field) {
@@ -1025,7 +1026,12 @@ namespace j2cp2 {
10251026
w.writeln("v(v, \"", field->ident->ident, "\",", ident, ");");
10261027
}
10271028
}
1028-
}
1029+
};
1030+
write_body();
1031+
w.writeln("}");
1032+
w.writeln("template<typename Visitor>");
1033+
w.writeln("void visit(Visitor&& v) const {");
1034+
write_body();
10291035
w.writeln("}");
10301036
}
10311037

@@ -1109,7 +1115,7 @@ namespace j2cp2 {
11091115
if (s->fixed_header_size) {
11101116
w.writeln("static constexpr size_t fixed_header_size = ", brgen::nums(s->fixed_header_size / 8), ";");
11111117
}
1112-
write_visit_struct(s);
1118+
write_visit_struct(struct_name, s);
11131119
}
11141120
}
11151121
w.write("}");

0 commit comments

Comments
 (0)