示例#1
0
 public override void GenerateForEquals(StructuredStringBuilder sb, Accessor accessor, Accessor rhsAccessor, Accessor maskAccessor)
 {
     sb.AppendLine($"if ({GetTranslationIfAccessor(maskAccessor)})");
     using (sb.CurlyBrace())
     {
         if (SubTypeGeneration is LoquiType subLoq &&
             subLoq.TargetObjectGeneration != null)
         {
             sb.AppendLine($"if (!{accessor.Access}.{(Nullable ? nameof(ICollectionExt.SequenceEqualNullable) : nameof(ICollectionExt.SequenceEqual))}({rhsAccessor.Access}, (l, r) => {subLoq.TargetObjectGeneration.CommonClassSpeccedInstance("l", LoquiInterfaceType.IGetter, CommonGenerics.Class, subLoq.GenericSpecification)}.Equals(l, r, {maskAccessor}?.GetSubCrystal({IndexEnumInt})))) return false;");
         }
示例#2
0
 public override void GenerateToString(StructuredStringBuilder sb, string name, Accessor accessor, string sbAccessor)
 {
     sb.AppendLine($"sb.{nameof(StructuredStringBuilder.AppendLine)}(\"{name} =>\");");
     sb.AppendLine($"using (sb.Brace())");
     using (sb.CurlyBrace())
     {
         sb.AppendLine($"foreach (var subItem in {accessor})");
         using (sb.CurlyBrace())
         {
             sb.AppendLine($"using ({sbAccessor}.IncreaseDepth())");
             sb.AppendLine($"using ({sbAccessor}.Brace())");
             using (sb.CurlyBrace())
             {
                 ValueTypeGen.GenerateToString(sb, "Item", new Accessor("subItem.Value"), sbAccessor);
             }
         }
     }
     sb.AppendLine($"sb.{nameof(StructuredStringBuilder.AppendLine)}(\"]\");");
 }
示例#3
0
 public override void GenerateForCopy(StructuredStringBuilder sb, Accessor accessor, Accessor rhs, Accessor copyMaskAccessor, bool protectedMembers, bool deepCopy)
 {
     if (!AlwaysCopy)
     {
         sb.AppendLine($"if ({(deepCopy ? GetTranslationIfAccessor(copyMaskAccessor) : SkipCheck(copyMaskAccessor, deepCopy))})");
     }
     using (sb.CurlyBrace(doIt: !AlwaysCopy))
     {
         sb.AppendLine($"{accessor.Access} = {rhs};");
     }
 }
示例#4
0
 public override void GenerateForCopy(
     StructuredStringBuilder sb,
     Accessor accessor,
     Accessor rhs,
     Accessor copyMaskAccessor,
     bool protectedMembers,
     bool deepCopy)
 {
     if (!AlwaysCopy)
     {
         sb.AppendLine($"if ({(deepCopy ? GetTranslationIfAccessor(copyMaskAccessor) : SkipCheck(copyMaskAccessor, deepCopy))})");
     }
     using (sb.CurlyBrace(doIt: !AlwaysCopy))
     {
         MaskGenerationUtility.WrapErrorFieldIndexPush(
             sb,
             () =>
         {
             if (Nullable)
             {
                 sb.AppendLine($"if(rhs.{Name} is {{}} {Name}rhs)");
                 using (sb.CurlyBrace())
                 {
                     sb.AppendLine($"{accessor.Access} = {Name}rhs{(deepCopy ? null : ".Value")}.ToArray();");
                 }
                 sb.AppendLine("else");
                 using (sb.CurlyBrace())
                 {
                     sb.AppendLine($"{accessor.Access} = default;");
                 }
             }
             else
             {
                 sb.AppendLine($"{accessor.Access} = {rhs}.ToArray();");
             }
         },
             errorMaskAccessor: "errorMask",
             indexAccessor: HasIndex?IndexEnumInt: default(Accessor),
             doIt: CopyNeedsTryCatch);
     }
 }
示例#5
0
 public override void GenerateForEquals(StructuredStringBuilder sb, Accessor accessor, Accessor rhsAccessor, Accessor maskAccessor)
 {
     if (!IntegrateField)
     {
         return;
     }
     sb.AppendLine($"if ({GetTranslationIfAccessor(maskAccessor)})");
     using (sb.CurlyBrace())
     {
         sb.AppendLine($"if ({GenerateEqualsSnippet(accessor, rhsAccessor, negate: true)}) return false;");
     }
 }
示例#6
0
 public virtual void GenerateMaskToString(StructuredStringBuilder sb, TypeGeneration field, Accessor accessor, bool topLevel, bool printMask)
 {
     if (!field.IntegrateField)
     {
         return;
     }
     if (printMask)
     {
         sb.AppendLine($"if ({GenerateBoolMaskCheck(field, "printMask")})");
     }
     using (sb.CurlyBrace(printMask))
     {
         sb.AppendLine($"sb.{nameof(StructuredStringBuilder.AppendItem)}({accessor}{(string.IsNullOrWhiteSpace(field.Name) ? null : $", \"{field.Name}\"")});");
示例#7
0
文件: ListType.cs 项目: Noggog/Loqui
 public override async Task GenerateForClass(StructuredStringBuilder sb)
 {
     sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
     sb.AppendLine($"private {TypeName(getter: false)}{NullChar} _{Name}{(Nullable ? null : $" = {GetActualItemClass(ctor: true)}")};");
     Comments?.Apply(sb, LoquiInterfaceType.Direct);
     sb.AppendLine($"public {TypeName(getter: false)}{NullChar} {Name}");
     using (sb.CurlyBrace())
     {
         sb.AppendLine($"get => this._{Name};");
         sb.AppendLine($"{((ReadOnly || !Nullable) ? "init" : "set")} => this._{Name} = value;");
     }
     GenerateInterfaceMembers(sb, $"_{Name}");
 }
示例#8
0
 protected override async Task GenerateNewSnippet(ObjectGeneration obj, StructuredStringBuilder sb)
 {
     if (obj.Abstract)
     {
         sb.AppendLine($"if (!LoquiXmlTranslation.Instance.TryCreate<{obj.Name}>(node, out var ret, errorMask, translationMask))");
         using (sb.CurlyBrace())
         {
             sb.AppendLine($"throw new ArgumentException($\"Unknown {obj.Name} subclass: {{node.Name.LocalName}}\");");
         }
     }
     else
     {
         sb.AppendLine($"var ret = new {obj.Name}{obj.GetGenericTypes(MaskType.Normal)}();");
     }
 }
示例#9
0
 public override void GenerateForHash(StructuredStringBuilder sb, Accessor accessor, string hashResultAccessor)
 {
     if (!IntegrateField)
     {
         return;
     }
     if (Nullable)
     {
         sb.AppendLine($"if ({accessor} is {{}} {Name}Item)");
         accessor = $"{Name}Item";
     }
     using (sb.CurlyBrace(doIt: Nullable))
     {
         sb.AppendLine($"{hashResultAccessor}.Add({accessor});");
     }
 }
示例#10
0
    public override void GenerateForHash(StructuredStringBuilder sb, Accessor accessor, string hashResultAccessor)
    {
        if (!IntegrateField)
        {
            return;
        }
        var doIf = Nullable && CanBeNullable(getter: true);

        if (doIf)
        {
            sb.AppendLine($"if ({accessor} is {{}} {Name}item)");
            accessor = $"{Name}item";
        }
        using (sb.CurlyBrace(doIt: doIf))
        {
            sb.AppendLine($"{hashResultAccessor}.Add({accessor});");
        }
    }
示例#11
0
文件: ArrayType.cs 项目: Noggog/Loqui
 public override void GenerateForEquals(StructuredStringBuilder sb, Accessor accessor, Accessor rhsAccessor, Accessor maskAccessor)
 {
     sb.AppendLine($"if ({GetTranslationIfAccessor(maskAccessor)})");
     using (sb.CurlyBrace())
     {
         if (SubTypeGeneration.IsIEquatable)
         {
             if (Nullable)
             {
                 sb.AppendLine($"if (!{nameof(ObjectExt)}.{nameof(ObjectExt.NullSame)}({accessor}, {rhsAccessor})) return false;");
             }
             sb.AppendLine($"if (!MemoryExtensions.SequenceEqual<{SubTypeGeneration.TypeName(getter: true)}>({accessor.Access}{(Nullable ? "!.Value" : null)}.Span!, {rhsAccessor.Access}{(Nullable ? "!.Value" : null)}.Span!)) return false;");
         }
         else
         {
             sb.AppendLine($"if (!{accessor}.{nameof(EnumerableExt.SequenceEqualNullable)}({rhsAccessor})) return false;");
         }
     }
 }
示例#12
0
    public override void GenerateForAny(StructuredStringBuilder sb, TypeGeneration field, Accessor accessor, bool nullCheck, bool indexed)
    {
        LoquiType loqui = field as LoquiType;

        if (nullCheck)
        {
            sb.AppendLine($"if ({field.Name} != null)");
        }
        using (sb.CurlyBrace(doIt: nullCheck))
        {
            sb.AppendLine($"if (eval({accessor.Access}.Overall)) return true;");
            if (!IsUnknownGeneric(loqui))
            {
                sb.AppendLine($"if ({accessor.Access}.Specific != null && {accessor.Access}.Specific.Any(eval)) return true;");
            }
            else
            {
                sb.AppendLine($"if (({accessor.Access}.Specific?.Any(eval) ?? false)) return true;");
            }
        }
    }
示例#13
0
 public static void WrapErrorFieldIndexPush(
     StructuredStringBuilder sb,
     Action toDo,
     Accessor errorMaskAccessor,
     Accessor indexAccessor,
     bool doIt = true)
 {
     if (!doIt)
     {
         toDo();
         return;
     }
     if (!string.IsNullOrWhiteSpace(indexAccessor.Access))
     {
         sb.AppendLine($"{errorMaskAccessor}?.PushIndex({indexAccessor});");
     }
     sb.AppendLine("try");
     using (sb.CurlyBrace())
     {
         toDo();
     }
     GenerateExceptionCatcher(sb, errorMaskAccessor);
 }
示例#14
0
    protected override async Task GenerateCtor(StructuredStringBuilder sb)
    {
        if (BasicCtorPermission == CtorPermissionLevel.noGeneration)
        {
            return;
        }
        if (BasicCtorPermission == CtorPermissionLevel.@public)
        {
            sb.AppendLine($"public {Name}(");
            List <string> lines = new List <string>();
            foreach (var field in IterateFields())
            {
                lines.Add($"{field.TypeName(getter: false)} {field.Name} = default({field.TypeName(getter: false)})");
            }
            for (int i = 0; i < lines.Count; i++)
            {
                using (sb.IncreaseDepth())
                {
                    using (sb.Line())
                    {
                        sb.Append(lines[i]);
                        if (i != lines.Count - 1)
                        {
                            sb.Append(",");
                        }
                        else
                        {
                            sb.Append(")");
                        }
                    }
                }
            }

            using (sb.CurlyBrace())
            {
                foreach (var field in IterateFields())
                {
                    sb.AppendLine($"this.{field.Name} = {field.Name};");
                }
                foreach (var mod in gen.GenerationModules)
                {
                    await mod.GenerateInCtor(this, sb);
                }
                sb.AppendLine("CustomCtor();");
            }
            sb.AppendLine();
        }

        sb.AppendLine($"{BasicCtorPermission.ToStringFast_Enum_Only()} {Name}({Interface(getter: true)} rhs)");
        using (sb.CurlyBrace())
        {
            foreach (var field in IterateFields())
            {
                sb.AppendLine($"this.{field.Name} = {field.GenerateACopy("rhs." + field.Name)};");
            }
        }
        sb.AppendLine();

        sb.AppendLine("partial void CustomCtor();");
        sb.AppendLine();
    }
示例#15
0
文件: ClassType.cs 项目: Noggog/Loqui
 public override async Task GenerateForClass(StructuredStringBuilder sb)
 {
     if (!IntegrateField)
     {
         return;
     }
     if (Nullable)
     {
         if (CanBeNullable(false))
         {
             if (!TrueReadOnly)
             {
                 sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
                 sb.AppendLine($"protected {TypeName(getter: false)}? _{Name};");
                 Comments?.Apply(sb, LoquiInterfaceType.Direct);
                 sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)}? {Name}");
                 using (sb.CurlyBrace())
                 {
                     sb.AppendLine($"get => this._{ Name};");
                     sb.AppendLine($"{SetPermissionStr}set => this._{Name} = value;");
                 }
                 sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
                 if (CanBeNullable(getter: true))
                 {
                     sb.AppendLine($"{TypeName(getter: true)}? {ObjectGen.Interface(getter: true, InternalGetInterface)}.{Name} => this.{Name};");
                 }
                 else
                 {
                     sb.AppendLine($"{TypeName(getter: true)} {ObjectGen.Interface(getter: true, InternalGetInterface)}.{Name} => this.{Name};");
                     sb.AppendLine($"bool {ObjectGen.Interface(getter: true, InternalGetInterface)}.{Name}_IsSet => this.{Name} != null;");
                 }
             }
             else
             {
                 Comments?.Apply(sb, LoquiInterfaceType.Direct);
                 sb.AppendLine($"public readonly {TypeName(getter: false)}? {Name};");
                 sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
             }
         }
         else
         {
             throw new NotImplementedException();
         }
     }
     else
     {
         sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
         sb.AppendLine($"private {TypeName(getter: false)} _{Name}{(IsNullable ? string.Empty : $" = {GetNewForNonNullable()}")};");
         Comments?.Apply(sb, LoquiInterfaceType.Direct);
         if (Singleton)
         {
             sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)} {Name} => {ProtectedName};");
         }
         else
         {
             sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)} {Name}");
             using (sb.CurlyBrace())
             {
                 sb.AppendLine($"get => {ProtectedName};");
                 sb.AppendLine($"{SetPermissionStr}set => this._{Name} = value;");
             }
         }
         if (TypeName(getter: true) != TypeName(getter: false))
         {
             sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
             sb.AppendLine($"{TypeName(getter: true)} {ObjectGen.Interface(getter: true, InternalGetInterface)}.{Name} => this.{Name};");
         }
     }
 }
