Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the missing BaseUnits #1473

Merged
merged 9 commits into from
Dec 30, 2024
  •  
  •  
  •  
76 changes: 30 additions & 46 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ private void GenerateInstanceConstructors()
Writer.WL($@"
_unit = unit;
}}

");
if (!_isDimensionless)
{
Writer.WL($@"
/// <summary>
/// Creates an instance of the quantity with the given numeric value in units compatible with the given <see cref=""UnitSystem""/>.
/// If multiple compatible units were found, the first match is used.
Expand All @@ -225,18 +228,11 @@ private void GenerateInstanceConstructors()
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
public {_quantity.Name}(double value, UnitSystem unitSystem)
{{
if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
var firstUnitInfo = unitInfos.FirstOrDefault();
");

Writer.WL(@"
_value = value;");
Writer.WL($@"
_unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
_value = value;
_unit = Info.GetDefaultUnit(unitSystem);
}}
");
}
}

private void GenerateStaticProperties()
Expand Down Expand Up @@ -1000,34 +996,16 @@ public double As({_unitEnumName} unit)
}}
");

Writer.WL($@"
Writer.WL( $@"

/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
public double As(UnitSystem unitSystem)
{{
if (unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);

var firstUnitInfo = unitInfos.FirstOrDefault();
if (firstUnitInfo == null)
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));

return As(firstUnitInfo.Value);
return As(Info.GetDefaultUnit(unitSystem));
}}
");

Writer.WL($@"
/// <inheritdoc />
double IQuantity.As(Enum unit)
{{
if (!(unit is {_unitEnumName} typedUnit))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return As(typedUnit);
}}

/// <summary>
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
/// </summary>
Expand Down Expand Up @@ -1123,29 +1101,33 @@ private bool TryToUnit({_unitEnumName} unit, [NotNullWhen(true)] out {_quantity.
converted = convertedOrNull.Value;
return true;
}}
");
Writer.WL($@"
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
{{
return ToUnit(Info.GetDefaultUnit(unitSystem));
}}
");
lipchev marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc />
IQuantity IQuantity.ToUnit(Enum unit)
Writer.WL($@"
#region Explicit implementations

double IQuantity.As(Enum unit)
{{
if (!(unit is {_unitEnumName} typedUnit))
if (unit is not {_unitEnumName} typedUnit)
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return ToUnit(typedUnit, DefaultConversionFunctions);
return As(typedUnit);
}}

/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
/// <inheritdoc />
IQuantity IQuantity.ToUnit(Enum unit)
{{
if (unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);

var firstUnitInfo = unitInfos.FirstOrDefault();
if (firstUnitInfo == null)
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
if (!(unit is {_unitEnumName} typedUnit))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return ToUnit(firstUnitInfo.Value);
return ToUnit(typedUnit, DefaultConversionFunctions);
}}

/// <inheritdoc />
Expand All @@ -1158,6 +1140,8 @@ IQuantity IQuantity.ToUnit(Enum unit)
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);

#endregion

#endregion
");
}

Expand Down
Loading