示例#1
0
        public void VisitReadDictionary(string variable, string reader, DictionaryTypeNode node, TextBuilder builder)
        {
            string equalityComparer = GetEqualityComparer(node.SerializeKey.Type);

            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("if({0} == null) ", variable);
            builder.WriteLine("\t{0} = new {1}(size{2});", variable, node.TypeName,
                              string.IsNullOrEmpty(equalityComparer) ? "" : ", " + equalityComparer);
            builder.WriteLine("else");
            builder.WriteLine("\t{0}.Clear();", variable);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("{0} key = ", node.ParameterNodes[0].TypeName);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[0].TypeName), reader, node.SerializeKey, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteIndents();
            builder.Write("{0} value = ", node.ParameterNodes[1].TypeName);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[1].TypeName), reader, node.SerializeValue, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteLine("{0}.Add(key, value);", variable);

            builder.UnIndent();
            builder.RightPar();
        }
示例#2
0
        protected void VisitReadPolyClass(string variable, string reader, PolyClassTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int type = {0}.ReadInt32();", reader);
            builder.WriteLine("switch(type)");
            builder.LeftPar();
            builder.Indent();

            var children = node.Children.ToArray();

            for (int index = 0; index < children.Length; index++)
            {
                var n = children[index] as SerializeNode;

                builder.WriteLine("case {0}:", index + 1);

                builder.Indent();
                builder.WriteIndents();
                builder.Write("{0} = ", variable);
                VisitDeserialization(variable, reader, n, builder);
                builder.Write(";");
                builder.NextLine();

                builder.WriteLine("break;");
                builder.UnIndent();
            }

            builder.UnIndent();
            builder.RightPar();
        }
