public System.CodeDom.CodeExpression GetWriterExpression(CodeExpression writer, CodeExpression obj) { if (OnlyCalculated) { return(CodeGen.GetResolve(WriteExpression, obj, writer)); } else { obj = CodeGen.GetCalc(WriteExpression, BaseEntry.GetTypeReference(), CodeGen.GetRef(obj)); return(_memberEntry.GetWriterExpression(writer, obj)); } }
public override System.CodeDom.CodeMemberMethod GetSerializerMethod() { CodeMemberMethod method = base.GetSerializerMethod(); IMemberReaderWriter entry = BaseEntry as IMemberReaderWriter; List <CodeStatement> loopStatements = new List <CodeStatement>(); loopStatements.Add(CodeGen.GetStatement( entry.GetWriterExpression(CodeGen.GetWriter(), new CodeIndexerExpression(CodeGen.GetObject(), CodeGen.GetVariable("i"))))); method.Statements.Add(CodeGen.GetForLoop("i", 0, CodeGen.GetProperty(CodeGen.GetObject(), "Length"), 1, loopStatements.ToArray())); return(method); }
public override System.CodeDom.CodeMemberMethod GetSerializerMethod() { CodeMemberMethod method = base.GetSerializerMethod(); // No point doing anything if there is no length to write if (_reference.IsValid()) { IMemberReaderWriter entry = BaseEntry as IMemberReaderWriter; List <CodeStatement> loopStatements = new List <CodeStatement>(); loopStatements.Add(CodeGen.GetStatement(entry.GetWriterExpression(CodeGen.GetWriter(), CodeGen.GetIndex(CodeGen.GetObject(), CodeGen.GetVariable("i"))))); method.Statements.Add(CodeGen.GetForLoop("i", 0, CodeGen.GetProperty(CodeGen.GetObject(), "Length"), 1, loopStatements.ToArray())); } return(method); }
public override System.CodeDom.CodeMemberMethod GetPreSerializeMethod() { CodeMemberMethod method = base.GetPreSerializeMethod(); if (_reference.IsValid()) { CodeExpression right; if (_isByteLength) { // Should really save this out and reuse when it comes to actually writing the data, seems a waste method.Statements.Add(CodeGen.MakeVariable(typeof(DataWriter), "writer", CodeGen.CreateObject(typeof(DataWriter)))); IMemberReaderWriter entry = BaseEntry as IMemberReaderWriter; List <CodeStatement> loopStatements = new List <CodeStatement>(); loopStatements.Add(CodeGen.GetStatement(entry.GetWriterExpression(CodeGen.GetWriter(), CodeGen.GetIndex(CodeGen.GetThisField(Name), CodeGen.GetVariable("i"))))); method.Statements.Add(CodeGen.GetForLoop("i", 0, CodeGen.GetProperty(CodeGen.GetThisField(Name), "Length"), 1, loopStatements.ToArray())); right = CodeGen.GetProperty(CodeGen.GetVariable("writer"), "BytesWritten"); } else { right = CodeGen.GetProperty(CodeGen.GetThisField(Name), "Length"); } if (_adjustment != 0) { right = CodeGen.GetOperator(right, CodeBinaryOperatorType.Subtract, CodeGen.GetPrimitive(_adjustment)); } method.Statements.Add(CodeGen.GetAssign(CodeGen.GetField(CodeGen.GetThis(), _reference.GetNames()), CodeGen.GetCast(_reference.GetTargetMember().GetTypeReference(), right))); } return(method); }
/// <summary> /// Method to generate the code to implement the type /// </summary> /// <returns></returns> public override CodeTypeDeclaration GetCodeType() { CodeTypeDeclaration type = new CodeTypeDeclaration(Name); type.IsClass = true; type.BaseTypes.Add(new CodeTypeReference(typeof(BaseParser))); type.Attributes = MemberAttributes.Public | MemberAttributes.Final; type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(GuidAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(Uuid.ToString())))); if (DisplayClass != Guid.Empty) { type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DisplayClassAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(DisplayClass.ToString())))); } if (!String.IsNullOrWhiteSpace(_formatString)) { type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(FormatStringAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(_formatString)))); } CodeConstructor defaultConstructor = new CodeConstructor(); defaultConstructor.Attributes = MemberAttributes.Public; type.Members.Add(defaultConstructor); CodeConstructor fromStreamConstructor = new CodeConstructor(); fromStreamConstructor.Attributes = MemberAttributes.Public; fromStreamConstructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(DataReader)), "reader")); fromStreamConstructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(StateDictionary)), "state")); fromStreamConstructor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(Logger)), "logger")); fromStreamConstructor.BaseConstructorArgs.Add(CodeGen.GetArgument("reader")); fromStreamConstructor.BaseConstructorArgs.Add(CodeGen.GetArgument("state")); fromStreamConstructor.BaseConstructorArgs.Add(CodeGen.GetArgument("logger")); type.Members.Add(fromStreamConstructor); CodeMemberMethod preSerializer = new CodeMemberMethod(); preSerializer.Name = "PreSerializer"; preSerializer.Attributes = MemberAttributes.Private; CodeMemberMethod postSerializer = new CodeMemberMethod(); postSerializer.Name = "PostSerializer"; postSerializer.Attributes = MemberAttributes.Private; CodeMemberMethod preDeserializer = new CodeMemberMethod(); preDeserializer.Name = "PreDeserializer"; preDeserializer.Attributes = MemberAttributes.Private; CodeMemberMethod postDeserializer = new CodeMemberMethod(); postDeserializer.Name = "PostDeserializer"; postDeserializer.Attributes = MemberAttributes.Private; CodeMemberMethod fromStreamMethod = new CodeMemberMethod(); fromStreamMethod.Name = "FromStream"; fromStreamMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override; CodeMemberMethod toStreamMethod = new CodeMemberMethod(); toStreamMethod.Name = "ToStream"; toStreamMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override; if (PreserializeExpression.IsValid) { toStreamMethod.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), "Resolve", CodeGen.GetPrimitive(PreserializeExpression.Expression))); } foreach (MemberEntry entry in Members) { IMemberReaderWriter readerWriter = entry as IMemberReaderWriter; CodeMemberField field = entry.GetMemberDeclaration(); if (entry.Hidden) { field.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(HiddenMemberAttribute)), new CodeAttributeArgument(CodeGen.GetPrimitive(true)))); } if (entry.ReadOnly) { field.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(ReadOnlyMemberAttribute)), new CodeAttributeArgument(CodeGen.GetPrimitive(true)))); } if (entry.DisplayClass != Guid.Empty) { field.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(DisplayClassAttribute)), new CodeAttributeArgument(CodeGen.GetPrimitive(entry.DisplayClass.ToString())))); } type.Members.Add(field); CodeMemberMethod serMethod = entry.GetSerializerMethod(); CodeMemberMethod deserMethod = entry.GetDeserializerMethod(); CodeMemberMethod preserMethod = entry.GetPreSerializeMethod(); CodeMemberMethod postserMethod = entry.GetPostSerializeMethod(); CodeMemberMethod predeserMethod = entry.GetPreDeserializeMethod(); CodeMemberMethod postdeserMethod = entry.GetPostDeserializeMethod(); if (readerWriter == null) { type.Members.Add(serMethod); type.Members.Add(deserMethod); } if (preserMethod.Statements.Count > 0) { type.Members.Add(preserMethod); preSerializer.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), preserMethod.Name)); } if (postserMethod.Statements.Count > 0) { type.Members.Add(postserMethod); postSerializer.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), postserMethod.Name)); } if (predeserMethod.Statements.Count > 0) { type.Members.Add(predeserMethod); preDeserializer.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), predeserMethod.Name)); } if (postdeserMethod.Statements.Count > 0) { type.Members.Add(postdeserMethod); postDeserializer.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), postserMethod.Name)); } CodeStatement readStatement = null; CodeStatement writeStatement = null; if (readerWriter != null) { CodeExpression readExpression = readerWriter.GetReaderExpression(GetReader()); if (entry.ValidateExpression.IsValid) { readExpression = CodeGen.CallMethod(CodeGen.GetThis(), "V", readExpression, CodeGen.GetPrimitive(entry.ValidateExpression.Expression)); } readStatement = CodeGen.GetAssign(CodeGen.GetThisField(field.Name), readExpression); writeStatement = new CodeExpressionStatement(readerWriter.GetWriterExpression(GetWriter(), CodeGen.GetThisField(field.Name))); } else { CodeExpression readExpression = CodeGen.CallMethod(CodeGen.GetThis(), deserMethod.Name, GetReader()); if (entry.ValidateExpression.IsValid) { readExpression = CodeGen.CallMethod(CodeGen.GetThis(), "V", readExpression, CodeGen.GetPrimitive(entry.ValidateExpression.Expression)); } readStatement = CodeGen.GetAssign(CodeGen.GetThisField(field.Name), readExpression); writeStatement = new CodeExpressionStatement(CodeGen.CallMethod(CodeGen.GetThis(), serMethod.Name, GetWriter(), CodeGen.GetThisField(field.Name))); } if (entry.OptionalExpression.IsValid) { readStatement = CodeGen.GetIf(entry.OptionalExpression.GetCheckExpression(), new[] { readStatement }); writeStatement = CodeGen.GetIf(entry.OptionalExpression.GetCheckExpression(), new[] { writeStatement }); } fromStreamMethod.Statements.Add(readStatement); toStreamMethod.Statements.Add(writeStatement); } if (preDeserializer.Statements.Count > 0) { fromStreamMethod.Statements.Insert(0, CodeGen.GetStatement(CodeGen.CallMethod(CodeGen.GetThis(), "PreDeserializer"))); type.Members.Add(preDeserializer); } toStreamMethod.Statements.Add(new CodeMethodInvokeExpression(GetWriter(), "Flush")); if (preSerializer.Statements.Count > 0) { toStreamMethod.Statements.Insert(0, CodeGen.GetStatement(CodeGen.CallMethod(CodeGen.GetThis(), "PreSerializer"))); type.Members.Add(preSerializer); } if (postDeserializer.Statements.Count > 0) { fromStreamMethod.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), "PostDeserializer")); type.Members.Add(postDeserializer); } if (PostDeserializeExpression.IsValid) { fromStreamMethod.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), "Resolve", CodeGen.GetPrimitive(PostDeserializeExpression.Expression))); } if (postSerializer.Statements.Count > 0) { toStreamMethod.Statements.Add(CodeGen.CallMethod(CodeGen.GetThis(), "PostSerializer")); type.Members.Add(postSerializer); } type.Members.Add(fromStreamMethod); type.Members.Add(toStreamMethod); return(type); }
public System.CodeDom.CodeExpression GetWriterExpression(CodeExpression writer, System.CodeDom.CodeExpression obj) { return(_baseWriter.GetWriterExpression(writer, obj)); }