Exemplo n.º 1
0
        public static void Add_FromMethods(Type t, XValueTypeName valueTypeName,
                                           TypesGroup types, CsClass target, IRelatedUnitDefinition u)
        {
#if DEBUG
            if (target.Name == "Power")
            {
                System.Diagnostics.Debug.Write("");
            }
#endif
            foreach (var inputType in "decimal,double,int,long".Split(','))
            {
                var arg = "value";
                if (inputType == OtherValuePropertyType)
                {
                    arg = $"({ValuePropertyType}){arg}";
                }

                var args = new CsArguments(arg, types.Container + "." + u.FieldName)
                           .Create(target.Name);
                var valueIn           = $" value in {u.UnitShortCode.EffectiveValue}";
                var methodName        = "From" + u.FromMethodNameSufix.CoalesceNullOrWhiteSpace(u.FieldName);
                var methodDescription = $"creates {types.Value.FirstLower()} from{valueIn}";

                var cw = Ext.Create(t);
                cw.WriteReturn(args);
                var m = target.AddMethod(methodName, target.Name, methodDescription)
                        .WithStatic()
                        .WithBody(cw);
                m.AddParam("value", inputType).Description = string.Format("{0}{1}", valueTypeName, valueIn);
            }
        }
        private void Add_ConvertOtherPower()
        {
            var infos = RelatedUnitGeneratorDefs.All;

            var tmp = infos.GetPowers(Cfg);

            if (tmp?.MyInfo is null || tmp.Other.Count == 0)
            {
                return;
            }
            foreach (var i in tmp.Other.Values)
            {
                var targetUnit = new TypesGroup(i.Name);
                if (Cfg == targetUnit.Unit)
                {
                    continue;
                }
                var cw   = Ext.Create <BasicUnitGenerator>();
                var code = new CsArguments(Target.Name, targetUnit.Unit.GetTypename())
                           .MakeGenericTypeMethodCall("GlobalUnitRegistry.Relations.GetOrThrow", "this");
                cw.WriteReturn(code);
                var m = Target.AddMethod("Get" + targetUnit.Unit.TypeName, targetUnit.Unit.GetTypename()).WithBody(cw);
                m.WithAggressiveInlining(Target);

                m.AddRelatedUnitSourceAttribute(Target, RelatedUnitSourceUsage.ProvidesRelatedUnit, 10);
            }
        }
Exemplo n.º 3
0
        public InversedUnit(Type type)
        {
            Source = new TypesGroup(new XValueTypeName(type.Name));
            const string inversed = "Inversed";
            var          a        = new TypesGroup(new XValueTypeName(inversed + type.Name));
            var          b        = new TypesGroup(new XValueTypeName(inversed + Source.Value.CoreName));

            Target = new TypesGroup(a.Value, b.Unit, b.Container);
        }
Exemplo n.º 4
0
        private void Add_NumberDiv()
        {
            TypesGroup invTypes             = Cfg.Source;
            var        unitConversionMethod = "Get" + invTypes.Unit.TypeName;
            var        resultUnit           = $"value.Unit.{unitConversionMethod}()";

            Target.AddOperator("/", new CsArguments("number / value.Value", resultUnit),
                               invTypes.Value.GetTypename())
            .WithLeftRightArguments(ValuePropertyType, Target.Name, "number", "value");
        }
Exemplo n.º 5
0
 public OperatorParamsBase(TypesGroup left, TypesGroup right, TypesGroup result, string leftMethodArgumentName,
                           string rightMethodArgumentName, string oper)
 {
     Left   = left;
     Right  = right;
     Result = result;
     LeftMethodArgumentName  = leftMethodArgumentName;
     RightMethodArgumentName = rightMethodArgumentName;
     Oper = oper;
 }
 public CompositeUnitGeneratorInfo(string firstPropertyName, TypesGroup first,
                                   string secondPropertyName,
                                   TypesGroup second,
                                   TypesGroup result)
 {
     FirstPropertyName  = firstPropertyName;
     SecondPropertyName = secondPropertyName;
     First  = first;
     Second = second;
     Result = result;
 }
Exemplo n.º 7
0
 public static ProductUnit IsProduct(TypesGroup right)
 {
     foreach (var i in All.Items)
     {
         if (i.UnitTypes.Value == right.Value)
         {
             return(i);
         }
     }
     return(null);
 }