示例#3
0
        public void VisitReadTwoDimArray(string variable, string reader, TwoDimArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size0 = {0}.ReadInt32();", reader);
            builder.WriteLine("{0} = new {1}(size0);", variable, node.TypeName);
            builder.WriteLine("for(int i = 0; i < size0; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("int size1 = {0}.ReadInt32();", reader);
            builder.WriteLine("{0}[i] = new {1}[size1];", variable, node.ParameterNodes[0].TypeName);
            builder.WriteLine("for(int j = 0; j < size1; ++j)");
            builder.LeftPar();
            builder.Indent();

            string target = variable + "[i, j]";

            builder.WriteIndents();
            builder.Write(target + " = ");
            VisitDeserialization(target, reader, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();

            builder.UnIndent();
            builder.RightPar();
        }
示例#4
0
        public void VisitReadField(string variable, string reader, bool readOnly, SerializeNode node, TextBuilder builder)
        {
            if (node == null)
            {
                return;
            }

            builder.WriteIndents();
            if (node.Category == SerializeNode.NodeCategory.LOCALE)
            {
                builder.Write("{0}.Set(StringSerializer.Read({1}, null));", variable, reader);
            }
            else
            {
                if (!readOnly)
                {
                    builder.Write(variable);
                    builder.Write(" = ");
                }
                VisitDeserialization(variable, reader, node, builder);
                if (!readOnly && node.Category == SerializeNode.NodeCategory.CLASS)
                {
                    builder.Write(" as {0}", node.TypeName);
                }
                builder.Write(";");
            }
            builder.NextLine();
        }
示例#5
0
 public void VisitNullableSerialization(string variable, string writer, NullableSerializationNode node, TextBuilder builder)
 {
     builder.Write("{0}.Write({1} != null);", writer, variable);
     builder.NextLine();
     builder.WriteIndents();
     builder.Write("if({0} != null) ", variable);
     VisitSerialization(variable + ".Value", writer, node.Serialize, builder);
 }
示例#6
0
        public void VisitWritePolyClass(string variable, string writer, PolyClassTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("Type type = {0}.GetType();", variable);

            var children = node.Children.ToArray();

            for (int index = 0; index < children.Length; index++)
            {
                var n = children[index] as SerializeNode;

                builder.WriteIndents();
                if (index > 0)
                {
                    builder.Write("else ");
                }
                builder.Write("if(type == typeof({0}))", n.TypeName);
                builder.NextLine();

                builder.LeftPar();
                builder.Indent();

                builder.WriteLine("{0}.Write({1});", writer, index + 1);

                builder.WriteIndents();
                VisitSerialization(variable + " as " + n.TypeName, writer, n, builder);
                builder.Write(";");
                builder.NextLine();

                builder.UnIndent();
                builder.RightPar();
            }
            if (children.Length > 0)
            {
                builder.WriteLine("else");
                builder.LeftPar();
                builder.Indent();
                builder.WriteLine("{0}.Write(0);", writer);
                builder.UnIndent();
                builder.RightPar();
            }
        }
示例#7
0
        public void VisitWriteDictionary(string variable, string writer, DictionaryTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.Count;", variable);
            builder.WriteLine("{0}.Write(size);", writer);
            builder.WriteLine("foreach(var elem in {0})", variable);
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization("elem.Key", writer, node.SerializeKey, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteIndents();
            VisitSerialization("elem.Value", writer, node.SerializeValue, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
示例#8
0
        public void VisitWriteList(string variable, string writer, ListTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.Count;", variable);
            builder.WriteLine("{0}.Write(size);", writer);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization(variable + "[i]", writer, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
示例#9
0
        public void VisitWriteField(string variable, string writer, SerializeNode node, TextBuilder builder)
        {
            if (node == null)
            {
                return;
            }

            builder.WriteIndents();
            if (node.Category == SerializeNode.NodeCategory.LOCALE)
            {
                builder.Write("StringSerializer.Write({0}, {1}.ToString());", writer, variable);
            }
            else
            {
                VisitSerialization(variable, writer, node, builder);
                builder.Write(";");
            }
            builder.NextLine();
        }
示例#10
0
        public void VisitReadHashSet(string variable, string reader, HashSetTypeNode node, TextBuilder builder)
        {
            string equalityComparer = GetEqualityComparer(node.Serialize.Type);

            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("{0} = new {1}({2});", variable, node.TypeName, equalityComparer);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("{0}.Add(", variable);
            VisitDeserialization(string.Format("default({0})", node.ParameterNodes[0].TypeName), reader, node.Serialize, builder);
            builder.Write(");");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
        }
示例#11
0
        public void VisitReadList(string variable, string reader, ListTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size = {0}.ReadInt32();", reader);
            builder.WriteLine("if({0} == null) ", variable);
            builder.WriteLine("\t{0} = new {1}(size);", variable, node.TypeName);
            builder.WriteLine("else if({0}.Count != size)", variable);
            builder.WriteLine("\t{0}.Clear();", variable);
            builder.WriteLine("for(int i = 0; i < size; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            builder.Write("var temp = ", variable);
            VisitDeserialization(string.Format("(d.Count > i ? d[i] : default({0}))", node.ParameterNodes[0].TypeName), reader, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.WriteLine("if(d.Count <= i) d.Add(temp); else d[i] = temp;");

            builder.UnIndent();
            builder.RightPar();
        }
示例#12
0
        public void VisitWriteTwoDimArray(string variable, string writer, TwoDimArrayTypeNode node, TextBuilder builder)
        {
            builder.WriteLine("int size0 = {0}.Length;", variable);
            builder.WriteLine("{0}.Write(size0);", writer);
            builder.WriteLine("for(int i = 0; i < size0; ++i)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteLine("int size1 = {0}[i] != null ? {0}[i].Count : 0;", variable);
            builder.WriteLine("{0}.Write(size1);", writer);
            builder.WriteLine("for(int j = 0; j < size1; ++j)");
            builder.LeftPar();
            builder.Indent();

            builder.WriteIndents();
            VisitSerialization(variable + "[i, j]", writer, node.Serialize, builder);
            builder.Write(";");
            builder.NextLine();

            builder.UnIndent();
            builder.RightPar();
            builder.UnIndent();
            builder.RightPar();
        }