示例#16
0
文件: ListType.cs 项目: Noggog/Loqui
    public override void GenerateForCopy(
        StructuredStringBuilder sb,
        Accessor accessor,
        Accessor rhs,
        Accessor copyMaskAccessor,
        bool protectedMembers,
        bool deepCopy)
    {
        void GenerateSet()
        {
            if (isLoquiSingle)
            {
                if (deepCopy)
                {
                    LoquiType loqui = SubTypeGeneration as LoquiType;
                    WrapSet(sb, accessor, (f) =>
                    {
                        f.AppendLine(rhs.ToString());
                        f.AppendLine(".Select(r =>");
                        using (new CurlyBrace(f)
                        {
                            AppendParenthesis = true
                        })
                        {
                            loqui.GenerateTypicalMakeCopy(
                                f,
                                retAccessor: $"return ",
                                rhsAccessor: Accessor.FromType(loqui, "r"),
                                copyMaskAccessor: copyMaskAccessor,
                                deepCopy: deepCopy,
                                doTranslationMask: false);
                        }
                    });
                }
                else
                {
                    LoquiType loqui = SubTypeGeneration as LoquiType;
                    using (var args = sb.Call(
                               $"{accessor}.SetTo<{SubTypeGeneration.TypeName(getter: false)}, {SubTypeGeneration.TypeName(getter: false)}>"))
                    {
                        args.Add($"items: {rhs}");
                        args.Add((gen) =>
                        {
                            gen.AppendLine("converter: (r) =>");
                            using (new CurlyBrace(gen))
                            {
                                var supportsCopy = loqui.SupportsMask(MaskType.Copy);
                                var accessorStr  = $"copyMask?.{Name}{(supportsCopy ? ".Overall" : string.Empty)}";
                                gen.AppendLine($"switch ({accessorStr} ?? {nameof(CopyOption)}.{nameof(CopyOption.Reference)})");
                                using (new CurlyBrace(gen))
                                {
                                    gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.Reference)}:");
                                    using (gen.IncreaseDepth())
                                    {
                                        gen.AppendLine($"return ({loqui.TypeName()})r;");
                                    }
                                    gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.MakeCopy)}:");
                                    using (gen.IncreaseDepth())
                                    {
                                        loqui.GenerateTypicalMakeCopy(
                                            gen,
                                            retAccessor: $"return ",
                                            rhsAccessor: new Accessor("r"),
                                            copyMaskAccessor: copyMaskAccessor,
                                            deepCopy: deepCopy,
                                            doTranslationMask: false);
                                    }
                                    gen.AppendLine($"default:");
                                    using (gen.IncreaseDepth())
                                    {
                                        gen.AppendLine($"throw new NotImplementedException($\"Unknown {nameof(CopyOption)} {{{accessorStr}}}. Cannot execute copy.\");");
                                    }
                                }
                            }
                        });
                    }
                }
            }
            else
            {
                WrapSet(sb, accessor, (f) =>
                {
                    f.AppendLine($"rhs.{Name}");
                    SubTypeGeneration.GenerateCopySetToConverter(f);
                });
            }
        }

        if (!AlwaysCopy)
        {
            sb.AppendLine($"if ({(deepCopy ? GetTranslationIfAccessor(copyMaskAccessor) : SkipCheck(copyMaskAccessor, deepCopy))})");
        }
        using (sb.CurlyBrace(doIt: !AlwaysCopy))
        {
            MaskGenerationUtility.WrapErrorFieldIndexPush(
                sb,
                () =>
            {
                if (Nullable)
                {
                    sb.AppendLine($"if ({NullableAccessor(getter: false, rhs)})");
                    using (sb.CurlyBrace())
                    {
                        GenerateSet();
                    }
                    sb.AppendLine("else");
                    using (sb.CurlyBrace())
                    {
                        GenerateClear(sb, accessor);
                    }
                }
                else
                {
                    GenerateSet();
                }
            },
                errorMaskAccessor: "errorMask",
                indexAccessor: HasIndex?IndexEnumInt: default(Accessor),
                doIt: CopyNeedsTryCatch);
        }
    }
