private void Add_StaticConstructor() { var cw = Ext.Create(GetType()); var array = new CsArguments(_relatedUnit.Units.Select(q => q.FieldName).ToArray()); array.CreateArray(cw, "All = "); var c = Target.AddConstructor().WithStatic().WithBody(cw); Target.AddField("All", AllPropertyTypeName).WithStatic().WithIsReadOnly() .Description = $"All known {Cfg.TargetUnitTypename.FirstLower()} units"; }
private void Add_AlternateConstructor() { var f = GenInfo.FirstPropertyName.FirstLower(); var s = GenInfo.SecondPropertyName.FirstLower(); var cw = new CsCodeWriter(); var code = new CsArguments(f, s).Create(GenInfo.Result.Unit); cw.WriteLine("{0} = {1};", ValuePropName, ValuePropName.FirstLower()); cw.WriteLine("{0} = {1};", UnitPropName, code); var m = Target.AddConstructor() .WithBody(cw); m.AddParam(ValuePropName.FirstLower(), ValuePropertyType); m.AddParam(f, GenInfo.First.Unit.GetTypename()); m.AddParam(s, GenInfo.Second.Unit.GetTypename()); }
protected override void GenerateOne() { Target.Kind = CsNamespaceMemberKind.Struct; Target.Description = $"Reprezentuje {Cfg.Description} w [{Cfg.Unit}]"; { var p = Target.AddProperty("Value", "double"); p.IsPropertyReadOnly = true; p.EmitField = false; p.MakeAutoImplementIfPossible = true; } { var c = Target.AddConstructor("").WithBody("Value = value;"); c.AddParam("value", "double"); } { Add_ImplicitOperator("double", Cfg.ClassName, "src.Value"); /* * var m = cl.AddMethod(CsMethod.Implicit, "double", "implicit converts double into " + Cfg.ClassName) * .WithBody("return src.Value;"); * m.AddParam("src", Cfg.ClassName); */ } { // operatory mnożenia przez liczbę var m = Target.AddMethod("*", Cfg.ClassName) .WithBody($"return new {Cfg.ClassName}(number * x.Value);"); m.AddParam("number", "double"); m.AddParam("x", Cfg.ClassName); // odwrócona wersja m = Target.AddMethod("*", Cfg.ClassName) .WithBody($"return new {Cfg.ClassName}(x.Value * number);"); m.AddParam("x", Cfg.ClassName); m.AddParam("number", "double"); // operator dzielenia m = Target.AddMethod("/", Cfg.ClassName) .WithBody($"return new {Cfg.ClassName}(x.Value / number);"); m.AddParam("x", Cfg.ClassName); m.AddParam("number", "double"); // operator dzielenia m = Target.AddMethod("/", "double") .WithBody("return x.Value / y.Value;"); m.AddParam("x", Cfg.ClassName); m.AddParam("y", Cfg.ClassName); } // operatory dodawania/odejmowania foreach (var op in "+-") { var m = Target.AddMethod(op.ToString(), Cfg.ClassName) .WithBody($"return new {Cfg.ClassName}(left.Value {op} right.Value);"); m.AddParam("left", Cfg.ClassName); m.AddParam("right", Cfg.ClassName); } // operator negacji { var m = Target.AddMethod("-", Cfg.ClassName) .WithBody($"return new {Cfg.ClassName}(-x.Value);"); m.AddParam("x", Cfg.ClassName); } { // operatory mnożenia przez inne jednostki foreach (var i in Cfg.MulsAndDivs) { AddOperatorMethod(Target, i); if (i.Operator == "*") { var swapped = i.GetSwap(); if (!i.Equals(swapped)) { AddOperatorMethod(Target, swapped); } } } } MakeToString(Target, $@"$""{{Value}} {Cfg.Unit}"""); }