Beispiel #1
0
        protected void Add_Properties(Col1 c)
        {
            foreach (var i in c.Items)
            {
                if ((i.CheckingFlags & Flags1.DoNotCreateProperty) != 0)
                {
                    continue;
                }
                var p = Add_Property(i.PropertyName, i.PropertyType, i.Description);
                i.PropertyCreated?.Invoke(p);
                if ((i.CheckingFlags & Flags1.NotNull) == 0)
                {
                    continue;
                }

                if (Target.Kind == CsNamespaceMemberKind.Class)
                {
                    if ((i.CheckingFlags & Flags1.AddNotNullAttributeToPropertyIfPossible) != 0)
                    {
                        p.WithAttribute(CsAttribute.Make <NotNullAttribute>(Target));
                    }
                }

                if ((i.CheckingFlags & Flags1.PropertyCanBeNull) != 0)
                {
                    p.WithAttribute(CsAttribute.Make <CanBeNullAttribute>(Target));
                }
            }
        }
        private Col1 GetConstructorProperties(int nr)
        {
            string GetExpressionPlus()
            {
                switch (Cfg.TypeName)
                {
                case "AreaUnit":
                    return(@"?.Replace('2', '²')");

                case "VolumeUnit":
                    return(@"?.Replace('3', '³')");

                default:
                    return(null);
                }
            }

            var expr = PropertyName.FirstLower() + GetExpressionPlus();

            if (nr == 1)
            {
                if (Related is null || !Related.IsPower2OrHigher)
                {
                    return(null);
                }
                var a = new[]
                {
                    new ConstructorParameterInfo("BaseUnit", Related.MyInfo.PowerOne.Unit.TypeName, null, "based on",
                                                 Flags1.NotNull,
                                                 property =>
                    {
                        property.AddRelatedUnitSourceAttribute(Target, RelatedUnitSourceUsage.DoNotUse, 0);
                    }),
                    new ConstructorParameterInfo(PropertyName, "string", expr, "name of unit",
                                                 Flags1.Optional | Flags1.DoNotAssignProperty | Flags1.DoNotCreateProperty)
                };
                var h         = new Col1(a);
                var powerChar = Ext.GetPowerSuffix(Related.MyInfo.Power);
                h.Writer2.WriteLine("unitName = unitName?.Trim();");
                h.Writer2.WriteLine(PropertyName + " = string.IsNullOrEmpty(unitName) ? baseUnit.UnitName + " + powerChar.CsEncode() + " : unitName;");
                return(h);
            }

            {
                var b = new[]
                {
                    new ConstructorParameterInfo(PropertyName, "string", expr, "name of unit",
                                                 Flags1.NormalizedString)
                };
                return(new Col1(b));
            }
        }
Beispiel #3
0
        protected override void GenerateOne()
        {
            _info           = GetInfo();
            _info2          = GetInfo2();
            Target.Kind     = CsNamespaceMemberKind.Class;
            Target.IsSealed = true;

            foreach (var i in GetImplementedInterfaces())
            {
                Target.ImplementedInterfaces.Add(i);
            }

            var name = new CsArguments(Cfg.UnitTypes.Unit.TypeName).MakeGenericType("IEquatable");

            Target.ImplementedInterfaces.Add(name);

            var pi = new[]
            {
                new ConstructorParameterInfo(_info.FirstPropertyName, _info.First.Unit.TypeName, null,
                                             _info.FirstPropertyName.Decamelize().ToLower()),
                new ConstructorParameterInfo(_info.SecondPropertyName, _info.Second.Unit.TypeName, null,
                                             _info.SecondPropertyName.Decamelize().ToLower())
            };
            var col1 = new Col1(pi);

            Add_Constructor(col1);
            Add_Properties(col1);
            Add_UnitNameProperty();
            Add_Equals();
            Add_GetHashCode(
                $"({_info.FirstPropertyName}.GetHashCode() * 397) ^ {_info.SecondPropertyName}.GetHashCode()");
            AddCommon_EqualityOperators();
            Add_ToString(PropertyName);

            Add_WithFirst();
            Add_WithSecond();

            Add_Decompose();
        }
        private Col1 GetPropertiesInfo(int nr)
        {
            IReadOnlyList <ConstructorParameterInfo> items = new[]
            {
                new ConstructorParameterInfo(PropName,
                                             "string",
                                             null,
                                             "name of unit",
                                             Flags1.NormalizedString | Flags1.AddNotNullAttributeToPropertyIfPossible)
            };

            if (nr == 0)
            {
                return(new Col1(items));
            }

            items = new[]
            {
                new ConstructorParameterInfo("BaseUnit",
                                             Cfg.Source.Unit.GetTypename(),
                                             null,
                                             "base unit",
                                             Flags1.NotNull | Flags1.PropertyCanBeNull),
                new ConstructorParameterInfo(PropName,
                                             "string",
                                             null,
                                             "name of unit",
                                             Flags1.TrimValue | Flags1.Optional | Flags1.DoNotAssignProperty | Flags1.DoNotCreateProperty)
            };

            var a                = new Col1(items);
            var forceValue       = new CsExpression("unitName");
            var takeFromBaseUnit = new CsExpression("1/".CsEncode()) + new CsExpression("baseUnit.UnitName");
            var c3               = new CsExpression("string.IsNullOrEmpty(unitName)").Conditional(takeFromBaseUnit, forceValue);

            a.Writer2.WriteAssign("UnitName", c3.Code);
            return(a);
        }
