コード例 #1
0
 private void FormatMethods(CellDef def)
 {
     FormatStaticConstructor(def);
     Out.WriteLine();
     FormatConstructor(def);
     Out.WriteLine();
     FormatEqualsTo(def);
     Out.WriteLine();
     FormatGetHashCode(def);
     Out.WriteLine();
     FormatGetType(def);
     Out.WriteLine();
     FormatIsEquivalent(def);
     if (!def.IsLocal)
     {
         Out.WriteLine();
         FormatDeserialize(def);
         Out.WriteLine();
         FormatSerialize(def);
     }
     Out.WriteLine();
     FormatDescribe(def);
     if (!def.IsLocal)
     {
         Out.WriteLine();
         FormatInitializer(def);
     }
 }
コード例 #2
0
 private void FormatProperties(CellDef def)
 {
     var leading = true;
     foreach (var property in def.Properties)
     {
         if (leading)
         {
             leading = false;
         }
         else
         {
             Out.WriteLine();
         }
         FormatComments(1, property.Comments);
         Indent(1); Out.WriteLine("public {0} {1}", property.NativeType, property.Name);
         Indent(1); Out.WriteLine("{");
         Indent(2); Out.WriteLine("get {{ return {0}; }}", property.NativeName);
         Indent(2); Out.WriteLine("set");
         Indent(2); Out.WriteLine("{");
         Indent(3); Out.WriteLine("fingerprint.Touch(tag.Offset + {0});", property.Index);
         Indent(3); Out.WriteLine("{0} = value;", property.NativeName);
         Indent(2); Out.WriteLine("}");
         Indent(1); Out.WriteLine("}");
     }
 }
コード例 #3
0
 private void FormatInitializer(CellDef def)
 {
     Indent(1); Out.WriteLine("private void Initialize()");
     Indent(1); Out.WriteLine("{");
     foreach (var property in def.Properties)
     {
         Indent(2);
         Out.WriteLine("{0} = {1};", property.NativeName, property.DefaultValue);
     }
     Indent(1); Out.WriteLine("}");
 }
コード例 #4
0
 private void FormatIsEquivalent(CellDef def)
 {
     Indent(1); Out.WriteLine("public override bool IsEquivalent(Cell other)");
     Indent(1); Out.WriteLine("{");
     Indent(2); Out.WriteLine("if (!base.IsEquivalent(other))");
     Indent(2); Out.WriteLine("{");
     Indent(3); Out.WriteLine("return false;");
     Indent(2); Out.WriteLine("}");
     if (def.HasProperties)
     {
         Indent(2); Out.WriteLine("{0} o = ({0})other;", def.Name);
         Indent(2); Out.WriteLine("var touched = new Capo<bool>(fingerprint, tag.Offset);");
         foreach (var property in def.Properties)
         {
             Indent(2); Out.WriteLine("if (touched[{0}])", property.Index);
             Indent(2); Out.WriteLine("{");
             //if (Types.IsPrimitive(property.TypeSpec.Type))
             //{
                 Indent(3); Out.WriteLine("if ({0} != o.{0})", property.NativeName);
             //}
             /*
             else
             {
                 Indent(3); Out.WriteLine("if ((object){0} == null || !{0}.IsEquivalent(o.{0}))", property.NativeName);
             }
             */
             Indent(3); Out.WriteLine("{");
             Indent(4); Out.WriteLine("return false;");
             Indent(3); Out.WriteLine("}");
             Indent(2); Out.WriteLine("}");
         }
     }
     Indent(2); Out.WriteLine("return true;");
     Indent(1); Out.WriteLine("}");
 }
コード例 #5
0
        private void FormatGetHashCode(CellDef def)
        {
            Indent(1); Out.WriteLine("public override int GetHashCode(Fingerprint fingerprint)");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("var hash = new Hash(base.GetHashCode(fingerprint));");
            if (def.HasProperties)
            {
                Indent(2); Out.WriteLine("if (fingerprint.Length <= tag.Offset)");
                Indent(2); Out.WriteLine("{");
                Indent(3); Out.WriteLine("return hash.Code;");
                Indent(2); Out.WriteLine("}");

                Indent(2); Out.WriteLine("var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("if (touched[{0}])", property.Index);
                    Indent(2); Out.WriteLine("{");
                    Indent(3); Out.WriteLine("hash.Update({0});", property.NativeName);
                    Indent(2); Out.WriteLine("}");
                }
            }
            Indent(2); Out.WriteLine("return hash.Code;");
            Indent(1); Out.WriteLine("}");
        }
コード例 #6
0
 private void FormatGetType(CellDef def)
 {
     if (def.IsEvent)
     {
         Indent(1); Out.WriteLine("public override int GetTypeId()");
         Indent(1); Out.WriteLine("{");
         Indent(2); Out.WriteLine("return tag.TypeId;");
         Indent(1); Out.WriteLine("}");
         Out.WriteLine();
     }
     Indent(1); Out.WriteLine("public override Cell.Tag GetTypeTag() ");
     Indent(1); Out.WriteLine("{");
     Indent(2); Out.WriteLine("return tag;");
     Indent(1); Out.WriteLine("}");
 }