示例#17
0
    public virtual void GenerateWriteToNode(ObjectGeneration obj, StructuredStringBuilder sb)
    {
        using (var args = new Function(sb,
                                       $"public static void WriteToNode{ModuleNickname}{obj.GetGenericTypes(MaskType.Normal)}"))
        {
            args.Add($"{obj.Interface(internalInterface: true, getter: true)} item");
            args.Add($"XElement {XElementLine.GetParameterName(obj, Context.Backend)}");
            args.Add($"ErrorMaskBuilder? errorMask");
            args.Add($"{nameof(TranslationCrystal)}? translationMask");
        }
        using (sb.CurlyBrace())
        {
            if (obj.HasLoquiBaseObject)
            {
                using (var args = sb.Call(
                           $"{TranslationWriteClass(obj.BaseClass)}.WriteToNode{ModuleNickname}"))
                {
                    args.Add($"item: item");
                    args.Add($"{XElementLine.GetParameterName(obj, Context.Backend)}: {XElementLine.GetParameterName(obj, Context.Backend)}");
                    args.Add($"errorMask: errorMask");
                    args.Add($"translationMask: translationMask");
                }
            }
            foreach (var field in obj.IterateFieldIndices())
            {
                if (!TryGetTypeGeneration(field.Field.GetType(), out var generator))
                {
                    throw new ArgumentException("Unsupported type generator: " + field.Field);
                }

                if (!generator.ShouldGenerateWrite(field.Field))
                {
                    continue;
                }

                List <string> conditions = new List <string>();
                if (field.Field.Nullable)
                {
                    conditions.Add($"{field.Field.NullableAccessor(getter: true, accessor: Accessor.FromType(field.Field, "item"))}");
                }
                if (TranslationMaskParameter)
                {
                    conditions.Add(field.Field.GetTranslationIfAccessor("translationMask"));
                }
                if (conditions.Count > 0)
                {
                    using (var args = sb.If(ands: true))
                    {
                        foreach (var item in conditions)
                        {
                            args.Add(item);
                        }
                    }
                }
                using (sb.CurlyBrace(doIt: conditions.Count > 0))
                {
                    var maskType = Gen.MaskModule.GetMaskModule(field.Field.GetType()).GetErrorMaskTypeStr(field.Field);
                    generator.GenerateWrite(
                        sb: sb,
                        objGen: obj,
                        typeGen: field.Field,
                        writerAccessor: $"{XElementLine.GetParameterName(obj, Context.Backend)}",
                        itemAccessor: Accessor.FromType(field.Field, "item"),
                        errorMaskAccessor: $"errorMask",
                        translationMaskAccessor: "translationMask",
                        nameAccessor: $"nameof(item.{field.Field.Name})");
                }
            }
        }
        sb.AppendLine();
    }
    public override void GenerateWrite(
        StructuredStringBuilder sb,
        ObjectGeneration objGen,
        TypeGeneration typeGen,
        Accessor writerAccessor,
        Accessor itemAccessor,
        Accessor errorMaskAccessor,
        Accessor nameAccessor,
        Accessor translationMaskAccessor)
    {
        using (sb.CurlyBrace(doIt: !XmlMod.TranslationMaskParameter))
        {
            if (typeGen.Nullable)
            {
                sb.AppendLine($"if ({itemAccessor.Access} is {{}} {typeGen.Name}Item)");
                itemAccessor = $"{typeGen.Name}Item";
            }
            else
            {
                // We want to cache retrievals, in case it's a wrapper being written
                sb.AppendLine($"var {typeGen.Name}Item = {itemAccessor.Access};");
                itemAccessor = $"{typeGen.Name}Item";
            }

            using (sb.CurlyBrace(doIt: typeGen.Nullable))
            {
                var    loquiGen = typeGen as LoquiType;
                string line;
                if (loquiGen.TargetObjectGeneration != null)
                {
                    line = $"(({XmlMod.TranslationWriteClassName(loquiGen.TargetObjectGeneration)})(({nameof(IXmlItem)}){typeGen.Name}Item).{XmlMod.TranslationWriteItemMember})";
                }
                else
                {
                    line = $"(({XmlMod.TranslationWriteInterface})(({nameof(IXmlItem)}){typeGen.Name}Item).{XmlMod.TranslationWriteItemMember})";
                }
                using (var args = sb.Call($"{line}.Write{loquiGen.GetGenericTypes(getter: true, additionalMasks: new MaskType[] { MaskType.Normal })}"))
                {
                    args.Add($"item: {typeGen.Name}Item");
                    args.Add($"{XmlTranslationModule.XElementLine.GetParameterName(objGen, Context.Backend)}: {writerAccessor}");
                    args.Add($"name: {nameAccessor}");
                    if (typeGen.HasIndex)
                    {
                        args.Add($"fieldIndex: (int){typeGen.IndexEnumName}");
                    }
                    args.Add($"errorMask: {errorMaskAccessor}");
                    if (XmlMod.TranslationMaskParameter)
                    {
                        if (typeGen.HasIndex)
                        {
                            args.Add($"translationMask: {translationMaskAccessor}?.GetSubCrystal({typeGen.IndexEnumInt})");
                        }
                        else
                        {
                            args.Add($"translationMask: {translationMaskAccessor}");
                        }
                    }
                }
            }
        }
    }