Exemplo n.º 8
0
        protected void Add_Round(TypesGroup names)
        {
            var cs = Ext.Create <BasicUnitValuesGenerator>();

            cs.WriteLine($"var parseResult = CommonParse.Parse(value, typeof({names.Value}));");
            cs.WriteLine(
                $"return new {names.Value}(parseResult.Value, new {names.Unit}(parseResult.UnitName));");
            Target.AddMethod("Round", names.Value.ValueTypeName)
            .WithBodyFromExpression("new " + names.Value + "(Math.Round(Value, decimalPlaces), Unit)")
            .AddParam <int>("decimalPlaces", Target);
        }
 public static FractionUnit IsFraction(TypesGroup right)
 {
     foreach (var i in All.Items)
     {
         if (i.UnitTypes.Value == right.Value)
         {
             return(i);
         }
     }
     return(null);
 }
Exemplo n.º 10
0
        public RelatedUnitsFamily GetPowers(XUnitTypeName unitName)
        {
            var myInfo = FindByUnitName(unitName);

            if (myInfo is null)
            {
                return(null);
            }
            var other = new Dictionary <int, RelatedUnit>();

            var powerOneUnit = myInfo.Power == 1
                ? new TypesGroup(myInfo.Name)
                : myInfo.PowerOne;

            if (powerOneUnit is null)
            {
                throw new NotImplementedException();
            }

            var powerOneUnitUnitName = powerOneUnit.Unit;

            foreach (var i in Items)
            {
                if (i.Name == myInfo.Name)
                {
                    continue;
                }
                if (i.Power == 1)
                {
                    var coo = new TypesGroup(i.Name).Unit;
                    if (coo != powerOneUnitUnitName)
                    {
                        continue;
                    }
                }
                else
                {
                    if (powerOneUnitUnitName != i.PowerOne?.Unit)
                    {
                        continue;
                    }
                }

                other[i.Power] = i;
            }

            return(new RelatedUnitsFamily(myInfo, other));
        }
Exemplo n.º 11
0
        private RelatedUnit FindByUnitName(XUnitTypeName unitName)
        {
            RelatedUnit myInfo = null;

            foreach (var i in Items)
            {
                var t = new TypesGroup(i.Name);
                if (t.Unit != unitName)
                {
                    continue;
                }
                myInfo = i;
                break;
            }

            if (myInfo is null)
            {
                return(null);
            }
            //throw new NotImplementedException();
            return(myInfo);
        }
Exemplo n.º 12
0
        private RelatedUnit WithPowerDerivedUnits(int power, PrefixedUnitInfo[] items, XValueTypeName[] values)
        {
            Power = power;
            if (power != 1)
            {
                PowerOne = new TypesGroup(values[0]);
            }

            var propertyPrefix = Prefix(power);

            foreach (var i in items)
            {
                var fromMethodNameSufix = i.FromMethodNameSufix.AddPrefix(propertyPrefix);
                var unitShortCodeSource = UnitShortCodeSource.MakePower(i.UnitShortCode, power);
                var q = new AliasedPrefixedUnitInfo(propertyPrefix + i.FieldName,
                                                    unitShortCodeSource,
                                                    Mul(power, i.ScaleFactor), fromMethodNameSufix, null);
                Units.Add(q);
            }

            if (power <= 1)
            {
                return(this);
            }
            for (var i = 1; i < power; i++)
            {
                var otherUnitContainer = values[i - 1] + "Unit";
                var myUnitContainer    = values[power - 1] + "Unit";
                var relation           = new UnitNamePrefixRelation(
                    Prefix(i), Prefix(power),
                    myUnitContainer, otherUnitContainer);
                PrefixRelations.Add(relation);
            }

            return(this);
        }
Exemplo n.º 13
0
 public BasicUnit(string valueTypeName, string mainUnit, bool isComparable)
 {
     UnitTypes    = new TypesGroup(new XValueTypeName(valueTypeName));
     IsComparable = isComparable;
     BaseUnit     = new TypeAndFieldName(UnitTypes.Container.GetTypename(), mainUnit);
 }
Exemplo n.º 14
0
 private FractionUnit(TypesGroup unitTypes, TypesGroup counterUnit, TypesGroup denominatorUnit)
 {
     UnitTypes       = unitTypes;
     CounterUnit     = counterUnit;
     DenominatorUnit = denominatorUnit;
 }
