From 989da676227a031a1b9b841f3aa6f03283d81086 Mon Sep 17 00:00:00 2001 From: neko1990 Date: Tue, 9 Apr 2024 01:47:02 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20cpp-bin=20=E5=A4=9A=E7=BB=B4=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E6=97=B6=E4=B8=BA?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E6=B7=B1=E5=BA=A6=E4=BF=A1=E6=81=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TypeVisitors/ITypeFuncVisitor.cs | 33 ++++++++++++++ src/Luban.Core/Types/TArray.cs | 5 +++ src/Luban.Core/Types/TBean.cs | 5 +++ src/Luban.Core/Types/TBool.cs | 5 +++ src/Luban.Core/Types/TByte.cs | 5 +++ src/Luban.Core/Types/TDateTime.cs | 4 ++ src/Luban.Core/Types/TDouble.cs | 5 +++ src/Luban.Core/Types/TEnum.cs | 5 +++ src/Luban.Core/Types/TFloat.cs | 5 +++ src/Luban.Core/Types/TInt.cs | 5 +++ src/Luban.Core/Types/TList.cs | 5 +++ src/Luban.Core/Types/TLong.cs | 5 +++ src/Luban.Core/Types/TMap.cs | 5 +++ src/Luban.Core/Types/TSet.cs | 5 +++ src/Luban.Core/Types/TShort.cs | 5 +++ src/Luban.Core/Types/TString.cs | 5 ++- src/Luban.Core/Types/TType.cs | 2 + .../CppRawptrBinTemplateExtension.cs | 2 +- .../CppSharedptrBinTemplateExtension.cs | 2 +- .../CppRawptrDeserializeVisitor.cs | 8 ++-- .../CppSharedptrDeserializeVisitor.cs | 8 ++-- .../CppUnderlyingDeserializeVisitorBase.cs | 44 ++++++++++--------- 22 files changed, 142 insertions(+), 31 deletions(-) diff --git a/src/Luban.Core/TypeVisitors/ITypeFuncVisitor.cs b/src/Luban.Core/TypeVisitors/ITypeFuncVisitor.cs index 059b93aa..213f2451 100644 --- a/src/Luban.Core/TypeVisitors/ITypeFuncVisitor.cs +++ b/src/Luban.Core/TypeVisitors/ITypeFuncVisitor.cs @@ -133,3 +133,36 @@ public interface ITypeFuncVisitor TR Accept(TMap type, T x, T2 y, T3 z); } + +public interface ITypeFuncVisitor +{ + TR Accept(TBool type, T x, T2 y, T3 z, T4 w); + + TR Accept(TByte type, T x, T2 y, T3 z, T4 w); + + TR Accept(TShort type, T x, T2 y, T3 z, T4 w); + + TR Accept(TInt type, T x, T2 y, T3 z, T4 w); + + TR Accept(TLong type, T x, T2 y, T3 z, T4 w); + + TR Accept(TFloat type, T x, T2 y, T3 z, T4 w); + + TR Accept(TDouble type, T x, T2 y, T3 z, T4 w); + + TR Accept(TEnum type, T x, T2 y, T3 z, T4 w); + + TR Accept(TString type, T x, T2 y, T3 z, T4 w); + + TR Accept(TDateTime type, T x, T2 y, T3 z, T4 w); + + TR Accept(TBean type, T x, T2 y, T3 z, T4 w); + + TR Accept(TArray type, T x, T2 y, T3 z, T4 w); + + TR Accept(TList type, T x, T2 y, T3 z, T4 w); + + TR Accept(TSet type, T x, T2 y, T3 z, T4 w); + + TR Accept(TMap type, T x, T2 y, T3 z, T4 w); +} diff --git a/src/Luban.Core/Types/TArray.cs b/src/Luban.Core/Types/TArray.cs index c329dad9..7da90ab0 100644 --- a/src/Luban.Core/Types/TArray.cs +++ b/src/Luban.Core/Types/TArray.cs @@ -78,4 +78,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TBean.cs b/src/Luban.Core/Types/TBean.cs index 1b5bd186..2c881aa7 100644 --- a/src/Luban.Core/Types/TBean.cs +++ b/src/Luban.Core/Types/TBean.cs @@ -59,4 +59,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TBool.cs b/src/Luban.Core/Types/TBool.cs index ae208103..a96faab8 100644 --- a/src/Luban.Core/Types/TBool.cs +++ b/src/Luban.Core/Types/TBool.cs @@ -51,4 +51,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TByte.cs b/src/Luban.Core/Types/TByte.cs index 0b41a360..b474651f 100644 --- a/src/Luban.Core/Types/TByte.cs +++ b/src/Luban.Core/Types/TByte.cs @@ -49,4 +49,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TDateTime.cs b/src/Luban.Core/Types/TDateTime.cs index 7527a544..4132e635 100644 --- a/src/Luban.Core/Types/TDateTime.cs +++ b/src/Luban.Core/Types/TDateTime.cs @@ -50,4 +50,8 @@ public override TR Apply(ITypeFuncVisitor visito return visitor.Accept(this, x, y, z); } + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TDouble.cs b/src/Luban.Core/Types/TDouble.cs index fd0caa26..ed238d44 100644 --- a/src/Luban.Core/Types/TDouble.cs +++ b/src/Luban.Core/Types/TDouble.cs @@ -49,4 +49,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TEnum.cs b/src/Luban.Core/Types/TEnum.cs index bf6f24c5..f3e9bb6b 100644 --- a/src/Luban.Core/Types/TEnum.cs +++ b/src/Luban.Core/Types/TEnum.cs @@ -56,4 +56,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TFloat.cs b/src/Luban.Core/Types/TFloat.cs index b475f090..d1813bfa 100644 --- a/src/Luban.Core/Types/TFloat.cs +++ b/src/Luban.Core/Types/TFloat.cs @@ -51,4 +51,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TInt.cs b/src/Luban.Core/Types/TInt.cs index 5793088f..00b1bffc 100644 --- a/src/Luban.Core/Types/TInt.cs +++ b/src/Luban.Core/Types/TInt.cs @@ -49,4 +49,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TList.cs b/src/Luban.Core/Types/TList.cs index b6089da1..d3a1e7d0 100644 --- a/src/Luban.Core/Types/TList.cs +++ b/src/Luban.Core/Types/TList.cs @@ -68,4 +68,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TLong.cs b/src/Luban.Core/Types/TLong.cs index 3e249f35..879db13a 100644 --- a/src/Luban.Core/Types/TLong.cs +++ b/src/Luban.Core/Types/TLong.cs @@ -52,4 +52,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TMap.cs b/src/Luban.Core/Types/TMap.cs index a12790f0..d1af30cc 100644 --- a/src/Luban.Core/Types/TMap.cs +++ b/src/Luban.Core/Types/TMap.cs @@ -75,4 +75,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TSet.cs b/src/Luban.Core/Types/TSet.cs index 54743289..791a0b9f 100644 --- a/src/Luban.Core/Types/TSet.cs +++ b/src/Luban.Core/Types/TSet.cs @@ -68,4 +68,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TShort.cs b/src/Luban.Core/Types/TShort.cs index 971cd8ef..259254d8 100644 --- a/src/Luban.Core/Types/TShort.cs +++ b/src/Luban.Core/Types/TShort.cs @@ -49,4 +49,9 @@ public override TR Apply(ITypeFuncVisitor visito { return visitor.Accept(this, x, y, z); } + + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TString.cs b/src/Luban.Core/Types/TString.cs index 3edf88c6..9a36ce10 100644 --- a/src/Luban.Core/Types/TString.cs +++ b/src/Luban.Core/Types/TString.cs @@ -51,5 +51,8 @@ public override TR Apply(ITypeFuncVisitor visito return visitor.Accept(this, x, y, z); } - + public override TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w) + { + return visitor.Accept(this, x, y, z, w); + } } diff --git a/src/Luban.Core/Types/TType.cs b/src/Luban.Core/Types/TType.cs index 90721887..b25f8b44 100644 --- a/src/Luban.Core/Types/TType.cs +++ b/src/Luban.Core/Types/TType.cs @@ -59,4 +59,6 @@ public virtual void PostCompile(DefField field) public abstract TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y); public abstract TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z); + + public abstract TR Apply(ITypeFuncVisitor visitor, T1 x, T2 y, T3 z, T4 w); } diff --git a/src/Luban.Cpp/TemplateExtensions/CppRawptrBinTemplateExtension.cs b/src/Luban.Cpp/TemplateExtensions/CppRawptrBinTemplateExtension.cs index d622ebcd..c76acabf 100644 --- a/src/Luban.Cpp/TemplateExtensions/CppRawptrBinTemplateExtension.cs +++ b/src/Luban.Cpp/TemplateExtensions/CppRawptrBinTemplateExtension.cs @@ -8,7 +8,7 @@ public class CppRawptrBinTemplateExtension : ScriptObject { public static string Deserialize(string bufName, string fieldName, TType type) { - return type.Apply(CppRawptrDeserializeVisitor.Ins, bufName, fieldName); + return type.Apply(CppRawptrDeserializeVisitor.Ins, bufName, fieldName, 0); } public static string DeclaringTypeName(TType type) diff --git a/src/Luban.Cpp/TemplateExtensions/CppSharedptrBinTemplateExtension.cs b/src/Luban.Cpp/TemplateExtensions/CppSharedptrBinTemplateExtension.cs index 0ce46927..f6498892 100644 --- a/src/Luban.Cpp/TemplateExtensions/CppSharedptrBinTemplateExtension.cs +++ b/src/Luban.Cpp/TemplateExtensions/CppSharedptrBinTemplateExtension.cs @@ -8,7 +8,7 @@ public class CppSharedptrBinTemplateExtension : ScriptObject { public static string Deserialize(string bufName, string fieldName, TType type) { - return type.Apply(CppSharedptrDeserializeVisitor.Ins, bufName, fieldName); + return type.Apply(CppSharedptrDeserializeVisitor.Ins, bufName, fieldName,0); } public static string DeclaringTypeName(TType type) diff --git a/src/Luban.Cpp/TypeVisitors/CppRawptrDeserializeVisitor.cs b/src/Luban.Cpp/TypeVisitors/CppRawptrDeserializeVisitor.cs index 0cb6e311..8f7f995b 100644 --- a/src/Luban.Cpp/TypeVisitors/CppRawptrDeserializeVisitor.cs +++ b/src/Luban.Cpp/TypeVisitors/CppRawptrDeserializeVisitor.cs @@ -3,19 +3,19 @@ namespace Luban.Cpp.TypeVisitors; -public class CppRawptrDeserializeVisitor : DecoratorFuncVisitor +public class CppRawptrDeserializeVisitor : DecoratorFuncVisitor { public static CppRawptrDeserializeVisitor Ins { get; } = new CppRawptrDeserializeVisitor(); - public override string DoAccept(TType type, string bufName, string fieldName) + public override string DoAccept(TType type, string bufName, string fieldName, int depth) { if (type.IsNullable) { - return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{ {fieldName} = {(type.IsBean ? "nullptr" : $"new {type.Apply(CppUnderlyingDeclaringTypeNameVisitor.Ins)}{{}}")}; {type.Apply(CppRawptrUnderlyingDeserializeVisitor.Ins, bufName, type.IsBean ? fieldName : $"*{fieldName}", CppRawptrDeclaringTypeNameVisitor.Ins)} }} else {{ {fieldName} = nullptr; }} }}"; + return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{ {fieldName} = {(type.IsBean ? "nullptr" : $"new {type.Apply(CppUnderlyingDeclaringTypeNameVisitor.Ins)}{{}}")}; {type.Apply(CppRawptrUnderlyingDeserializeVisitor.Ins, bufName, type.IsBean ? fieldName : $"*{fieldName}",depth + 1, CppRawptrDeclaringTypeNameVisitor.Ins)} }} else {{ {fieldName} = nullptr; }} }}"; } else { - return type.Apply(CppRawptrUnderlyingDeserializeVisitor.Ins, bufName, fieldName, CppRawptrDeclaringTypeNameVisitor.Ins); + return type.Apply(CppRawptrUnderlyingDeserializeVisitor.Ins, bufName, fieldName, depth, CppRawptrDeclaringTypeNameVisitor.Ins); } } } diff --git a/src/Luban.Cpp/TypeVisitors/CppSharedptrDeserializeVisitor.cs b/src/Luban.Cpp/TypeVisitors/CppSharedptrDeserializeVisitor.cs index 7fdf2410..83139ee8 100644 --- a/src/Luban.Cpp/TypeVisitors/CppSharedptrDeserializeVisitor.cs +++ b/src/Luban.Cpp/TypeVisitors/CppSharedptrDeserializeVisitor.cs @@ -3,19 +3,19 @@ namespace Luban.Cpp.TypeVisitors; -public class CppSharedptrDeserializeVisitor : DecoratorFuncVisitor +public class CppSharedptrDeserializeVisitor : DecoratorFuncVisitor { public static CppSharedptrDeserializeVisitor Ins { get; } = new CppSharedptrDeserializeVisitor(); - public override string DoAccept(TType type, string bufName, string fieldName) + public override string DoAccept(TType type, string bufName, string fieldName, int depth) { if (type.IsNullable) { - return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{ {fieldName}.reset({(type.IsBean ? "" : $"new {type.Apply(CppUnderlyingDeclaringTypeNameVisitor.Ins)}()")}); {type.Apply(CppSharedptrUnderlyingDeserializeVisitor.Ins, bufName, $"{(type.IsBean ? "" : "*")}{fieldName}", CppSharedptrDeclaringTypeNameVisitor.Ins)} }} else {{ {fieldName}.reset(); }} }}"; + return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{ {fieldName}.reset({(type.IsBean ? "" : $"new {type.Apply(CppUnderlyingDeclaringTypeNameVisitor.Ins)}()")}); {type.Apply(CppSharedptrUnderlyingDeserializeVisitor.Ins, bufName, $"{(type.IsBean ? "" : "*")}{fieldName}",depth+1, CppSharedptrDeclaringTypeNameVisitor.Ins)} }} else {{ {fieldName}.reset(); }} }}"; } else { - return type.Apply(CppSharedptrUnderlyingDeserializeVisitor.Ins, bufName, fieldName, CppSharedptrDeclaringTypeNameVisitor.Ins); + return type.Apply(CppSharedptrUnderlyingDeserializeVisitor.Ins, bufName, fieldName, depth ,CppSharedptrDeclaringTypeNameVisitor.Ins); } } } diff --git a/src/Luban.Cpp/TypeVisitors/CppUnderlyingDeserializeVisitorBase.cs b/src/Luban.Cpp/TypeVisitors/CppUnderlyingDeserializeVisitorBase.cs index a14228ec..0e9622d9 100644 --- a/src/Luban.Cpp/TypeVisitors/CppUnderlyingDeserializeVisitorBase.cs +++ b/src/Luban.Cpp/TypeVisitors/CppUnderlyingDeserializeVisitorBase.cs @@ -4,80 +4,84 @@ namespace Luban.Cpp.TypeVisitors; -public abstract class CppUnderlyingDeserializeVisitorBase : ITypeFuncVisitor, string> +public abstract class CppUnderlyingDeserializeVisitorBase : ITypeFuncVisitor, string> { - public string Accept(TBool type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TBool type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if (!{bufName}.readBool({fieldName})) return false;"; } - public string Accept(TByte type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TByte type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readByte({fieldName})) return false;"; } - public string Accept(TShort type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TShort type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readShort({fieldName})) return false;"; } - public string Accept(TInt type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TInt type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readInt({fieldName})) return false;"; } - public string Accept(TLong type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TLong type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readLong({fieldName})) return false;"; } - public string Accept(TFloat type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TFloat type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readFloat({fieldName})) return false;"; } - public string Accept(TDouble type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TDouble type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readDouble({fieldName})) return false;"; } - public string Accept(TEnum type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TEnum type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"{{int __enum_temp__; if(!{bufName}.readInt(__enum_temp__)) return false; {fieldName} = {CppTemplateExtension.MakeTypeCppName(type.DefEnum)}(__enum_temp__); }}"; } - public string Accept(TString type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TString type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readString({fieldName})) return false;"; } - public string Accept(TBean type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TBean type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{CppTemplateExtension.MakeTypeCppName(type.DefBean)}::deserialize{type.DefBean.Name}({bufName}, {fieldName})) return false;"; } - public string Accept(TDateTime type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TDateTime type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { return $"if(!{bufName}.readLong({fieldName})) return false;"; } - public string Accept(TArray type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TArray type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { - return $"{{::luban::int32 n; if(!{bufName}.readSize(n)) return false; n = std::min(n, ::luban::int32({bufName}.size()));{fieldName}.reserve(n);for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(typeVisitor)} _e; {type.ElementType.Apply(this, bufName, "_e", typeVisitor)} {fieldName}.push_back(_e);}}}}"; + var suffix = depth == 0 ? "" : $"_{depth}"; + return $"{{::luban::int32 n{suffix}; if(!{bufName}.readSize(n{suffix})) return false; n{suffix} = std::min(n{suffix}, ::luban::int32({bufName}.size())); {fieldName}.reserve(n{suffix});for(int i{suffix} = 0 ; i{suffix} < n{suffix} ; i{suffix}++) {{ {type.ElementType.Apply(typeVisitor)} _e{suffix}; {type.ElementType.Apply(this, bufName, $"_e{suffix}", depth + 1, typeVisitor)} {fieldName}.push_back(_e{suffix});}}}}"; } - public string Accept(TList type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TList type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { - return $"{{::luban::int32 n; if(!{bufName}.readSize(n)) return false; n = std::min(n, ::luban::int32({bufName}.size())); {fieldName}.reserve(n);for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(typeVisitor)} _e; {type.ElementType.Apply(this, bufName, "_e", typeVisitor)} {fieldName}.push_back(_e);}}}}"; + var suffix = depth == 0 ? "" : $"_{depth}"; + return $"{{::luban::int32 n{suffix}; if(!{bufName}.readSize(n{suffix})) return false; n{suffix} = std::min(n{suffix}, ::luban::int32({bufName}.size())); {fieldName}.reserve(n{suffix});for(int i{suffix} = 0 ; i{suffix} < n{suffix} ; i{suffix}++) {{ {type.ElementType.Apply(typeVisitor)} _e{suffix}; {type.ElementType.Apply(this, bufName, $"_e{suffix}", depth + 1, typeVisitor)} {fieldName}.push_back(_e{suffix});}}}}"; } - public string Accept(TSet type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TSet type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { - return $"{{::luban::int32 n; if(!{bufName}.readSize(n)) return false; n = std::min(n, ::luban::int32({bufName}.size())); {fieldName}.reserve(n * 3 / 2);for(int i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(typeVisitor)} _e; {type.ElementType.Apply(this, bufName, "_e", typeVisitor)} {fieldName}.insert(_e);}}}}"; + var suffix = depth == 0 ? "" : $"_{depth}"; + return $"{{::luban::int32 n{suffix}; if(!{bufName}.readSize(n{suffix})) return false; n{suffix} = std::min(n{suffix}, ::luban::int32({bufName}.size())); {fieldName}.reserve(n{suffix} * 3 / 2);for(int i{suffix} = 0 ; i{suffix} < n{suffix} ; i{suffix}++) {{ {type.ElementType.Apply(typeVisitor)} _e{suffix}; {type.ElementType.Apply(this, bufName, $"_e{suffix}", depth + 1, typeVisitor)} {fieldName}.insert(_e{suffix});}}}}"; } - public string Accept(TMap type, string bufName, string fieldName, ITypeFuncVisitor typeVisitor) + public string Accept(TMap type, string bufName, string fieldName, int depth, ITypeFuncVisitor typeVisitor) { - return $"{{::luban::int32 n; if(!{bufName}.readSize(n)) return false; n = std::min(n, (::luban::int32){bufName}.size()); {fieldName}.reserve(n * 3 / 2);for(int i = 0 ; i < n ; i++) {{ {type.KeyType.Apply(typeVisitor)} _k; {type.KeyType.Apply(this, bufName, "_k", typeVisitor)} {type.ValueType.Apply(typeVisitor)} _v; {type.ValueType.Apply(this, bufName, "_v", typeVisitor)} {fieldName}[_k] = _v;}}}}"; + var suffix = depth == 0 ? "" : $"_{depth}"; + return $"{{::luban::int32 n{suffix}; if(!{bufName}.readSize(n{suffix})) return false; n{suffix} = std::min(n{suffix}, (::luban::int32){bufName}.size()); {fieldName}.reserve(n{suffix} * 3 / 2);for(int i{suffix} = 0 ; i{suffix} < n{suffix} ; i{suffix}++) {{ {type.KeyType.Apply(typeVisitor)} _k{suffix}; {type.KeyType.Apply(this, bufName, $"_k{suffix}", depth + 1, typeVisitor)} {type.ValueType.Apply(typeVisitor)} _v{suffix}; {type.ValueType.Apply(this, bufName, $"_v{suffix}",depth + 1, typeVisitor)} {fieldName}[_k{suffix}] = _v{suffix};}}}}"; } }