示例#19
0
    protected virtual void FillPublicElement(ObjectGeneration obj, StructuredStringBuilder sb)
    {
        using (var args = new Function(sb,
                                       $"public static void FillPublicElement{ModuleNickname}"))
        {
            args.Add($"{obj.Interface(getter: false)} item");
            args.Add($"XElement {XElementLine.GetParameterName(obj, Context.Backend)}");
            args.Add("string name");
            args.Add($"ErrorMaskBuilder? errorMask");
            args.Add($"{nameof(TranslationCrystal)}? translationMask");
        }
        using (sb.CurlyBrace())
        {
            sb.AppendLine("switch (name)");
            using (sb.CurlyBrace())
            {
                foreach (var field in obj.IterateFields())
                {
                    if (field.Derivative)
                    {
                        continue;
                    }
                    if (field.ReadOnly)
                    {
                        continue;
                    }
                    if (!TryGetTypeGeneration(field.GetType(), out var generator))
                    {
                        throw new ArgumentException("Unsupported type generator: " + field);
                    }

                    sb.AppendLine($"case \"{field.Name}\":");
                    using (sb.IncreaseDepth())
                    {
                        if (generator.ShouldGenerateCopyIn(field))
                        {
                            List <string> conditions = new List <string>();
                            if (TranslationMaskParameter)
                            {
                                conditions.Add(field.GetTranslationIfAccessor("translationMask"));
                            }
                            if (conditions.Count > 0)
                            {
                                using (var args = sb.If(ands: true))
                                {
                                    foreach (var item in conditions)
                                    {
                                        args.Add(item);
                                    }
                                }
                            }
                            using (sb.CurlyBrace(doIt: conditions.Count > 0))
                            {
                                generator.GenerateCopyIn(
                                    sb: sb,
                                    objGen: obj,
                                    typeGen: field,
                                    nodeAccessor: XElementLine.GetParameterName(obj, Context.Backend).Result,
                                    itemAccessor: Accessor.FromType(field, "item"),
                                    translationMaskAccessor: "translationMask",
                                    errorMaskAccessor: $"errorMask");
                            }
                        }
                        sb.AppendLine("break;");
                    }
                }

                sb.AppendLine("default:");
                using (sb.IncreaseDepth())
                {
                    if (obj.HasLoquiBaseObject)
                    {
                        using (var args = sb.Call(
                                   $"{obj.BaseClass.CommonClassName(LoquiInterfaceType.ISetter)}.FillPublicElement{ModuleNickname}{obj.GetBaseMask_GenericTypes(MaskType.Error)}"))
                        {
                            args.Add("item: item");
                            args.Add($"{XElementLine.GetParameterName(obj, Context.Backend)}: {XElementLine.GetParameterName(obj, Context.Backend)}");
                            args.Add("name: name");
                            args.Add("errorMask: errorMask");
                            if (TranslationMaskParameter)
                            {
                                args.Add($"translationMask: translationMask");
                            }
                        }
                    }
                    sb.AppendLine("break;");
                }
            }
        }
        sb.AppendLine();
    }
