public static void DeclareType(SequenceOrSetType pThis, StreamWriterLevel csFile, string TypeName, int level, string baseClassName) { csFile.WL(level, "public partial class {0} : {1}", TypeName, baseClassName); csFile.WL(level++, "{"); foreach (SequenceOrSetType.Child ch in pThis.m_children.Values) { csFile.WriteLine(); ICSharpType childType = ch.m_type as ICSharpType; string propName = C.ID(ch.m_childVarName.Substring(0, 1).ToUpper() + ch.m_childVarName.Substring(1)); if (childType.RequiresNewTypeDeclaration()) { childType.DeclareType(csFile, propName, level); csFile.WriteLine(); } csFile.WL(level, "public {0} {1}", ((ICSharpType)ch.m_type).DeclaredTypeName, C.ID(ch.m_childVarName)); csFile.WL(level++, "{"); csFile.WL(level, "get {{ return m_children[ClassDef.m_children[\"{0}\"].m_index] as {1}; }}", C.ID(ch.m_childVarName), ((ICSharpType)ch.m_type).DeclaredTypeName); csFile.WL(--level, "}"); csFile.WL(level, "public {0} Create_{1}()", childType.DeclaredTypeName, C.ID(ch.m_childVarName)); csFile.WL(level++, "{"); // csFile.WL(level, "return CreateChild(\"{0}\");", C.ID(ch.m_childVarName)); csFile.WL(level, "return CreateChild(\"{0}\") as {1};", C.ID(ch.m_childVarName), childType.DeclaredTypeName); csFile.WL(--level, "}"); } CSharpType.WriteCustomAttrList(pThis, csFile, level); CSharpType.WriteEncodeDecodeMethods(pThis, csFile, level); csFile.WL(level, "static Asn1CompositeClass<OptionalNamedChild> _clsDef = new {0}ClassDefinition();",TypeName); csFile.WL(level, "public override Asn1CompositeClass<OptionalNamedChild> ClassDef"); csFile.WL(level++, "{"); csFile.WL(level, "get"); csFile.WL(level++, "{"); csFile.WL(level, "return _clsDef;"); csFile.WL(--level, "}"); csFile.WL(--level, "}"); csFile.WL(level, "public {0}()", TypeName); csFile.WL(level++, "{"); csFile.WL(level, "m_children = new Asn1Object[{0}];",pThis.m_children.Count); csFile.WL(--level, "}"); //class definition csFile.WL(level, "public class {0}ClassDefinition : Asn1CompositeClass<OptionalNamedChild>", TypeName); csFile.WL(level++, "{"); csFile.WL(level, "public {0}ClassDefinition()", TypeName); csFile.WL(level++, "{"); csFile.WL(level, "OptionalNamedChild ch;"); int idx = 0; foreach (SequenceOrSetType.Child ch in pThis.m_children.Values) { ICSharpType childType = ch.m_type as ICSharpType; csFile.WL(level, "ch = new OptionalNamedChild(\"{0}\", {1}, Create{2}, {3}, null);", C.ID(ch.m_childVarName), idx, // csFile.WL(level, "ch = new OptionalNamedChild(\"{0}\", {1}, delegate() {{return new {2}();}}, {3}, null);", C.ID(ch.m_childVarName), idx, childType.DeclaredTypeName, (ch.m_optional ? "true" : "false")); idx++; ChoiceType chChild = ch.m_type.GetFinalType() as ChoiceType; if (chChild != null && !ch.m_type.IsTagged()) { // csFile.WL(level, "ch.UnTaggedChoice = true;"); // csFile.WL(level, "ch.m_childrenTags = new List<Tag>();"); List<Asn1Type.TagSequence> tags = chChild.getChildrenTags(); foreach (Asn1Type.TagSequence tgSq in tags) { // csFile.WL(level, "ch.m_childrenTags.Add(new Tag({0}, TagClass.{1}));", tgSq.m_tags[0].m_tag, tgSq.m_tags[0].m_class); csFile.WL(level, "m_posChildren.Add(0x{0:X}, ch);", CalculateTag(tgSq.m_tags[0])); } } else { Asn1Type.Tag lstTag = ch.m_type.Tags.m_tags[0]; // csFile.WL(level, "ch.m_Tag = new Tag({0}, TagClass.{1});", lstTag.m_tag, lstTag.m_class); csFile.WL(level, "m_posChildren.Add(0x{0:X}, ch);", CalculateTag(lstTag)); } csFile.WL(level, "m_children.Add(\"{0}\", ch);", C.ID(ch.m_childVarName)); csFile.WriteLine(); } csFile.WL(--level, "}"); //constructor foreach (SequenceOrSetType.Child ch in pThis.m_children.Values) { ICSharpType childType = ch.m_type as ICSharpType; csFile.WL(level, "public static {0} Create{0}() {{ return new {0}();}}", childType.DeclaredTypeName); } csFile.WL(--level, "}"); //internal class csFile.WL(--level, "}"); //class }
public static void WriteEncodeDecodeMethods(Asn1Type pThis, StreamWriterLevel csFile, int level) { if (pThis.m_tag == null) return; csFile.WriteLine(); csFile.WL(level, "public override uint Encode(Stream strm, EncodingRules encRule)"); csFile.WL(level++, "{"); if (pThis.m_tag.m_taggingMode== TaggingMode.EXPLICIT) // csFile.WL(level, "return Encode(strm, encRule, TagClass.{0}, false, {1});", pThis.m_tag.m_class, pThis.m_tag.m_tag); csFile.WL(level, "return Encode(strm, encRule, false, 0x{0:X});", CSharpSequenceOrSetType.CalculateTag(pThis.m_tag)); else csFile.WL(level, "return Encode(strm, encRule, IsPrimitive, 0x{0:X});", CSharpSequenceOrSetType.CalculateTag(pThis.m_tag)); // csFile.WL(level, "return Encode(strm, encRule, TagClass.{0}, IsPrimitive, {1});", pThis.m_tag.m_class, pThis.m_tag.m_tag); csFile.WL(--level, "}"); csFile.WriteLine(); csFile.WL(level, "public override uint Decode(Stream strm, EncodingRules encRule)"); csFile.WL(level++, "{"); if (pThis.m_tag.m_taggingMode == TaggingMode.EXPLICIT) csFile.WL(level, "return Decode(strm, encRule, false, 0x{0:X});", CSharpSequenceOrSetType.CalculateTag(pThis.m_tag)); // csFile.WL(level, "return Decode(strm, encRule, TagClass.{0}, false, {1});", pThis.m_tag.m_class, pThis.m_tag.m_tag); else csFile.WL(level, "return Decode(strm, encRule, IsPrimitive, 0x{0:X});", CSharpSequenceOrSetType.CalculateTag(pThis.m_tag)); // csFile.WL(level, "return Decode(strm, encRule, TagClass.{0}, IsPrimitive, {1});", pThis.m_tag.m_class, pThis.m_tag.m_tag); csFile.WL(--level, "}"); if (pThis.m_tag.m_taggingMode == TaggingMode.EXPLICIT) { csFile.WriteLine(); csFile.WL(level, "public override uint EncodeContent(Stream strm, EncodingRules encRule)"); csFile.WL(level++, "{"); csFile.WL(level, "return base.Encode(strm, encRule);"); csFile.WL(--level, "}"); csFile.WriteLine(); csFile.WL(level, "public override uint DecodeContent(Stream strm, EncodingRules encRule, uint dataLen)"); csFile.WL(level++, "{"); csFile.WL(level, "return base.Decode(strm, encRule);"); csFile.WL(--level, "}"); } }
public static void WriteIsConstraintValid(Asn1Type pThis, StreamWriterLevel csFile, int level, string valueName, string enumPrefix) { if (pThis.m_constraints.Count == 0) return; csFile.WL(level, "public override bool IsConstraintValid()"); csFile.WL(level++, "{"); csFile.P(level); csFile.Write("return "); foreach (ICSharpConstraint con in pThis.m_constraints) { csFile.Write("{0} && ",con.PrintConstraintCode(valueName,enumPrefix)); } csFile.WriteLine("base.IsConstraintValid();"); csFile.WL(--level, "}"); }
public static void WriteCustomAttrList(Asn1Type pThis, StreamWriterLevel csFile, int level) { if (pThis.m_CustomAttributes.Count == 0) return; csFile.WL(level, "private static Dictionary<string, string> m_CustomAttrs = new Dictionary<string, string>()"); csFile.WL(level++, "{"); foreach (var kv in pThis.m_CustomAttributes) { csFile.WL(level, "{{\"{0}\",\"{1}\"}},", kv.Key, kv.Value); } csFile.WL(--level, "};"); csFile.WL(level, "public override Dictionary<string, string> CustomAttrs { get { return m_CustomAttrs; } }"); }
public static void DeclareType(Asn1Type pThis, StreamWriterLevel csFile, string TypeName, int level, string baseClassName, out string DeclaredTypeName, string valueName, string enumPrefix) { DeclaredTypeName = TypeName; csFile.WL(level, "public partial class {0} : {1}", DeclaredTypeName, baseClassName); csFile.WL(level++, "{"); WriteCustomAttrList(pThis, csFile, level); WriteEncodeDecodeMethods(pThis, csFile, level); WriteIsConstraintValid(pThis, csFile, level, valueName, enumPrefix); csFile.WL(--level, "}"); }
public void DeclareType(StreamWriterLevel csFile, string TypeName, int level) { _declaredTypeName = TypeName; csFile.WL(level, "public partial class {0} : Asn1EnumeratedObject<{0}.Enumerated>", DeclaredTypeName); csFile.WL(level++, "{"); csFile.WL(level,"public enum Enumerated"); csFile.WL(level++, "{"); foreach(EnumeratedType.Item it in m_enumValues.Values) { csFile.WL(level, "{0} = {1},", it.CID, it.m_value); } csFile.WL(--level, "}"); csFile.WL(level, "protected override long ValueAsLong { get {return (long)Value;}}"); CSharpType.WriteEncodeDecodeMethods(this, csFile, level); if (m_constraints.Count > 0) { csFile.WL(level, "public override bool IsConstraintValid()"); csFile.WL(level++, "{"); csFile.WL(level, "bool ret=true;"); foreach (ICSharpConstraint con in m_constraints) { csFile.WL(level, "ret = ret && {0};", con.PrintConstraintCode("Value", DeclaredTypeName + ".Enumerated.")); } csFile.WL(level, "return base.IsConstraintValid() && ret;"); csFile.WL(--level, "}"); } csFile.WL(--level, "}"); }
public static void WriteConstructorCodeA(ArrayType pThis, StreamWriterLevel csFile, int level, string pThisString, List<IConstraint> constraints, Asn1Type.Tag tag) { int levCount=0; StackTrace str = new StackTrace(); foreach (StackFrame frm in str.GetFrames()) if (frm.GetMethod().Name.Contains("WriteConstructorCodeA")) levCount++; string retVarName = "ch" + levCount.ToString(); ICSharpType childType = pThis.m_type as ICSharpType; string prefix = pThisString; if (pThisString != "") prefix += "."; csFile.WL(level, "{0}m_child.createObj = new Func<{1}>(delegate()", prefix,childType.DeclaredTypeName); csFile.WL(level++, "{"); csFile.WL(level, "{0} {1} = new {0}();", childType.DeclaredTypeName, retVarName); csFile.WL(level, "return {0};", retVarName); csFile.WL(--level, "});"); ChoiceType chChild = pThis.m_type.GetFinalType() as ChoiceType; if (chChild != null && !pThis.m_type.IsTagged()) { csFile.WL(level, "{0}m_child.UnTaggedChoice = true;", prefix); csFile.WL(level, "{0}m_child.m_childrenTags = new List<Tag>();", prefix); List<Asn1Type.TagSequence> tags = chChild.getChildrenTags(); foreach (Asn1Type.TagSequence tgSq in tags) { csFile.WL(level, "{0}m_child.m_childrenTags.Add(new Tag({1}, TagClass.{2}));",prefix, tgSq.m_tags[0].m_tag, tgSq.m_tags[0].m_class); } } else { Asn1Type.Tag lstTag = pThis.m_type.Tags.m_tags[0]; csFile.WL(level, "{0}m_child.m_Tag = new Tag({1}, TagClass.{2});",prefix, lstTag.m_tag, lstTag.m_class); } }
public static void DeclareTypeA(ArrayType pThis, StreamWriterLevel csFile, string TypeName, int level, string baseClassName, out string DeclaredTypeName) { ICSharpType childType = pThis.m_type as ICSharpType; if (childType.RequiresNewTypeDeclaration()) { DeclaredTypeName = TypeName; string internalTypeName = "InternalType"; int levCount = 0; StackTrace str = new StackTrace(); foreach (StackFrame frm in str.GetFrames()) if (frm.GetMethod().Name.Contains("DeclareTypeA")) levCount++; if (levCount > 1) internalTypeName += (--levCount).ToString(); csFile.WL(level, "public partial class {0} : {1}<{0}.{2}>", TypeName, baseClassName, internalTypeName); csFile.WL(level++, "{"); childType.DeclareType(csFile, internalTypeName, level); csFile.WriteLine(); } else { DeclaredTypeName = baseClassName + "<" + childType.DeclaredTypeName + ">"; csFile.WL(level, "public partial class {0} : {1}<{2}>", TypeName, baseClassName, childType.DeclaredTypeName); csFile.WL(level++, "{"); } CSharpType.WriteCustomAttrList(pThis, csFile, level); CSharpType.WriteEncodeDecodeMethods(pThis, csFile, level); CSharpType.WriteIsConstraintValid(pThis, csFile, level, "m_children", ""); string childTags = string.Empty; int chTagsNo=0; ChoiceType chChild = pThis.m_type.GetFinalType() as ChoiceType; if (chChild != null && !pThis.m_type.IsTagged()) { List<Asn1Type.TagSequence> tags = chChild.getChildrenTags(); foreach (Asn1Type.TagSequence tgSq in tags) { childTags += string.Format("0x{0:X},", CSharpSequenceOrSetType.CalculateTag(tgSq.m_tags[0])); chTagsNo++; } childTags = childTags.Substring(0, childTags.Length - 1); } else { Asn1Type.Tag lstTag = pThis.m_type.Tags.m_tags[0]; childTags = string.Format("0x{0:X}", CSharpSequenceOrSetType.CalculateTag(lstTag)); chTagsNo=1; } csFile.WL(level, "static List<UInt32> _childTags = new List<UInt32>( new UInt32[{0}] {{ {1} }} );", chTagsNo, childTags); csFile.WL(level, "protected override List<UInt32> ChildTags { get { return _childTags; } }"); csFile.WL(level, "protected override Func<{0}> CreateEmptyChild {{ get {{ return delegate() {{ return new {0} (); }}; }} }}", childType.DeclaredTypeName); //csFile.WL(level, "public {0}()", TypeName); //csFile.WL(level++, "{"); //WriteConstructorCodeA(pThis, csFile, level, "", pThis.m_constraints, pThis.m_tag); //csFile.WL(--level, "}"); csFile.WL(--level, "}"); }