static void GeneratePooled(ProtoMessage m, CodeWriter cw, Options options) { cw.WriteLine("#region [Methods] Pooled"); cw.WriteLine("private bool _disposed;"); cw.WriteLine("public bool ShouldPool = true;"); cw.WriteLine(); #region [Method] Dispose cw.Bracket("public virtual void Dispose()"); cw.WriteLine("if (this._disposed)"); cw.WriteIndent("return;"); cw.WriteLine("this.ResetToPool();"); cw.WriteLine("this._disposed = true;"); cw.EndBracketSpace(); #endregion #region [Method] ResetToPool cw.Bracket("public void ResetToPool()"); cw.WriteLine("ResetToPool(this);"); cw.EndBracketSpace(); #endregion #region [Method] [Static] ResetToPool cw.Bracket($"public static void ResetToPool({m.CsType} instance)"); cw.WriteLine("if (!instance.ShouldPool)"); cw.WriteIndent("return;"); string GetFieldType(Field field) { string csType = field.ProtoType.FullCsType; if (field.OptionCodeType != null) { csType = field.OptionCodeType; } return(csType); } // Generate Default Structs List <string> defStructs = new List <string>(); foreach (var field in m.Fields.Values) { string csFullType = GetFieldType(field); string csType = field.ProtoType.CsType; string fName = field.CsName; cw.Comment($"[{csType}] {fName}"); // Generate Default Structs if (field.Rule != FieldRule.Repeated && field.ProtoType.OptionType == "struct" && string.IsNullOrEmpty(field.OptionDefault) && defStructs.Contains(csFullType) == false) { cw.WriteLine($"{csFullType} def{csType} = new {csFullType}();"); defStructs.Add(csFullType); } if (field.Rule == FieldRule.Repeated) { cw.Bracket($"if (instance.{field.CsName} != null)"); if (field.ProtoType is ProtoMessage) { cw.ForBracket($"int i = 0; i < instance.{fName}.Count; i++"); cw.Bracket($"if (instance.{fName}[i] != null)"); cw.WriteLine($"instance.{fName}[i].ResetToPool();"); cw.WriteLine($"instance.{fName}[i] = null;"); cw.EndBracket(); cw.EndBracket(); } cw.WriteLine($"List<{csFullType}> ins{fName} = instance.{fName};"); cw.WriteLine($"Pool.FreeList<{csFullType}>(ref ins{fName});"); cw.WriteLine($"instance.{fName} = ins{fName};"); cw.EndBracket(); } else { if (field.ProtoType is ProtoMessage && field.ProtoType.OptionType != "struct") { cw.Bracket($"if (instance.{fName} != null)"); cw.WriteLine($"instance.{fName}.ResetToPool();"); cw.WriteLine($"instance.{fName} = null;"); cw.EndBracket(); } else { if (string.IsNullOrEmpty(field.OptionDefault) == false) { cw.WriteLine($"instance.{fName} = {field.FormatForTypeAssignment()};"); } else if (field.ProtoType.OptionType == "struct") { cw.WriteLine($"instance.{fName} = def{csType};"); } else { cw.WriteLine($"instance.{fName} = default({csFullType});"); } } } cw.WriteLine(); } defStructs.Clear(); cw.WriteLine($"Pool.Free<{m.FullCsType}>(ref instance);"); cw.EndBracketSpace(); #endregion #region [Method] EnterPool cw.Bracket("public virtual void EnterPool()"); cw.WriteLine("this._disposed = true;"); cw.EndBracketSpace(); #endregion #region [Method] LeavePool cw.Bracket("public virtual void LeavePool()"); cw.WriteLine("this._disposed = false;"); cw.EndBracketSpace(); #endregion cw.WriteLine("#endregion"); }