コード例 #7
0
 private void FormatEqualsTo(CellDef def)
 {
     Indent(1); Out.WriteLine("public override bool EqualsTo(Cell other)");
     Indent(1); Out.WriteLine("{");
     Indent(2); Out.WriteLine("if (!base.EqualsTo(other))");
     Indent(2); Out.WriteLine("{");
     Indent(3); Out.WriteLine("return false;");
     Indent(2); Out.WriteLine("}");
     if (def.HasProperties)
     {
         Indent(2); Out.WriteLine("{0} o = ({0})other;", def.Name);
         foreach (var property in def.Properties)
         {
             if (Types.IsCollection(property.TypeSpec.Type))
             {
                 Indent(2); Out.WriteLine("if (!Extensions.EqualsExtended({0}, o.{0}))", property.NativeName);
             }
             else
             {
                 Indent(2); Out.WriteLine("if ({0} != o.{0})", property.NativeName);
             }
             Indent(2); Out.WriteLine("{");
             Indent(3); Out.WriteLine("return false;");
             Indent(2); Out.WriteLine("}");
         }
     }
     Indent(2); Out.WriteLine("return true;");
     Indent(1); Out.WriteLine("}");
 }
コード例 #8
0
ファイル: Formatter.cs プロジェクト: gitter-badger/x2clr
 public abstract void FormatCell(CellDef def);
コード例 #9
0
 private void FormatDescribe(CellDef def)
 {
     Indent(1); Out.WriteLine("protected override void Describe(StringBuilder stringBuilder)");
     Indent(1); Out.WriteLine("{");
     Indent(2); Out.WriteLine("base.Describe(stringBuilder);");
     foreach (var property in def.Properties)
     {
         Indent(2);
         if (Types.IsCollection(property.TypeSpec.Type))
         {
             Out.WriteLine("stringBuilder.AppendFormat(\" {0}={{0}}\", {1}.ToStringExtended());",
                 property.Name, property.NativeName);
         }
         else if (property.NativeType == "string")
         {
             Out.WriteLine("stringBuilder.AppendFormat(\" {0}=\\\"{{0}}\\\"\", {1}.Replace(\"\\\"\", \"\\\\\\\"\"));",
                 property.Name, property.NativeName);
         }
         else
         {
             Out.WriteLine("stringBuilder.AppendFormat(\" {0}={{0}}\", {1});",
                 property.Name, property.NativeName);
         }
     }
     Indent(1); Out.WriteLine("}");
 }
コード例 #10
0
        private void FormatDeserialize(CellDef def)
        {
            Indent(1); Out.WriteLine("public override void Deserialize(Deserializer deserializer)");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("base.Deserialize(deserializer);");
            if (def.HasProperties)
            {
                Indent(2); Out.WriteLine("var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("if (touched[{0}])", property.Index);
                    Indent(2); Out.WriteLine("{");
                    Indent(3); Out.WriteLine("deserializer.Read(out {0});", property.NativeName);
                    Indent(2); Out.WriteLine("}");
                }
            }
            Indent(1); Out.WriteLine("}");

            Out.WriteLine();
            Indent(1); Out.WriteLine("public override void Deserialize(VerboseDeserializer deserializer)");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("base.Deserialize(deserializer);");
            if (def.HasProperties)
            {
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("deserializer.Read(\"{0}\", out {1});", property.Name, property.NativeName);
                }
            }
            Indent(1); Out.WriteLine("}");
        }
コード例 #11
0
 private void FormatConstructor(CellDef def)
 {
     Indent(1); Out.WriteLine("public {0}()", def.Name);
     Indent(2); Out.WriteLine(": base(tag.NumProps)");
     Indent(1); Out.WriteLine("{");
     if (!def.IsLocal)
     {
         Indent(2); Out.WriteLine("Initialize();");
     }
     Indent(1); Out.WriteLine("}");
     Out.WriteLine();
     Indent(1); Out.WriteLine("protected {0}(int length)", def.Name);
     Indent(2); Out.WriteLine(": base(length + tag.NumProps)");
     Indent(1); Out.WriteLine("{");
     if (!def.IsLocal)
     {
         Indent(2); Out.WriteLine("Initialize();");
     }
     Indent(1); Out.WriteLine("}");
 }
コード例 #12
0
        private static void PreprocessProperties(CellDef def)
        {
            int index = 0;
            foreach (var property in def.Properties)
            {
                property.Index = index++;

                property.NativeName = FirstToLower(property.Name) + "_";
                property.Name = FirstToUpper(property.Name);

                if (Types.IsPrimitive(property.TypeSpec.Type))
                {
                    if (String.IsNullOrEmpty(property.DefaultValue))
                    {
                        property.DefaultValue = defaultValues[property.TypeSpec.Type];
                    }
                    if (property.TypeSpec.Type == "string")
                    {
                        property.DefaultValue = "\"" + property.DefaultValue + "\"";
                    }
                }
                else
                {
                    property.DefaultValue = "null";
                }

                property.NativeType = FormatTypeSpec(property.TypeSpec);
            }
        }