Exemplo n.º 15
0
 public ThermalUnit(Type type, Type inversed)
 {
     Source   = new TypesGroup(new XValueTypeName(type.Name));
     Inversed = new TypesGroup(new XValueTypeName(inversed.Name));
 }
Exemplo n.º 16
0
 public OperatorParams(TypesGroup left, TypesGroup right, TypesGroup result, string leftMethodArgumentName,
                       string rightMethodArgumentName, string oper, OperatorHints operatorHints) : base(left, right, result,
                                                                                                        leftMethodArgumentName, rightMethodArgumentName, oper)
 {
     OperatorHints = operatorHints;
 }
Exemplo n.º 17
0
 private ProductUnit(TypesGroup unitTypes, TypesGroup counterUnit, TypesGroup denominatorUnit)
 {
     UnitTypes       = unitTypes;
     CounterUnit     = counterUnit;
     DenominatorUnit = denominatorUnit;
 }
        private void CreateOperator(TypesGroup left, TypesGroup right, TypesGroup result, string op,
                                    bool areRelatedUnits, NullableArguments nullableArguments, OperatorHints operatorHints)
        {
            var leftValue  = left.Value.ValueTypeName;
            var rightValue = right.Value.ValueTypeName;

            if ((nullableArguments & NullableArguments.Left) != 0)
            {
                leftValue += "?";
            }
            if ((nullableArguments & NullableArguments.Right) != 0)
            {
                rightValue += "?";
            }

            var operatorGenerationKey = new OperatorGenerationKey(leftValue, rightValue, op);

            var tResult = result.Value;

            if (_done.TryGetValue(operatorGenerationKey, out var resultType))
            {
                if (resultType != tResult.ValueTypeName)
                {
                    throw new NotSupportedException();
                }
                return;
            }

            _done[operatorGenerationKey] = tResult.ValueTypeName;

            var info = op == "/" ? OperatorInfo.Div : OperatorInfo.Mul;

            var leftName  = info.Left.Name;
            var rightName = info.Right.Name;

            if (left.Value != right.Value)
            {
                leftName  = left.Value.FirstLower();
                rightName = right.Value.FirstLower();
            }

            var cl = GetClass(operatorGenerationKey.GetOperatorTargetType());

            cl.Kind = CsNamespaceMemberKind.Struct;

            // cw.WriteLine("// " + key + " " + areRelatedUnits);

            var rightFraction = FractionUnitDefs.IsFraction(right);
            var leftFraction  = FractionUnitDefs.IsFraction(left);

            var rightUnit  = "rightUnit";
            var resultUnit = "resultUnit";

            CsCodeWriter PrepareCode()
            {
                if (nullableArguments == NullableArguments.None)
                {
                    var ppp = new OperatorParams(left, right, result, leftName, rightName, op, operatorHints);
                    if (areRelatedUnits)
                    {
                        return(CreateCodeForRelatedUnits(ppp, ref rightUnit, ref resultUnit));
                    }
                    if (leftFraction != null)
                    {
                        if (rightFraction == null)
                        {
                            return(CreateCodeForLeftFractionValue(ppp, leftFraction));
                        }

                        {
                            var cw = Ext.Create <Self>();

                            if (TryHint(ppp, cw))
                            {
                                cw.WriteLine("// scenario F1");
                                return(cw);
                            }

                            cw.WriteLine("// scenario F2");
                            cw.WithThrowNotImplementedException();
                            return(cw);
                        }
                    }

                    if (rightFraction != null)
                    {
                        return(CreateCodeForRightFractionValue(rightFraction, ppp));
                    }

                    {
                        var cw = Ext.Create <Self>();

                        if (TryHint(ppp, cw))
                        {
                            cw.WriteLine("// scenario F3");
                            return(cw);
                        }
                    }
                    return(CreateCodeForFractionalResult(ppp));
                }

                return(NullableArgument.CreateCode(leftName, rightName, op, nullableArguments));
            }

            var cw1 = PrepareCode();

            var method = cl.AddMethod(op, tResult + (nullableArguments == NullableArguments.None ? "" : "?"),
                                      info.Description)
                         .WithBody(cw1);

            var p = method.AddParam(leftName, leftValue);

            p.Description = info.Left.Desctiption;
            p             = method.AddParam(rightName, rightValue);
            p.Description = info.Right.Desctiption;
        }