示例#20
0
    protected override async Task GenerateCopyInSnippet(ObjectGeneration obj, StructuredStringBuilder sb, Accessor accessor)
    {
        sb.AppendLine("try");
        using (sb.CurlyBrace())
        {
            await PreCreateLoop(obj, sb);

            sb.AppendLine($"foreach (var elem in {XElementLine.GetParameterName(obj, Context.Backend)}.Elements())");
            using (sb.CurlyBrace())
            {
                if (obj.IterateFields(includeBaseClass: true).Any(f => f.ReadOnly))
                {
                    using (var args = sb.Call(
                               $"FillPrivateElement{ModuleNickname}"))
                    {
                        args.Add($"item: {accessor}");
                        args.Add($"{XElementLine.GetParameterName(obj, Context.Backend)}: elem");
                        args.Add("name: elem.Name.LocalName");
                        foreach (var item in MainAPI.ReaderAPI.CustomAPI)
                        {
                            if (!item.API.TryGetPassthrough(obj, TranslationDirection.Reader, Context.Backend, out var passthrough))
                            {
                                continue;
                            }
                            args.Add(passthrough);
                        }
                        args.Add("errorMask: errorMask");
                        if (TranslationMaskParameter)
                        {
                            args.Add("translationMask: translationMask");
                        }
                    }
                }
                using (var args = sb.Call(
                           $"{TranslationCreateClass(obj)}.FillPublicElement{ModuleNickname}"))
                {
                    args.Add($"item: {accessor}");
                    args.Add($"{XElementLine.GetParameterName(obj, Context.Backend)}: elem");
                    args.Add("name: elem.Name.LocalName");
                    foreach (var item in MainAPI.ReaderAPI.CustomAPI)
                    {
                        if (!item.API.TryGetPassthrough(obj, TranslationDirection.Reader, Context.Backend, out var passthrough))
                        {
                            continue;
                        }
                        args.Add(passthrough);
                    }
                    args.Add("errorMask: errorMask");
                    if (TranslationMaskParameter)
                    {
                        args.Add("translationMask: translationMask");
                    }
                }
            }
            await PostCreateLoop(obj, sb);
        }
        sb.AppendLine("catch (Exception ex)");
        sb.AppendLine("when (errorMask != null)");
        using (sb.CurlyBrace())
        {
            sb.AppendLine("errorMask.ReportException(ex);");
        }
    }