コード例 #13
0
        public override void FormatCell(CellDef def)
        {
            def.BaseClass = def.Base;
            if (String.IsNullOrEmpty(def.BaseClass))
            {
                def.BaseClass = (def.IsEvent ? "Event" : "Cell");
            }
            PreprocessProperties(def);

            FormatComments(0, def.Comments);
            Indent(0); Out.WriteLine("public class {0} : {1}", def.Name, def.BaseClass);
            Indent(0); Out.WriteLine("{");
            Indent(1); Out.WriteLine("new protected static readonly Tag tag;");
            Out.WriteLine();
            if (def.IsEvent)
            {
                Indent(1); Out.WriteLine("new public static int TypeId { get { return tag.TypeId; } }");
                Out.WriteLine();
            }
            FormatFields(def);
            if (def.HasProperties)
            {
                Out.WriteLine();
            }
            FormatProperties(def);
            if (def.HasProperties)
            {
                Out.WriteLine();
            }
            FormatMethods(def);
            Indent(0); Out.WriteLine("}");
        }
コード例 #14
0
        private void FormatSerialize(CellDef def)
        {
            Indent(1); Out.WriteLine("public override void Serialize(Serializer serializer)");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("base.Serialize(serializer);");
            if (def.HasProperties)
            {
                Indent(2); Out.WriteLine("var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("if (touched[{0}])", property.Index);
                    Indent(2); Out.WriteLine("{");
                    Indent(3); Out.WriteLine("serializer.Write({0});", property.NativeName);
                    Indent(2); Out.WriteLine("}");
                }
            }
            Indent(1); Out.WriteLine("}");

            Out.WriteLine();
            Indent(1); Out.WriteLine("public override void Serialize(VerboseSerializer serializer)");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("base.Serialize(serializer);");
            if (def.HasProperties)
            {
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("serializer.Write(\"{0}\", {1});", property.Name, property.NativeName);
                }
            }
            Indent(1); Out.WriteLine("}");

            Out.WriteLine();
            Indent(1); Out.WriteLine("public override int GetEncodedLength()");
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("int length = base.GetEncodedLength();");
            if (def.HasProperties)
            {
                Indent(2); Out.WriteLine("var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Indent(2); Out.WriteLine("if (touched[{0}])", property.Index);
                    Indent(2); Out.WriteLine("{");
                    Indent(3); Out.WriteLine("length += Serializer.GetEncodedLength({0});", property.NativeName);
                    Indent(2); Out.WriteLine("}");
                }
            }
            Indent(2); Out.WriteLine("return length;");
            Indent(1); Out.WriteLine("}");
        }
コード例 #15
0
 private void FormatFields(CellDef def)
 {
     foreach (var property in def.Properties)
     {
         Indent(1);
         Out.WriteLine("private {0} {1};", property.NativeType, property.NativeName);
     }
 }
コード例 #16
0
        private void FormatStaticConstructor(CellDef def)
        {
            string baseTag = def.Base;
            if (String.IsNullOrEmpty(baseTag))
            {
                baseTag = (def.IsEvent ? "Event.tag" : "null");
            }
            else
            {
                baseTag += ".tag";
            }
            Indent(1); Out.WriteLine("static {0}()", def.Name);
            Indent(1); Out.WriteLine("{");
            Indent(2);
            Out.Write("tag = new Tag({0}, typeof({1}), {2}", baseTag, def.Name,
                def.Properties.Count);
            if (def.IsEvent)
            {
                int i;
                string s = ((EventDef)def).Id;
                Out.WriteLine(",");
                Out.Write("                    ");
                if (!Int32.TryParse(s, out i))
                {
                    Out.Write("(int)");
                }
                Out.Write("{0}", s);
            }
            Out.WriteLine(");");
            Indent(1); Out.WriteLine("}");

            if (def.IsEvent)
            {
                Out.WriteLine();
                Indent(1); Out.WriteLine("new public static {0} New()", def.Name);
                Indent(1); Out.WriteLine("{");
                Indent(2); Out.WriteLine("return new {0}();", def.Name);
                Indent(1); Out.WriteLine("}");
            }
        }
コード例 #17
0
ファイル: XmlHandler.cs プロジェクト: gitter-badger/x2clr
        private bool ParseCellProperty(CellDef def, XmlElement elem, string comments)
        {
            var name = elem.GetAttribute("name");
            var type = elem.GetAttribute("type");
            if (String.IsNullOrEmpty(name))
            {
                return false;
            }
            if (String.IsNullOrEmpty(type))
            {
                return false;
            }
            var property = new CellDef.Property();
            property.Name = name;
            property.DefaultValue = elem.InnerText.Trim();
            property.Comments = comments;
            def.Properties.Add(property);

            property.TypeSpec = Types.Parse(type);
            if (property.TypeSpec == null)
            {
                return false;
            }

            return true;
        }