Beispiel #5
0
        protected void Add_Constructor(Col1 col)
        {
            var target = Target;
            var code   = new CsCodeWriter();
            var m      = target.AddConstructor("creates instance of " + target.Name);
            var c      = col.Writer1.Code;

            if (!string.IsNullOrEmpty(c))
            {
                code.WriteLine(c);
            }
            foreach (var i in col.Items)
            {
                var flags = i.CheckingFlags;
                code.CheckArgument(i.ArgName, flags.ConvertToArgChecking(), Target);
                var p = m.AddParam(i.PropertyName.FirstLower(), i.PropertyType, i.Description);
                if ((flags & Flags1.Optional) != 0)
                {
                    p.ConstValue = "null";
                }

                /*if ((flags & Flags1.TrimValue) != 0)
                 * {
                 *  code.WriteLine("{0} = {0}?.Trim();", i.ArgName);
                 *  flags &= ~Flags1.TrimValue;
                 *
                 *  if ((flags & Flags1.NotWhitespace) != 0)
                 *  {
                 *      flags &= ~Flags1.NotWhitespace;
                 *      flags |= Flags1.NotEmpty;
                 *  }
                 * }*/

                /*if ((flags & Flags1.NotNull) != 0)
                 * {
                 *  flags &= ~Flags1.NotNull;
                 *  p.Attributes.Add(CsAttribute.Make<NotNullAttribute>(target));
                 *  var throwCode = new Args($"nameof({i.ArgName})")
                 *      .Throw<NullReferenceException>(target);
                 *  code.SingleLineIf($"{i.ArgName} is null", throwCode);
                 * }*/

                /*if ((flags & Flags1.NotWhitespace) != 0)
                 * {
                 *  flags &= ~(Flags1.NotEmpty | Flags1.NotWhitespace);
                 *  // var m = nameof(string.IsNullOrWhiteSpace);
                 *  var throwCode = new Args($"nameof({i.ArgName})")
                 *      .Throw<ArgumentException>(target);
                 *  code.SingleLineIf($"string.IsNullOrWhiteSpace({i.ArgName})", throwCode);
                 *
                 *  flags &= ~(Flags1.NotNullOrWhitespace | Flags1.NotNullOrEmpty);
                 * }
                 *
                 * if ((flags & Flags1.NotEmpty) != 0)
                 * {
                 *  flags &= ~Flags1.NotEmpty;
                 *  //var m = nameof(string.IsNullOrEmpty);
                 *  var throwCode = new Args($"nameof({i.ArgName})")
                 *      .Throw<ArgumentException>(target);
                 *  code.SingleLineIf($"string.IsNullOrEmpty({i.ArgName})", throwCode);
                 *
                 *  flags &= ~(Flags1.NotNullOrWhitespace | Flags1.NotNullOrEmpty);
                 * }*/

                if ((flags & Flags1.DoNotAssignProperty) == 0)
                {
                    code.WriteAssign(i.PropertyName, i.Expression);
                }
            }

            c = col.Writer2.Code;
            if (!string.IsNullOrEmpty(c))
            {
                code.WriteLine(c);
            }

            m.WithBody(code);
        }