Skip to content

Commit f2b4a46

Browse files
committed
Remove decimal support from CodeGen
1 parent d60bae3 commit f2b4a46

File tree

7 files changed

+45
-131
lines changed

7 files changed

+45
-131
lines changed

CodeGen/Generators/NanoFrameworkGen/QuantityGenerator.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public struct {_quantity.Name}
4343
/// <summary>
4444
/// The numeric value this quantity was constructed with.
4545
/// </summary>
46-
private readonly {_quantity.ValueType} _value;
46+
private readonly double _value;
4747
4848
/// <summary>
4949
/// The unit this quantity was constructed with.
@@ -53,7 +53,7 @@ public struct {_quantity.Name}
5353
/// <summary>
5454
/// The numeric value this quantity was constructed with.
5555
/// </summary>
56-
public {_quantity.ValueType} Value => _value;
56+
public double Value => _value;
5757
5858
/// <inheritdoc />
5959
public {_unitEnumName} Unit => _unit;
@@ -66,7 +66,7 @@ public struct {_quantity.Name}
6666
/// <param name=""value"">The numeric value to construct this quantity with.</param>
6767
/// <param name=""unit"">The unit representation to construct this quantity with.</param>
6868
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>
69-
public {_quantity.Name}({_quantity.ValueType} value, {_unitEnumName} unit)
69+
public {_quantity.Name}(double value, {_unitEnumName} unit)
7070
{{
7171
_value = value;
7272
_unit = unit;
@@ -79,29 +79,14 @@ public struct {_quantity.Name}
7979
8080
/// <summary>
8181
/// Represents the largest possible value of Duration
82-
/// </summary>");
83-
84-
// Non decimal
85-
Writer.WLCondition(_quantity.ValueType != "decimal", $@"
86-
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MaxValue, BaseUnit);
87-
88-
/// <summary>
89-
/// Represents the smallest possible value of Duration
9082
/// </summary>
91-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}({_quantity.ValueType}.MinValue, BaseUnit);
92-
");
93-
94-
// Decimal MaxValue = 79228162514264337593543950335M
95-
Writer.WLCondition(_quantity.ValueType == "decimal", $@"
96-
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}(79228162514264337593543950335M, BaseUnit);
83+
public static {_quantity.Name} MaxValue {{ get; }} = new {_quantity.Name}(double.MaxValue, BaseUnit);
9784
9885
/// <summary>
9986
/// Represents the smallest possible value of Duration
10087
/// </summary>
101-
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(-79228162514264337593543950335M, BaseUnit);
102-
");
88+
public static {_quantity.Name} MinValue {{ get; }} = new {_quantity.Name}(double.MinValue, BaseUnit);
10389
104-
Writer.WL($@"
10590
/// <summary>
10691
/// Gets an instance of this quantity with a value of 0 in the base unit Second.
10792
/// </summary>
@@ -134,7 +119,7 @@ private void GenerateConversionProperties()
134119
/// </summary>");
135120
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
136121
Writer.WL($@"
137-
public {_quantity.ValueType} {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
122+
public double {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
138123
");
139124
}
140125

@@ -161,7 +146,7 @@ private void GenerateStaticFactoryMethods()
161146
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>");
162147
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
163148
Writer.WL($@"
164-
public static {_quantity.Name} From{unit.PluralName}({_quantity.ValueType} {valueParamName}) => new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
149+
public static {_quantity.Name} From{unit.PluralName}(double {valueParamName}) => new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
165150
");
166151
}
167152

@@ -172,7 +157,7 @@ private void GenerateStaticFactoryMethods()
172157
/// <param name=""value"">Value to convert from.</param>
173158
/// <param name=""fromUnit"">Unit to convert from.</param>
174159
/// <returns>{_quantity.Name} unit value.</returns>
175-
public static {_quantity.Name} From({_quantity.ValueType} value, {_unitEnumName} fromUnit)
160+
public static {_quantity.Name} From(double value, {_unitEnumName} fromUnit)
176161
{{
177162
return new {_quantity.Name}(value, fromUnit);
178163
}}
@@ -190,7 +175,7 @@ private void GenerateConversionMethods()
190175
/// Convert to the unit representation <paramref name=""unit"" />.
191176
/// </summary>
192177
/// <returns>Value converted to the specified unit.</returns>
193-
public {_quantity.ValueType} As({_unitEnumName} unit) => GetValueAs(unit);
178+
public double As({_unitEnumName} unit) => GetValueAs(unit);
194179
195180
/// <summary>
196181
/// Converts this Duration to another Duration with the unit representation <paramref name=""unit"" />.
@@ -207,7 +192,7 @@ private void GenerateConversionMethods()
207192
/// This is typically the first step in converting from one unit to another.
208193
/// </summary>
209194
/// <returns>The value in the base unit representation.</returns>
210-
private {_quantity.ValueType} GetValueInBaseUnit()
195+
private double GetValueInBaseUnit()
211196
{{
212197
return Unit switch
213198
{{");
@@ -223,7 +208,7 @@ private void GenerateConversionMethods()
223208
}};
224209
}}
225210
226-
private {_quantity.ValueType} GetValueAs({_unitEnumName} unit)
211+
private double GetValueAs({_unitEnumName} unit)
227212
{{
228213
if (Unit == unit)
229214
return _value;

CodeGen/Generators/NanoFrameworkGenerator.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,6 @@ public static void Generate(string rootDir, Quantity[] quantities, QuantityNameT
9090
GenerateQuantity(quantity, Path.Combine(outputQuantities, $"{quantity.Name}.g.cs"));
9191
GenerateProject(quantity, Path.Combine(projectPath, $"{quantity.Name}.nfproj"), versions);
9292

93-
// Convert decimal based units to floats; decimals are not supported by nanoFramework
94-
if (quantity.ValueType == "decimal")
95-
{
96-
var replacements = new Dictionary<string, string>
97-
{
98-
{ "(\\d)m", "$1d" },
99-
{ "(\\d)M", "$1d" },
100-
{ " decimal ", " double " },
101-
{ "(decimal ", "(double " }
102-
};
103-
new FileInfo(Path.Combine(outputDir, "Units", $"{quantity.Name}Unit.g.cs")).EditFile(replacements);
104-
new FileInfo(Path.Combine(outputDir, "Quantities", $"{quantity.Name}.g.cs")).EditFile(replacements);
105-
}
106-
10793
Log.Information("✅ {Quantity} (nanoFramework)", quantity.Name);
10894
}
10995
Log.Information("");

CodeGen/Generators/QuantityJsonFilesParser.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ private static Quantity ParseQuantityFile(string jsonFileName)
4848
?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFileName}");
4949

5050
AddPrefixUnits(quantity);
51-
FixConversionFunctionsForDecimalValueTypes(quantity);
5251
OrderUnitsByName(quantity);
5352
return quantity;
5453
}
@@ -63,20 +62,6 @@ private static void OrderUnitsByName(Quantity quantity)
6362
quantity.Units = quantity.Units.OrderBy(u => u.SingularName, StringComparer.OrdinalIgnoreCase).ToArray();
6463
}
6564

66-
private static void FixConversionFunctionsForDecimalValueTypes(Quantity quantity)
67-
{
68-
foreach (Unit u in quantity.Units)
69-
// Use decimal for internal calculations if base type is not double, such as for long or int.
70-
{
71-
if (string.Equals(quantity.ValueType, "decimal", StringComparison.OrdinalIgnoreCase))
72-
{
73-
// Change any double literals like "1024d" to decimal literals "1024m"
74-
u.FromUnitToBaseFunc = u.FromUnitToBaseFunc.Replace("d", "m");
75-
u.FromBaseToUnitFunc = u.FromBaseToUnitFunc.Replace("d", "m");
76-
}
77-
}
78-
}
79-
8065
private static void AddPrefixUnits(Quantity quantity)
8166
{
8267
var unitsToAdd = new List<Unit>();

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class QuantityGenerator : GeneratorBase
1414

1515
private readonly bool _isDimensionless;
1616
private readonly string _unitEnumName;
17-
private readonly string _valueType;
1817
private readonly Unit _baseUnit;
1918

2019
public QuantityGenerator(Quantity quantity)
@@ -25,7 +24,6 @@ public QuantityGenerator(Quantity quantity)
2524
throw new ArgumentException($"No unit found with SingularName equal to BaseUnit [{_quantity.BaseUnit}]. This unit must be defined.",
2625
nameof(quantity));
2726

28-
_valueType = quantity.ValueType;
2927
_unitEnumName = $"{quantity.Name}Unit";
3028

3129
BaseDimensions baseDimensions = quantity.BaseDimensions;
@@ -67,9 +65,6 @@ namespace UnitsNet
6765
public readonly partial struct {_quantity.Name} :
6866
{(_quantity.GenerateArithmetic ? "IArithmeticQuantity" : "IQuantity")}<{_quantity.Name}, {_unitEnumName}>,");
6967

70-
if (_quantity.ValueType == "decimal") Writer.WL(@$"
71-
IDecimalQuantity,");
72-
7368
Writer.WL(@$"
7469
IComparable,
7570
IComparable<{_quantity.Name}>,
@@ -83,7 +78,7 @@ namespace UnitsNet
8378
/// The numeric value this quantity was constructed with.
8479
/// </summary>
8580
[DataMember(Name = ""Value"", Order = 0)]
86-
private readonly {_quantity.ValueType} _value;
81+
private readonly double _value;
8782
8883
/// <summary>
8984
/// The unit this quantity was constructed with.
@@ -175,7 +170,7 @@ private void GenerateInstanceConstructors()
175170
/// <param name=""value"">The numeric value to construct this quantity with.</param>
176171
/// <param name=""unit"">The unit representation to construct this quantity with.</param>
177172
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>
178-
public {_quantity.Name}({_quantity.ValueType} value, {_unitEnumName} unit)
173+
public {_quantity.Name}(double value, {_unitEnumName} unit)
179174
{{");
180175
Writer.WL(@"
181176
_value = value;");
@@ -191,7 +186,7 @@ private void GenerateInstanceConstructors()
191186
/// <param name=""unitSystem"">The unit system to create the quantity with.</param>
192187
/// <exception cref=""ArgumentNullException"">The given <see cref=""UnitSystem""/> is null.</exception>
193188
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
194-
public {_quantity.Name}({_valueType} value, UnitSystem unitSystem)
189+
public {_quantity.Name}(double value, UnitSystem unitSystem)
195190
{{
196191
if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));
197192
@@ -262,7 +257,7 @@ private void GenerateProperties()
262257
/// <summary>
263258
/// The numeric value this quantity was constructed with.
264259
/// </summary>
265-
public {_valueType} Value => _value;
260+
public double Value => _value;
266261
267262
/// <inheritdoc />
268263
double IQuantity.Value => _value;
@@ -298,11 +293,11 @@ private void GenerateConversionProperties()
298293

299294
Writer.WL($@"
300295
/// <summary>
301-
/// Gets a <see cref=""{_quantity.ValueType}""/> value of this quantity converted into <see cref=""{_unitEnumName}.{unit.SingularName}""/>
296+
/// Gets a <see cref=""double""/> value of this quantity converted into <see cref=""{_unitEnumName}.{unit.SingularName}""/>
302297
/// </summary>");
303298
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
304299
Writer.WL($@"
305-
public {_quantity.ValueType} {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
300+
public double {unit.PluralName} => As({_unitEnumName}.{unit.SingularName});
306301
");
307302
}
308303

@@ -397,8 +392,7 @@ private void GenerateStaticFactoryMethods()
397392
Writer.WL($@"
398393
public static {_quantity.Name} From{unit.PluralName}(double {valueParamName})
399394
{{
400-
{_valueType} value = ({_valueType}) {valueParamName};
401-
return new {_quantity.Name}(value, {_unitEnumName}.{unit.SingularName});
395+
return new {_quantity.Name}({valueParamName}, {_unitEnumName}.{unit.SingularName});
402396
}}
403397
");
404398
}
@@ -412,7 +406,7 @@ private void GenerateStaticFactoryMethods()
412406
/// <returns>{_quantity.Name} unit value.</returns>
413407
public static {_quantity.Name} From(double value, {_unitEnumName} fromUnit)
414408
{{
415-
return new {_quantity.Name}(({_valueType})value, fromUnit);
409+
return new {_quantity.Name}(value, fromUnit);
416410
}}
417411
418412
#endregion
@@ -601,25 +595,25 @@ private void GenerateArithmeticOperators()
601595
}}
602596
603597
/// <summary>Get <see cref=""{_quantity.Name}""/> from multiplying value and <see cref=""{_quantity.Name}""/>.</summary>
604-
public static {_quantity.Name} operator *({_valueType} left, {_quantity.Name} right)
598+
public static {_quantity.Name} operator *(double left, {_quantity.Name} right)
605599
{{
606600
return new {_quantity.Name}(left * right.Value, right.Unit);
607601
}}
608602
609603
/// <summary>Get <see cref=""{_quantity.Name}""/> from multiplying value and <see cref=""{_quantity.Name}""/>.</summary>
610-
public static {_quantity.Name} operator *({_quantity.Name} left, {_valueType} right)
604+
public static {_quantity.Name} operator *({_quantity.Name} left, double right)
611605
{{
612606
return new {_quantity.Name}(left.Value * right, left.Unit);
613607
}}
614608
615609
/// <summary>Get <see cref=""{_quantity.Name}""/> from dividing <see cref=""{_quantity.Name}""/> by value.</summary>
616-
public static {_quantity.Name} operator /({_quantity.Name} left, {_valueType} right)
610+
public static {_quantity.Name} operator /({_quantity.Name} left, double right)
617611
{{
618612
return new {_quantity.Name}(left.Value / right, left.Unit);
619613
}}
620614
621615
/// <summary>Get ratio value from dividing <see cref=""{_quantity.Name}""/> by <see cref=""{_quantity.Name}""/>.</summary>
622-
public static {_quantity.ValueType} operator /({_quantity.Name} left, {_quantity.Name} right)
616+
public static double operator /({_quantity.Name} left, {_quantity.Name} right)
623617
{{
624618
return left.{_baseUnit.PluralName} / right.{_baseUnit.PluralName};
625619
}}
@@ -659,7 +653,7 @@ private void GenerateLogarithmicArithmeticOperators()
659653
}}
660654
661655
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic multiplication of value and <see cref=""{_quantity.Name}""/>.</summary>
662-
public static {_quantity.Name} operator *({_valueType} left, {_quantity.Name} right)
656+
public static {_quantity.Name} operator *(double left, {_quantity.Name} right)
663657
{{
664658
// Logarithmic multiplication = addition
665659
return new {_quantity.Name}(left + right.Value, right.Unit);
@@ -669,14 +663,14 @@ private void GenerateLogarithmicArithmeticOperators()
669663
public static {_quantity.Name} operator *({_quantity.Name} left, double right)
670664
{{
671665
// Logarithmic multiplication = addition
672-
return new {_quantity.Name}(left.Value + ({_valueType})right, left.Unit);
666+
return new {_quantity.Name}(left.Value + right, left.Unit);
673667
}}
674668
675669
/// <summary>Get <see cref=""{_quantity.Name}""/> from logarithmic division of <see cref=""{_quantity.Name}""/> by value.</summary>
676670
public static {_quantity.Name} operator /({_quantity.Name} left, double right)
677671
{{
678672
// Logarithmic division = subtraction
679-
return new {_quantity.Name}(left.Value - ({_valueType})right, left.Unit);
673+
return new {_quantity.Name}(left.Value - right, left.Unit);
680674
}}
681675
682676
/// <summary>Get ratio value from logarithmic division of <see cref=""{_quantity.Name}""/> by <see cref=""{_quantity.Name}""/>.</summary>
@@ -827,15 +821,15 @@ public int CompareTo({_quantity.Name} other)
827821
/// </para>
828822
/// <para>
829823
/// Note that it is advised against specifying zero difference, due to the nature
830-
/// of floating-point operations and using {_valueType} internally.
824+
/// of floating-point operations and using double internally.
831825
/// </para>
832826
/// </summary>
833827
/// <param name=""other"">The other quantity to compare to.</param>
834828
/// <param name=""tolerance"">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
835829
/// <param name=""comparisonType"">The comparison type: either relative or absolute.</param>
836830
/// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
837831
[Obsolete(""Use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
838-
public bool Equals({_quantity.Name} other, {_quantity.ValueType} tolerance, ComparisonType comparisonType)
832+
public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comparisonType)
839833
{{
840834
if (tolerance < 0)
841835
throw new ArgumentOutOfRangeException(nameof(tolerance), ""Tolerance must be greater than or equal to 0."");
@@ -889,7 +883,7 @@ private void GenerateConversionMethods()
889883
/// Convert to the unit representation <paramref name=""unit"" />.
890884
/// </summary>
891885
/// <returns>Value converted to the specified unit.</returns>
892-
public {_quantity.ValueType} As({_unitEnumName} unit)
886+
public double As({_unitEnumName} unit)
893887
{{
894888
if (Unit == unit)
895889
return Value;
@@ -898,21 +892,10 @@ private void GenerateConversionMethods()
898892
}}
899893
");
900894

901-
if (_quantity.ValueType == "decimal")
902-
{
903-
Writer.WL($@"
904-
905-
double IQuantity<{_unitEnumName}>.As({_unitEnumName} unit)
906-
{{
907-
return (double)As(unit);
908-
}}
909-
");
910-
}
911-
912895
Writer.WL($@"
913896
914897
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
915-
public {_quantity.ValueType} As(UnitSystem unitSystem)
898+
public double As(UnitSystem unitSystem)
916899
{{
917900
if (unitSystem is null)
918901
throw new ArgumentNullException(nameof(unitSystem));
@@ -927,25 +910,14 @@ private void GenerateConversionMethods()
927910
}}
928911
");
929912

930-
if (_quantity.ValueType == "decimal")
931-
{
932-
Writer.WL($@"
933-
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
934-
double IQuantity.As(UnitSystem unitSystem)
935-
{{
936-
return (double)As(unitSystem);
937-
}}
938-
");
939-
}
940-
941913
Writer.WL($@"
942914
/// <inheritdoc />
943915
double IQuantity.As(Enum unit)
944916
{{
945917
if (!(unit is {_unitEnumName} typedUnit))
946918
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
947919
948-
return (double)As(typedUnit);
920+
return As(typedUnit);
949921
}}
950922
951923
/// <summary>

0 commit comments

Comments
 (0)