protected virtual void WriteClass(ClassObject classObject)
        {
            var generics    = CodeGeneratorUtils.CreateGenericClassString(classObject.Generics);
            var constraints = CodeGeneratorUtils.CreateGenericConstraintsString(classObject.Generics);

            this.WriteClassObjectMethod(generics, constraints, classObject);

            writer.Indent++;
            WriteFields(classObject);
            writer.Indent--;

            writer.WriteLine("}");
        }
Example #2
0
        public static void ReadFieldsToCtor(Manager manager, ClassObject classObject, IndentedTextWriter writer, ReadFieldHandler readFieldHandler)
        {
            foreach (var field in classObject.Fields)
            {
                readFieldHandler(field);
            }

            var ctorArgs = string.Join(", ", classObject.CtorFields.Select(f => f.Field.SafeCamelCaseName));
            var generics = CodeGeneratorUtils.CreateGenericClassString(classObject.Generics);

            writer.WriteLine();
            writer.WriteLine($"return new {classObject.FullName.TrimNameSpace(manager.NameSpace)}{generics}({ctorArgs});");
        }
        protected virtual void WriteBaseClass(ClassBaseObject classBaseObject)
        {
            var generics    = CodeGeneratorUtils.CreateGenericClassString(classBaseObject.Generics);
            var constraints = CodeGeneratorUtils.CreateGenericConstraintsString(classBaseObject.Generics);

            if (!classBaseObject.Subclasses.Any())
            {
                writer.WriteLine($"// No derived classes found for base class: {classBaseObject.FullName.Value}");
                return;
            }

            var interfaceName = this.TrimNameSpace(classBaseObject.InterfaceName.IsEmpty ? classBaseObject.FullName : classBaseObject.InterfaceName);

            this.WriterClassBaseObjectMethod(interfaceName, generics, constraints, classBaseObject);
            writer.WriteLine("{");

            writer.Indent++;
            var addElse = false;

            foreach (var subclass in classBaseObject.Subclasses)
            {
                if (addElse)
                {
                    writer.Write("else ");
                }

                var castedName = $"input{subclass.Subclass.FullName.Value.Replace(".", "")}";
                writer.WriteLine($"if (input is {this.TrimNameSpace(subclass.Subclass.FullName)} {castedName})");
                writer.WriteLine("{");
                writer.Indent++;
                var paramName = $"{this.TrimNameSpace(subclass.Subclass.FullName)}.{classBaseObject.TypeDiscriminator.Name}";

                this.WriteBaseClassHandler(classBaseObject, subclass, castedName);

                writer.Indent--;
                writer.WriteLine("}");

                addElse = true;
            }

            writer.WriteLine("else");
            writer.WriteLine("{");
            writer.Indent++;
            writer.WriteLine("throw new Exception(\"Unknown base class type\");");
            writer.Indent--;
            writer.WriteLine("}");
            writer.Indent--;

            writer.WriteLine("}");
        }
        protected virtual void ReadBaseClass(ClassBaseObject classBaseObject)
        {
            var readName    = this.MakeReadValueMethod(classBaseObject.FullName);
            var generics    = CodeGeneratorUtils.CreateGenericClassString(classBaseObject.Generics);
            var constraints = CodeGeneratorUtils.CreateGenericConstraintsString(classBaseObject.Generics);

            this.WriteReadClassBaseMethod(classBaseObject, readName, generics, constraints);
            writer.WriteLine("{");
            writer.Indent++;

            this.WriteReadBaseClassTypeHandler(classBaseObject);

            var addElse = false;

            foreach (var subclassPair in classBaseObject.Subclasses)
            {
                if (addElse)
                {
                    writer.Write("else ");
                }

                var paramName = $"{subclassPair.Subclass.FullName}.{classBaseObject.TypeDiscriminator.Name}";
                writer.WriteLine($"if (type == {paramName})");
                writer.WriteLine("{");
                writer.Indent++;
                if (manager.ClassMap.TryGetValue(subclassPair.Subclass.FullName, out var fieldClass))
                {
                    this.WriteReadBaseClassHandler(classBaseObject, subclassPair);
                }
                else
                {
                    throw new Exception("Unknown subclass!");
                }
                writer.Indent--;
                writer.WriteLine("}");

                addElse = true;
            }

            writer.WriteLine("else");
            writer.WriteLine("{");
            writer.Indent++;
            writer.WriteLine("throw new Exception(\"Unknown base class type\");");
            writer.Indent--;
            writer.WriteLine("}");

            writer.Indent--;
            writer.WriteLine("}");
        }
        protected virtual void ReadClass(ClassObject classObject)
        {
            var readName    = this.MakeReadValueMethod(classObject.FullName);
            var generics    = CodeGeneratorUtils.CreateGenericClassString(classObject.Generics);
            var constraints = CodeGeneratorUtils.CreateGenericConstraintsString(classObject.Generics);

            writer.Write($"public static {this.TrimNameSpace(classObject.FullName)}{generics} {readName}{generics}({this.ReadObject} input)");
            writer.WriteLine(constraints);
            writer.WriteLine("{");

            writer.Indent++;
            this.ReadClassInner(classObject);
            writer.Indent--;

            writer.WriteLine("}");
        }