示例#1
0
 public override void GenerateForCopy(FileGeneration fg, Accessor accessor, Accessor rhs, Accessor copyMaskAccessor, bool protectedMembers, bool deepCopy)
 {
     if (!deepCopy)
     {
         throw new NotImplementedException();
     }
     if (this.Nullable)
     {
         fg.AppendLine($"if (!{rhs}.TryGet(out var rhs{this.Name}item))");
         using (new BraceWrapper(fg))
         {
             fg.AppendLine($"{accessor} = null;");
         }
         fg.AppendLine("else");
         rhs = $"rhs{this.Name}item";
     }
     using (new BraceWrapper(fg, doIt: this.Nullable))
     {
         using (var args = new ArgsWrapper(fg,
                                           $"{accessor} = new GenderedItem<{this.SubTypeGeneration.TypeName(getter: false, needsCovariance: true)}{this.SubTypeGeneration.NullChar}>"))
         {
             if (this.isLoquiSingle)
             {
                 LoquiType loqui = this.SubTypeGeneration as LoquiType;
                 args.Add(subFg =>
                 {
                     loqui.GenerateTypicalMakeCopy(
                         subFg,
                         retAccessor: $"male: ",
                         rhsAccessor: $"{rhs}.Male{this.SubTypeGeneration.NullChar}",
                         copyMaskAccessor: $"{copyMaskAccessor}.Male",
                         deepCopy: deepCopy,
                         doTranslationMask: false);
                 });
                 args.Add(subFg =>
                 {
                     loqui.GenerateTypicalMakeCopy(
                         subFg,
                         retAccessor: $"female: ",
                         rhsAccessor: $"{rhs}.Female{this.SubTypeGeneration.NullChar}",
                         copyMaskAccessor: $"{copyMaskAccessor}.Female",
                         deepCopy: deepCopy,
                         doTranslationMask: false);
                 });
             }
             else
             {
                 args.Add($"male: {this.SubTypeGeneration.GetDuplicate($"{rhs}.Male")}");
                 args.Add($"female: {this.SubTypeGeneration.GetDuplicate($"{rhs}.Female")}");
             }
         }
     }
 }
示例#2
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);
        }
    }