示例#21
0
 public override void GenerateForCopy(
     StructuredStringBuilder sb,
     Accessor accessor,
     Accessor rhs,
     Accessor copyMaskAccessor,
     bool protectedMembers,
     bool deepCopy)
 {
     if (!AlwaysCopy)
     {
         sb.AppendLine($"if ({(deepCopy ? GetTranslationIfAccessor(copyMaskAccessor) : SkipCheck(copyMaskAccessor, deepCopy))})");
     }
     using (sb.CurlyBrace(doIt: !AlwaysCopy))
     {
         MaskGenerationUtility.WrapErrorFieldIndexPush(
             sb,
             () =>
         {
             var loqui = ValueTypeGen as LoquiType;
             if (Nullable)
             {
                 using (var args = sb.Call(
                            $"{accessor}.SetTo"))
                 {
                     args.Add($"rhs.{Name}");
                     args.Add((gen) =>
                     {
                         gen.AppendLine("(r) =>");
                         using (new CurlyBrace(gen))
                         {
                             gen.AppendLine($"switch (copyMask?.{Name}.Overall ?? {nameof(CopyOption)}.{nameof(CopyOption.Reference)})");
                             using (new CurlyBrace(gen))
                             {
                                 gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.Reference)}:");
                                 using (gen.IncreaseDepth())
                                 {
                                     gen.AppendLine("return r;");
                                 }
                                 gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.MakeCopy)}:");
                                 using (gen.IncreaseDepth())
                                 {
                                     loqui.GenerateTypicalMakeCopy(
                                         gen,
                                         retAccessor: $"return ",
                                         rhsAccessor: new Accessor("r"),
                                         copyMaskAccessor: copyMaskAccessor,
                                         deepCopy: deepCopy,
                                         doTranslationMask: false);
                                 }
                                 gen.AppendLine($"default:");
                                 using (gen.IncreaseDepth())
                                 {
                                     gen.AppendLine($"throw new NotImplementedException($\"Unknown {nameof(CopyOption)} {{copyMask?.{Name}.Overall}}. Cannot execute copy.\");");
                                 }
                             }
                         }
                     });
                 }
             }
             else
             {
                 using (var args = sb.Call(
                            $"{accessor}.SetTo"))
                 {
                     args.Add((gen) =>
                     {
                         gen.AppendLine($"rhs.{Name}.Items");
                         using (gen.IncreaseDepth())
                         {
                             gen.AppendLine(".Select((r) =>");
                             using (new CurlyBrace(gen)
                             {
                                 AppendParenthesis = true
                             })
                             {
                                 if (deepCopy)
                                 {
                                     loqui.GenerateTypicalMakeCopy(
                                         gen,
                                         retAccessor: $"return ",
                                         rhsAccessor: new Accessor("r"),
                                         copyMaskAccessor: copyMaskAccessor,
                                         deepCopy: deepCopy,
                                         doTranslationMask: false);
                                 }
                                 else
                                 {
                                     gen.AppendLine($"switch (copyMask?.{Name}.Overall ?? {nameof(CopyOption)}.{nameof(CopyOption.Reference)})");
                                     using (new CurlyBrace(gen))
                                     {
                                         gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.Reference)}:");
                                         using (gen.IncreaseDepth())
                                         {
                                             gen.AppendLine("return r;");
                                         }
                                         gen.AppendLine($"case {nameof(CopyOption)}.{nameof(CopyOption.MakeCopy)}:");
                                         using (gen.IncreaseDepth())
                                         {
                                             loqui.GenerateTypicalMakeCopy(
                                                 gen,
                                                 retAccessor: $"return ",
                                                 rhsAccessor: new Accessor("r"),
                                                 copyMaskAccessor: copyMaskAccessor,
                                                 deepCopy: deepCopy,
                                                 doTranslationMask: false);
                                         }
                                         gen.AppendLine($"default:");
                                         using (gen.IncreaseDepth())
                                         {
                                             gen.AppendLine($"throw new NotImplementedException($\"Unknown {nameof(CopyOption)} {{copyMask?.{Name}.Overall}}. Cannot execute copy.\");");
                                         }
                                     }
                                 }
                             }
                         }
                     });
                 }
             }
         },
             errorMaskAccessor: "errorMask",
             indexAccessor: HasIndex?IndexEnumInt: default(Accessor),
             doIt: CopyNeedsTryCatch);
     }
 }
示例#22
0
    public override void GenerateForCopy(
        StructuredStringBuilder sb,
        Accessor accessor,
        Accessor rhs,
        Accessor copyMaskAccessor,
        bool protectedMembers,
        bool deepCopy)
    {
        if (!AlwaysCopy)
        {
            sb.AppendLine($"if ({(deepCopy ? GetTranslationIfAccessor(copyMaskAccessor) : SkipCheck(copyMaskAccessor, deepCopy))})");
        }
        using (sb.CurlyBrace(doIt: !AlwaysCopy))
        {
            if (!KeyIsLoqui && !ValueIsLoqui)
            {
                using (var args = sb.Call(
                           $"{accessor}.SetTo"))
                {
                    args.Add($"rhs.{Name}");
                }
                return;
            }
            if (deepCopy)
            {
                if (KeyIsLoqui)
                {
                    throw new NotImplementedException();
                }
                using (var args = sb.Call(
                           $"{accessor}.SetTo"))
                {
                    args.Add((gen) =>
                    {
                        gen.AppendLine($"rhs.{Name}");
                        using (gen.IncreaseDepth())
                        {
                            gen.AppendLine(".Select((r) =>");
                            using (new CurlyBrace(gen)
                            {
                                AppendParenthesis = true
                            })
                            {
                                (ValueTypeGen as LoquiType).GenerateTypicalMakeCopy(
                                    gen,
                                    retAccessor: $"var value = ",
                                    rhsAccessor: new Accessor("r.Value"),
                                    copyMaskAccessor: copyMaskAccessor,
                                    deepCopy: deepCopy,
                                    doTranslationMask: false);
                                gen.AppendLine($"return new KeyValuePair<{KeyTypeGen.TypeName(getter: true)}, {ValueTypeGen.TypeName(getter: false)}>(r.Key, value);");
                            }
                        }
                    });
                }
                return;
            }
            throw new NotImplementedException();
            MaskGenerationUtility.WrapErrorFieldIndexPush(
                sb,
                () =>
            {
                if (!KeyIsLoqui && !ValueIsLoqui)
                {
                    using (var args = sb.Call(
                               $"{accessor}.SetTo"))
                    {
                        args.Add($"rhs.{Name}");
                    }
                    return;
                }
                using (var args = sb.Call(
                           $"{accessor}.SetTo"))
                {
                    args.Add((gen) =>
                    {
                        gen.AppendLine($"rhs.{Name}");
                        using (gen.IncreaseDepth())
                        {
                            gen.AppendLine(".Select((r) =>");
                            using (new CurlyBrace(gen)
                            {
                                AppendParenthesis = true
                            })
                            {
                                if (KeyIsLoqui)
                                {
                                    throw new NotImplementedException();
                                    gen.AppendLine($"{KeyTypeGen.TypeName(getter: false)} key;");
                                    gen.AppendLine($"switch ({copyMaskAccessor}?.Specific.{(BothAreLoqui ? "Key." : string.Empty)}Type ?? {nameof(RefCopyType)}.{nameof(RefCopyType.Reference)})");
                                    using (new CurlyBrace(gen))
                                    {
                                        gen.AppendLine($"case {nameof(RefCopyType)}.{nameof(RefCopyType.Reference)}:");
                                        using (gen.IncreaseDepth())
                                        {
                                            gen.AppendLine($"key = r.Key;");
                                            gen.AppendLine($"break;");
                                        }
                                        gen.AppendLine($"case {nameof(RefCopyType)}.{nameof(RefCopyType.MakeCopy)}:");
                                        using (gen.IncreaseDepth())
                                        {
                                            gen.AppendLine($"key = r.Key.Copy(copyMask: {copyMaskAccessor}?.Specific.{(BothAreLoqui ? "Key." : string.Empty)}Mask);");
                                            gen.AppendLine($"break;");
                                        }
                                        gen.AppendLine($"default:");
                                        using (gen.IncreaseDepth())
                                        {
                                            gen.AppendLine($"throw new NotImplementedException($\"Unknown {nameof(RefCopyType)} {{copyMask?.{Name}.Overall}}. Cannot execute copy.\");");
                                        }
                                    }
                                }
                                if (ValueTypeGen is LoquiType valLoqui)
                                {
                                    gen.AppendLine($"{ValueTypeGen.TypeName(getter: false)} val;");
                                    gen.AppendLine($"switch ({copyMaskAccessor}?.Specific.{(BothAreLoqui ? "Value." : string.Empty)}Type ?? {nameof(RefCopyType)}.{nameof(RefCopyType.Reference)})");
                                    using (new CurlyBrace(gen))
                                    {
                                        gen.AppendLine($"case {nameof(RefCopyType)}.{nameof(RefCopyType.Reference)}:");
                                        using (gen.IncreaseDepth())
                                        {
                                            gen.AppendLine($"val = r.Value;");
                                            gen.AppendLine($"break;");
                                        }
                                        gen.AppendLine($"case {nameof(RefCopyType)}.{nameof(RefCopyType.MakeCopy)}:");
                                        using (gen.IncreaseDepth())
                                        {
                                            valLoqui.GenerateTypicalMakeCopy(
                                                gen,
                                                retAccessor: $"val = ",
                                                rhsAccessor: new Accessor("r.Value"),
                                                copyMaskAccessor: copyMaskAccessor,
                                                deepCopy: deepCopy,
                                                doTranslationMask: false);
                                            gen.AppendLine($"break;");
                                        }
                                        gen.AppendLine($"default:");
                                        using (gen.IncreaseDepth())
                                        {
                                            gen.AppendLine($"throw new NotImplementedException($\"Unknown {nameof(RefCopyType)} {{copyMask?.{Name}.Overall}}. Cannot execute copy.\");");
                                        }
                                    }
                                }

                                gen.AppendLine($"return new KeyValuePair<{KeyTypeGen.TypeName(getter: false)}, {ValueTypeGen.TypeName(getter: false)}>({(KeyIsLoqui ? "key" : "r.Key")}, {(ValueIsLoqui ? "val" : "r.Value")});");
                            }
                        }
                    });
                }
            },
                errorMaskAccessor: "errorMask",
                indexAccessor: HasIndex?IndexEnumInt: default(Accessor),
                doIt: CopyNeedsTryCatch);
        }
    }
示例#23
0
    public override async Task GenerateForClass(StructuredStringBuilder sb)
    {
        void GenerateTypicalNullableMembers(bool notifying)
        {
            Comments?.Apply(sb, LoquiInterfaceType.Direct);
            sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)}{NullChar} {Name} {{ get; {(ReadOnly ? "protected " : string.Empty)}set; }}{(HasDefault ? $" = {DefaultValueMemberName};" : null)}");
            sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
            sb.AppendLine($"{TypeName(getter: true)}{NullChar} {ObjectGen.Interface(getter: true, internalInterface: InternalGetInterface)}.{Name} => this.{Name};");
        }

        if (HasDefault)
        {
            Comments?.Apply(sb, LoquiInterfaceType.Direct);
            sb.AppendLine($"public readonly static {TypeName(getter: false)} {DefaultValueMemberName} = {DefaultValue};");
        }
        if (Nullable)
        {
            if (!TrueReadOnly)
            {
                GenerateTypicalNullableMembers(false);
            }
            else
            {
                Comments?.Apply(sb, LoquiInterfaceType.Direct);
                sb.AppendLine($"public readonly {TypeName(getter: false)} {Name};");
                sb.AppendLine($"{TypeName(getter: false)} {ObjectGen.Interface(getter: true, internalInterface: InternalGetInterface)}.{Name} => this.{Name};");
            }
        }
        else
        {
            var subFg = new StructuredStringBuilder();
            WrapSetAccessor(subFg,
                            linePrefix: $"{SetPermissionStr}set",
                            toDo: subGen =>
            {
                if (subGen.Count == 0)
                {
                    return;
                }
                subGen.AppendLine($"this._{Name} = value;");
            });
            if (subFg.Count > 1)
            {
                sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
                sb.AppendLine($"private {TypeName(getter: false)} _{Name};");
                Comments?.Apply(sb, LoquiInterfaceType.Direct);
                sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)} {Name}");
                using (sb.CurlyBrace())
                {
                    sb.AppendLine($"get => this._{Name};");
                    sb.AppendLines(subFg);
                }
            }
            else if (subFg.Count == 1)
            {
                Comments?.Apply(sb, LoquiInterfaceType.Direct);
                sb.AppendLine($"public {OverrideStr}{TypeName(getter: false)} {Name} {{ get; {subFg[0]}; }} = {(HasDefault ? DefaultValueMemberName : GetDefault(getter: false))};");
            }
            else
            {
                throw new ArgumentException();
            }
            if (!InternalGetInterface && TypeName(getter: true) != TypeName(getter: false))
            {
                sb.AppendLine($"{TypeName(getter: true)} {ObjectGen.Interface(getter: true, internalInterface: InternalGetInterface)}.{Name} => this.{Name};");
            }
        }
        if (InternalSetInterface)
        {
            sb.AppendLine($"[DebuggerBrowsable(DebuggerBrowsableState.Never)]");
            sb.AppendLine($"{TypeName(getter: false)}{NullChar} {ObjectGen.Interface(getter: false, internalInterface: true)}.{Name}");
            using (sb.CurlyBrace())
            {
                sb.AppendLine($"get => this.{Name};");
                sb.AppendLine($"set => this.{Name} = {GetValueSetString("value")};");
            }
        }
        if (InternalGetInterface)
        {
            if (Nullable)
            {
                if (CanBeNullable(getter: true))
                {
                    sb.AppendLine($"{TypeName(getter: false)}? {ObjectGen.Interface(getter: true, internalInterface: true)}.{Name} => this.{Name}");
                }
                else
                {
                    sb.AppendLine($"{TypeName(getter: false)} {ObjectGen.Interface(getter: true, internalInterface: true)}.{Name} => this.{Name}");
                    sb.AppendLine($"bool {ObjectGen.Interface(getter: true, internalInterface: true)}.{Name}_IsSet => this.{Name} != null");
                }
            }
            else
            {
                sb.AppendLine($"{TypeName(getter: false)} {ObjectGen.Interface(getter: true, internalInterface: true)}.{Name} => this.{Name}");
            }
        }
    }